I'm writing a client to upload files via regular http multipart/form-data to megaupload. Now, the point is not megaupload per se, but the behaviour of their webserver.
Curl could upload without any problem, while my client couldn't, even by sending the exact same request (sniffed with wireshark) -- but it was stuck waiting for the response, and eventually timing out after 30 minutes.
After playing with raw sockets and strace for a while, it turns out the only difference between the two is that curl sends the header block with only one call to sendto(2), and then the rest with other calls to sendto(2). My client, on the other hand, sends every header separately with a write(2).
Now, sendto and write should be equivalent, if send doesn't specify any flag, and it didn't. In fact I made it work with write, but only by sending the header block in a single call. Every other sequence of write calls caused the request to be stuck waiting.
So the question is: how is this even possible? Tcp doesn't preserve message boundaries, it being a stream protocol.
The only thing I can think of is that every write/send syscall causes a packet to be sent, and that the remote server is sniffing raw packets and lying about being apache.
Ideas? Or am I being a moron, and this is normal behaviour for a compliant http server? It sure is the first webserver to behave that way to me.