本文整理汇总了C++中NPT_HttpEntity类的典型用法代码示例。如果您正苦于以下问题:C++ NPT_HttpEntity类的具体用法?C++ NPT_HttpEntity怎么用?C++ NPT_HttpEntity使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NPT_HttpEntity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SetupResponse
NPT_Result SetupResponse(NPT_HttpRequest& /*request*/,
const NPT_HttpRequestContext& /*context*/,
NPT_HttpResponse& response) {
NPT_HttpEntity* entity = response.GetEntity();
entity->SetContentType("text/html");
return NPT_SUCCESS;
}
示例2: query
/*----------------------------------------------------------------------
| CUPnPRenderer::ProcessHttpRequest
+---------------------------------------------------------------------*/
NPT_Result
CUPnPRenderer::ProcessHttpGetRequest(NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse& response)
{
// get the address of who sent us some data back
NPT_String ip_address = context.GetRemoteAddress().GetIpAddress().ToString();
NPT_String method = request.GetMethod();
NPT_String protocol = request.GetProtocol();
NPT_HttpUrl url = request.GetUrl();
if (url.GetPath() == "/thumb") {
NPT_HttpUrlQuery query(url.GetQuery());
NPT_String filepath = query.GetField("path");
if (!filepath.IsEmpty()) {
NPT_HttpEntity* entity = response.GetEntity();
if (entity == NULL) return NPT_ERROR_INVALID_STATE;
// check the method
if (request.GetMethod() != NPT_HTTP_METHOD_GET &&
request.GetMethod() != NPT_HTTP_METHOD_HEAD) {
response.SetStatus(405, "Method Not Allowed");
return NPT_SUCCESS;
}
// prevent hackers from accessing files outside of our root
if ((filepath.Find("/..") >= 0) || (filepath.Find("\\..") >=0)) {
return NPT_FAILURE;
}
#if 1
std::string path;
//url
#else
// open the file
CStdString path = CURL::Decode((const char*) filepath);
#endif
NPT_File file(path.c_str());
NPT_Result result = file.Open(NPT_FILE_OPEN_MODE_READ);
if (NPT_FAILED(result)) {
response.SetStatus(404, "Not Found");
return NPT_SUCCESS;
}
NPT_InputStreamReference stream;
file.GetInputStream(stream);
entity->SetContentType(GetMimeType(filepath));
entity->SetInputStream(stream, true);
return NPT_SUCCESS;
}
}
return PLT_MediaRenderer::ProcessHttpGetRequest(request, context, response);
}
示例3:
/*----------------------------------------------------------------------
| NPT_HttpLoggerConfigurator::SetupResponse
+---------------------------------------------------------------------*/
NPT_Result
NPT_HttpLoggerConfigurator::SetupResponse(NPT_HttpRequest& request,
const NPT_HttpRequestContext& /*context*/,
NPT_HttpResponse& response)
{
// we only support GET here
if (request.GetMethod() != NPT_HTTP_METHOD_GET) return NPT_ERROR_HTTP_METHOD_NOT_SUPPORTED;
// construct the response message
NPT_String msg;
msg = "<ul>";
NPT_List<NPT_LogConfigEntry>& config = LogManager.GetConfig();
NPT_List<NPT_LogConfigEntry>::Iterator cit = config.GetFirstItem();
for (; cit; ++cit) {
NPT_LogConfigEntry& entry = (*cit);
msg += "<li>";
msg += entry.m_Key;
msg += "=";
msg += entry.m_Value;
msg += "</li>";
}
msg += "</ul>";
msg += "<ul>";
NPT_List<NPT_Logger*>& loggers = LogManager.GetLoggers();
NPT_List<NPT_Logger*>::Iterator lit = loggers.GetFirstItem();
for (;lit;++lit) {
NPT_Logger* logger = (*lit);
msg += "<li>";
msg += logger->GetName();
msg += ", level=";
msg += NPT_String::FromInteger(logger->GetLevel());
NPT_List<NPT_LogHandler*>& handlers = logger->GetHandlers();
NPT_List<NPT_LogHandler*>::Iterator hit = handlers.GetFirstItem();
msg += ", handlers=";
for (;hit;++hit) {
NPT_LogHandler* handler = (*hit);
msg += handler->ToString();
}
msg += "</li>";
}
msg += "</ul>";
// setup the response body
NPT_HttpEntity* entity = response.GetEntity();
entity->SetContentType("text/html");
entity->SetInputStream(msg);
return NPT_SUCCESS;
}
示例4:
/*----------------------------------------------------------------------
| PLT_HttpHelper::IsBodyStreamSeekable
+---------------------------------------------------------------------*/
bool
PLT_HttpHelper::IsBodyStreamSeekable(NPT_HttpMessage* message)
{
NPT_HttpEntity* entity = message->GetEntity();
NPT_InputStreamReference stream;
if (!entity || NPT_FAILED(entity->GetInputStream(stream))) return true;
// try to get current position and seek there
NPT_Position position;
if (NPT_FAILED(stream->Tell(position)) ||
NPT_FAILED(stream->Seek(position))) {
return false;
}
return true;
}
示例5: request
bool CStreamCtrl::RecvHeaderData(NPT_HttpUrl url,NPT_DataBuffer& buffer)
{
NPT_HttpClient client;
NPT_String rdUrl=url.ToString();
// first request
NPT_HttpRequest request(url, NPT_HTTP_METHOD_GET, NPT_HTTP_PROTOCOL_1_1);
NPT_HttpResponse* response = NULL;
client.SendRequest(request, response);
NPT_HttpEntity* entity = NULL;
if (response && (entity = response->GetEntity())) {
if (NPT_FAILED(entity->Load(buffer))) return false;
}
else
return false;
return true;
}
示例6: ShowResponse
/*----------------------------------------------------------------------
| ShowResponse
+---------------------------------------------------------------------*/
static void
ShowResponse(NPT_HttpResponse* response)
{
bool check_available = true;//true;
// show entity
NPT_HttpEntity* entity = response->GetEntity();
if (entity == NULL) return;
NPT_Console::OutputF("ENTITY: length=%lld, type=%s, encoding=%s\n",
entity->GetContentLength(),
entity->GetContentType().GetChars(),
entity->GetContentEncoding().GetChars());
NPT_DataBuffer buffer(65536);
NPT_Result result;
NPT_InputStreamReference input;
entity->GetInputStream(input);
NPT_TimeStamp start;
NPT_System::GetCurrentTimeStamp(start);
float total_read = 0.0f;
for (;;) {
NPT_Size bytes_read = 0;
NPT_LargeSize available = 0;
NPT_Size to_read = 65536;
if (check_available) {
input->GetAvailable(available);
if ((NPT_Size)available < to_read) to_read = (NPT_Size)available;
if (to_read == 0) {
to_read = 1;
NPT_TimeStamp sleep_time(0.01f);
NPT_System::Sleep(sleep_time);
}
}
result = input->Read(buffer.UseData(), to_read, &bytes_read);
if (NPT_FAILED(result)) break;
total_read += bytes_read;
NPT_TimeStamp now;
NPT_System::GetCurrentTimeStamp(now);
NPT_TimeStamp duration = now-start;
NPT_Console::OutputF("%6d avail, read %6d bytes, %6.3f KB/s\n", (int)available, bytes_read, (float)((total_read/1024.0)/(double)duration));
}
}
示例7: NPT_COMPILER_UNUSED
/*----------------------------------------------------------------------
| PLT_Downloader::ProcessResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_Downloader::ProcessResponse(NPT_Result res,
const NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse* response)
{
NPT_COMPILER_UNUSED(request);
NPT_COMPILER_UNUSED(context);
if (NPT_FAILED(res)) {
NPT_LOG_WARNING_2("Downloader error %d for %s", res, m_URL.ToString().GetChars());
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
m_State = PLT_DOWNLOADER_DOWNLOADING;
NPT_HttpEntity* entity;
NPT_InputStreamReference body;
if (!response ||
!(entity = response->GetEntity()) ||
NPT_FAILED(entity->GetInputStream(body)) ||
body.IsNull()) {
m_State = PLT_DOWNLOADER_ERROR;
NPT_LOG_WARNING_2("No body %d for %s", res, m_URL.ToString().GetChars());
return NPT_FAILURE;
}
// Read body (no content length means until socket is closed)
res = NPT_StreamToStreamCopy(*body.AsPointer(),
*m_Output.AsPointer(),
0,
entity->GetContentLength());
if (NPT_FAILED(res)) {
NPT_LOG_WARNING_2("Downloader error %d for %s", res, m_URL.ToString().GetChars());
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
NPT_LOG_INFO_1("Finished downloading %s", m_URL.ToString().GetChars());
m_State = PLT_DOWNLOADER_SUCCESS;
return NPT_SUCCESS;
}
示例8: NPT_CHECK_SEVERE
/*----------------------------------------------------------------------
| NPT_HttpMessage::SetBody
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpHelper::SetBody(NPT_HttpMessage* message, NPT_InputStreamReference& stream, NPT_Size len)
{
if (len == 0) {
NPT_CHECK_SEVERE(stream->GetAvailable(len));
}
// get the entity
NPT_HttpEntity* entity = message->GetEntity();
if (entity == NULL) {
// no entity yet, create one
message->SetEntity(entity = new NPT_HttpEntity());
}
// set the entity body
entity->SetInputStream(stream);
entity->SetContentLength(len);
return NPT_SUCCESS;
}
示例9: ProcessGetSCPD
/*----------------------------------------------------------------------
| PLT_MediaConnect::ProcessGetSCPD
+---------------------------------------------------------------------*/
NPT_Result
PLT_MediaConnect::ProcessGetSCPD(PLT_Service* service,
NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse& response)
{
PLT_DeviceSignature signature = PLT_HttpHelper::GetDeviceSignature(request);
// Override SCPD response by providing an SCPD without a Search action
// to all devices except XBox or WMP which need it
if (service->GetServiceType() == "urn:schemas-upnp-org:service:ContentDirectory:1" &&
signature != PLT_DEVICE_XBOX && signature != PLT_DEVICE_WMP && signature != PLT_DEVICE_SONOS) {
NPT_HttpEntity* entity;
PLT_HttpHelper::SetBody(response, (const char*) MS_ContentDirectorySCPD, &entity);
entity->SetContentType("text/xml; charset=\"utf-8\"");
return NPT_SUCCESS;
}
return PLT_MediaServer::ProcessGetSCPD(service, request, context, response);
}
示例10: SetupResponse
NPT_Result SetupResponse(NPT_HttpRequest& request,
NPT_HttpResponse& response,
NPT_SocketInfo& /*info*/) {
NPT_String msg = "<HTML>";
msg += "PATH=";
msg += request.GetUrl().GetPath();
msg += " <P><UL>";
if (request.GetUrl().HasQuery()) {
NPT_HttpUrlQuery query(request.GetUrl().GetQuery());
for (NPT_List<NPT_HttpUrlQuery::Field>::Iterator it = query.GetFields().GetFirstItem();
it;
++it) {
NPT_HttpUrlQuery::Field& field = *it;
msg += "<LI>";
msg += field.m_Name;
msg += " = ";
msg += field.m_Value;
msg += " </LI>";
}
}
msg += "</UL></HTML>";
if (request.GetMethod() == NPT_HTTP_METHOD_POST) {
NPT_DataBuffer request_body;
request.GetEntity()->Load(request_body);
NPT_Debug("REQUEST: body = %d bytes\n", request_body.GetDataSize());
NPT_Debug("REQUEST: content type = %s\n", request.GetEntity()->GetContentType().GetChars());
if (request.GetEntity()->GetContentType().StartsWith("text") ||
request.GetEntity()->GetContentType() == "application/x-www-form-urlencoded") {
NPT_String body_string;
body_string.Assign((char*)request_body.GetData(), request_body.GetDataSize());
NPT_Debug("%s", body_string.GetChars());
}
}
NPT_HttpEntity* entity = response.GetEntity();
entity->SetContentType("text/html");
entity->SetInputStream(msg);
return NPT_SUCCESS;
}
示例11: NPT_COMPILER_UNUSED
/*----------------------------------------------------------------------
| PLT_Downloader::ProcessResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_Downloader::ProcessResponse(NPT_Result res,
NPT_HttpRequest* request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse* response)
{
NPT_COMPILER_UNUSED(request);
NPT_COMPILER_UNUSED(context);
if (NPT_FAILED(res)) {
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
m_State = PLT_DOWNLOADER_DOWNLOADING;
NPT_HttpEntity* entity;
NPT_InputStreamReference body;
if (!response || !(entity = response->GetEntity()) ||
NPT_FAILED(entity->GetInputStream(body)) || body.IsNull()) {
m_State = PLT_DOWNLOADER_ERROR;
return NPT_FAILURE;
}
// Read body (no content length means until socket is closed)
res = NPT_StreamToStreamCopy(*body.AsPointer(),
*m_Output.AsPointer(),
0,
entity->GetContentLength());
if (NPT_FAILED(res)) {
m_State = PLT_DOWNLOADER_ERROR;
return res;
}
m_State = PLT_DOWNLOADER_SUCCESS;
return NPT_SUCCESS;
}
示例12: bytes
/*----------------------------------------------------------------------
| 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;
}
示例13: PLT_LOG_HTTP_REQUEST
/*----------------------------------------------------------------------
| PLT_HttpStreamRequestHandler::SetupResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpStreamRequestHandler::SetupResponse(NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse& response)
{
PLT_LOG_HTTP_REQUEST(NPT_LOG_LEVEL_FINE, "PLT_HttpStreamRequestHandler::SetupResponse:", &request);
if (request.GetMethod().Compare("GET") &&
request.GetMethod().Compare("HEAD")) {
return NPT_FAILURE;
}
NPT_Reference<PLT_FrameBuffer> buffer;
if (!m_StreamValidator.OnNewRequestAccept(request, context, response, buffer)) {
return NPT_ERROR_NO_SUCH_ITEM;
}
response.SetProtocol(NPT_HTTP_PROTOCOL_1_0);
response.GetHeaders().SetHeader(NPT_HTTP_HEADER_CONNECTION, "close");
response.GetHeaders().SetHeader("Cache-Control", "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0");
response.GetHeaders().SetHeader("Pragma", "no-cache");
response.GetHeaders().SetHeader("Expires", "Tue, 4 Jan 2000 02:43:05 GMT");
// HEAD request has no entity or if status code is not 2xx
if (!request.GetMethod().Compare("HEAD") || response.GetStatusCode()/100 != 2)
return NPT_SUCCESS;
NPT_HttpEntity* entity = response.GetEntity();
NPT_CHECK_POINTER_FATAL(entity);
entity->SetContentType("multipart/x-mixed-replace;boundary=" BOUNDARY);
NPT_InputStreamReference body(new PLT_InputFrameStream(buffer, BOUNDARY));
entity->SetInputStream(body, false);
return NPT_SUCCESS;
}
示例14: Test5
/*----------------------------------------------------------------------
| Test5
+---------------------------------------------------------------------*/
static bool
Test5(NPT_HttpUrl url)
{
NPT_LOG_INFO("########### TEST 5 ######################");
NPT_HttpClient client;
// first request
NPT_HttpRequest request(url, NPT_HTTP_METHOD_POST, NPT_HTTP_PROTOCOL_1_1);
NPT_HttpEntity* request_entity = new NPT_HttpEntity();
request_entity->SetInputStream("Testing");
request.SetEntity(request_entity);
NPT_HttpResponse* response = NULL;
client.SendRequest(request, response);
NPT_HttpEntity* entity = NULL;
if (response && (entity = response->GetEntity())) {
NPT_DataBuffer buffer;
if (NPT_FAILED(entity->Load(buffer))) return false;
}
// try again
delete response;
response = NULL;
request_entity = new NPT_HttpEntity();
request_entity->SetInputStream("Testing2");
request.SetEntity(request_entity);
client.SendRequest(request, response);
entity = NULL;
if (response && (entity = response->GetEntity())) {
NPT_DataBuffer buffer;
if (NPT_FAILED(entity->Load(buffer))) return false;
}
return true;
}
示例15: NPT_HttpEntity
/*----------------------------------------------------------------------
| PLT_HttpServerSocketTask::RespondToClient
+---------------------------------------------------------------------*/
NPT_Result
PLT_HttpServerSocketTask::RespondToClient(NPT_HttpRequest& request,
const NPT_HttpRequestContext& context,
NPT_HttpResponse*& response)
{
NPT_Result result = NPT_ERROR_NO_SUCH_ITEM;
// reset output params first
response = NULL;
// prepare the response body
NPT_HttpEntity* body = new NPT_HttpEntity();
response = new NPT_HttpResponse(200, "OK", NPT_HTTP_PROTOCOL_1_1);
response->SetEntity(body);
// ask to setup the response
result = SetupResponse(request, context, *response);
// handle result
if (result == NPT_ERROR_NO_SUCH_ITEM) {
body->SetInputStream(PLT_HTTP_DEFAULT_404_HTML);
body->SetContentType("text/html");
response->SetStatus(404, "Not Found");
} else if (result == NPT_ERROR_PERMISSION_DENIED) {
body->SetInputStream(PLT_HTTP_DEFAULT_403_HTML);
body->SetContentType("text/html");
response->SetStatus(403, "Forbidden");
} else if (result == NPT_ERROR_TERMINATED) {
// mark that we want to exit
delete response;
response = NULL;
} else if (NPT_FAILED(result)) {
body->SetInputStream(PLT_HTTP_DEFAULT_500_HTML);
body->SetContentType("text/html");
response->SetStatus(500, "Internal Error");
}
return NPT_SUCCESS;
}