Skip to content

Commit

Permalink
GH-45: dasht-server: serve file:// URLs over HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaku committed May 5, 2020
1 parent 6427554 commit 7b15337
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
36 changes: 0 additions & 36 deletions bin/dasht-server
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,6 @@
#
# dasht-server | xargs -n1 w3m
#
# ### Browser refuses to load `file://` links
#
# Some browsers refuse to load `file://` links from `http://127.0.0.1` URLs.
# However, the following browsers do not suffer from this limitation and are
# thereby recommended for use with the search engine provided by this script.
#
# Recommended terminal web browsers:
#
# * [w3m]( http://w3m.sourceforge.net )
#
# * [elinks]( http://elinks.or.cz )
#
# * [lynx]( http://lynx.invisible-island.net )
#
# Recommended graphical web browsers:
#
# * [Dillo]( http://www.dillo.org )
#
# * [NetSurf]( http://www.netsurf-browser.org )
#
# * [Chrome]( https://www.google.com/chrome/ ) using the [LocalLinks extension](
# https://chrome.google.com/webstore/detail/locallinks/jllpkdkcdjndhggodimiphkghogcpida
# )
#
# * [Firefox]( http://www.mozilla.org/firefox ) using the following workaround
#
# #### Firefox
#
# The workaround for Firefox is to add this snippet to your `prefs.js` file:
#
# user_pref("capability.policy.policynames", "localfilelinks");
# user_pref("capability.policy.localfilelinks.sites", "http://127.0.0.1:54321");
# user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");
#
# See http://kb.mozillazine.org/Links_to_local_pages_do_not_work for details.
#
# ## ENVIRONMENT
#
# `DASHT_DOCSETS_DIR`
Expand Down
25 changes: 23 additions & 2 deletions bin/dasht-server-http
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,30 @@ hex2byte() {
printf "\\$oct"
}

# parse URL out of HTTP request
# parse URL from request: only GET is supported
url=$(awk '
/^GET [[:print:]]+ HTTP\/1.+\r$/ { print $2 }
/^\r$/ { exit } # reached end of HTTP request
')

# serve translated local file:// URLs over HTTP
# (skip the / homepage and /? form submissions)
if ! echo "$url" | grep -q '^/$\|^/?'; then
file=${url%#*} # strip URI fragment, if any

if test -f "$file"; then
printf 'HTTP/1.0 200 OK\r\n'
printf 'Content-Type: %s\r\n' "$(file -b --mime "$file" 2>&1)"
printf '\r\n'
cat "$file" 2>&1 || :
else
printf 'HTTP/1.0 400 Bad Request\r\n'
printf '\r\n'
fi

exit
fi

# parse URL query parameters as shell variables
eval "$(
# split URL on query parameter delimiters (?&)
Expand Down Expand Up @@ -184,7 +202,10 @@ HEADER
if test $1 -eq 0; then
# notify user when no docsets are installed so they can go install them
echo "<h1><i>${docsets_html}</i> docsets not installed so <i>$query_html</i> not searched</h1>"
elif ! dasht-query-html "$query" $docsets; then
elif results_html=$(dasht-query-html "$query" $docsets); then
# translate local file:// URLs into HTTP ones so that we can serve them
echo "$results_html" | sed 's,file://,,'
else
# notify user when no results are found so they can refine their search
echo "<h1><i>$query_html</i> not found in $1 docsets matching <i>${docsets_html:-.*}</i></h1>"
fi
Expand Down

0 comments on commit 7b15337

Please sign in to comment.