00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _phttpclient_h_
00021 #define _phttpclient_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/ptypes.h>
00025 #include <pclasses/psocket.h>
00026 #include <pclasses/purl.h>
00027 #include <memory>
00028 #include <map>
00029 #include <string>
00030
00031 namespace P {
00032
00034
00038 enum httpResponseCode_t {
00039 HTTP_100_CONTINUE = 100,
00040 HTTP_101_SWITCHING_PROTOCOL = 101,
00041 HTTP_200_OK = 200,
00042 HTTP_201_CREATED = 201,
00043 HTTP_202_ACCEPTED = 202,
00044 HTTP_203_NON_AUTH_INFO = 203,
00045 HTTP_204_NO_CONTENT = 204,
00046 HTTP_205_RESET_CONTENT = 205,
00047 HTTP_206_PARTIAL_CONTENT = 206,
00048 HTTP_300_MULTIPLE_CHOICES = 300,
00049 HTTP_301_MOVED_PERMANENTLY = 301,
00050 HTTP_302_FOUND = 302,
00051 HTTP_303_SEE_OTHER = 303,
00052 HTTP_304_NOT_MODIFIED = 304,
00053 HTTP_305_USE_PROXY = 305,
00054 HTTP_307_TEMPORARY_REDIRECT = 307,
00055 HTTP_400_BAD_REQUEST = 400,
00056 HTTP_401_UNAUTHORIZED = 401,
00057 HTTP_402_PAYMENT_REQUIRED = 402,
00058 HTTP_403_FORBIDDEN = 403,
00059 HTTP_404_NOT_FOUND = 404,
00060 HTTP_405_METHOD_NOT_ALLOWED = 405,
00061 HTTP_406_NOT_ACCEPTABLE = 406,
00062 HTTP_407_PROXY_AUTH_REQUIRED= 407,
00063 HTTP_408_REQUEST_TIMEOUT = 408,
00064 HTTP_409_CONFLICT = 409,
00065 HTTP_410_GONE = 410,
00066 HTTP_411_LENGTH_REQUIRED = 411,
00067 HTTP_412_PRECONDITION_FAILED= 412,
00068 HTTP_413_REQUEST_TOO_LARGE = 413,
00069 HTTP_414_REQ_URI_TOO_LONG = 414,
00070 HTTP_415_UNSUPP_MEDIA_TYPE = 415,
00071 HTTP_416_REQ_RANGE_INVALID = 416,
00072 HTTP_417_EXPECTATION_FAILED = 417,
00073 HTTP_500_INTERNAL_ERROR = 500,
00074 HTTP_501_NOT_IMPLEMENTED = 501,
00075 HTTP_502_BAD_GATEWAY = 502,
00076 HTTP_503_SERVICE_UNAVAILABLE= 503,
00077 HTTP_504_GATEWAY_TIMEOUT = 504,
00078 HTTP_505_VERSION_UNSUPPORTED= 505
00079 };
00080
00082
00087 class PNET_EXPORT HTTPHeader {
00088 public:
00089 typedef std::map<std::string, std::string> field_map;
00090 typedef field_map::iterator iterator;
00091 typedef field_map::const_iterator const_iterator;
00092
00093 HTTPHeader();
00094 ~HTTPHeader();
00095
00096 void set(const std::string& head, const std::string& val);
00097 const std::string& get(const std::string& head) const;
00098
00099 bool remove(const std::string& head);
00100 bool contains(const std::string& head) const;
00101
00102 inline void setCacheControl(const std::string& val)
00103 { set("Cache-Control", val); }
00104
00105 inline void setConnection(const std::string& val)
00106 { set("Connection", val); }
00107
00108 inline const std::string& connection() const
00109 { return get("Connection"); }
00110
00111 inline void setDate(const std::string& val)
00112 { set("Date", val); }
00113
00114 inline void setPragma(const std::string& val)
00115 { set("Pragma", val); }
00116
00117 inline void setTrailer(const std::string& val)
00118 { set("Trailer", val); }
00119
00120 inline void setTransferEncoding(const std::string& val)
00121 { set("Transfer-Encoding", val); }
00122
00123 inline const std::string& transferEncoding() const
00124 { return get("Transfer-Encoding"); }
00125
00126 inline void setUpgrade(const std::string& val)
00127 { set("Upgrade", val); }
00128
00129 inline void setVia(const std::string& val)
00130 { set("Via", val); }
00131
00132 inline void setWarning(const std::string& val)
00133 { set("Warning", val); }
00134
00135 const std::string& operator[](const std::string& head) const;
00136
00137 inline const_iterator begin() const
00138 { return m_values.begin(); }
00139
00140 inline const_iterator end() const
00141 { return m_values.end(); }
00142
00143 inline iterator begin()
00144 { return m_values.begin(); }
00145
00146 inline iterator end()
00147 { return m_values.end(); }
00148
00149 private:
00150 field_map m_values;
00151 };
00152
00154
00159 class PNET_EXPORT HTTPRequestHeader: public HTTPHeader {
00160 public:
00161 HTTPRequestHeader() {}
00162 ~HTTPRequestHeader() {}
00163
00164 inline void setAccept(const std::string& val)
00165 { set("Accept", val); }
00166
00167 inline void setAcceptCharset(const std::string& val)
00168 { set("Accept-Charset", val); }
00169
00170 inline void setAcceptEncoding(const std::string& val)
00171 { set("Accept-Encoding", val); }
00172
00173 inline void setAcceptLanguage(const std::string& val)
00174 { set("Accept-Language", val); }
00175
00176 inline void setAuthorization(const std::string& val)
00177 { set("Authorization", val); }
00178
00179 inline void setExpect(const std::string& val)
00180 { set("Expect", val); }
00181
00182 inline void setFrom(const std::string& val)
00183 { set("From", val); }
00184
00185 inline void setHost(const std::string& val)
00186 { set("Host", val); }
00187
00188 inline void setIfMatch(const std::string& val)
00189 { set("If-Match", val); }
00190
00191 inline void setIfModifiedSince(const std::string& val)
00192 { set("If-Modified-Since", val); }
00193
00194 inline void setIfNoneMatch(const std::string& val)
00195 { set("If-None-Match", val); }
00196
00197 inline void setIfRange(const std::string& val)
00198 { set("If-Range", val); }
00199
00200 inline void setIfUnmodifiedSince(const std::string& val)
00201 { set("If-Unmodified-Since", val); }
00202
00203 inline void setMaxForwards(const std::string& val)
00204 { set("Max-Forwards", val); }
00205
00206 inline void setProxyAuthorization(const std::string& val)
00207 { set("Proxy-Authorization", val); }
00208
00209 inline void setRange(const std::string& val)
00210 { set("Range", val); }
00211
00212 inline void setReferer(const std::string& val)
00213 { set("Referer", val); }
00214
00215 inline void setTE(const std::string& val)
00216 { set("TE", val); }
00217
00218 inline void setUserAgent(const std::string& val)
00219 { set("User-Agent", val); }
00220
00221 };
00222
00224
00229 class PNET_EXPORT HTTPResponseHeader: public HTTPHeader {
00230 public:
00231 HTTPResponseHeader() {}
00232 ~HTTPResponseHeader() {}
00233
00234 inline void setAcceptRanges(const std::string& val)
00235 { set("Accept-Ranges", val); }
00236
00237 inline void setAge(const std::string& val)
00238 { set("Age", val); }
00239
00240 inline void setETag(const std::string& val)
00241 { set("ETag", val); }
00242
00243 inline void setLocation(const std::string& val)
00244 { set("Location", val); }
00245
00246 inline void setProxyAuthenticate(const std::string& val)
00247 { set("Proxy-Authenticate", val); }
00248
00249 inline void setRetryAfter(const std::string& val)
00250 { set("Retry-After", val); }
00251
00252 inline void setServer(const std::string& val)
00253 { set("Server", val); }
00254
00255 inline void setVary(const std::string& val)
00256 { set("Vary", val); }
00257
00258 inline void setWWWAuthenticate(const std::string& val)
00259 { set("WWW-Authenticate", val); }
00260
00261 inline void setAllow(const std::string& val)
00262 { set("Allow", val); }
00263
00264 inline void setContentEncoding(const std::string& val)
00265 { set("Content-Encoding", val); }
00266
00267 inline void setContentLanguage(const std::string& val)
00268 { set("Content-Language", val); }
00269
00270 inline void setContentLegth(const std::string& val)
00271 { set("Content-Legth", val); }
00272
00273 inline void setContentLocation(const std::string& val)
00274 { set("Content-Location", val); }
00275
00276 inline void setContentMD5(const std::string& val)
00277 { set("Content-MD5", val); }
00278
00279 inline void setContentRange(const std::string& val)
00280 { set("Content-Range", val); }
00281
00282 inline void setContentType(const std::string& val)
00283 { set("Content-Type", val); }
00284
00285 inline void setExpires(const std::string& val)
00286 { set("Expires", val); }
00287
00288 inline void setLastModified(const std::string& val)
00289 { set("Last-Modified", val); }
00290
00291 };
00292
00294
00298 class PNET_EXPORT HTTPRequest {
00299 public:
00301 enum reqmethod_t {
00302 METHOD_HEAD = 0,
00303 METHOD_GET = 1,
00304 METHOD_POST = 2,
00305 METHOD_PUT = 3,
00306 METHOD_DELETE = 4
00307 };
00308
00309 HTTPRequest(reqmethod_t method, const URL& url);
00310 ~HTTPRequest();
00311
00312 inline reqmethod_t method() const
00313 { return m_method; }
00314
00315 inline const URL& url() const
00316 { return m_url; }
00317
00318 inline const HTTPRequestHeader& header() const
00319 { return m_header; }
00320
00321 inline HTTPRequestHeader& header()
00322 { return m_header; }
00323
00324 private:
00325 reqmethod_t m_method;
00326 URL m_url;
00327 HTTPRequestHeader m_header;
00328 };
00329
00331
00335 class PNET_EXPORT HTTPResponse {
00336 public:
00337 HTTPResponse(const std::string& proto, int code, const std::string& resp);
00338 ~HTTPResponse();
00339
00340 inline const HTTPResponseHeader& header() const
00341 { return m_header; }
00342
00343 inline HTTPResponseHeader& header()
00344 { return m_header; }
00345
00346 inline int code() const
00347 { return m_code; }
00348
00349 private:
00350 std::string m_proto;
00351 int m_code;
00352 std::string m_resp;
00353 HTTPResponseHeader m_header;
00354 };
00355
00357
00361 class PNET_EXPORT HTTPClient: protected StreamSocket {
00362 public:
00363 HTTPClient(int domain) throw(IOError);
00364
00365 HTTPClient(const NetworkAddress& addr, port_t port) throw(IOError);
00366
00367 virtual ~HTTPClient() throw();
00368
00369 void connect(const NetworkAddress& addr, port_t port) throw(IOError)
00370 { StreamSocket::connect(addr, port); }
00371
00372 void sendRequest(const HTTPRequest& req) throw(IOError);
00373
00374 std::auto_ptr<HTTPResponse> readResponse() throw(IOError);
00375
00376 size_t readBody(const HTTPResponse& resp, char* buffer, size_t size);
00377
00378 private:
00379 size_t m_contentLength;
00380 bool m_chunkedEncoding;
00381 size_t m_chunkSize;
00382 size_t m_bodyLength;
00384 };
00385
00386 }
00387
00388 #endif