XBOX::VError VHTTPServerLog::_WriteFileHeader (XBOX::VString& ioBuffer) { /* see: http://www.w3.org/TR/WD-logfile.html */ XBOX::VString serverName; XBOX::VString dateString; XBOX::VString formatName; XBOX::VString tokenNames; fNeedHeader = false; /* #Version: 1.0 #Date: 12-Jan-1996 00:00:00 */ HTTPProtocol::MakeServerString (serverName); _GetCurrentFormatedDate (dateString); HTTPServerTools::GetLogFormatName (fSettings.GetLogFormat(), formatName); HTTPServerTools::GetLogTokenNamesList (fSettings.GetLogTokens(), tokenNames); ioBuffer.AppendCString ("#Version: 1.0\n#Software: "); ioBuffer.AppendString (serverName); ioBuffer.AppendCString ("\n#Date: "); ioBuffer.AppendString (dateString); ioBuffer.AppendCString ("\n#Remark: format "); ioBuffer.AppendString (formatName); ioBuffer.AppendCString ("\n#Fields: "); ioBuffer.AppendString (tokenNames); ioBuffer.AppendUniChar (HTTP_LF); return XBOX::VE_OK; }
/* private */ void VVirtualFolder::_NormalizeFolder() { assert (NULL != fFolder); if (!fFolder->Exists()) fFolder->CreateRecursive(); if (fFolder->Exists()) { XBOX::VFilePath folderPath (fFolder->GetPath()); XBOX::VFilePath indexPath; indexPath = folderPath.ToSubFile (fIndexFileName); if (indexPath.IsFile()) { XBOX::VFile indexFile (indexPath); if (!indexFile.Exists()) { XBOX::VFolder *componentFolder = VHTTPServer::RetainComponentFolder (kBF_RESOURCES_FOLDER); if (NULL != componentFolder) { XBOX::VFilePath defaultIndexPath = componentFolder->GetPath(); XBOX::DialectCode dialectCode = XBOX::VIntlMgr::GetDefaultMgr()->GetCurrentDialectCode(); XBOX::VString languageCode; XBOX::VString fileName; XBOX::VIntlMgr::GetDefaultMgr()->GetISO6391LanguageCode (dialectCode, languageCode); fileName.AppendCString ("index_"); fileName.AppendString (languageCode); fileName.AppendCString (".html"); defaultIndexPath.ToSubFolder (CVSTR ("Default Page")).ToSubFile (fileName); if (defaultIndexPath.IsFile()) { XBOX::VFile defaultIndexFile (defaultIndexPath); if (defaultIndexFile.Exists()) indexFile.CopyFrom (defaultIndexFile); } XBOX::QuickReleaseRefCountable (componentFolder); } } } } }
BEGIN_TOOLBOX_NAMESPACE USING_TOOLBOX_NAMESPACE //-------------------------------------------------------------------------------------------------- static void _GetRFC1123DateString (const XBOX::VTime& inDate, XBOX::VString& outString) { sWORD year, month, day, hour, minute, second, millisecond; inDate.GetUTCTime (year, month, day, hour, minute, second, millisecond); outString.Clear(); switch (inDate.GetWeekDay()) { case 0: outString.AppendCString ("Sun"); break; case 1: outString.AppendCString ("Mon"); break; case 2: outString.AppendCString ("Tue"); break; case 3: outString.AppendCString ("Wed"); break; case 4: outString.AppendCString ("Thu"); break; case 5: outString.AppendCString ("Fri"); break; case 6: outString.AppendCString ("Sat"); break; } outString.AppendPrintf (", %02d-", day); switch (month) { case 1: outString.AppendCString ("Jan"); break; case 2: outString.AppendCString ("Feb"); break; case 3: outString.AppendCString ("Mar"); break; case 4: outString.AppendCString ("Apr"); break; case 5: outString.AppendCString ("May"); break; case 6: outString.AppendCString ("Jun"); break; case 7: outString.AppendCString ("Jul"); break; case 8: outString.AppendCString ("Aug"); break; case 9: outString.AppendCString ("Sep"); break; case 10: outString.AppendCString ("Oct"); break; case 11: outString.AppendCString ("Nov"); break; case 12: outString.AppendCString ("Dec"); break; } outString.AppendPrintf ("-%04d %02d:%02d:%02d GMT", year, hour, minute, second); }
XBOX::VError VHTTPServerProject::_BuildRegexMatcher (const XBOX::VString& inPatternString, XBOX::VRegexMatcher **outMatcher) { if (NULL == outMatcher) return XBOX::VE_INVALID_PARAMETER; XBOX::VString patternString (inPatternString); XBOX::VError error = XBOX::VE_OK; sLONG pos = 0; if (!HTTPServerTools::BeginsWithASCIICString (patternString.GetCPointer(), "(?i)")) patternString.Insert (CVSTR ("(?i)"), 1); if (HTTPServerTools::EndsWithASCIICString (patternString, "/") && !HTTPServerTools::EndsWithASCIICString (patternString, "$")) patternString.AppendString (CVSTR (".*")); XBOX::VString string; if ((pos = patternString.FindUniChar (CHAR_CIRCUMFLEX_ACCENT)) == 0) { string.FromCString ("^"); pos = HTTPServerTools::FindASCIICString (patternString, "(?i)") + 4; } else { ++pos; } #if HTTP_SERVER_USE_PROJECT_PATTERNS if (!fSettings->GetProjectPattern().IsEmpty()) { string.AppendCString ("(/"); string.AppendString (fSettings->GetProjectPattern()); string.AppendCString ("|)"); } #endif patternString.Insert (string, pos); if (!patternString.IsEmpty()) *outMatcher = XBOX::VRegexMatcher::Create (patternString, &error); return error; }
XBOX::VError VHTTPResponse::_SendResponseHeader() { if (fHeaderSent) return XBOX::VE_OK; // Not sure we have to report the error _NormalizeResponseHeader(); // Write Status-Line XBOX::VString string; XBOX::VString fieldValue; HTTPProtocol::MakeStatusLine (fHTTPVersion, fResponseStatusCode, string); /* Write Server Header */ if (GetHeaders().IsHeaderSet (STRING_HEADER_SERVER)) GetHeaders().RemoveHeader (STRING_HEADER_SERVER); HTTPProtocol::MakeServerString (fieldValue, false, true); string.AppendString (fieldValue); string.AppendCString (HTTP_CRLF); /* Write Date Header */ if (!GetHeaders().IsHeaderSet (STRING_HEADER_DATE)) { HTTPProtocol::MakeRFC822GMTDateString (GMT_NOW, fieldValue, true); string.AppendString (fieldValue); string.AppendCString (HTTP_CRLF); } // Write Headers GetHeaders().ToString (string); string.AppendCString (HTTP_CRLF); XBOX::StStringConverter<char> converter (string, XBOX::VTC_UTF_8); char * buffer = (char *)converter.GetCPointer(); uLONG bufferSize = (uLONG)converter.GetSize(); XBOX::VError error = _WriteToSocket (buffer, &bufferSize); fHeaderSent = (XBOX::VE_OK == error); return error; }
void MakeIPv4AddressString (IP4 inIPv4, XBOX::VString& outIPv4String) { struct in_addr addr = {0}; char * buffer = NULL; addr.s_addr = htonl (inIPv4); buffer = inet_ntoa (addr); if (NULL != buffer) outIPv4String.AppendCString (buffer); else outIPv4String.Clear(); }
XBOX::VString VHTTPCookie::ToString (bool inAlwaysExpires) const { XBOX::VString result; if (!IsValid()) return result; result.AppendString (fName); result.AppendUniChar (CHAR_EQUALS_SIGN); if (0 == fVersion) { // Netscape cookie result.AppendString (fValue); if (!fDomain.IsEmpty()) { result.AppendCString ("; Domain="); result.AppendString (fDomain); } if (!fPath.IsEmpty()) { result.AppendCString ("; Path="); result.AppendString (fPath); } if (fMaxAge >= 0) { XBOX::VTime curTime; XBOX::VString timeString; XBOX::VTime::Now (curTime); curTime.AddSeconds (fMaxAge); _GetRFC1123DateString (curTime, timeString); result.AppendCString ("; Expires="); result.AppendString (timeString); } if (fSecure) { result.AppendCString ("; Secure"); } if (fHTTPOnly) { result.AppendCString ("; HttpOnly"); } } else { // RFC 2109 cookie result.AppendString (fValue); if (!fComment.IsEmpty()) { result.AppendCString ("; Comment=\""); result.AppendString (fComment); result.AppendUniChar (CHAR_QUOTATION_MARK); } if (!fDomain.IsEmpty()) { result.AppendCString ("; Domain="); result.AppendString (fDomain); } if (!fPath.IsEmpty()) { result.AppendCString ("; Path="); result.AppendString (fPath); } else { result.AppendCString ("; Path=/"); } if (fMaxAge >= 0) { result.AppendCString ("; Max-Age="); result.AppendLong (fMaxAge); /* For Internet Explorer 6, 7 & 8 which does not support 'max-age' */ if (inAlwaysExpires) { XBOX::VTime curTime; XBOX::VString timeString; XBOX::VTime::Now (curTime); curTime.AddSeconds (fMaxAge); _GetRFC1123DateString (curTime, timeString); result.AppendCString ("; Expires="); result.AppendString (timeString); } } if (fSecure) { result.AppendCString ("; Secure"); } if (fHTTPOnly) { result.AppendCString ("; HttpOnly"); } result.AppendCString ("; Version=1"); } return result; }
XBOX::VError VMIMEMessage::ToStream (XBOX::VStream& outStream, sLONG inEncoding) { XBOX::VError error = XBOX::VE_OK; if (XBOX::VE_OK == outStream.OpenWriting()) { outStream.SetCarriageReturnMode(eCRM_CRLF); if (!fMIMEParts.empty()) { XBOX::VString string; XBOX::VString charsetName; bool bEncodeBody; // Encode using base64. for (XBOX::VectorOfMIMEPart::const_iterator it = fMIMEParts.begin(); it != fMIMEParts.end(); ++it) { outStream.PutPrintf ("\r\n--%S\r\n", &fBoundary); string.FromCString ("Content-Type: "); string.AppendString ((*it)->GetMediaType()); if (!(*it)->GetFileName().IsEmpty()) { string.AppendCString ("; name=\""); if (!(*it)->GetName().IsEmpty()) string.AppendString ((*it)->GetName()); else string.AppendString ((*it)->GetFileName()); string.AppendCString("\"\r\nContent-Disposition: "); string.AppendCString((*it)->IsInline() ? "inline; " : "attachment; "); string.AppendCString("filename=\""); string.AppendString ((*it)->GetFileName()); string.AppendCString ("\"\r\n"); if (inEncoding == ENCODING_BINARY) { string.AppendCString ("Content-Transfer-Encoding: 8bit\r\n"); bEncodeBody = false; } else { if ((inEncoding == ENCODING_BINARY_ONLY) && ((*it)->GetMediaTypeKind() == MIMETYPE_TEXT)) { bEncodeBody = false; } else { string.AppendCString ("Content-Transfer-Encoding: base64\r\n"); bEncodeBody = true; } } } else { if ((*it)->GetMediaTypeCharSet() != XBOX::VTC_UNKNOWN) { string.AppendCString ("; charset=\""); XBOX::VTextConverters::Get()->GetNameFromCharSet ((*it)->GetMediaTypeCharSet(), charsetName); string.AppendString (charsetName); string.AppendCString ("\""); } if (!(*it)->GetName().IsEmpty()) { string.AppendCString("; name=\""); string.AppendString((*it)->GetName()); string.AppendCString("\""); } string.AppendCString ("\r\n"); if ((*it)->IsInline()) string.AppendCString("Content-Disposition: inline\r\n"); if (inEncoding == ENCODING_7BIT || ((inEncoding == ENCODING_BINARY_ONLY) && ((*it)->GetMediaTypeKind() != MIMETYPE_TEXT))) { string.AppendCString ("Content-Transfer-Encoding: base64\r\n"); bEncodeBody = true; } else { string.AppendCString("Content-Transfer-Encoding: 8bit\r\n"); bEncodeBody = false; } } if ((*it)->GetContentID().GetLength()) { string.AppendCString("Content-ID: <"); string.AppendString((*it)->GetContentID()); string.AppendCString(">\r\n"); } string.AppendCString ("\r\n"); outStream.PutText (string); if (bEncodeBody) { XBOX::VMemoryBuffer<> buffer; if (XBOX::Base64Coder::Encode ((*it)->GetData().GetDataPtr(), (*it)->GetData().GetDataSize(), buffer, kBASE64_QUADS_PER_LINE)) { outStream.PutData (buffer.GetDataPtr(), buffer.GetDataSize()); } } else { outStream.PutData ((*it)->GetData().GetDataPtr(), (*it)->GetData().GetDataSize()); } } outStream.PutPrintf ("\r\n--%S--\r\n", &fBoundary); } outStream.CloseWriting(); } return error; }