jeremycw / httpserver.h

Single header library for writing non-blocking HTTP servers in C
MIT License
1.78k stars 143 forks source link

Missing Headers Causes Request Timeout #42

Closed nickav closed 4 years ago

nickav commented 4 years ago

First of all thank you so much for this project, it's amazing!

The hello world server seems to be timing out depending on the headers sent:

Steps to repro

1. Compile and run the server:

#define HTTPSERVER_IMPL
#include "httpserver.h"

#define RESPONSE "Hello, World!"

void handle_request(struct http_request_s* request) {
  struct http_response_s* response = http_response_init();
  http_response_status(response, 200);
  http_response_header(response, "Content-Type", "text/plain");
  http_response_body(response, RESPONSE, sizeof(RESPONSE) - 1);
  http_respond(request, response);
}

int main() {
  struct http_server_s* server = http_server_init(8080, handle_request);
  http_server_listen(server);
}

2. Make a request

With the server running, attempt to send a request to POST http://localhost:8080 with Postman (using the default headers). Notice that going to Headers, clicking "7 hidden" and then un-checking Content-Length: 0 will allow the request to be processed successfully.

Alternatively, you can run the following code in the browser which also will cause the request to timeout:

fetch('http://localhost:8080/', { method: 'POST' }).then(resp => resp.text()).then(console.log);

While this seems to work OK:

fetch('http://localhost:8080/', {
    method: 'POST',
    body: JSON.stringify(null),
  }).then(resp => resp.text()).then(console.log);

Note that removing body or making body: "" also will cause a timeout.

Let me know if there's anything I should be doing to fix this. Thanks!

jeremycw commented 4 years ago

Thanks for the bug report. I've been busy with other things recently but I will try and take a look at this soon.

jeremycw commented 4 years ago

Resolved