简体   繁体   English

HTTP 发送对 OPTIONS 请求的响应 [C]

[英]HTTP sending response to OPTIONS request [C]

Getting Response is null error while receiving HTTP response.收到 HTTP 响应时,得到Response is null错误。
I am developing an sample small HTTP server in C using row sockets.我正在使用 sockets 行在C中开发一个示例小型 HTTP 服务器。

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. 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:我从 JavaScript 发送请求如下:

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:我已使用套接字写入 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.我不知道应选项请求发送的 HTTP 实际响应应该是什么。
After this I get my Actual POST request that I have sent from JavaScript and then I respond back with在此之后,我收到了从 JavaScript 发送的实际 POST 请求,然后我回复

HTTP/1.1 200 OK\n\n

And then at the browser end get error Response is null .然后在浏览器端得到错误Response is null

So how to send headers/data as HTTP Response using row sockets in 'C' and how to respond to OPTIONS request.那么如何使用“C”中的行 sockets 将标头/数据作为 HTTP 响应发送以及如何响应 OPTIONS 请求。 Can someone explain me by giving some example?有人可以举一些例子来解释我吗?

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.您应该包括其他字段,尤其是 Content-Length 和 Content-Type。 If you're going to build your own HTTP server, then you should review the protocol specifications .如果您要构建自己的 HTTP 服务器,那么您应该查看协议规范

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).也就是说,完全不清楚为什么需要更换 HTTP 服务器而不是使用 CGI 或其他服务器端语言(PHP、Java 等)。 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.确保这仅通过某种加密 (HTTPS) 或物理安全连接完成。

I'm not sure what you're asking, but you might find the following useful:我不确定您在问什么,但您可能会发现以下内容很有用:

HTTP Made Really Easy HTTP 真的很容易

HTTP/1.1 rfc2616.txt HTTP/1.1 rfc2616.txt

MAMA - Opera Developer Community MAMA - Opera 开发者社区

I found them all quite useful when I was writing a HTTP client.当我编写 HTTP 客户端时,我发现它们都非常有用。

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.发生此问题是因为在我们的服务器处理OPTIONS请求后,出于某种原因,任何后续请求都需要以“ Access-Control-Allow-Origin: * ”以及其他正常的标头和响应正文进行响应。

After providing this line in our responses, I always got the desired responseText/responseXML in my javascript.在我们的回复中提供这一行后,我总是在我的 javascript 中获得所需的 responseText/responseXML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM