source: box/trunk/lib/httpserver/HTTPRequest.h @ 2721

Revision 2721, 5.1 KB checked in by chris, 21 months ago (diff)

Fix compile error on OpenSolaris? 10/Sun Studio 12. (merges [2720])

Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    HTTPRequest.h
5//              Purpose: Request object for HTTP connections
6//              Created: 26/3/04
7//
8// --------------------------------------------------------------------------
9
10#ifndef HTTPREQUEST__H
11#define HTTPREQUEST__H
12
13#include <string>
14#include <map>
15
16#include "CollectInBufferStream.h"
17
18class HTTPResponse;
19class IOStream;
20class IOStreamGetLine;
21
22// --------------------------------------------------------------------------
23//
24// Class
25//              Name:    HTTPRequest
26//              Purpose: Request object for HTTP connections
27//              Created: 26/3/04
28//
29// --------------------------------------------------------------------------
30class HTTPRequest : public CollectInBufferStream
31{
32public:
33        enum Method
34        {
35                Method_UNINITIALISED = -1,
36                Method_UNKNOWN = 0,
37                Method_GET = 1,
38                Method_HEAD = 2,
39                Method_POST = 3,
40                Method_PUT = 4
41        };
42       
43        HTTPRequest();
44        HTTPRequest(enum Method method, const std::string& rURI);
45        ~HTTPRequest();
46private:
47        // no copying
48        HTTPRequest(const HTTPRequest &);
49        HTTPRequest &operator=(const HTTPRequest &);
50public:
51        typedef std::multimap<std::string, std::string> Query_t;
52        typedef Query_t::value_type QueryEn_t;
53        typedef std::pair<std::string, std::string> Header;
54
55        enum
56        {
57                HTTPVersion__MajorMultiplier = 1000,
58                HTTPVersion_0_9 = 9,
59                HTTPVersion_1_0 = 1000,
60                HTTPVersion_1_1 = 1001
61        };
62
63        bool Receive(IOStreamGetLine &rGetLine, int Timeout);
64        bool Send(IOStream &rStream, int Timeout, bool ExpectContinue = false);
65        void SendWithStream(IOStream &rStreamToSendTo, int Timeout,
66                IOStream* pStreamToSend, HTTPResponse& rResponse);
67        void ReadContent(IOStream& rStreamToWriteTo);
68
69        typedef std::map<std::string, std::string> CookieJar_t;
70       
71        // --------------------------------------------------------------------------
72        //
73        // Function
74        //              Name:    HTTPResponse::Get*()
75        //              Purpose: Various Get accessors
76        //              Created: 26/3/04
77        //
78        // --------------------------------------------------------------------------
79        enum Method GetMethod() const {return mMethod;}
80        const std::string &GetRequestURI() const {return mRequestURI;}
81
82        // Note: the HTTPRequest generates and parses the Host: header
83        // Do not attempt to set one yourself with AddHeader().
84        const std::string &GetHostName() const {return mHostName;}
85        void SetHostName(const std::string& rHostName)
86        {
87                mHostName = rHostName;
88        }
89
90        const int GetHostPort() const {return mHostPort;}
91        const std::string &GetQueryString() const {return mQueryString;}
92        int GetHTTPVersion() const {return mHTTPVersion;}
93        const Query_t &GetQuery() const {return mQuery;}
94        int GetContentLength() const {return mContentLength;}
95        const std::string &GetContentType() const {return mContentType;}
96        const CookieJar_t *GetCookies() const {return mpCookies;} // WARNING: May return NULL
97        bool GetCookie(const char *CookieName, std::string &rValueOut) const;
98        const std::string &GetCookie(const char *CookieName) const;
99        bool GetHeader(const std::string& rName, std::string* pValueOut) const
100        {
101                std::string header = ToLowerCase(rName);
102
103                for (std::vector<Header>::const_iterator
104                        i  = mExtraHeaders.begin();
105                        i != mExtraHeaders.end(); i++)
106                {
107                        if (i->first == header)
108                        {
109                                *pValueOut = i->second;
110                                return true;
111                        }
112                }
113
114                return false;
115        }
116        std::vector<Header> GetHeaders() { return mExtraHeaders; }
117
118        // --------------------------------------------------------------------------
119        //
120        // Function
121        //              Name:    HTTPRequest::GetClientKeepAliveRequested()
122        //              Purpose: Returns true if the client requested that the connection
123        //                               should be kept open for further requests.
124        //              Created: 22/12/04
125        //
126        // --------------------------------------------------------------------------
127        bool GetClientKeepAliveRequested() const {return mClientKeepAliveRequested;}
128        void SetClientKeepAliveRequested(bool keepAlive)
129        {
130                mClientKeepAliveRequested = keepAlive;
131        }
132
133        void AddHeader(const std::string& rName, const std::string& rValue)
134        {
135                mExtraHeaders.push_back(Header(ToLowerCase(rName), rValue));
136        }
137        bool IsExpectingContinue() const { return mExpectContinue; }
138        const char* GetVerb() const
139        {
140                if (!mHttpVerb.empty())
141                {
142                        return mHttpVerb.c_str();
143                }
144                switch (mMethod)
145                {
146                        case Method_UNINITIALISED: return "Uninitialized";
147                        case Method_UNKNOWN: return "Unknown";
148                        case Method_GET: return "GET";
149                        case Method_HEAD: return "HEAD";
150                        case Method_POST: return "POST";
151                        case Method_PUT: return "PUT";
152                }
153                return "Bad";
154        }
155       
156private:
157        void ParseHeaders(IOStreamGetLine &rGetLine, int Timeout);
158        void ParseCookies(const std::string &rHeader, int DataStarts);
159
160        enum Method mMethod;
161        std::string mRequestURI;
162        std::string mHostName;
163        int mHostPort;
164        std::string mQueryString;
165        int mHTTPVersion;
166        Query_t mQuery;
167        int mContentLength;
168        std::string mContentType;
169        CookieJar_t *mpCookies;
170        bool mClientKeepAliveRequested;
171        std::vector<Header> mExtraHeaders;
172        bool mExpectContinue;
173        IOStream* mpStreamToReadFrom;
174        std::string mHttpVerb;
175
176        std::string ToLowerCase(const std::string& rInput) const
177        {
178                std::string output = rInput;
179                for (std::string::iterator c = output.begin();
180                        c != output.end(); c++)
181                {
182                        *c = tolower(*c);
183                }
184                return output;
185        }
186};
187
188#endif // HTTPREQUEST__H
189
Note: See TracBrowser for help on using the repository browser.