~cadence/bliz-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH] Neaten up url parsing

Details
Message ID
<20220329161600.1935497-1-tim@clifford.lol>
DKIM signature
missing
Download raw message
Patch: +11 -21
---
Was doing some hacking on bliz (procrastination from studying lol) and
neatened up the url parsing with fish magic :)) It doesn't actually
change any functionality so feel free to ignore if you like your way lol

 src/cgi.fish | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/src/cgi.fish b/src/cgi.fish
index 1f66a18..a918f9b 100644
--- a/src/cgi.fish
+++ b/src/cgi.fish
@@ -9,21 +9,13 @@ read req_url # gemini requests are just a url
set req_url (string match -r -- '\S*' $req_url)

# parse url
set req_protocol gemini://
set req_host
set req_path
set req_query ''
begin
    set -l req_no_protocol $req_url
    if string match -q -- '*://*' $req_url
        set req_protocol (string match -r -- '^.*://' $req_url)
        set req_no_protocol (string sub -s (math (string length -- $req_protocol) + 1) -- $req_url)
    end
    set req_host (string match -r -- '^(?:.*?://)?[^/]+' $req_no_protocol; or echo default)
    set -l prefix_length (string match -r -- '^[^/]*' $req_no_protocol | string length)
    set req_path (string sub -s (math "$prefix_length + 1") $req_no_protocol)
    string length -q -- $req_path; or set req_path /
end
string match -rq -- '^(?<req_protocol>.*://)?(?<req_host>[^/]*)(?<req_path>[^?]*)\??(?<req_query>.*)' $req_url

# set defaults
test -z $req_protocol; and set req_protocol gemini://
test -z $req_host;     and set req_host     default
test -z $req_path;     and set req_path     /
# setting default for $req_query is not required since it's ''

# quit on non-gemini protocol
if test "$req_protocol" != gemini://
@@ -31,12 +23,10 @@ if test "$req_protocol" != gemini://
    exit
end

server_echo "Incoming request for: $req_path"

# get query string
set req_query (string match -r -- '\?(.*)$' $req_path)[2]; or set req_query ''
if test $req_query
    set req_path (string match -r -- '^[^?]+' $req_path)
if test -z $req_query
	server_echo "Incoming request for: $req_path"
else
	server_echo "Incoming request for: $req_path?$req_query"
end

# check for file source mode
--
2.35.1
Reply to thread Export thread (mbox)