0
char get_buffer[10000];
SSL_write(conn,https_request_get,strlen(https_request_get));
printf("GET Sent...\n");
byte_count = SSL_read(conn,get_buffer,sizeof(get_buffer));
printf("recv()'d %d bytes of data in get_buff\n",byte_count);

printf("%s",get_buffer); 
fprintf(html, "%s",get_buffer);
fwrite(get_buffer,sizeof(get_buffer),1,html);     //html is the file pointer

I've written a C program with sockets that "downloads" the landing page HTML of a given website from the HTTP response. I was able to store the entire HTTP response in a char array and was also able to print it in the console using printf("%s",buffer_name).

Now I am trying to write the same array into a file ( fprintf()), but it only prints the HTTP response excluding the HTML of the page. I understand that there can be null characters and I also tried

fwrite(buffer,sizeof(buffer),1,file_ptr)

which gave me the same output as before (didn't put HTML into the file).
Can anyone help me out with this ?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • In `fwrite()` I would use `byte_count` instead of `sizeof(get_buffer)` but that does not explain the truncation. – prog-fh Feb 17 '21 at 07:27
  • @prog-fh unfortunately that did not make any difference – Silent Guardian Feb 17 '21 at 07:33
  • 2
    Show the declaration of the `get_buffer` variable. And don't contrive an "its like this". The actual variable declaration, *verbatim*, in the context of this code, please. Update your question with that info. I find it interesting that your `printf` works, because `SSL_read` is certainly not obliged to terminate the data it's sending, but `%s` mandates that condition. – WhozCraig Feb 17 '21 at 07:34
  • @WhozCraig I edited code in the question – Silent Guardian Feb 17 '21 at 07:38
  • 2
    Other than not terminating the string for the `fprintf` call (which you really should do, and in so doing, should be passing `sizeof(get_buffer)-1` as the buffer size to `SSL_read` to ensure space for it), and using `byte_count` properly in the `fwrite` call, and of course, no error checking, this looks like it should work. What is the reported read-size? And what file mode was `html` opened with, perchance? Further you claim the `printf` works. Does `fwrite(get_buffer, byte_count, 1, stdout); fflush(stdout);` also "work" ? You did flush `html`, right? – WhozCraig Feb 17 '21 at 07:43
  • 2
    So `printf` prints the complete response (HTTP header+Payload) while `fprintf` doesn't? Did I understand correctly? How did you open the file? Why the _double-write_ `fprintf`/`fwrite`? – Roberto Caboni Feb 17 '21 at 07:47
  • I just realized what mistake I made thanks to @WhozCraig. Its working fine when I use .txt instead of .html because I was transferring the entire HTTP response to the file. So in the end it had to do with the mode in which the HTML file is opened. – Silent Guardian Feb 17 '21 at 07:49
  • This is absolutely the wrong way to handle HTTP. You need to actually parse the HTTP response and analyze what it is actually sending you. For instance, see: https://stackoverflow.com/a/16247097/65863 – Remy Lebeau Feb 17 '21 at 19:24

0 Answers0