1

For various reasons, I am trying to download a CRL file using crude tools in C. I'm opening a tcp connection using good old socket(), sending a hardcoded plaintext http request via send(), reading the results into a buffer via recv(), and then writing that buffer into a file (which I will later use to verify various certs).

The recv() and write-to-file portions are inside a while loop so that I can get it all.

My problem is that I'm having a heck of a time coming up with a reliable means of determining when I'm done receiving the file (and therefore can break out of the while loop). Everything I've come up with so far has either had false positives or false negatives (getting back 0 bytes happens too frequently, and either the EOF marker wasn't there or I was looking in the wrong byte for it). Preferably, it would be a technique that wouldn't introduce a lot of additional complexity.

Really, I have a host, port, and a path (all as char*). On the far end, there's a friendly http server (though not one that I control). I'd be happy with anything that could get me the file without a large quantity of additional code complexity. If I had access to a command line, I'd go for something like wget, but I haven't found any direct equivalents over on the C API side, and system() is a poor choice for the situation.

Ben Barden
  • 2,001
  • 2
  • 20
  • 28

1 Answers1

1

'Getting back zero bytes', by which I assume you mean recv() returning zero, only happens when the peer has finished sending data and has closed the connection. Unless the peer is sending you multiple files per connection, this is an infallible sign of the end of this file. 'Too frequently' is nonsense: it can only happen once per connection.

But if the peer is an HTTP server it should be sending you a Content-length header. See RFc 2616.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • the OP has already stated that he gets back a number of reads that returned 0. To me, this indicates the OP has already found that looking for a 0 byte count is not sufficient. – user3629249 Jul 30 '15 at 22:07
  • 2
    ...or it means that I was misdiagnosing a bug. And, indeed, the problems with misdiagnosing bugs are *why* I was hoping for a canonical answer from someone. – Ben Barden Jul 30 '15 at 22:09
  • 1
    @user3629249 To me it only indicates that you haven't read the *man* page for `recv().` If you are asserting that it can happen more than once per connection, you are talking nonsense. Of course if the OP didn't stop reading at that point he would keep getting zeros, but that doesn't indicate a problem with this answer. – user207421 Jul 30 '15 at 22:21
  • It's true. I've gotten so used to using google search for things like this that the fact that it had a man page didn't even occur to me - and the resources I'd managed to find online were not at all helpful. – Ben Barden Jul 31 '15 at 14:11