本文整理汇总了C++中NPT_HttpEntity::ContentLengthIsKnown方法的典型用法代码示例。如果您正苦于以下问题:C++ NPT_HttpEntity::ContentLengthIsKnown方法的具体用法?C++ NPT_HttpEntity::ContentLengthIsKnown怎么用?C++ NPT_HttpEntity::ContentLengthIsKnown使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NPT_HttpEntity
的用法示例。
在下文中一共展示了NPT_HttpEntity::ContentLengthIsKnown方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: if
/*----------------------------------------------------------------------
| PLT_HttpServerSocketTask::SendResponseHeaders
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpServerSocketTask::SendResponseHeaders(NPT_HttpResponse* response,
NPT_OutputStream& output_stream,
bool& keep_alive)
{
// add any headers that may be missing
NPT_HttpHeaders& headers = response->GetHeaders();
// get the request entity to set additional headers
NPT_InputStreamReference body_stream;
NPT_HttpEntity* entity = response->GetEntity();
if (entity && NPT_SUCCEEDED(entity->GetInputStream(body_stream))) {
// set the content length if known
if (entity->ContentLengthIsKnown()) {
headers.SetHeader(NPT_HTTP_HEADER_CONTENT_LENGTH,
NPT_String::FromIntegerU(entity->GetContentLength()));
}
// content type
NPT_String content_type = entity->GetContentType();
if (!content_type.IsEmpty()) {
headers.SetHeader(NPT_HTTP_HEADER_CONTENT_TYPE, content_type);
}
// content encoding
NPT_String content_encoding = entity->GetContentEncoding();
if (!content_encoding.IsEmpty()) {
headers.SetHeader(NPT_HTTP_HEADER_CONTENT_ENCODING, content_encoding);
}
// transfer encoding
const NPT_String& transfer_encoding = entity->GetTransferEncoding();
if (!transfer_encoding.IsEmpty()) {
headers.SetHeader(NPT_HTTP_HEADER_TRANSFER_ENCODING, transfer_encoding);
}
} else if (!headers.GetHeader(NPT_HTTP_HEADER_CONTENT_LENGTH)) {
// force content length to 0 if there is no message body
// (necessary for 1.1 or 1.0 with keep-alive connections)
headers.SetHeader(NPT_HTTP_HEADER_CONTENT_LENGTH, "0");
}
const NPT_String* content_length = headers.GetHeaderValue(NPT_HTTP_HEADER_CONTENT_LENGTH);
const NPT_String* transfer_encoding = headers.GetHeaderValue(NPT_HTTP_HEADER_TRANSFER_ENCODING);
const NPT_String* connection_header = headers.GetHeaderValue(NPT_HTTP_HEADER_CONNECTION);
if (keep_alive) {
if (connection_header && connection_header->Compare("close") == 0) {
keep_alive = false;
} else {
// the request says client supports keep-alive
// but override if response has content-length header or
// transfer chunked encoding
keep_alive = content_length ||
(transfer_encoding && transfer_encoding->Compare(NPT_HTTP_TRANSFER_ENCODING_CHUNKED) == 0);
}
}
// only write keep-alive header for 1.1 if it's close
NPT_String protocol = response->GetProtocol();
if (protocol.Compare(NPT_HTTP_PROTOCOL_1_0, true) == 0 || !keep_alive) {
headers.SetHeader(NPT_HTTP_HEADER_CONNECTION, keep_alive?"keep-alive":"close", true);
}
headers.SetHeader(NPT_HTTP_HEADER_SERVER, PLT_HTTP_DEFAULT_SERVER, false); // set but don't replace
PLT_LOG_HTTP_RESPONSE(NPT_LOG_LEVEL_FINE, "PLT_HttpServerSocketTask::Write", response);
// create a memory stream to buffer the headers
NPT_MemoryStream header_stream;
response->Emit(header_stream);
// send the headers
NPT_CHECK_WARNING(output_stream.WriteFully(header_stream.GetData(), header_stream.GetDataSize()));
return NPT_SUCCESS;
}