httpd.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "base.h"
00015
00016 #ifndef __HTTPD_H__
00017 #define __HTTPD_H__
00018
00019 #include <string>
00020 #include <sstream>
00021 #include <map>
00022
00025 class HTTPHeaders : public classbase
00026 {
00027 protected:
00028 std::map<std::string,std::string> headers;
00029 public:
00030
00034 void SetHeader(const std::string &name, const std::string &data)
00035 {
00036 headers[name] = data;
00037 }
00038
00042 void CreateHeader(const std::string &name, const std::string &data)
00043 {
00044 if (!IsSet(name))
00045 SetHeader(name, data);
00046 }
00047
00050 void RemoveHeader(const std::string &name)
00051 {
00052 headers.erase(name);
00053 }
00054
00057 void Clear()
00058 {
00059 headers.clear();
00060 }
00061
00065 std::string GetHeader(const std::string &name)
00066 {
00067 std::map<std::string,std::string>::iterator it = headers.find(name);
00068 if (it == headers.end())
00069 return std::string();
00070
00071 return it->second;
00072 }
00073
00077 bool IsSet(const std::string &name)
00078 {
00079 std::map<std::string,std::string>::iterator it = headers.find(name);
00080 return (it != headers.end());
00081 }
00082
00086 std::string GetFormattedHeaders()
00087 {
00088 std::string re;
00089
00090 for (std::map<std::string,std::string>::iterator i = headers.begin(); i != headers.end(); i++)
00091 re += i->first + ": " + i->second + "\r\n";
00092
00093 return re;
00094 }
00095 };
00096
00101 class HTTPRequest : public classbase
00102 {
00103 protected:
00104 std::string type;
00105 std::string document;
00106 std::string ipaddr;
00107 std::string postdata;
00108
00109 public:
00110
00111 HTTPHeaders *headers;
00112 int errorcode;
00113
00117 void* sock;
00118
00128 HTTPRequest(const std::string &request_type, const std::string &uri, HTTPHeaders* hdr, void* opaque, const std::string &ip, const std::string &pdata)
00129 : type(request_type), document(uri), ipaddr(ip), postdata(pdata), headers(hdr), sock(opaque)
00130 {
00131 }
00132
00137 std::string& GetPostData()
00138 {
00139 return postdata;
00140 }
00141
00146 std::string& GetType()
00147 {
00148 return type;
00149 }
00150
00155 std::string& GetURI()
00156 {
00157 return document;
00158 }
00159
00164 std::string& GetIP()
00165 {
00166 return ipaddr;
00167 }
00168 };
00169
00174 class HTTPDocument : public classbase
00175 {
00176 protected:
00177
00178 std::stringstream* document;
00179 int responsecode;
00180
00181 public:
00182
00183 HTTPHeaders headers;
00184
00187 void* sock;
00188
00196 HTTPDocument(void* opaque, std::stringstream* doc, int response) : document(doc), responsecode(response), sock(opaque)
00197 {
00198 }
00199
00203 std::stringstream* GetDocument()
00204 {
00205 return this->document;
00206 }
00207
00211 unsigned long GetDocumentSize()
00212 {
00213 return this->document->str().length();
00214 }
00215
00219 int GetResponseCode()
00220 {
00221 return this->responsecode;
00222 }
00223 };
00224
00225 #endif
00226