/*---------------------------------------------------------------------- | Test1 +---------------------------------------------------------------------*/ static bool Test1(PLT_TaskManager* task_manager, const char* url, NPT_Size& size) { NPT_LOG_INFO("########### TEST 1 ######################"); NPT_MemoryStreamReference memory_stream(new NPT_MemoryStream()); NPT_OutputStreamReference output_stream(memory_stream); PLT_Downloader downloader(task_manager, url, output_stream); downloader.Start(); while (1) { switch(downloader.GetState()) { case PLT_DOWNLOADER_SUCCESS: { size = memory_stream->GetDataSize(); return true; } case PLT_DOWNLOADER_ERROR: return false; default: NPT_System::Sleep(NPT_TimeInterval(0, 10000)); break; } }; return false; }
/*---------------------------------------------------------------------- | Test3 +---------------------------------------------------------------------*/ static bool Test3(PLT_TaskManager* task_manager, NPT_HttpUrl url, PLT_RingBufferStreamReference& ringbuffer_stream, NPT_Size& size) { NPT_LOG_INFO("########### TEST 3 ######################"); /* reset output param first */ size = 0; NPT_MemoryStreamReference memory_stream(new NPT_MemoryStream()); NPT_OutputStreamReference output_stream(memory_stream); PLT_Downloader downloader(task_manager, url, output_stream); downloader.Start(); /* asynchronously write onto ring buffer stream */ task_manager->StartTask(new RingBufferWriterTask(ringbuffer_stream)); /* start pulling data */ while (1) { switch(downloader.GetState()) { case PLT_DOWNLOADER_SUCCESS: size = memory_stream->GetDataSize(); return true; case PLT_DOWNLOADER_ERROR: return false; default: NPT_System::Sleep(NPT_TimeInterval(.1f)); break; } }; return false; }
/*---------------------------------------------------------------------- | NPT_LogConsoleHandler::Log +---------------------------------------------------------------------*/ void NPT_LogConsoleHandler::Log(const NPT_LogRecord& record) { NPT_MemoryStream memory_stream(4096); NPT_Log::FormatRecordToStream(record, memory_stream, m_UseColors, m_FormatFilter); memory_stream.Write("\0", 1); if (m_Outputs & OUTPUT_TO_CONSOLE) { NPT_Console::Output((const char*)memory_stream.GetData()); } if (m_Outputs & OUTPUT_TO_DEBUG) { NPT_DebugOutput((const char*)memory_stream.GetData()); } }
/*---------------------------------------------------------------------- | Test3 +---------------------------------------------------------------------*/ static bool Test3(PLT_TaskManager* task_manager, const char* url, PLT_RingBufferStreamReference& ringbuffer_stream, NPT_Size& size) { NPT_LOG_INFO("########### TEST 3 ######################"); /* reset output param first */ size = 0; NPT_MemoryStreamReference memory_stream(new NPT_MemoryStream()); NPT_OutputStreamReference output_stream(memory_stream); PLT_Downloader downloader(task_manager, url, output_stream); downloader.Start(); /* asynchronously write onto ring buffer stream */ char buffer[32768]; ringbuffer_stream->WriteFully(buffer, 32768); /* mark as done */ ringbuffer_stream->SetEos(); while (1) { switch(downloader.GetState()) { case PLT_DOWNLOADER_SUCCESS: size = memory_stream->GetDataSize(); return true; case PLT_DOWNLOADER_ERROR: return false; default: NPT_System::Sleep(NPT_TimeInterval(0, 10000)); break; } }; return false; }
NPT_Result SetupResponse(NPT_HttpRequest& request, const NPT_HttpRequestContext& context, NPT_HttpResponse& response) { NPT_String msg = "<HTML>"; msg += "PATH="; msg += request.GetUrl().GetPath(); msg += "<P><B>Local Address:</B> "; msg += context.GetLocalAddress().ToString(); msg += "<P>"; msg += "<B>Remote Address:</B> "; msg += context.GetRemoteAddress().ToString(); msg += "<P><UL>"; if (request.GetUrl().HasQuery()) { NPT_UrlQuery query(request.GetUrl().GetQuery()); for (NPT_List<NPT_UrlQuery::Field>::Iterator it = query.GetFields().GetFirstItem(); it; ++it) { NPT_UrlQuery::Field& field = *it; msg += "<LI>"; msg += field.m_Name; msg += " = "; msg += field.m_Value; msg += " </LI>"; // check for a 'delay' field if (field.m_Name == "delay") { NPT_UInt32 delay = 0; field.m_Value.ToInteger(delay); NPT_Debug("DELAY: %d seconds\n", delay); NPT_System::Sleep(NPT_TimeInterval((float)delay)); } } } 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"); NPT_MemoryStreamReference memory_stream( new NPT_MemoryStream((const void*)msg.GetChars(), msg.GetLength())); entity->SetInputStream(memory_stream, !m_Chunked); if (m_Chunked) { entity->SetTransferEncoding(NPT_HTTP_TRANSFER_ENCODING_CHUNKED); } return NPT_SUCCESS; }