Send image over http via socat bash webserver
Asked Answered
T

1

1

I am trying to write a webserver in bash using socat. I am having trouble serving image requests. I'm opening the socat listening connection like so:

socat -T 30 -d -d TCP-L:$LISTENIP,reuseaddr,fork,crlf SYSTEM:"$0 \"docroot=$DOCROOT\""

I'm serving the image with the following, where $1 is the docroot and $2 is the image file name.

function serve_png {
  if [ -e $1$2 ]
  then
    SIZE=`stat -c '%s' $1$2`
    echo -ne "HTTP/1.1 200 OK\nContent-type: image/png\nContent-length: $SIZE\n\n"
    cat $1$2
  else
    echo -ne "HTTP/1.1 404 Not Found\nContent-type: text/html\n\n404 - Not found\n"
  fi
}

The image fails to display in firefox due to it "containing errors." I'm getting the following output at console.

2014/01/25 08:00:41 socat[11551] N listening on AF=2 0.0.0.0:8080
2014/01/25 08:00:45 socat[11551] N accepting connection from AF=2 $MYIP:55765 on AF=2 $SERVERIP:8080
2014/01/25 08:00:45 socat[11552] N forking off child, using socket for reading and writing
2014/01/25 08:00:45 socat[11551] N forked off child process 11552
2014/01/25 08:00:45 socat[11551] N listening on AF=2 0.0.0.0:8080
2014/01/25 08:00:45 socat[11552] N forked off child process 11553
2014/01/25 08:00:45 socat[11552] N forked off child process 11553
2014/01/25 08:00:45 socat[11552] N starting data transfer loop with FDs [4,4] and [3,3]
2014/01/25 08:00:45 socat[11552] W read(3, 0x8e2e388, 8192): Connection reset by peer
2014/01/25 08:00:45 socat[11552] N socket 2 to socket 1 is in error
2014/01/25 08:00:45 socat[11552] N socket 2 (fd 3) is at EOF
2014/01/25 08:00:45 socat[11552] N socket 1 (fd 4) is at EOF
2014/01/25 08:00:45 socat[11552] N socket 2 (fd 3) is at EOF
2014/01/25 08:00:45 socat[11552] N exiting with status 0

I have seen similar scripts using netcat, but I'm unable to get it working using socat. I'd like to keep using socat as it has the ability to fork and handle multiple connections. Any insights would be appreciated.

Tague answered 25/1, 2014 at 13:21 Comment(8)
you are a freak! like that :) Have you opened the image in a text / hex editor?Deerskin
The image had not been opened in any kind of editor server side. I wgot it prior to starting the server for the express purpose of testing.Tague
I mean have you checked the file after it has been downloaded from your socat server?Deerskin
Opening '/home/spikec/test.png' failed: Error while reading '/home/spikec/test.png'. File corrupted? When I view them in a text editor it seems that there are line breaks where there should not be. Was not able to get lines to break where needed in this comment after several edits. :(Tague
It also seems to be missing control characters such as ^M.Tague
Try to omit the crlf option and use \r\n instead of \n in the echo statements. While I cannot find any documentation for the crlf option I assume it to be the same as the documented crnl and in this case it would change all 0x0a bytes in the PNG into 0x0d0a and thus corrupt the image.Subjacent
Steffen: That was indeed the cause of the corruption. Thanks for the assist.Tague
You can see an example of a bash webserver I have forked from another project and have been working on: github.com/AdamDanischewski/bashttpd it uses socat the command I use is socat TCP4-LISTEN:8080,fork EXEC:"bashttpd" Formfitting
T
0

Steffen Ullrich had it with omitting the crlf flag from the socat command. It was causing Cariage Returns/Line Feeds to be inserted into the stream automatically by socat (hence the file corruption). After omitting that option, everything worked as expected.

Tague answered 24/6, 2014 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.