Skip to content

Instantly share code, notes, and snippets.

@DaisukeTujita
Created November 3, 2014 13:16
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 DaisukeTujita/249da1909040e985f808 to your computer and use it in GitHub Desktop.
Save DaisukeTujita/249da1909040e985f808 to your computer and use it in GitHub Desktop.
WebService by gawk and bash. Please make response.txt include HTTP Responce Header.
#!/bin/bash
while :
do
echo "=== WebServer Start and Listen ==="
gawk 'BEGIN{ \
SOCK="/inet/tcp/80/0/0"; \
body_size=0; \
#request head \
while( (SOCK |& getline REQ) > 0){ \
print REQ; \
if( REQ~/^Content-Length/ ){ \
split(REQ,cl,":"); \
body_size=cl[2]; \
} \
if( REQ=="\r" ){ break; } \
} \
#request body \
recive_size=0; \
if(body_size > 0){ \
while( (SOCK |& getline REQ) > 0){ \
print REQ; \
recive_size=recive_size + length(REQ) + 1; \
if(recive_size >= body_size){ break; } \
} \
} \
#response \
RES_TXT="./response.txt"; \
while( getline line < RES_TXT > 0){ \
print line |& SOCK; \
}\
close(RES_TXT); \
close(SOCK); \
}'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment