diff options
Diffstat (limited to 'lib/httpserver/HTTPResponse.cpp')
-rw-r--r-- | lib/httpserver/HTTPResponse.cpp | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/lib/httpserver/HTTPResponse.cpp b/lib/httpserver/HTTPResponse.cpp index 1a8c8447..c56f286f 100644 --- a/lib/httpserver/HTTPResponse.cpp +++ b/lib/httpserver/HTTPResponse.cpp @@ -138,8 +138,7 @@ void HTTPResponse::SetContentType(const char *ContentType) // Function // Name: HTTPResponse::Send(IOStream &, bool) // Purpose: Build the response, and send via the stream. -// Optionally omitting the content. -// Created: 26/3/04 +// Created: 26/3/2004 // // -------------------------------------------------------------------------- void HTTPResponse::Send(bool OmitContent) @@ -148,7 +147,7 @@ void HTTPResponse::Send(bool OmitContent) { THROW_EXCEPTION(HTTPException, NoStreamConfigured); } - + if (GetSize() != 0 && mContentType.empty()) { THROW_EXCEPTION(HTTPException, NoContentTypeSet); @@ -184,6 +183,7 @@ void HTTPResponse::Send(bool OmitContent) // static is allowed to be cached for a day header += "\r\nCache-Control: max-age=86400"; } + if(mKeepAlive) { header += "\r\nConnection: keep-alive\r\n\r\n"; @@ -192,12 +192,13 @@ void HTTPResponse::Send(bool OmitContent) { header += "\r\nConnection: close\r\n\r\n"; } + // NOTE: header ends with blank line in all cases - + // Write to stream mpStreamToSendTo->Write(header.c_str(), header.size()); } - + // Send content if(!OmitContent) { @@ -227,16 +228,16 @@ void HTTPResponse::ParseHeaders(IOStreamGetLine &rGetLine, int Timeout) if(rGetLine.IsEOF()) { // Header terminates unexpectedly - THROW_EXCEPTION(HTTPException, BadRequest) + THROW_EXCEPTION(HTTPException, BadRequest) } - std::string currentLine; + std::string currentLine; if(!rGetLine.GetLine(currentLine, false /* no preprocess */, Timeout)) { // Timeout THROW_EXCEPTION(HTTPException, RequestReadFailed) } - + // Is this a continuation of the previous line? bool processHeader = haveHeader; if(!currentLine.empty() && (currentLine[0] == ' ' || currentLine[0] == '\t')) @@ -245,7 +246,7 @@ void HTTPResponse::ParseHeaders(IOStreamGetLine &rGetLine, int Timeout) processHeader = false; } //TRACE3("%d:%d:%s\n", processHeader, haveHeader, currentLine.c_str()); - + // Parse the header -- this will actually process the header // from the previous run around the loop. if(processHeader) @@ -263,7 +264,7 @@ void HTTPResponse::ParseHeaders(IOStreamGetLine &rGetLine, int Timeout) { ++dataStart; } - + if(p == sizeof("Content-Length")-1 && ::strncasecmp(h, "Content-Length", sizeof("Content-Length")-1) == 0) { @@ -308,7 +309,7 @@ void HTTPResponse::ParseHeaders(IOStreamGetLine &rGetLine, int Timeout) std::string headerName = header.substr(0, p); AddHeader(headerName, h + dataStart); } - + // Unset have header flag, as it's now been processed haveHeader = false; } @@ -329,7 +330,7 @@ void HTTPResponse::ParseHeaders(IOStreamGetLine &rGetLine, int Timeout) { // All done! break; - } + } } } @@ -340,22 +341,22 @@ void HTTPResponse::Receive(IOStream& rStream, int Timeout) if(rGetLine.IsEOF()) { // Connection terminated unexpectedly - THROW_EXCEPTION(HTTPException, BadResponse) + THROW_EXCEPTION_MESSAGE(HTTPException, BadResponse, + "HTTP server closed the connection without sending a response"); } - std::string statusLine; + std::string statusLine; if(!rGetLine.GetLine(statusLine, false /* no preprocess */, Timeout)) { // Timeout - THROW_EXCEPTION(HTTPException, ResponseReadFailed) + THROW_EXCEPTION_MESSAGE(HTTPException, ResponseReadFailed, + "Failed to get a response from the HTTP server within the timeout"); } - if (statusLine.substr(0, 7) != "HTTP/1." || - statusLine[8] != ' ') + if (statusLine.substr(0, 7) != "HTTP/1." || statusLine[8] != ' ') { - // Status line terminated unexpectedly - BOX_ERROR("Bad response status line: " << statusLine); - THROW_EXCEPTION(HTTPException, BadResponse) + THROW_EXCEPTION_MESSAGE(HTTPException, BadResponse, + "HTTP server sent an invalid HTTP status line: " << statusLine); } if (statusLine[5] == '1' && statusLine[7] == '1') @@ -363,7 +364,7 @@ void HTTPResponse::Receive(IOStream& rStream, int Timeout) // HTTP/1.1 default is to keep alive mKeepAlive = true; } - + // Decode the status code long status = ::strtol(statusLine.substr(9, 3).c_str(), NULL, 10); // returns zero in error case, this is OK @@ -376,7 +377,7 @@ void HTTPResponse::Receive(IOStream& rStream, int Timeout) { return; } - + ParseHeaders(rGetLine, Timeout); // push back whatever bytes we have left @@ -549,7 +550,7 @@ void HTTPResponse::SetAsRedirect(const char *RedirectTo, bool IsLocalURI) if(IsLocalURI) header += msDefaultURIPrefix; header += RedirectTo; mExtraHeaders.push_back(Header("Location", header)); - + // Set up some default content mContentType = "text/html"; #define REDIRECT_HTML_1 "<html><head><title>Redirection</title></head>\n<body><p><a href=\"" @@ -622,7 +623,7 @@ void HTTPResponse::WriteStringDefang(const char *String, unsigned int StringLen) StringLen -= toWrite; String += toWrite; } - + // Is it a bad character next? while(StringLen > 0) { |