Use a variable on the stack to not have a temporary in the call
[ACE_TAO.git] / ACE / protocols / ace / INet / HTTP_Request.cpp
blob1511672f91e254c658dcc513088cc504212599c8
1 #include "ace/String_Base.h"
2 #include "ace/OS_NS_ctype.h"
3 #include "ace/INet/HTTP_Request.h"
5 #if !defined (__ACE_INLINE__)
6 #include "ace/INet/HTTP_Request.inl"
7 #endif
9 #include "ace/INet/INet_Log.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 namespace ACE
16 namespace HTTP
18 const ACE_CString Request::HTTP_GET = "GET";
19 const ACE_CString Request::HTTP_HEAD = "HEAD";
20 const ACE_CString Request::HTTP_PUT = "PUT";
21 const ACE_CString Request::HTTP_POST = "POST";
22 const ACE_CString Request::HTTP_OPTIONS = "OPTIONS";
23 const ACE_CString Request::HTTP_DELETE = "DELETE";
24 const ACE_CString Request::HTTP_TRACE = "TRACE";
25 const ACE_CString Request::HTTP_CONNECT = "CONNECT";
26 const ACE_CString Request::HOST = "Host";
27 const ACE_CString Request::COOKIE = "Cookie";
28 const ACE_CString Request::AUTHORIZATION = "Authorization";
30 Request::Request()
31 : method_ (HTTP_GET),
32 uri_ ("/")
36 Request::Request(const ACE_CString& version)
37 : Header (version),
38 method_ (HTTP_GET),
39 uri_ ("/")
43 Request::Request(const ACE_CString& method, const ACE_CString& uri)
44 : method_ (method),
45 uri_ (uri)
49 Request::Request(const ACE_CString& method, const ACE_CString& uri, const ACE_CString& version)
50 : Header (version),
51 method_ (method),
52 uri_ (uri)
56 Request::~Request()
60 void Request::set_host(const ACE_CString& host, u_short port)
62 ACE_CString val(host);
63 val += ':';
64 char buf[16];
65 val += ACE_OS::itoa (port, buf, 10);
66 this->set (HOST, val);
69 void Request::add_cookie(const ACE_CString & cookie)
71 this->add (COOKIE, cookie);
74 void Request::get_cookies(ACE_Array<ACE_CString> & cookies) const
76 this->get_values (COOKIE, cookies);
79 void Request::get_credentials(ACE_CString& scheme, ACE_CString& auth_info) const
81 if (this->has_credentials ())
83 ACE_CString auth;
84 this->get (AUTHORIZATION, auth);
85 ACE_CString::ITERATOR it (auth);
86 while (!it.done () && ACE_OS::ace_isspace (*it))
87 ++it;
88 while (!it.done () && !ACE_OS::ace_isspace (*it))
89 scheme += *it++;
90 while (!it.done () && ACE_OS::ace_isspace (*it))
91 ++it;
92 while (!it.done ())
93 auth_info += *it++;
97 void Request::set_credentials(const ACE_CString& scheme, const ACE_CString& auth_info)
99 ACE_CString val (scheme);
100 val += " ";
101 val += auth_info;
102 this->set (AUTHORIZATION, val);
105 void Request::write(std::ostream& str) const
107 str << this->method_.c_str () << " " << this->uri_.c_str () << " " << this->get_version ().c_str () << "\r\n";
109 INET_DEBUG (6, (LM_DEBUG, DLINFO
110 ACE_TEXT ("ACE_INet_HTTP: --> %C %C %C\n"),
111 this->method_.c_str (),
112 this->uri_.c_str (),
113 this->get_version ().c_str ()));
115 Header::write (str);
116 str << "\r\n";
119 bool Request::read(std::istream& str)
121 ACE_CString method (16, '\0');
122 ACE_CString uri (128, '\0');
123 ACE_CString version (16, '\0');
125 int ch = str.peek ();
126 if (ch == eof_)
128 str.get (); // skip to eof
129 return false;
131 // skip whitespace
132 while (ACE_OS::ace_isspace (str.peek ()))
134 str.get ();
136 // get method
137 ch = this->read_ws_field (str, method, MAX_METHOD_LENGTH);
138 if (ch == eof_ || !ACE_OS::ace_isspace (ch))
139 return false; // invalid HTTP method string
140 // skip whitespace
141 while (ACE_OS::ace_isspace (str.peek ()))
143 str.get ();
145 // get uri
146 ch = this->read_ws_field (str, uri, MAX_URI_LENGTH);
147 if (ch == eof_ || !ACE_OS::ace_isspace (ch))
148 return false; // invalid HTTP uri string
149 // skip whitespace
150 while (ACE_OS::ace_isspace (str.peek ()))
152 str.get ();
154 // get version
155 ch = this->read_ws_field (str, version, MAX_VERSION_LENGTH);
156 if (ch == eof_ || !ACE_OS::ace_isspace (ch))
157 return false; // invalid HTTP version string
158 // skip to eol
159 while (ch != '\n' && ch != eof_)
160 ch = str.get ();
161 // get header lines
162 if (!Header::read (str))
163 return false;
164 // skip empty line
165 ch = str.get ();
166 while (ch != '\n' && ch != eof_)
167 ch = str.get ();
168 this->set_method (method);
169 this->set_URI (uri);
170 this->set_version(version);
171 return true;
177 ACE_END_VERSIONED_NAMESPACE_DECL