ufairiya / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

Request for binary output command #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently to output binary you need to do a work around using the following
code:
[code]
void show_data_Image( struct shttpd_arg *arg )
{
    #pragma warning(disable:4311)
    #pragma warning(disable:4312)

...

    int state = (int)arg->state;
    shttpd_printf( arg,
        "HTTP/1.1 200 OK\r\n"
        "Server: SlingIt\r\n"
        "Content-Type: image/jpeg\r\n"
        "Content-Length: %d\r\n"
        "\r\n",
        iContentLength
    );

    char b;
    int iPos = 0;
    while( arg->out.num_bytes < arg->out.len && sImageInfo.iFileSize > iPos )
    {
        b = cImage[ iPos ];
        iPos++;
        arg->out.buf[arg->out.num_bytes] = b;
        arg->out.num_bytes++;
        state++;
    }
    delete [] &b;
    arg->flags |= SHTTPD_END_OF_OUTPUT;
    arg->state = (void*)state;
}
    #pragma warning(default:4311)
    #pragma warning(default:4312)
}
[/code]
Is it possible for their to be a function added so that  binary data can be
output correctly.

What version of the product are you using? On what operating system?
Version:  SHTTPD - Ver. 1.18, Rev. 42
OS:  Any

Original issue reported on code.google.com by matthewj...@googlemail.com on 10 Dec 2008 at 8:21

GoogleCodeExporter commented 9 years ago
Please use the current version, the API is quite similar to SHTTPD one.
Binary data must be sent using mg_write() function.

Note that your example has a bug: you are sending HTTP headers in every callback
invokation. This is incorrect. When the number of bytes to output is large, the
callback is invoked several times, and you need to send headers only in first
invokation. Use arg->state to figure out, do you need to send headers or not.

However, I highly recommend switching to Mongoose. In Mongoose, the callback is
invoked only once, and you do not need to track states, and your code can be
rewritten as:

static void send_image(struct mg_connection *conn,
     const struct mg_request_info *ri,
     void *user_data)
{
  mg_printf(conn, "HTTP/1.1 200 OK\r\n"
    "Server: SlingIt\r\n"
    "Content-Type: image/jpeg\r\n"
    "Content-Length: %d\r\n\r\n", iContentLength);

  mg_write(conn, cImage, sImageInfo.iFileSize);
}

Original comment by valenok on 10 Dec 2008 at 9:58

GoogleCodeExporter commented 9 years ago
Ok.
Thanks for the quick responce.
I'll update my app to use the new version then.

Original comment by matthewj...@googlemail.com on 10 Dec 2008 at 11:54