Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Sending Image via HTTP
    primarykey
    data
    text
    <p>I try to implement a simple HTTP server using C++. I was able to send text response to browser, but I am failing to send response for binary file request.</p> <p>Here is my code to get HTML response to PNG file request:</p> <pre><code>string create_html_output_for_binary(const string &amp;full_path) { const char* file_name = full_path.c_str(); FILE* file_stream = fopen(file_name, "rb"); string file_str; size_t file_size; if(file_stream != nullptr) { fseek(file_stream, 0, SEEK_END); long file_length = ftell(file_stream); rewind(file_stream); // Allocate memory. Info: http://www.cplusplus.com/reference/cstdio/fread/?kw=fread char* buffer = (char*) malloc(sizeof(char) * file_length); if(buffer != nullptr) { file_size = fread(buffer, 1, file_length, file_stream); stringstream out; for(int i = 0; i &lt; file_size; i++) { out &lt;&lt; buffer[i]; } string copy = out.str(); file_str = copy; } else { printf("buffer is null!"); } } else { printf("file_stream is null! file name -&gt; %s\n", file_name); } string html = "HTTP/1.1 200 Okay\r\nContent-Type: text/html; charset=ISO-8859-4 \r\n\r\n" + string("FILE NOT FOUND!!"); if(file_str.length() &gt; 0) { // HTTP/1.0 200 OK // Server: cchttpd/0.1.0 // Content-Type: image/gif // Content-Transfer-Encoding: binary // Content-Length: 41758 string file_size_str = to_string(file_str.length()); html = "HTTP/1.1 200 Okay\r\nContent-Type: image/png; Content-Transfer-Encoding: binary; Content-Length: " + file_size_str + ";charset=ISO-8859-4 \r\n\r\n" + file_str; printf("\n\nHTML -&gt; %s\n\nfile_str -&gt; %ld\n\n\n", html.c_str(), file_str.length()); } return html; } </code></pre> <p>This code successfully read file and store data on <code>char* buffer</code>. What makes me confuse is the <code>file_str</code> always contains <code>\211PNG</code>, although when I check its size, is much large than <code>\211PNG</code>. I suspect this is the problem that cause my image does not loaded in browsers because when I printf the html, it only shows:</p> <pre><code>HTTP/1.1 200 Okay Content-Type: image/png; Content-Transfer-Encoding: binary; Content-Length: 187542;charset=ISO-8859-4 \211PNG </code></pre> <p>What I am thinking is the way to send binary data to browser is same with sending text data, so I make the string header first, then read file data, convert it to string and combine with the header, then finally send a large single string to HTTP socket.</p> <p>I also tried this code:</p> <pre><code>if (file_stream != NULL) { short stringlength = 6; string mystring; mystring.reserve(stringlength); fseek(file_stream , 0, SEEK_SET); fread(&amp;mystring[0], sizeof(char), (size_t)stringlength, file_stream); printf("TEST -&gt; %s, length -&gt; %ld\n", mystring.c_str(), mystring.length()); fclose(file_stream ); } </code></pre> <p>But the HTML output always the same, and <code>mystring</code> also contains only <code>\211PNG</code> when printf-ed.</p> <p>Am I in the wrong path? Please help find out the mistakes in my code. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload