Last active
May 19, 2023 14:25
-
-
Save ramn/58a1e0df9689f9563d07 to your computer and use it in GitHub Desktop.
Serve file over HTTP with Socat
This file contains 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
#!/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