Skip to content

Instantly share code, notes, and snippets.

@ramn
Last active May 19, 2023 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramn/58a1e0df9689f9563d07 to your computer and use it in GitHub Desktop.
Save ramn/58a1e0df9689f9563d07 to your computer and use it in GitHub Desktop.
Serve file over HTTP with Socat
#!/bin/bash
FILE="$1"
PORT=${PORT:-9999}
MIME_TYPE=$(mimetype "$FILE")
SIZE_BYTES=$(du -b "$FILE" | cut -f1)
FILE_NAME=$(basename "$FILE")
HEADER="\
HTTP/1.1 200 OK
Content-Type: $MIME_TYPE
Content-Disposition: attachment; filename=$FILE_NAME
Content-Length: $SIZE_BYTES
"
socat -d -d - tcp-l:"$PORT",reuseaddr,fork < <(printf "$HEADER"; cat "$FILE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment