[Box Backup-commit] COMMIT r2434 - box/trunk/lib/httpserver
boxbackup-dev@boxbackup.org
boxbackup-dev@boxbackup.org
Sun, 4 Jan 2009 13:56:55 +0000 (GMT)
Author: chris
Date: 2009-01-04 13:56:55 +0000 (Sun, 04 Jan 2009)
New Revision: 2434
Modified:
box/trunk/lib/httpserver/HTTPRequest.cpp
box/trunk/lib/httpserver/HTTPRequest.h
box/trunk/lib/httpserver/HTTPResponse.h
box/trunk/lib/httpserver/HTTPServer.cpp
Log:
Allow adding headers to an HTTPRequest.
Allow getting response headers out of HTTPResponse.
Rename HTTPRequest Read and Write methods to Send and Receive, to avoid
confusion with IOStream.
Modified: box/trunk/lib/httpserver/HTTPRequest.cpp
===================================================================
--- box/trunk/lib/httpserver/HTTPRequest.cpp 2009-01-04 13:44:15 UTC (rev 2433)
+++ box/trunk/lib/httpserver/HTTPRequest.cpp 2009-01-04 13:56:55 UTC (rev 2434)
@@ -93,7 +93,7 @@
// --------------------------------------------------------------------------
//
// Function
-// Name: HTTPRequest::Read(IOStreamGetLine &, int)
+// Name: HTTPRequest::Receive(IOStreamGetLine &, int)
// Purpose: Read the request from an IOStreamGetLine (and
// attached stream).
// Returns false if there was no valid request,
@@ -101,7 +101,7 @@
// Created: 26/3/04
//
// --------------------------------------------------------------------------
-bool HTTPRequest::Read(IOStreamGetLine &rGetLine, int Timeout)
+bool HTTPRequest::Receive(IOStreamGetLine &rGetLine, int Timeout)
{
// Check caller's logic
if(mMethod != Method_UNINITIALISED)
@@ -312,12 +312,12 @@
// --------------------------------------------------------------------------
//
// Function
-// Name: HTTPRequest::Write(IOStream &, int)
+// Name: HTTPRequest::Send(IOStream &, int)
// Purpose: Write the request to an IOStream using HTTP.
// Created: 03/01/09
//
// --------------------------------------------------------------------------
-bool HTTPRequest::Write(IOStream &rStream, int Timeout)
+bool HTTPRequest::Send(IOStream &rStream, int Timeout)
{
switch (mMethod)
{
@@ -386,6 +386,12 @@
oss << "Connection: close\n";
}
+ for (std::vector<Header>::iterator i = mExtraHeaders.begin();
+ i != mExtraHeaders.end(); i++)
+ {
+ oss << i->first << ": " << i->second << "\n";
+ }
+
rStream.Write(oss.str().c_str());
rStream.Write("\n");
Modified: box/trunk/lib/httpserver/HTTPRequest.h
===================================================================
--- box/trunk/lib/httpserver/HTTPRequest.h 2009-01-04 13:44:15 UTC (rev 2433)
+++ box/trunk/lib/httpserver/HTTPRequest.h 2009-01-04 13:56:55 UTC (rev 2434)
@@ -44,9 +44,8 @@
HTTPRequest(const HTTPRequest &);
HTTPRequest &operator=(const HTTPRequest &);
public:
-
typedef std::multimap<std::string, std::string> Query_t;
- typedef std::pair<std::string, std::string> QueryEn_t;
+ typedef std::pair<std::string, std::string> QueryEn_t, Header;
enum
{
@@ -56,8 +55,8 @@
HTTPVersion_1_1 = 1001
};
- bool Read(IOStreamGetLine &rGetLine, int Timeout);
- bool Write(IOStream &rStream, int Timeout);
+ bool Receive(IOStreamGetLine &rGetLine, int Timeout);
+ bool Send(IOStream &rStream, int Timeout);
typedef std::map<std::string, std::string> CookieJar_t;
@@ -71,7 +70,15 @@
// --------------------------------------------------------------------------
enum Method GetMethod() const {return mMethod;}
const std::string &GetRequestURI() const {return mRequestURI;}
- const std::string &GetHostName() const {return mHostName;} // note: request does splitting of Host: header
+
+ // Note: the HTTPRequest generates and parses the Host: header
+ // Do not attempt to set one yourself with AddHeader().
+ const std::string &GetHostName() const {return mHostName;}
+ void SetHostName(const std::string& rHostName)
+ {
+ mHostName = rHostName;
+ }
+
const int GetHostPort() const {return mHostPort;} // into host name and port number
const std::string &GetQueryString() const {return mQueryString;}
int GetHTTPVersion() const {return mHTTPVersion;}
@@ -98,6 +105,11 @@
mClientKeepAliveRequested = keepAlive;
}
+ void AddHeader(const std::string& rName, const std::string& rValue)
+ {
+ mExtraHeaders.push_back(Header(rName, rValue));
+ }
+
private:
void ParseHeaders(IOStreamGetLine &rGetLine, int Timeout);
void ParseCookies(const std::string &rHeader, int DataStarts);
@@ -114,6 +126,7 @@
std::string mContentType;
CookieJar_t *mpCookies;
bool mClientKeepAliveRequested;
+ std::vector<Header> mExtraHeaders;
};
#endif // HTTPREQUEST__H
Modified: box/trunk/lib/httpserver/HTTPResponse.h
===================================================================
--- box/trunk/lib/httpserver/HTTPResponse.h 2009-01-04 13:44:15 UTC (rev 2433)
+++ box/trunk/lib/httpserver/HTTPResponse.h 2009-01-04 13:56:55 UTC (rev 2434)
@@ -30,12 +30,14 @@
public:
HTTPResponse();
~HTTPResponse();
+
private:
// no copying
HTTPResponse(const HTTPResponse &);
HTTPResponse &operator=(const HTTPResponse &);
+ typedef std::pair<std::string, std::string> Header;
+
public:
-
void SetResponseCode(int Code);
int GetResponseCode() { return mResponseCode; }
void SetContentType(const char *ContentType);
@@ -52,6 +54,29 @@
void AddHeader(const char *Header, const char *Value);
void AddHeader(const char *Header, const std::string &rValue);
void AddHeader(const std::string &rHeader, const std::string &rValue);
+ bool GetHeader(const std::string& rName, std::string* pValueOut) const
+ {
+ for (std::vector<Header>::const_iterator
+ i = mExtraHeaders.begin();
+ i != mExtraHeaders.end(); i++)
+ {
+ if (i->first == rName)
+ {
+ *pValueOut = i->second;
+ return true;
+ }
+ }
+ return false;
+ }
+ std::string GetHeaderValue(const std::string& rName)
+ {
+ std::string value;
+ if (!GetHeader(rName, &value))
+ {
+ THROW_EXCEPTION(CommonException, ConfigNoKey);
+ }
+ return value;
+ }
// Set dynamic content flag, default is content is dynamic
void SetResponseIsDynamicContent(bool IsDynamic) {mResponseIsDynamicContent = IsDynamic;}
@@ -68,6 +93,7 @@
Code_Found = 302, // redirection
Code_NotModified = 304,
Code_TemporaryRedirect = 307,
+ Code_MethodNotAllowed = 400,
Code_Unauthorized = 401,
Code_Forbidden = 403,
Code_NotFound = 404,
@@ -111,7 +137,6 @@
bool mResponseIsDynamicContent;
bool mKeepAlive;
std::string mContentType;
- typedef std::pair<std::string, std::string> Header;
std::vector<Header> mExtraHeaders;
int mContentLength; // only used when reading response from stream
Modified: box/trunk/lib/httpserver/HTTPServer.cpp
===================================================================
--- box/trunk/lib/httpserver/HTTPServer.cpp 2009-01-04 13:44:15 UTC (rev 2433)
+++ box/trunk/lib/httpserver/HTTPServer.cpp 2009-01-04 13:56:55 UTC (rev 2434)
@@ -145,7 +145,7 @@
{
// Parse the request
HTTPRequest request;
- if(!request.Read(getLine, mTimeout))
+ if(!request.Receive(getLine, mTimeout))
{
// Didn't get request, connection probably closed.
break;
@@ -186,7 +186,7 @@
response.Send(rStream, request.GetMethod() == HTTPRequest::Method_HEAD);
}
- // Notify dervived claases
+ // Notify derived claases
HTTPConnectionClosing();
}