cgcgbcbc cgcgbcbc - 3 months ago 6x
Node.js Question

How to accept http request without http version and host header in Go or node.js?

The hardware is old, and it only send data like
GET /?a=1

Seems that server write in golang/node.js will take this as HTTP/1.1 and since it does not have Host header, the server will respond 400 bad request.
And the http 1.0 standard needs GET /?a=1 HTTP/1.0, is there exist an out of box http server that can accept such non standard request?

I've tried with go's net/http package and node.js's http package, both won't work, since they obey the standard.

Is it possible write such a http server in go or node.js?


using netcat to inspect the request:
enter image description here


HTTP servers are fairly easy to write. I've written at least 2. As a guide I strongly recommend you read "HTTP Made Really Easy". It was first published in 1997 and I first used it to write my first server in 2002.

Basically, an HTTP server needs to wait for double newlines ("\n\n"). If there is a Content-Length header then it needs to read that many bytes of input (that is for POST and PUT requests).

That's it. Everything else is about how to interpret the data you've got.

Oh, there's also chunked transfer encoding but that's only mandatory for HTTP/1.1 servers and above. Just identify yourself as an HTTP/0.9 server.

To see how simple a web server can be see: (8 lines of code)

Here's a similar server in javascript:

// WARNING: Does not support POST or PUT body
var net = require('net');
var server = net.createServer(function(sock){
  var buffer = '';

  sock.on('data', function(chunk){
    buffer += chunk.toString('utf8'); // assumptions!!

    if (buffer.match(/(\r?\n){2}/)) { // looking for double \n\n
      sock.write("HTTP/0.9 200 OK\nConnection:close\n\n");

server.listen(8080, function(){
  console.log('Server started');

You can process the request by processing the buffer string. The first line is the GET line.