/*---------------------------------------------------------------------- | 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)); } }
/*---------------------------------------------------------------------- | 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; }
/*---------------------------------------------------------------------- | DumpBody +---------------------------------------------------------------------*/ static NPT_Result ReadBody(PLT_Downloader& downloader, NPT_InputStreamReference& stream, NPT_Size& size) { NPT_LargeSize avail; char buffer[2048]; NPT_Result ret = NPT_ERROR_WOULD_BLOCK; /* reset output param first */ size = 0; /* we test for availability first to avoid getting stuck in Read forever in case blocking is true and the download is done writing to the stream */ NPT_CHECK(stream->GetAvailable(avail)); if (avail) { ret = stream->Read(buffer, 2048, &size); NPT_LOG_FINER_2("Read %d bytes (result = %d)\n", size, ret); return ret; } else { Plt_DowloaderState state = downloader.GetState(); switch (state) { case PLT_DOWNLOADER_ERROR: return NPT_FAILURE; case PLT_DOWNLOADER_SUCCESS: /* no more data expected */ return NPT_ERROR_EOS; default: NPT_System::Sleep(NPT_TimeInterval(0, 10000)); break; } } return NPT_SUCCESS; }