void Http::TRequestProtocol::Encode(TStreamBuffer& Buffer) { // set a default if method not specified // gr: this was set in the constructor, but now for decoding we want to initialise it blank if ( mMethod.empty() ) mMethod = "GET"; Soy::Assert( mMethod=="GET" || mMethod=="POST", "Invalid method for HTTP request" ); // write request header { std::string HttpVersion; if ( mHost.empty() ) HttpVersion = "HTTP/1.0"; else HttpVersion = "HTTP/1.1"; std::stringstream RequestHeader; RequestHeader << mMethod << " /" << mUrl << " " << HttpVersion << "\r\n"; Buffer.Push( RequestHeader.str() ); } //mHeaders["Accept"] = "text/html"; if ( !mHost.empty() ) mHeaders["Host"] = mHost; //mHeaders["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36"; BakeHeaders(); WriteHeaders( Buffer ); WriteContent( Buffer ); }
wyBool ExportAsSimpleSQL::StartSQLExport(ExportSQLData *data) { DWORD dwbyteswritten; wyInt32 ret; wyString error; Init(data); //first add in a buffer and then write to file if(WriteHeaders(data) == wyFalse) return wyFalse; if(WriteSetNames() == wyFalse) return wyFalse; if(data->m_structonly == wyTrue || data->m_structdata == wyTrue) { if(WriteCreateStatement(data) == wyFalse) return wyFalse; } if(data->m_dataonly == wyTrue || data->m_structdata == wyTrue) { if(WriteInsertStatement(data) == wyFalse) return wyFalse; } //if filebuffer is not empty then write to file if(m_buffer.GetLength() != 0) { ret = WriteFile(m_filename, m_buffer.GetString(), m_buffer.GetLength(), &dwbyteswritten, NULL); m_buffer.Clear(); if(!ret) { error.Sprintf(_("Error no : %d"), GetLastError()); MessageBox(m_hwnd, error.GetAsWideChar(), pGlobals->m_appname.GetAsWideChar(), MB_OK | MB_ICONERROR); return wyFalse; } } return wyTrue; }
bool wxTarOutputStream::PutNextEntry(wxTarEntry *entry) { wxTarEntryPtr e(entry); if (!CloseEntry()) return false; if (!m_tarsize) { wxLogNull nolog; m_tarstart = m_parent_o_stream->TellO(); } if (m_tarstart != wxInvalidOffset) m_headpos = m_tarstart + m_tarsize; if (WriteHeaders(*e)) { m_pos = 0; m_maxpos = 0; m_size = GetDataSize(*e); if (m_tarstart != wxInvalidOffset) m_datapos = m_tarstart + m_tarsize; // types that are not allowed any data const char nodata[] = { wxTAR_LNKTYPE, wxTAR_SYMTYPE, wxTAR_CHRTYPE, wxTAR_BLKTYPE, wxTAR_DIRTYPE, wxTAR_FIFOTYPE, 0 }; int typeflag = e->GetTypeFlag(); // pax does now allow data for wxTAR_LNKTYPE if (!m_pax || typeflag != wxTAR_LNKTYPE) if (strchr(nodata, typeflag) != NULL) CloseEntry(); } return IsOk(); }
void Http::TResponseProtocol::Encode(TStreamBuffer& Buffer) { size_t ResultCode = Response_OK; std::string ResultString; // specific response if ( mResponseCode != 0 ) { ResultCode = mResponseCode; } if ( !mUrl.empty() ) { ResultString = mUrl; } if ( ResultString.empty() ) ResultString = GetDefaultResponseString( ResultCode ); // write request header { std::string HttpVersion; /* if ( mHost.empty() ) HttpVersion = "HTTP/1.0"; else */ HttpVersion = "HTTP/1.1"; std::stringstream RequestHeader; RequestHeader << HttpVersion << " " << ResultCode << " " << ResultString << "\r\n"; Buffer.Push( RequestHeader.str() ); } BakeHeaders(); WriteHeaders( Buffer ); WriteContent( Buffer ); }
void CCentralDir::Write() { if (m_bOnDisk) return; if (!m_pStorage->IsSpanMode()) { m_pStorage->Flush(); m_pStorage->m_file.SeekToEnd(); } if (m_szComment.GetLength() > USHRT_MAX) m_szComment = m_szComment.Left(USHRT_MAX); m_uCommentSize = (WORD)m_szComment.GetLength(); m_uEntriesNumber = (WORD)m_headers.GetSize(); m_uSize = 0; bool bDontAllowDiskChange = false; // if there is a disk spanning archive in creation and it is only one-volume, // (current disk is 0 so far, no bytes has been written so we know they are // all in the buffer) make sure that it will be after writting central dir // and make it a non disk spanning archive if (m_pStorage->IsSpanMode() && !m_pStorage->GetCurrentDisk()) { DWORD uVolumeFree = m_pStorage->VolumeLeft(); // calculate the size of data descriptors already in the buffer or on the disk // (they will be removed in the non disk spanning archive): // multi span signature at the beginnig (4 bytes) + the size of the data // descr. for each file (multi span signature + 12 bytes data) // the count of bytes to add: central dir size - total to remove; DWORD uToGrow = GetSize(true) - (4 + m_uEntriesNumber * (4 + 12)); if (uVolumeFree >= uToGrow) // lets make sure it will be one-disk archive { // can the operation be done only in the buffer? if (!m_pStorage->m_iBytesWritten && // no bytes on the disk yet (m_pStorage->GetFreeInBuffer() >= uToGrow)) // is the buffer big enough? { RemoveDataDescr(true); bDontAllowDiskChange = true; // if the disk change occurs somehow, we'll throw an error later } else { m_pStorage->Flush(); m_pStorage->m_file.Flush(); if (RemoveDataDescr(false)) bDontAllowDiskChange = true; // if the disk change occurs somehow, we'll throw an error later } } } WriteHeaders(); m_uThisDisk = (WORD)m_pStorage->GetCurrentDisk(); DWORD uSize = WriteCentralEnd(); if (bDontAllowDiskChange && (m_pStorage->GetCurrentDisk() != 0)) ThrowError(ZIP_BADZIPFILE); // if after adding a central directory there is a disk change, // update the information and write it again if (m_uThisDisk != m_pStorage->GetCurrentDisk()) { m_uThisDisk = (WORD)m_pStorage->GetCurrentDisk(); if (m_uEntriesNumber) { m_uDiskEntriesNo = 0; } else { m_uDiskWithCD = m_uThisDisk; m_uOffset = 0; } if (m_pStorage->m_uBytesInWriteBuffer >= uSize) // if the data is still in the buffer, simply remove it m_pStorage->m_uBytesInWriteBuffer -= uSize; else { m_pStorage->Flush(); m_pStorage->m_iBytesWritten -= uSize; m_pStorage->m_file.SeekToBegin(); } WriteCentralEnd(); } }
void CZipCentralDir::Write(CZipActionCallback* pCallback) { if (m_info.m_bOnDisk) return; if (!m_pStorage->IsSpanMode()) { m_pStorage->Flush(); m_pStorage->m_pFile->SeekToEnd(); } // else // // we are at the end already m_info.m_uEntriesNumber = (WORD)m_headers.GetSize(); m_info.m_uSize = 0; bool bDontAllowDiskChange = false; // if there is a disk spanning archive in creation and it is only one-volume, // (current disk is 0 so far, no bytes has been written so we know they are // all in the buffer) make sure that it will be after writing central dir // and make it a non disk spanning archive if (m_pStorage->IsSpanMode() && m_pStorage->GetCurrentDisk() == 0) { DWORD uVolumeFree = m_pStorage->VolumeLeft(); // calculate the size of data descriptors already in the buffer or on the disk // (they will be removed in the non disk spanning archive): // multi span signature at the beginnig (4 bytes) + the size of the data // descr. for each file (multi span signature + 12 bytes data) // the number of bytes to add: central dir size - total to remove; DWORD uToGrow = GetSize(true) - (4 + m_info.m_uEntriesNumber * (4 + 12)); if (uVolumeFree >= uToGrow) // lets make sure it will be one-disk archive { // can the operation be done only in the buffer? if (!m_pStorage->m_iBytesWritten && // no bytes on the disk yet (m_pStorage->GetFreeInBuffer() >= uToGrow)) // is the buffer big enough? { RemoveDataDescr(true); bDontAllowDiskChange = true; // if the disk change occurs somehow, we'll throw an error later } else { m_pStorage->Flush(); if (RemoveDataDescr(false)) bDontAllowDiskChange = true; // if the disk change occurs somehow, we'll throw an error later } } } try { WriteHeaders(pCallback, bDontAllowDiskChange || !m_pStorage->IsSpanMode()); m_info.m_uThisDisk = (WORD)m_pStorage->GetCurrentDisk(); DWORD uSize = WriteCentralEnd(); if (bDontAllowDiskChange) { if (m_pStorage->GetCurrentDisk() != 0) ThrowError(CZipException::badZipFile); } // if after adding a central directory there is a disk change, // update the information and write it again if (m_info.m_uThisDisk != m_pStorage->GetCurrentDisk()) { m_info.DiskChange(m_pStorage->GetCurrentDisk()); if (m_pStorage->m_uBytesInWriteBuffer >= uSize) // if the data is still in the buffer, simply remove it m_pStorage->m_uBytesInWriteBuffer -= uSize; else { m_pStorage->Flush(); m_pStorage->m_iBytesWritten -= uSize; m_pStorage->m_pFile->SeekToBegin(); } WriteCentralEnd(); } } catch (...) { if (bDontAllowDiskChange) { m_pStorage->FinalizeSpan(); m_info.m_uThisDisk = 0; } throw; } m_info.m_bOnDisk = true; }
nsresult nsEudoraCompose::CopyComposedMessage( nsCString& fromLine, nsIFile *pSrc, nsIOutputStream *pDst, SimpleBufferTonyRCopiedOnce& copy) { copy.m_bytesInBuf = 0; copy.m_writeOffset = 0; ReadFileState state; state.pFile = pSrc; state.offset = 0; state.size = 0; pSrc->GetFileSize( &state.size); if (!state.size) { IMPORT_LOG0( "*** Error, unexpected zero file size for composed message\n"); return( NS_ERROR_FAILURE); } nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(state.pInputStream), pSrc); if (NS_FAILED( rv)) { IMPORT_LOG0( "*** Error, unable to open composed message file\n"); return( NS_ERROR_FAILURE); } PRUint32 written; rv = pDst->Write( fromLine.get(), fromLine.Length(), &written); // well, isn't this a hoot! // Read the headers from the new message, get the ones we like // and write out only the headers we want from the new message, // along with all of the other headers from the "old" message! if (NS_SUCCEEDED( rv)) rv = FillMailBuffer( &state, copy); if (NS_SUCCEEDED( rv)) rv = ReadHeaders( &state, copy, m_readHeaders); if (NS_SUCCEEDED( rv)) { rv = WriteHeaders( pDst, m_readHeaders); } // We need to go ahead and write out the rest of the copy buffer // so that the following will properly copy the rest of the body char lastChar = 0; rv = EscapeFromSpaceLine(pDst, copy.m_pBuffer + copy.m_writeOffset, copy.m_pBuffer+copy.m_bytesInBuf); if (copy.m_bytesInBuf) lastChar = copy.m_pBuffer[copy.m_bytesInBuf - 1]; if (NS_SUCCEEDED(rv)) copy.m_writeOffset = copy.m_bytesInBuf; while ((state.offset < state.size) && NS_SUCCEEDED( rv)) { rv = FillMailBuffer( &state, copy); if (NS_SUCCEEDED( rv)) { rv = EscapeFromSpaceLine(pDst, copy.m_pBuffer + copy.m_writeOffset, copy.m_pBuffer+copy.m_bytesInBuf); lastChar = copy.m_pBuffer[copy.m_bytesInBuf - 1]; if (NS_SUCCEEDED( rv)) copy.m_writeOffset = copy.m_bytesInBuf; else IMPORT_LOG0( "*** Error writing to destination mailbox\n"); } } state.pInputStream->Close(); if ((lastChar != 0x0A) && NS_SUCCEEDED( rv)) { rv = pDst->Write( "\x0D\x0A", 2, &written); if (written != 2) rv = NS_ERROR_FAILURE; } return( rv); }
NS_IMETHODIMP ImportMessageRunnable::Run() { nsCOMPtr<nsIMsgPluggableStore> msgStore; nsresult rv = mFolder->GetMsgStore(getter_AddRefs(msgStore)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsILineInputStream> lineStream; rv = nsBeckyUtils::CreateLineInputStream(mMessageFile, getter_AddRefs(lineStream)); NS_ENSURE_SUCCESS(rv, rv); bool reusable; nsCOMPtr<nsIMsgDBHdr> msgHdr; nsCOMPtr<nsIOutputStream> outputStream; rv = msgStore->GetNewMsgOutputStream(mFolder, getter_AddRefs(msgHdr), &reusable, getter_AddRefs(outputStream)); NS_ENSURE_SUCCESS(rv, rv); bool inHeader = true; bool more = true; nsAutoCString headers; while (NS_SUCCEEDED(rv) && more) { nsAutoCString line; rv = lineStream->ReadLine(line, &more); if (NS_FAILED(rv)) break; if (inHeader) { if (IsEndOfHeaders(line)) { inHeader = false; rv = WriteHeaders(headers, outputStream); } else { rv = HandleHeaderLine(line, headers); } } else if (IsEndOfMessage(line)) { inHeader = true; rv = msgStore->FinishNewMessage(outputStream, msgHdr); if (!reusable) outputStream->Close(); rv = msgStore->GetNewMsgOutputStream(mFolder, getter_AddRefs(msgHdr), &reusable, getter_AddRefs(outputStream)); } else if (IsBeckyIncludeLine(line)) { rv = WriteAttachmentFile(mMessageFile, line, outputStream); } else { uint32_t writtenBytes = 0; if (StringBeginsWith(line, NS_LITERAL_CSTRING(".."))) line.Cut(0, 1); else if (CheckHeaderKey(line, "From")) line.Insert('>', 0); line.AppendLiteral(MSG_LINEBREAK); rv = outputStream->Write(line.get(), line.Length(), &writtenBytes); } } if (outputStream) { if (NS_FAILED(rv)) msgStore->DiscardNewMessage(outputStream, msgHdr); outputStream->Close(); } return rv; }