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

发布于 2024-11-03 16:33:52 字数 1687 浏览 0 评论 0原文

接收 HTTP 响应时出现 Response is null 错误。
我正在使用行套接字用 C 开发一个示例小型 HTTP 服务器。

我的应用程序中实际上有 2 个服务器,其中一个是标准 Apache 服务器,我用它来提供 HTML 页面,而我的小型服务器将仅响应从 HTML 页面内的 Javascript 发送的 XMLHttpRequest

我从 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);

当我发送此请求时,我在服务器代码中收到以下请求:

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

我已使用套接字写入功能回复此请求如下:

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

我不知道 HTTP 实际响应应该是什么根据选项的请求发送。
之后,我收到从 JavaScript 发送的实际 POST 请求,然后我回复

HTTP/1.1 200 OK\n\n

,然后在浏览器端收到错误 Response is null

那么如何使用“C”中的行套接字将标头/数据作为 HTTP 响应发送,以及如何响应 OPTIONS 请求。有人可以举一些例子来解释我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

像极了他 2024-11-10 16:33:52

很难理解您的问题,但我相信您指出这一点是给您带来麻烦的答复:

HTTP/1.1 200 OK\n\n

您应该包含其他字段,尤其是内容长度和内容类型。如果您要构建自己的 HTTP 服务器,那么您应该查看协议规范。

也就是说,根本不清楚为什么需要替换 HTTP 服务器而不是使用 CGI 或其他服务器端语言(PHP、Java 等)。这显着降低了可移植性和可维护性。

最后,您似乎在请求中传输密码。确保这仅通过某种加密 (HTTPS) 或其他物理安全连接来完成。

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.

罪#恶を代价 2024-11-10 16:33:52

我不确定您在问什么,但您可能会发现以下内容很有用:

HTTP 变得非常简单< /a>

HTTP/1.1 rfc2616.txt

MAMA - Opera 开发者社区

当我编写 HTTP 客户端时,我发现它们都非常有用。

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.

祁梦 2024-11-10 16:33:52

发生此问题的原因是,在我们的服务器处理OPTIONS请求后,出于某种原因,需要发出任何后续请求,以“访问- Control-Allow-Origin: *" 以及其他普通标头和响应正文。

在我们的响应中提供此行后,我总是在我的 javascript 中获得所需的responseText/responseXML。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文