3

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?

2
  • Did you try curl --http1.0 localhost:5555 -X POST --data-binary 'test' ? Commented Dec 31, 2020 at 11:48
  • @RamanSailopal thanks for hint, didn't know about it. --http1.0 did not work, but http0.9 worked. Maybe it will help someone. The issue is gone, but for some reason when ncat is exeduted like this it waits for more input from sender even if there isn't one. To be honest, I already trivially implemented all I needed it using java sockets so ncat debugging is pointless to me. But if someone has the same issue, Raman is right, this will help. Commented Dec 31, 2020 at 17:14

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.