Skip to content

Instantly share code, notes, and snippets.

@marcellodesales
Last active November 14, 2023 19:22

Revisions

  1. marcellodesales revised this gist Sep 17, 2020. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion formatted.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ mkfifo out
    trap "rm -f out" EXIT
    while true
    do
    cat out | nc -l 1500 > >( # parse the netcat output, to build the answer redirected to the pipe "out".
    cat out | nc -w1 -l 1500 > >( # parse the netcat output, to build the answer redirected to the pipe "out".
    export REQUEST=
    while read line
    do
    2 changes: 1 addition & 1 deletion one-liner.sh
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    rm -f out ; mkfifo out ; trap "rm -f out" EXIT ; while true ; do cat out | nc -l 1500 > >(export REQUEST= ; while read line ; do line=$(echo "$line" | tr -d '[\r\n]') ; if echo "$line" | grep -qE '^GET /' ; then REQUEST=$(echo "$line" | cut -d ' ' -f2) ; elif [ "x$line" = x ] ; then HTTP_200="HTTP/1.1 200 OK" ; HTTP_LOCATION="Location:" ; HTTP_404="HTTP/1.1 404 Not Found" ; if echo $REQUEST | grep -qE '^/echo/' ; then printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out ; elif echo $REQUEST | grep -qE '^/date' ; then date > out ; elif echo $REQUEST | grep -qE '^/stats' ; then vmstat -S M > out ; elif echo $REQUEST | grep -qE '^/net' ; then ifconfig > out ; else printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out ; fi ; fi ; done) ; done
    rm -f out ; mkfifo out ; trap "rm -f out" EXIT ; while true ; do cat out | nc -w1 -l 1500 > >(export REQUEST= ; while read line ; do line=$(echo "$line" | tr -d '[\r\n]') ; if echo "$line" | grep -qE '^GET /' ; then REQUEST=$(echo "$line" | cut -d ' ' -f2) ; elif [ "x$line" = x ] ; then HTTP_200="HTTP/1.1 200 OK" ; HTTP_LOCATION="Location:" ; HTTP_404="HTTP/1.1 404 Not Found" ; if echo $REQUEST | grep -qE '^/echo/' ; then printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out ; elif echo $REQUEST | grep -qE '^/date' ; then date > out ; elif echo $REQUEST | grep -qE '^/stats' ; then vmstat -S M > out ; elif echo $REQUEST | grep -qE '^/net' ; then ifconfig > out ; else printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out ; fi ; fi ; done) ; done
  2. marcellodesales revised this gist Oct 18, 2019. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. marcellodesales created this gist Oct 19, 2014.
    40 changes: 40 additions & 0 deletions formatted
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    rm -f out
    mkfifo out
    trap "rm -f out" EXIT
    while true
    do
    cat out | nc -l 1500 > >( # parse the netcat output, to build the answer redirected to the pipe "out".
    export REQUEST=
    while read line
    do
    line=$(echo "$line" | tr -d '[\r\n]')

    if echo "$line" | grep -qE '^GET /' # if line starts with "GET /"
    then
    REQUEST=$(echo "$line" | cut -d ' ' -f2) # extract the request
    elif [ "x$line" = x ] # empty line / end of request
    then
    HTTP_200="HTTP/1.1 200 OK"
    HTTP_LOCATION="Location:"
    HTTP_404="HTTP/1.1 404 Not Found"
    # call a script here
    # Note: REQUEST is exported, so the script can parse it (to answer 200/403/404 status code + content)
    if echo $REQUEST | grep -qE '^/echo/'
    then
    printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out
    elif echo $REQUEST | grep -qE '^/date'
    then
    date > out
    elif echo $REQUEST | grep -qE '^/stats'
    then
    vmstat -S M > out
    elif echo $REQUEST | grep -qE '^/net'
    then
    ifconfig > out
    else
    printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out
    fi
    fi
    done
    )
    done
    1 change: 1 addition & 0 deletions one-liner
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    rm -f out ; mkfifo out ; trap "rm -f out" EXIT ; while true ; do cat out | nc -l 1500 > >(export REQUEST= ; while read line ; do line=$(echo "$line" | tr -d '[\r\n]') ; if echo "$line" | grep -qE '^GET /' ; then REQUEST=$(echo "$line" | cut -d ' ' -f2) ; elif [ "x$line" = x ] ; then HTTP_200="HTTP/1.1 200 OK" ; HTTP_LOCATION="Location:" ; HTTP_404="HTTP/1.1 404 Not Found" ; if echo $REQUEST | grep -qE '^/echo/' ; then printf "%s\n%s %s\n\n%s\n" "$HTTP_200" "$HTTP_LOCATION" $REQUEST ${REQUEST#"/echo/"} > out ; elif echo $REQUEST | grep -qE '^/date' ; then date > out ; elif echo $REQUEST | grep -qE '^/stats' ; then vmstat -S M > out ; elif echo $REQUEST | grep -qE '^/net' ; then ifconfig > out ; else printf "%s\n%s %s\n\n%s\n" "$HTTP_404" "$HTTP_LOCATION" $REQUEST "Resource $REQUEST NOT FOUND!" > out ; fi ; fi ; done) ; done