loop
loop

Reputation: 3590

socat fake http server - use a file as server response

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.

Upvotes: 20

Views: 12575

Answers (3)

Jack Miller
Jack Miller

Reputation: 7667

Tested with cygwin:

socat -v -v 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 -v -v TCP-LISTEN:1234,crlf,reuseaddr,fork SYSTEM:"cat <some_file_response>"

Upvotes: 16

qeatzy
qeatzy

Reputation: 1561

This one works:

socat -v -v -d -d TCP-LISTEN:8080,reuseaddr,fork exec:"cat http.response",pipes

Two things need to be aware,

  • should you add crlf, as in other answers. I recommend not.

    1. crlf caused problem sending image
    2. just use \r\n explicitly in http response headers.
  • without pipes, seems no data sent to client. browser complains:

    127.0.0.1 didn’t send any data. ERR_EMPTY_RESPONSE

tested in cygwin.

== EDIT ==

If you want use inside cmd.exe, make sure PATH is correctly set, so that socat and cat can be found.

Say both socat.exe and cat.exe located under E:\cygwin64\bin

set PATH=%PATH%;E:\cygwin64\bin

Works in cmd.exe, with socat & cat from cygwin.

Upvotes: 2

flotto
flotto

Reputation: 561

Taken from socat examples

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

Upvotes: 6

Related Questions