コード例 #1
0
 NPT_Result SendResponseBody(const NPT_HttpRequestContext& /*context*/,
                             NPT_HttpResponse&             /*response*/,
                             NPT_OutputStream&             output) {
     output.WriteString("<html><body>\r\n");
     for (unsigned int i=0; i<30; i++) {
         output.WriteString("Line ");
         output.WriteString(NPT_String::FromInteger(i).GetChars());
         output.WriteString("\r\n");
         output.Flush();
         NPT_System::Sleep(NPT_TimeInterval(0.2f));
     }
     output.WriteString("</body></html>\r\n");
     return NPT_SUCCESS;
 }
コード例 #2
0
ファイル: PltHttpServerTask.cpp プロジェクト: 68foxboris/xbmc
/*----------------------------------------------------------------------
|   PLT_HttpServerSocketTask::SendResponseBody
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpServerSocketTask::SendResponseBody(NPT_HttpResponse* response,
                                           NPT_OutputStream& output_stream)
{
    NPT_HttpEntity* entity = response->GetEntity();
    if (!entity) return NPT_SUCCESS;

    NPT_InputStreamReference body_stream;
    entity->GetInputStream(body_stream);
    if (body_stream.IsNull()) return NPT_SUCCESS;

    // check for chunked transfer encoding
    NPT_OutputStream* dest = &output_stream;
    if (entity->GetTransferEncoding() == NPT_HTTP_TRANSFER_ENCODING_CHUNKED) {
        dest = new NPT_HttpChunkedOutputStream(output_stream);
    }

    // send body
    NPT_LOG_FINE_1("sending body stream, %lld bytes", entity->GetContentLength());
    NPT_LargeSize bytes_written = 0;
    NPT_Result result = NPT_StreamToStreamCopy(*body_stream, *dest, 0, entity->GetContentLength(), &bytes_written); /* passing 0 if content length is unknown will read until nothing is left */
    if (NPT_FAILED(result)) {
        NPT_LOG_FINE_3("body stream only partially sent, %lld bytes (%d:%s)",
                       bytes_written,
                       result,
                       NPT_ResultText(result));
    }

    // flush to write out any buffered data left in chunked output if used
    dest->Flush();

    // cleanup (this will send zero size chunk followed by CRLF)
    if (dest != &output_stream) delete dest;

    return result;
}