Skip to content

Commit

Permalink
GH-45: dasht-server-http: serve file:// over HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
sunaku committed May 2, 2020
1 parent 192153e commit c34e57c
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion bin/dasht-server-http
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ url=$(awk '
/^\r$/ { exit } # reached end of HTTP request
')

# intercept and serve up file:// URLs over HTTP
# TODO: also handle /favicon.ico and other URLs
case "$url" in (/file://*)
file=${url#*:} # strip file:// protocol
file=${file%#*} # strip any URI fragment

if test -f "$file"; then
printf 'HTTP/1.0 200 OK\r\n'
printf 'Content-Type: %s\r\n' "$(file -bi "$file")"
printf '\r\n'
cat "$file"
else
printf 'HTTP/1.0 404 Not Found\r\n'
printf '\r\n'
fi

exit
;;esac

# parse URL query parameters as shell variables
eval "$(
# split URL on query parameter delimiters (?&)
Expand Down Expand Up @@ -184,7 +203,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,href=",&/,'
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 c34e57c

Please sign in to comment.