socat fake http server - use a file as server response

loop picture loop · Apr 20, 2015 · Viewed 7k times · Source

I've been trying to use socat to respond on each connection to a socket it's listening to with a fake HTTP reply. I cannot get it working. It might be because I'm using the cygwin version of socat? I don't know.

Part of the problem is I want the second argument <some_file_response> not to be written to. In other words because it's bidirectional it will read what's in response.txt and then write to that same file, and I don't want that. Even if I do open:response.txt,rdonly it doesn't work repeatedly. system: doesn't seem to do anything. exec seems like it works, for example I can do exec:'cat response.txt' but that never gets sent to the client connecting to port 1234.

socat -vv tcp-listen:1234,reuseaddr,fork <some_file_response>

I want it to read a file to the client that's connected and then close the connection, and do that over and over again (that's why I used fork).

I am putting a bounty on this question. Please only give me solutions that work with the cygwin version from the windows command prompt.

Answer

Jack Miller picture Jack Miller · Apr 30, 2015

Tested with cygwin:

 socat -vv TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"echo HTTP/1.0 200; echo Content-Type\: text/plain; echo; cat <some_file_response>"

If you do not want a complete HTTP response, leave out the echos:

socat -vv TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"cat <some_file_response>"