http_listener, long running request

Nov 14, 2013 at 11:22 AM
void handle_request(http_request request)
{
    ...
    request.reply(status_codes::OK, jsonstring);
}
consider above snippet, it does some magic to obtain plain string and sends it as JSON, in 'one shot'

what I had like to accomplish is never close the connection and keep sending 'string lines' till some conditions is satisfied (e.g. when my sandwich is done</joke>)
void handle_request(http_request request)
{
    ...
    while(sandwich_not_done)
    {
        request.write_line(U("text"));
    }
    request.reply( status::OK, jsonString);
}
Nov 14, 2013 at 3:59 PM
If you need to send partial response, you could maybe use set_respense_stream and write to the ostream.

In the end if you need to send some HTTP response with full headers, you can maybe construct an additional http_response object and set its body to jsonString.
Nov 14, 2013 at 11:00 PM
You can keep sending data in the response using our producer_consumer_buffers.
Steps:
  1. Create a producer_consumer_buffer. One can create input and output streams over this buffer, to write and read data at two different ends.
  2. Construct an input stream head over the buffer and pass it as the response body, our internal implementation will read from this stream and pass the body over the network.
  3. Construct an output stream over the buffer and write data to the buffer. This data will be sent as the response body.
Here is an illustration:
void handle_get(http_request message)
{    
    streams::producer_consumer_buffer<char> rwbuf; // Create a producer consumer buffer
    streams::basic_istream<uint8_t> is(rwbuf); // create an input stream over the buffer
    
    // Now reply using the stream. This returns a task. 
    // You can attach a continuation to perform some action when the response has been sent completely
    // Or wait on the task once you are done writing all the data
    auto resp_task = message.reply(status_codes::OK, is);

    // Continue writing data to the stream. This data will be sent as the response body, on the same connection.
    while(sandwich_not_done)
    {
        rwbuf.putn("text", 4); // Note: putn returns a task
    }

    // Close the output stream. This signals the end of the reply data
    rwbuf.close(std::ios::out).wait();
    resp_task.wait();
}
Please refer to the listener testcases at Release\tests\Functional\http\listener\response_stream_tests.cpp for additional examples on how to send response body.

Also, the set_response_stream is to be used on the client side while sending a request, The output stream passed to it will be relied on to hold the body of the HTTP response message that results from the request. This API cannot be used on the listener side to send the data.

Thanks
Kavya.
Marked as answer by masoomshaikh on 11/15/2013 at 3:16 AM
Nov 15, 2013 at 3:26 AM
Edited Nov 15, 2013 at 11:13 AM
thanks Benoit, Kavya
solution looks plausible, let me try.

Edit: file_stream works for me