0

Getting Response is null error while receiving HTTP response.
I am developing an sample small HTTP server in C using row sockets.

There are actually 2 servers in my application one is standard Apache server which I am using for serving HTML pages and my small server will respond to only XMLHttpRequest sent from the Javascript within the HTML pages.

I am sending request from JavaScript as follows:

var sendReq = new XMLHttpRequest();
endReq.open("POST", "http://localhost:10000/", true);
sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
sendReq.onreadystatechange = handleResult;
var param = "REQUEST_TYPE=2002&userName=" + userName.value;
param += "&password=" + password.value;
sendReq.send(param);

When I send this request I receive following Request in my server code:

OPTIONS / HTTP/1.1
Host: localhost:10000
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:7777
Access-Control-Request-Method: POST

I have replied to this Request as follows using socket write function:

HTTP/1.1 200 OK\n
Access-Control-Allow-Origin: *\n
Server: PSL/1.0 (Unix) (Ubuntu/Linux)\n
Access-Control-Allow-Methods: POST, GET, OPTIONS\n
Accept-Ranges: bytes\n
Content-Length: 438\nConnection: close\n
Content-Type: text/html; charset=UTF-8\n\n

I don`t know What should be the HTTP actual response to be sent on request of OPTIONS.
After this I get my Actual POST request that I have sent from JavaScript and then I respond back with

HTTP/1.1 200 OK\n\n

And then at the browser end get error Response is null.

So how to send headers/data as HTTP Response using row sockets in 'C' and how to respond to OPTIONS request. Can someone explain me by giving some example?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Puneet
  • 565
  • 4
  • 9
  • 21

3 Answers3

2

It's hard to understand your question, but I believe you are pointing to this as the response giving you trouble:

HTTP/1.1 200 OK\n\n

You should be including other fields, especially the Content-Length and Content-Type. If you're going to build your own HTTP server, then you should review the protocol specifications.

That said, it's not at all clear why you need to replace the HTTP server instead of using either CGI or another server side language (PHP, Java, etc). This is significantly reducing your portability and maintainability.

Finally, you appear to be transmitting the password in the request. Make sure that this is only done over some kind of encrypted (HTTPS) or else physically secured connection.

BMitch
  • 231,797
  • 42
  • 475
  • 450
1

I'm not sure what you're asking, but you might find the following useful:

HTTP Made Really Easy

HTTP/1.1 rfc2616.txt

MAMA - Opera Developer Community

I found them all quite useful when I was writing a HTTP client.

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
0

This problem had occured as after processing the OPTIONS request by our server, any subsequent requests made, for some reason, were required to be responded back with "Access-Control-Allow-Origin: *" along with other normal headers and response body.

After providing this line in our responses, I always got the desired responseText/responseXML in my javascript.

Puneet
  • 565
  • 4
  • 9
  • 21