开发者

server not able to read payload for a given HTTP POST

开发者 https://www.devze.com 2023-03-27 23:10 出处:网络
I\'ve a client which generates HTTP POST request and server (socket server) which reads the data send by client. When I do a read operation on server end, the server just reads headers as shown below

I've a client which generates HTTP POST request and server (socket server) which reads the data send by client. When I do a read operation on server end, the server just reads headers as shown below and it does not read payload sent by the client. The client is sending approx 5KB of payload but the read operation on socket at server reads 70 bytes of data (headers) though Content-length is 4731 as shown below. I'm using read/recv system calls to carry out the read operation, can somebody tell why read/recv system call is not able to read the payload? (server is implemented using C programming language)

Read by server (no payload is being read)**

POST / HTTP/1.1

Content-Type: test/html

Host: 192.168.1.102:800

Content-Length: 4731

Expect: 100-continue

Connection:开发者_开发百科 Keep-Alive


pats


The thing that you are missing here is that TCP socket is a stream. When you read from it you receive up to the number of bytes you asked for meaning the kernel's network stack copies either size of your buffer or all bytes available so far, whichever is smaller.

The bottom line is that you need to read from TCP socket in a loop. One send from the client might end up in multiple reads by the server and vise versa.


the content type is "test"/html?


The client probably is not actually sending the payload over the network, because it is waiting for your server to respond with a 100 status before it sends the payload (or for your server to respond with a 417 or other final status code, in which case it wouldn't send the payload at all). Your server should return a 417 status if it sees an "Expect" header it does not understand. It could also decide to understand 100-continue and send the requested 100 status.

If this is indeed the case, the client is also not behaving correctly as it should eventually send the payload anyway if the server does not respond after a sufficient period of time (on the assumption the server is too old to properly handle the "Expect" header).

See RFC 2616 for details, particularly section 8.2.3.


There is no guarantee that the Content-Length is accurate. The client might not actually be sending the body.

To track down the problem, a network sniffer such as Wireshark or Fiddler is extremely useful. With that, you can see if the data was actually sent. Wireshark is an all-purpose network sniffer that captures all traffic that it "sees" on the wire. Fiddler focuses on HTTP traffic, so it might not be quite as useful if the data being sent is not recognized as HTTP.

0

精彩评论

暂无评论...
验证码 换一张
取 消