Go to the documentation of this file.00001
00002
00003 #include "ace/String_Base.h"
00004 #include "ace/OS_NS_ctype.h"
00005 #include "ace/INet/HTTP_Request.h"
00006
00007 #if !defined (__ACE_INLINE__)
00008 #include "ace/INet/HTTP_Request.inl"
00009 #endif
00010
00011 #include "ace/INet/INet_Log.h"
00012
00013 ACE_RCSID(NET_CLIENT,ACE_HTTP_Request,"$Id: HTTP_Request.cpp 90891 2010-06-28 09:55:39Z mcorino $")
00014
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016
00017 namespace ACE
00018 {
00019 namespace HTTP
00020 {
00021
00022 const ACE_CString Request::HTTP_GET = "GET";
00023 const ACE_CString Request::HTTP_HEAD = "HEAD";
00024 const ACE_CString Request::HTTP_PUT = "PUT";
00025 const ACE_CString Request::HTTP_POST = "POST";
00026 const ACE_CString Request::HTTP_OPTIONS = "OPTIONS";
00027 const ACE_CString Request::HTTP_DELETE = "DELETE";
00028 const ACE_CString Request::HTTP_TRACE = "TRACE";
00029 const ACE_CString Request::HTTP_CONNECT = "CONNECT";
00030 const ACE_CString Request::HOST = "Host";
00031 const ACE_CString Request::COOKIE = "Cookie";
00032 const ACE_CString Request::AUTHORIZATION = "Authorization";
00033
00034 Request::Request()
00035 : method_ (HTTP_GET),
00036 uri_ ("/")
00037 {
00038 }
00039
00040 Request::Request(const ACE_CString& version)
00041 : Header (version),
00042 method_ (HTTP_GET),
00043 uri_ ("/")
00044 {
00045 }
00046
00047 Request::Request(const ACE_CString& method, const ACE_CString& uri)
00048 : method_ (method),
00049 uri_ (uri)
00050 {
00051 }
00052
00053 Request::Request(const ACE_CString& method, const ACE_CString& uri, const ACE_CString& version)
00054 : Header (version),
00055 method_ (method),
00056 uri_ (uri)
00057 {
00058 }
00059
00060 Request::~Request()
00061 {
00062 }
00063
00064 void Request::set_host(const ACE_CString& host, u_short port)
00065 {
00066 ACE_CString val(host);
00067 val += ':';
00068 char buf[16];
00069 val += ACE_OS::itoa (port, buf, 10);
00070 this->set (HOST, val);
00071 }
00072
00073 void Request::add_cookie(const ACE_CString & cookie)
00074 {
00075 this->add (COOKIE, cookie);
00076 }
00077
00078 void Request::get_cookies(ACE_Array<ACE_CString> & cookies) const
00079 {
00080 this->get_values (COOKIE, cookies);
00081 }
00082
00083 void Request::get_credentials(ACE_CString& scheme, ACE_CString& auth_info) const
00084 {
00085 if (this->has_credentials ())
00086 {
00087 ACE_CString auth;
00088 this->get (AUTHORIZATION, auth);
00089 ACE_CString::ITERATOR it (auth);
00090 while (!it.done () && ACE_OS::ace_isspace (*it))
00091 ++it;
00092 while (!it.done () && !ACE_OS::ace_isspace (*it))
00093 scheme += *it++;
00094 while (!it.done () && ACE_OS::ace_isspace (*it))
00095 ++it;
00096 while (!it.done ())
00097 auth_info += *it++;
00098 }
00099 }
00100
00101 void Request::set_credentials(const ACE_CString& scheme, const ACE_CString& auth_info)
00102 {
00103 ACE_CString val (scheme);
00104 val += " ";
00105 val += auth_info;
00106 this->set (AUTHORIZATION, val);
00107 }
00108
00109 void Request::write(std::ostream& str) const
00110 {
00111 str << this->method_.c_str () << " " << this->uri_.c_str () << " " << this->get_version ().c_str () << "\r\n";
00112
00113 INET_DEBUG (6, (LM_DEBUG, DLINFO
00114 ACE_TEXT ("ACE_INet_HTTP: --> %C %C %C\n"),
00115 this->method_.c_str (),
00116 this->uri_.c_str (),
00117 this->get_version ().c_str ()));
00118
00119 Header::write (str);
00120 str << "\r\n";
00121 }
00122
00123 bool Request::read(std::istream& str)
00124 {
00125 ACE_CString method (16, '\0');
00126 ACE_CString uri (128, '\0');
00127 ACE_CString version (16, '\0');
00128
00129 int ch = str.peek ();
00130 if (ch == eof_)
00131 {
00132 str.get ();
00133 return false;
00134 }
00135
00136 while (ACE_OS::ace_isspace (str.peek ()))
00137 {
00138 str.get ();
00139 }
00140
00141 ch = this->read_ws_field (str, method, MAX_METHOD_LENGTH);
00142 if (ch == eof_ || !ACE_OS::ace_isspace (ch))
00143 return false;
00144
00145 while (ACE_OS::ace_isspace (str.peek ()))
00146 {
00147 str.get ();
00148 }
00149
00150 ch = this->read_ws_field (str, uri, MAX_URI_LENGTH);
00151 if (ch == eof_ || !ACE_OS::ace_isspace (ch))
00152 return false;
00153
00154 while (ACE_OS::ace_isspace (str.peek ()))
00155 {
00156 str.get ();
00157 }
00158
00159 ch = this->read_ws_field (str, version, MAX_VERSION_LENGTH);
00160 if (ch == eof_ || !ACE_OS::ace_isspace (ch))
00161 return false;
00162
00163 while (ch != '\n' && ch != eof_)
00164 ch = str.get ();
00165
00166 if (!Header::read (str))
00167 return false;
00168
00169 ch = str.get ();
00170 while (ch != '\n' && ch != eof_)
00171 ch = str.get ();
00172 this->set_method (method);
00173 this->set_URI (uri);
00174 this->set_version(version);
00175 return true;
00176 }
00177
00178 }
00179 }
00180
00181 ACE_END_VERSIONED_NAMESPACE_DECL