void CHTTPSock::ReadLine(const CString& sData) { if (m_bGotHeader) { return; } CString sLine = sData; sLine.TrimRight("\r\n"); CString sName = sLine.Token(0); if (sName.Equals("GET")) { m_bPost = false; m_sURI = sLine.Token(1); m_bHTTP10Client = sLine.Token(2).Equals("HTTP/1.0"); ParseURI(); } else if (sName.Equals("POST")) { m_bPost = true; m_sURI = sLine.Token(1); ParseURI(); } else if (sName.Equals("Cookie:")) { VCString vsNV; sLine.Token(1, true).Split(";", vsNV, false, "", "", true, true); for (unsigned int a = 0; a < vsNV.size(); a++) { CString s(vsNV[a]); m_msRequestCookies[s.Token(0, false, "=").Escape_n(CString::EURL, CString::EASCII)] = s.Token(1, true, "=").Escape_n(CString::EURL, CString::EASCII); } } else if (sName.Equals("Authorization:")) { CString sUnhashed; sLine.Token(2).Base64Decode(sUnhashed); m_sUser = sUnhashed.Token(0, false, ":"); m_sPass = sUnhashed.Token(1, true, ":"); m_bLoggedIn = OnLogin(m_sUser, m_sPass); } else if (sName.Equals("Content-Length:")) { m_uPostLen = sLine.Token(1).ToULong(); if (m_uPostLen > MAX_POST_SIZE) PrintErrorPage(413, "Request Entity Too Large", "The request you sent was too large."); } else if (sName.Equals("If-None-Match:")) { // this is for proper client cache support (HTTP 304) on static files: m_sIfNoneMatch = sLine.Token(1, true); } else if (sLine.empty()) { m_bGotHeader = true; if (m_bPost) { m_sPostData = GetInternalReadBuffer(); CheckPost(); } else { GetPage(); } DisableReadLine(); } }
nsresult nsAboutCacheEntry::OpenCacheEntry(nsIURI *uri) { nsresult rv; nsAutoCString clientID, key; bool streamBased = true; rv = ParseURI(uri, clientID, streamBased, key); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsICacheService> serv = do_GetService(NS_CACHESERVICE_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsICacheSession> session; rv = serv->CreateSession(clientID.get(), nsICache::STORE_ANYWHERE, streamBased, getter_AddRefs(session)); if (NS_FAILED(rv)) return rv; rv = session->SetDoomEntriesIfExpired(false); if (NS_FAILED(rv)) return rv; return session->AsyncOpenCacheEntry(key, nsICache::ACCESS_READ, this, true); }
nsresult nsAboutCacheEntry::OpenCacheEntry(nsIURI *uri, nsICacheEntryDescriptor **result) { nsresult rv; nsCAutoString clientID, key; PRBool streamBased = PR_TRUE; rv = ParseURI(uri, clientID, streamBased, key); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsICacheService> serv = do_GetService(NS_CACHESERVICE_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsICacheSession> session; rv = serv->CreateSession(clientID.get(), nsICache::STORE_ANYWHERE, streamBased, getter_AddRefs(session)); if (NS_FAILED(rv)) return rv; rv = session->SetDoomEntriesIfExpired(PR_FALSE); if (NS_FAILED(rv)) return rv; rv = session->OpenCacheEntry(key, nsICache::ACCESS_READ, PR_FALSE, result); return rv; }
QTSS_Error HTTPRequest::ParseRequestLine(StringParser* parser) { // Get the method - If the method is not one of the defined methods // then it doesn't return an error but sets fMethod to httpIllegalMethod StrPtrLen theParsedData; parser->ConsumeWord(&theParsedData); fMethod = HTTPProtocol::GetMethod(&theParsedData); //还有可能是HTTP Response类型 if((fMethod == httpIllegalMethod) && theParsedData.Equal("HTTP")) { parser->ConsumeUntilWhitespace();//过滤掉HTTP/1.1 parser->ConsumeUntilDigit(NULL); UInt32 statusCode = parser->ConsumeInteger(NULL); if( statusCode != 0 ) { fHTTPType = httpResponseType; parser->ConsumeWhitespace(); parser->ConsumeUntilWhitespace(NULL); // Go past the end of line if (!parser->ExpectEOL()) { fStatusCode = httpBadRequest; return QTSS_BadArgument; // Request line is not properly formatted! } return QTSS_NoErr; } } // Consume whitespace parser->ConsumeWhitespace(); // Parse the URI - If it fails returns an error after setting // the fStatusCode to the appropriate error code QTSS_Error err = ParseURI(parser); if (err != QTSS_NoErr) return err; // Consume whitespace parser->ConsumeWhitespace(); // If there is a version, consume the version string StrPtrLen versionStr; parser->ConsumeUntil(&versionStr, StringParser::sEOLMask); // Check the version if (versionStr.Len > 0) fVersion = HTTPProtocol::GetVersion(&versionStr); // Go past the end of line if (!parser->ExpectEOL()) { fStatusCode = httpBadRequest; return QTSS_BadArgument; // Request line is not properly formatted! } return QTSS_NoErr; }
NS_IMETHODIMP nsDataHandler::NewURI(const nsACString &aSpec, const char *aCharset, // ignore charset info nsIURI *aBaseURI, nsIURI **result) { nsresult rv; RefPtr<nsIURI> uri; nsCString spec(aSpec); if (aBaseURI && !spec.IsEmpty() && spec[0] == '#') { // Looks like a reference instead of a fully-specified URI. // --> initialize |uri| as a clone of |aBaseURI|, with ref appended. rv = aBaseURI->Clone(getter_AddRefs(uri)); if (NS_FAILED(rv)) return rv; rv = uri->SetRef(spec); } else { // Otherwise, we'll assume |spec| is a fully-specified data URI nsAutoCString contentType; bool base64; rv = ParseURI(spec, contentType, /* contentCharset = */ nullptr, base64, /* dataBuffer = */ nullptr); if (NS_FAILED(rv)) return rv; // Strip whitespace unless this is text, where whitespace is important // Don't strip escaped whitespace though (bug 391951) if (base64 || (strncmp(contentType.get(),"text/",5) != 0 && contentType.Find("xml") == kNotFound)) { // it's ascii encoded binary, don't let any spaces in spec.StripWhitespace(); } uri = do_CreateInstance(kSimpleURICID, &rv); if (NS_FAILED(rv)) return rv; rv = uri->SetSpec(spec); } if (NS_FAILED(rv)) return rv; uri.forget(result); return rv; }
//returns: StatusLineTooLong, SyntaxError, BadMethod QTSS_Error RTSPRequest::ParseFirstLine(StringParser &parser) { //first get the method StrPtrLen theParsedData; parser.ConsumeWord(&theParsedData); this->SetVal(qtssRTSPReqMethodStr, theParsedData.Ptr, theParsedData.Len); //THIS WORKS UNDER THE ASSUMPTION THAT: //valid HTTP/1.1 headers are: GET, HEAD, POST, PUT, OPTIONS, DELETE, TRACE fMethod = RTSPProtocol::GetMethod(theParsedData); if (fMethod == qtssIllegalMethod) return QTSSModuleUtils::SendErrorResponse(this, qtssClientBadRequest, qtssMsgBadRTSPMethod, &theParsedData); //no longer assume this is a space... instead, just consume whitespace parser.ConsumeWhitespace(); //now parse the uri QTSS_Error err = ParseURI(parser); if (err != QTSS_NoErr) return err; //no longer assume this is a space... instead, just consume whitespace parser.ConsumeWhitespace(); //if there is a version, consume the version string StrPtrLen versionStr; parser.ConsumeUntil(&versionStr, StringParser::sEOLMask); //check the version if (versionStr.Len > 0) fVersion = RTSPProtocol::GetVersion(versionStr); //go past the end of line if (!parser.ExpectEOL()) return QTSSModuleUtils::SendErrorResponse(this, qtssClientBadRequest, qtssMsgNoRTSPVersion,&theParsedData); return QTSS_NoErr; }
NS_IMETHODIMP nsAboutCache::NewChannel(nsIURI *aURI, nsIChannel **result) { NS_ENSURE_ARG_POINTER(aURI); nsresult rv; PRUint32 bytesWritten; *result = nsnull; /* nsXPIDLCString path; rv = aURI->GetPath(getter_Copies(path)); if (NS_FAILED(rv)) return rv; PRBool clear = PR_FALSE; PRBool leaks = PR_FALSE; nsCAutoString p(path); PRInt32 pos = p.Find("?"); if (pos > 0) { nsCAutoString param; (void)p.Right(param, p.Length() - (pos+1)); if (param.Equals("new")) statType = nsTraceRefcnt::NEW_STATS; else if (param.Equals("clear")) clear = PR_TRUE; else if (param.Equals("leaks")) leaks = PR_TRUE; } */ // Get the cache manager service nsCOMPtr<nsICacheService> cacheService = do_GetService(NS_CACHESERVICE_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; nsCOMPtr<nsIStorageStream> storageStream; nsCOMPtr<nsIOutputStream> outputStream; // Init: (block size, maximum length) rv = NS_NewStorageStream(256, (PRUint32)-1, getter_AddRefs(storageStream)); if (NS_FAILED(rv)) return rv; rv = storageStream->GetOutputStream(0, getter_AddRefs(outputStream)); if (NS_FAILED(rv)) return rv; mBuffer.AssignLiteral( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n" " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" "<head>\n<title>Information about the Cache Service</title>\n</head>\n" "<body>\n<div>\n"); outputStream->Write(mBuffer.get(), mBuffer.Length(), &bytesWritten); rv = ParseURI(aURI, mDeviceID); if (NS_FAILED(rv)) return rv; mStream = outputStream; rv = cacheService->VisitEntries(this); if (NS_FAILED(rv)) return rv; if (!mDeviceID.IsEmpty()) { mBuffer.AssignLiteral("</pre>\n"); } else { mBuffer.Truncate(); } mBuffer.AppendLiteral("</div>\n</body>\n</html>\n"); outputStream->Write(mBuffer.get(), mBuffer.Length(), &bytesWritten); nsCOMPtr<nsIInputStream> inStr; rv = storageStream->NewInputStream(0, getter_AddRefs(inStr)); if (NS_FAILED(rv)) return rv; nsIChannel* channel; rv = NS_NewInputStreamChannel(&channel, aURI, inStr, NS_LITERAL_CSTRING("text/html"), NS_LITERAL_CSTRING("utf-8")); if (NS_FAILED(rv)) return rv; *result = channel; return rv; }