/*---------------------------------------------------------------------- | NPT_Zip::Deflate +---------------------------------------------------------------------*/ NPT_Result NPT_Zip::Deflate(NPT_File& in, NPT_File& out, int compression_level, Format format /* = ZLIB */) { // check parameters if (compression_level < NPT_ZIP_COMPRESSION_LEVEL_DEFAULT || compression_level > NPT_ZIP_COMPRESSION_LEVEL_MAX) { return NPT_ERROR_INVALID_PARAMETERS; } NPT_InputStreamReference input; NPT_CHECK(in.GetInputStream(input)); NPT_OutputStreamReference output; NPT_CHECK(out.GetOutputStream(output)); NPT_ZipDeflatingInputStream deflating_stream(input, compression_level, format); return NPT_StreamToStreamCopy(deflating_stream, *output.AsPointer()); }
/*---------------------------------------------------------------------- | NPT_LogFileHandler::Log +---------------------------------------------------------------------*/ void NPT_LogFileHandler::Log(const NPT_LogRecord& record) { if (m_MaxFilesize > 0) { /* get current file size */ NPT_LargeSize size; NPT_File::GetSize(m_Filename, size); /* time to recycle ? */ if (size > m_MaxFilesize) { /* release stream to force a reopen later and to be able to rename file */ m_Stream = NULL; /* rename file using current time */ NPT_TimeStamp now; NPT_System::GetCurrentTimeStamp(now); NPT_String suffix = NPT_DateTime(now, true).ToString(NPT_DateTime::FORMAT_W3C); suffix.Replace(':', '_'); NPT_String new_name = NPT_FilePath::Create( NPT_FilePath::DirName(m_Filename), NPT_FilePath::BaseName(m_Filename, false) + "-" + suffix + NPT_FilePath::FileExtension(m_Filename)); NPT_File::Rename(m_Filename, new_name); } } /* try to reopen the file if it failed to open previously or if we rotated it */ if (m_Stream.IsNull()) { Open(m_Append); } if (m_Stream.AsPointer()) { NPT_Log::FormatRecordToStream(record, *m_Stream, false, m_FormatFilter); if (m_Flush) m_Stream->Flush(); } }
/*---------------------------------------------------------------------- | PLT_HttpClient::SendRequest +---------------------------------------------------------------------*/ NPT_Result PLT_HttpClient::SendRequest(NPT_OutputStreamReference& output_stream, NPT_HttpRequest& request, NPT_Timeout timeout) { NPT_COMPILER_UNUSED(timeout); // connect to the server NPT_LOG_FINE("Sending:"); PLT_LOG_HTTP_MESSAGE(NPT_LOG_LEVEL_FINE, &request); // add any headers that may be missing NPT_HttpHeaders& headers = request.GetHeaders(); //headers.SetHeader(NPT_HTTP_HEADER_CONNECTION, "close"); if (!headers.GetHeader(NPT_HTTP_HEADER_USER_AGENT)) { headers.SetHeader(NPT_HTTP_HEADER_USER_AGENT, "Platinum/" PLT_PLATINUM_VERSION_STRING); } // set host only if not already set if (!headers.GetHeader(NPT_HTTP_HEADER_HOST)) { NPT_String host = request.GetUrl().GetHost(); if (request.GetUrl().GetPort() != NPT_HTTP_DEFAULT_PORT) { host += ":"; host += NPT_String::FromInteger(request.GetUrl().GetPort()); } headers.SetHeader(NPT_HTTP_HEADER_HOST, host); } // get the request entity to set additional headers NPT_InputStreamReference body_stream; NPT_HttpEntity* entity = request.GetEntity(); if (entity && NPT_SUCCEEDED(entity->GetInputStream(body_stream))) { // content length headers.SetHeader(NPT_HTTP_HEADER_CONTENT_LENGTH, NPT_String::FromInteger(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); } } else { // force content length to 0 is there is no message body headers.SetHeader(NPT_HTTP_HEADER_CONTENT_LENGTH, "0"); } // create a memory stream to buffer the headers NPT_MemoryStream header_stream; // emit the request headers into the header buffer request.Emit(header_stream); // send the headers NPT_CHECK_SEVERE(output_stream->WriteFully(header_stream.GetData(), header_stream.GetDataSize())); // send request body if (!body_stream.IsNull() && entity->GetContentLength()) { NPT_CHECK_SEVERE(NPT_StreamToStreamCopy(*body_stream.AsPointer(), *output_stream.AsPointer())); } // flush the output stream so that everything is sent to the server output_stream->Flush(); return NPT_SUCCESS; }