Reputation: 3091
I'm trying to create mock server. I can definitely code it myself, but I found that ncat
could be theoretically used for that. And this should be available to more users already than my new code... However:
If I start "server" like this:
ncat -v -l -p 5555 -c 'while true; do read i && echo [echo] $i; done'
and issue request like this:
curl localhost:5555 -X POST --data-binary 'test'
I'm getting this:
curl: (1) Received HTTP/0.9 when not allowed
ncat version is 7.80. Why am I getting http version this old? Is it normal or is it a problem on my side? How to fix it? I definitely cannot adapt curl part to accept obsoleted responses.
If it's not possible, can someone recommend existing working alternative to using nc to listen to incoming requests and apply some command to each one of them?
Upvotes: 2
Views: 3265
Reputation: 165
This has to do with the response from netcat. It is not valid HTTP/1.x (see https://www.w3.org/Protocols/HTTP/1.0/spec.html#Response).
Here is a sample HTTP/1.1 response via a file.
$ cat index.html
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: netcat!
<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>
Now return via netcat.
~$ cat index.html | sudo nc -q0 -l 80
Curl is working (with http/1.1) by default.
% curl -v 34.145.32.6
* Trying 34.145.32.6:80...
* Connected to 34.145.32.6 (34.145.32.6) port 80 (#0)
> GET / HTTP/1.1
> Host: 34.145.32.6
> User-Agent: curl/7.79.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/html; charset=UTF-8
< Server: netcat!
* no chunk, no close, no size. Assume close to signal end
<
<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>
* Closing connection 0
Take note of things like HTTP/1.1 200 OK
and Content-Type: text/html; charset=UTF-8
.
If you want to use curl with an invalid HTTP server response, try using the telnet protocol (instead of http) with curl.
% curl -v telnet://34.145.32.6:80
* Trying 34.145.32.6:80...
* Connected to 34.145.32.6 (34.145.32.6) port 80 (#0)
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: netcat!
<!doctype html>
<html><body><h1>A webpage served by netcat</h1></body></html>
* Closing connection 0
While I am using maybe a different nc/netcat version; I do not believe that was the source of your issue.
Upvotes: 2