00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef _MSC_VER
00034 #pragma warning(disable:4786)
00035 #endif
00036 #include "HttpRequest.h"
00037 #include "MemFile.h"
00038 #include "HttpdForm.h"
00039 #include "HttpdCookies.h"
00040 #include "Parse.h"
00041 #include "Exception.h"
00042
00043 #ifdef SOCKETS_NAMESPACE
00044 namespace SOCKETS_NAMESPACE {
00045 #endif
00046
00047 #ifdef _DEBUG
00048 #define DEB(x) x; fflush(stderr);
00049 #else
00050 #define DEB(x)
00051 #endif
00052
00053
00054
00055 HttpRequest::HttpRequest() : HttpTransaction()
00056 , m_server_port(0)
00057 , m_is_ssl(false)
00058 , m_body_file(NULL)
00059 , m_form(NULL)
00060 {
00061 }
00062
00063
00064
00065 #ifndef _WIN32
00066 HttpRequest::HttpRequest(FILE *fil) : HttpTransaction()
00067 , m_server_port(0)
00068 , m_is_ssl(false)
00069 , m_body_file(NULL)
00070 , m_form(NULL)
00071 {
00072 int i = 0;
00073 DEB( std::cout << "Initialize HttpRequest from cgi...\n";)
00074 while (environ[i] && *environ[i])
00075 {
00076 Parse pa(environ[i], "=");
00077 std::string key = pa.getword();
00078 std::string value = pa.getrest();
00079 if (key == "REQUEST_METHOD")
00080 m_method = value;
00081 else
00082 if (key == "SERVER_PROTOCOL")
00083 m_protocol = value;
00084 else
00085 if (key == "PATH_INFO")
00086 m_req_uri = value;
00087 else
00088 if (key == "REMOTE_ADDR")
00089 m_remote_addr = value;
00090 else
00091 if (key == "REMOTE_HOST")
00092 m_remote_host = value;
00093 else
00094 if (key == "SERVER_NAME")
00095 m_server_name = value;
00096 else
00097 if (key == "SERVER_PORT")
00098 m_server_port = atoi(value.c_str());
00099 else
00100 if (key.size() > 5 && key.substr(0, 5) == "HTTP_")
00101 {
00102 key = key.substr(5);
00103 for (size_t pos = 0; pos < key.size(); pos++)
00104 {
00105 if (key[pos] == '_')
00106 key[pos] = '-';
00107 else
00108 if (key[pos] >= 'A' && key[pos] <= 'Z')
00109 key[pos] |= 32;
00110 }
00111 DEB( std::cout << " http header '" << key << "' == '" << value << "\n";)
00112 SetHeader(key, value);
00113 }
00114 ++i;
00115 }
00116 DEB( std::cout << " setup http form\n";)
00117 m_form = std::auto_ptr<HttpdForm>(new HttpdForm(fil));
00118 }
00119 #endif
00120
00121
00122
00123 HttpRequest::HttpRequest(const HttpRequest& src) : HttpTransaction(src)
00124 , m_method(src.m_method)
00125 , m_protocol(src.m_protocol)
00126 , m_req_uri(src.m_req_uri)
00127 , m_remote_addr(src.m_remote_addr)
00128 , m_remote_host(src.m_remote_host)
00129 , m_server_name(src.m_server_name)
00130 , m_server_port(src.m_server_port)
00131 , m_is_ssl(src.m_is_ssl)
00132 , m_attribute(src.m_attribute)
00133 , m_null(src.m_null)
00134 , m_body_file(src.m_body_file)
00135 , m_form(src.m_form)
00136 , m_cookies(src.m_cookies)
00137 , m_cookie(src.m_cookie)
00138 {
00139 }
00140
00141
00142
00143 HttpRequest::~HttpRequest()
00144 {
00145 }
00146
00147
00148
00149 HttpRequest& HttpRequest::operator=(const HttpRequest& src)
00150 {
00151 m_method = src.m_method;
00152 m_protocol = src.m_protocol;
00153 m_req_uri = src.m_req_uri;
00154 m_remote_addr = src.m_remote_addr;
00155 m_remote_host = src.m_remote_host;
00156 m_server_name = src.m_server_name;
00157 m_server_port = src.m_server_port;
00158 m_is_ssl = src.m_is_ssl;
00159 m_attribute = src.m_attribute;
00160 m_null = src.m_null;
00161 m_body_file = src.m_body_file;
00162 m_form = src.m_form;
00163 m_cookies = src.m_cookies;
00164 m_cookie = src.m_cookie;
00165
00166 HttpTransaction::operator=(src);
00167
00168 return *this;
00169 }
00170
00171
00172
00173 void HttpRequest::SetHttpMethod(const std::string& value)
00174 {
00175 m_method = value;
00176 }
00177
00178
00179 const std::string& HttpRequest::HttpMethod() const
00180 {
00181 return m_method;
00182 }
00183
00184
00185
00186
00187 void HttpRequest::SetHttpVersion(const std::string& value)
00188 {
00189 m_protocol = value;
00190 }
00191
00192
00193 const std::string& HttpRequest::HttpVersion() const
00194 {
00195 return m_protocol;
00196 }
00197
00198
00199
00200
00201 void HttpRequest::SetUri(const std::string& value)
00202 {
00203 m_req_uri = value;
00204 }
00205
00206
00207 const std::string& HttpRequest::Uri() const
00208 {
00209 return m_req_uri;
00210 }
00211
00212
00213
00214
00215 void HttpRequest::SetRemoteAddr(const std::string& value)
00216 {
00217 m_remote_addr = value;
00218 }
00219
00220
00221 const std::string& HttpRequest::RemoteAddr() const
00222 {
00223 return m_remote_addr;
00224 }
00225
00226
00227
00228
00229 void HttpRequest::SetRemoteHost(const std::string& value)
00230 {
00231 m_remote_host = value;
00232 }
00233
00234
00235 const std::string& HttpRequest::RemoteHost() const
00236 {
00237 return m_remote_host;
00238 }
00239
00240
00241
00242
00243 void HttpRequest::SetServerName(const std::string& value)
00244 {
00245 m_server_name = value;
00246 }
00247
00248
00249 const std::string& HttpRequest::ServerName() const
00250 {
00251 return m_server_name;
00252 }
00253
00254
00255
00256
00257 void HttpRequest::SetServerPort(int value)
00258 {
00259 m_server_port = value;
00260 }
00261
00262
00263 int HttpRequest::ServerPort() const
00264 {
00265 return m_server_port;
00266 }
00267
00268
00269
00270
00271 void HttpRequest::SetIsSsl(bool value)
00272 {
00273 m_is_ssl = value;
00274 }
00275
00276
00277 bool HttpRequest::IsSsl() const
00278 {
00279 return m_is_ssl;
00280 }
00281
00282
00283
00284
00285 void HttpRequest::SetAttribute(const std::string& key, const std::string& value)
00286 {
00287 m_attribute[key] = value;
00288 }
00289
00290
00291 void HttpRequest::SetAttribute(const std::string& key, long value)
00292 {
00293 m_attribute[key] = Utility::l2string(value);
00294 }
00295
00296
00297 const std::string& HttpRequest::Attribute(const std::string& key) const
00298 {
00299 Utility::ncmap<std::string>::const_iterator it;
00300 if ( (it = m_attribute.find(key)) != m_attribute.end())
00301 return it -> second;
00302 return m_null;
00303 }
00304
00305
00306
00307 const Utility::ncmap<std::string>& HttpRequest::Attributes() const
00308 {
00309 return m_attribute;
00310 }
00311
00312
00313
00314 void HttpRequest::AddCookie(const std::string& str)
00315 {
00316 m_cookies.add( str );
00317 Parse pa(str, ";");
00318 std::string lstr = pa.getword();
00319 while (!lstr.empty())
00320 {
00321 Parse pa2(lstr, "=");
00322 std::string name = pa2.getword();
00323 m_cookie[name] = lstr;
00324 DEB(fprintf(stderr, " *** AddCookie '%s' = '%s'\n", name.c_str(), lstr.c_str());)
00325 lstr = pa.getword();
00326 }
00327 }
00328
00329
00330
00331 void HttpRequest::InitBody( size_t sz )
00332 {
00333 if (!m_body_file.get())
00334 m_body_file = std::auto_ptr<IFile>(new MemFile);
00335 DEB( else
00336 fprintf(stderr, "Body data file already opened\n");)
00337 }
00338
00339
00340
00341 void HttpRequest::Write( const char *buf, size_t sz )
00342 {
00343 if (m_body_file.get())
00344 m_body_file -> fwrite(buf, 1, sz);
00345 DEB( else
00346 fprintf(stderr, "Write: Body data file not open\n");)
00347 }
00348
00349
00350
00351 void HttpRequest::CloseBody()
00352 {
00353 if (m_body_file.get())
00354 m_body_file -> fclose();
00355 DEB( else
00356 fprintf(stderr, "CloseBody: File not open\n");)
00357 }
00358
00359
00360
00361 void HttpRequest::ParseBody()
00362 {
00363 Utility::ncmap<std::string>::const_iterator it;
00364 if ( (it = m_attribute.find("query_string")) != m_attribute.end())
00365 {
00366 std::string qs = it -> second;
00367 m_form = std::auto_ptr<HttpdForm>(new HttpdForm( qs, qs.size() ));
00368 }
00369 else
00370 if (m_body_file.get())
00371 {
00372 m_form = std::auto_ptr<HttpdForm>(new HttpdForm( m_body_file.get(), ContentType(), ContentLength() ));
00373 }
00374 else
00375 {
00376
00377 m_form = std::auto_ptr<HttpdForm>(new HttpdForm( "", 0 ));
00378 }
00379 }
00380
00381
00382
00383 const HttpdForm& HttpRequest::Form() const
00384 {
00385 if (!m_form.get())
00386 throw Exception("Form not available");
00387 return *m_form;
00388 }
00389
00390
00391
00392 const HttpdCookies& HttpRequest::Cookies() const
00393 {
00394 return m_cookies;
00395 }
00396
00397
00398
00399 void HttpRequest::Reset()
00400 {
00401 HttpTransaction::Reset();
00402 m_method = "";
00403 m_protocol = "";
00404 m_req_uri = "";
00405 m_remote_addr = "";
00406 m_remote_host = "";
00407 m_server_name = "";
00408 m_server_port = 0;
00409 m_is_ssl = false;
00410 while (!m_attribute.empty())
00411 {
00412 m_attribute.erase(m_attribute.begin());
00413 }
00414 m_body_file = std::auto_ptr<IFile>(NULL);
00415 m_form = std::auto_ptr<HttpdForm>(NULL);
00416 m_cookies.Reset();
00417 while (!m_cookie.empty())
00418 {
00419 m_cookie.erase(m_cookie.begin());
00420 }
00421 }
00422
00423
00424 #ifdef SOCKETS_NAMESPACE
00425 }
00426 #endif
00427
00428