diff options
author | Chris Wilson <chris+github@qwirx.com> | 2009-01-05 00:41:11 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2009-01-05 00:41:11 +0000 |
commit | a54f89ea951a17679da6a0af51e496db986eaff9 (patch) | |
tree | 326cfd15e2949963f3e4dfe79a8c4dd27773ef2b /lib/httpserver | |
parent | bb8fcb09a0ef2965c2324a6549a60910cfdf42dc (diff) |
Add support for sending an HTTP/1.0 100 Continue response during
processing of a request by HTTPServer, by keeping a pointer to the
socket object.
Diffstat (limited to 'lib/httpserver')
-rw-r--r-- | lib/httpserver/HTTPResponse.cpp | 48 | ||||
-rw-r--r-- | lib/httpserver/HTTPResponse.h | 5 |
2 files changed, 45 insertions, 8 deletions
diff --git a/lib/httpserver/HTTPResponse.cpp b/lib/httpserver/HTTPResponse.cpp index 87d4295e..1a8c8447 100644 --- a/lib/httpserver/HTTPResponse.cpp +++ b/lib/httpserver/HTTPResponse.cpp @@ -25,6 +25,24 @@ std::string HTTPResponse::msDefaultURIPrefix; // -------------------------------------------------------------------------- // // Function +// Name: HTTPResponse::HTTPResponse(IOStream*) +// Purpose: Constructor for response to be sent to a stream +// Created: 04/01/09 +// +// -------------------------------------------------------------------------- +HTTPResponse::HTTPResponse(IOStream* pStreamToSendTo) + : mResponseCode(HTTPResponse::Code_NoContent), + mResponseIsDynamicContent(true), + mKeepAlive(false), + mContentLength(-1), + mpStreamToSendTo(pStreamToSendTo) +{ +} + + +// -------------------------------------------------------------------------- +// +// Function // Name: HTTPResponse::HTTPResponse() // Purpose: Constructor // Created: 26/3/04 @@ -34,7 +52,8 @@ HTTPResponse::HTTPResponse() : mResponseCode(HTTPResponse::Code_NoContent), mResponseIsDynamicContent(true), mKeepAlive(false), - mContentLength(-1) + mContentLength(-1), + mpStreamToSendTo(NULL) { } @@ -123,11 +142,16 @@ void HTTPResponse::SetContentType(const char *ContentType) // Created: 26/3/04 // // -------------------------------------------------------------------------- -void HTTPResponse::Send(IOStream &rStream, bool OmitContent) +void HTTPResponse::Send(bool OmitContent) { - if(mContentType.empty()) + if (!mpStreamToSendTo) + { + THROW_EXCEPTION(HTTPException, NoStreamConfigured); + } + + if (GetSize() != 0 && mContentType.empty()) { - THROW_EXCEPTION(HTTPException, NoContentTypeSet) + THROW_EXCEPTION(HTTPException, NoContentTypeSet); } // Build and send header @@ -171,16 +195,20 @@ void HTTPResponse::Send(IOStream &rStream, bool OmitContent) // NOTE: header ends with blank line in all cases // Write to stream - rStream.Write(header.c_str(), header.size()); + mpStreamToSendTo->Write(header.c_str(), header.size()); } // Send content if(!OmitContent) { - rStream.Write(GetBuffer(), GetSize()); + mpStreamToSendTo->Write(GetBuffer(), GetSize()); } } +void HTTPResponse::SendContinue() +{ + mpStreamToSendTo->Write("HTTP/1.1 100 Continue\r\n"); +} // -------------------------------------------------------------------------- // @@ -339,10 +367,16 @@ void HTTPResponse::Receive(IOStream& rStream, int Timeout) // Decode the status code long status = ::strtol(statusLine.substr(9, 3).c_str(), NULL, 10); // returns zero in error case, this is OK - if(status < 0) status = 0; + if (status < 0) status = 0; // Store mResponseCode = status; + // 100 Continue responses have no headers, terminating newline, or body + if (status == 100) + { + return; + } + ParseHeaders(rGetLine, Timeout); // push back whatever bytes we have left diff --git a/lib/httpserver/HTTPResponse.h b/lib/httpserver/HTTPResponse.h index e4d4fd91..07838cb0 100644 --- a/lib/httpserver/HTTPResponse.h +++ b/lib/httpserver/HTTPResponse.h @@ -28,6 +28,7 @@ class IOStreamGetLine; class HTTPResponse : public CollectInBufferStream { public: + HTTPResponse(IOStream* pStreamToSendTo); HTTPResponse(); ~HTTPResponse(); @@ -46,7 +47,8 @@ public: void SetAsRedirect(const char *RedirectTo, bool IsLocalURI = true); void SetAsNotFound(const char *URI); - void Send(IOStream &rStream, bool OmitContent = false); + void Send(bool OmitContent = false); + void SendContinue(); void Receive(IOStream& rStream, int Timeout = IOStream::TimeOutInfinite); // void AddHeader(const char *EntireHeaderLine); @@ -139,6 +141,7 @@ private: std::string mContentType; std::vector<Header> mExtraHeaders; int mContentLength; // only used when reading response from stream + IOStream* mpStreamToSendTo; // nonzero only when constructed with a stream static std::string msDefaultURIPrefix; |