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