-
-
Save coder0107git/fc157c9e1c84710632e8812198387995 to your computer and use it in GitHub Desktop.
Bash HTTP Server (using socat)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -o errexit | |
| set -o nounset | |
| function server() { | |
| echo "Starting TCP server at http://localhost:8000..." | |
| socat TCP-LISTEN:8000,pktinfo,reuseaddr,fork SYSTEM:"'${SHELL}' '${BASH_SOURCE[0]}' request" | |
| } | |
| function request() { | |
| read -r first_line | |
| export HTTP_METHOD="$(cut -d' ' -f 1 - <<< "${first_line}")" | |
| export HTTP_PATH="$(cut -d' ' -f 2 - <<< "${first_line}")" | |
| log "Received HTTP request" | |
| write_http "HTTP/1.1 200 OK" | |
| write_http "Content-Type: text/plain" | |
| write_http "Server: Shell Script" | |
| write_http | |
| write_http 'Hello, World!' | |
| write_http | |
| log "Sent HTTP response" | |
| } | |
| function log() { | |
| { | |
| echo -en "\e[1;32m[$(date) ${HTTP_METHOD} ${SOCAT_PEERADDR}:${SOCAT_PEERPORT}${HTTP_PATH}] " | |
| echo -en "\e[0m" | |
| echo "${@}" | |
| } >&2 | |
| } | |
| function write_http() { | |
| printf $"%s\r\n" "${1:-}" | |
| } | |
| operation="${1:-help}" | |
| shift 1 | |
| case "${operation}" in | |
| server) server "${@}" ;; | |
| request) request "${@}" ;; | |
| help|-h|--help) | |
| echo "USAGE: ${BASH_SOURCE[0]} <operation> [flags...]" | |
| echo "Operations:" | |
| echo " - help - Show this message." | |
| echo " - server - run a webserver." | |
| echo " - request - handle a request on stdin, dump the response to stdout." | |
| exit 0 | |
| ;; | |
| *) | |
| echo "ERROR: Invalid operation ${operation@Q}." | |
| echo "Run '${BASH_SOURCE[0]} --help' for script usage." | |
| exit 1 | |
| ;; | |
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ./http_server.sh server & sleep 2; curl -vvv http://localhost:8000/foo/bar; wait < <(jobs -p) | |
| [1] 20471 | |
| Starting TCP server at http://localhost:8000... | |
| * Trying 127.0.0.1:8000... | |
| * Connected to localhost (127.0.0.1) port 8000 (#0) > GET /foo/bar HTTP/1.1 | |
| > Host: localhost:8000 | |
| > User-Agent: curl/8.1.2 | |
| > Accept: */* | |
| > | |
| [Sun Jun 18 10:00:42 BST 2023 GET 127.0.0.1:45522/foo/bar] Received HTTP request | |
| < HTTP/1.1 200 OK | |
| < Content-Type: text/plain | |
| < Server: Shell Script | |
| * no chunk, no close, no size. Assume close to signal end | |
| < | |
| Hello, World! | |
| [Sun Jun 18 10:00:42 BST 2023 GET 127.0.0.1:45522/foo/bar] Sent HTTP response | |
| * Closing connection 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment