0

I have a mongoose server, with commands callable with AJAX. I get a CORS error if I call it without sending HTTP headers from mongoose (but visiting the address with the browser works just fine), but when I do send headers, it may take up to a minute before I get a response (but it does work), both with AJAX and the browser. My reply code:

//without headers
mg_printf(conn,reply.c_str());
//with headers
mg_printf(conn,"HTTP/1.1 200 OK\r\n"
    "Content-Type: text/plain\n"
    "Cache-Control: no-cache\n"
    "Access-Control-Allow-Origin: *\n\n"
    "%s\n", reply.c_str());

How can I speed this up? Am I sending my headers wrong?


Ok, I found a solution, it works if I first check whether the request is an api call or not, and only send the headers when it is.

Sam
  • 7,252
  • 16
  • 46
  • 65
Dirk
  • 2,094
  • 3
  • 25
  • 28

1 Answers1

1

The reason mongoose is slow is because it waits for the rest of the content until it times out. And the reason it waits is because you do not set Content-Length, in which case the "end of a content" marker is when connection closes.

So the correct solution is:

  • Add Content-Length header with correct body length, OR
  • Alternatively, use mg_send_header() and mg_printf_data() functions, in which case you don't need to bother with Content-Length cause these functions use chunked encoding.
valenok
  • 827
  • 7
  • 9
  • I doubt that this is the reason, mongoose doesn't care about Content-Length as it uses chunked encoding. I assume he just forget to return TRUE from the handler making mongoose wait for more data until as you wrote, it timed out. – Lothar Nov 13 '15 at 14:27