HTTPSocket Class ReferenceHTTP request/response base class.
More...
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Public Member Functions | |
| HTTPSocket (ISocketHandler &) | |
| ~HTTPSocket () | |
| void | OnRawData (const char *buf, size_t len) |
| This callback is executed after a successful read from the socket. | |
| void | OnLine (const std::string &line) |
| Callback fires when a socket in line protocol has read one full line. | |
| virtual void | OnFirst ()=0 |
| Callback executes when first line has been received. | |
| virtual void | OnHeader (const std::string &key, const std::string &value)=0 |
| For each header line this callback is executed. | |
| virtual void | OnHeaderComplete ()=0 |
| Callback fires when all http headers have been received. | |
| virtual void | OnData (const char *, size_t)=0 |
| Chunk of http body data recevied. | |
| virtual void | OnDataComplete () |
| The full request/response body has been received. | |
| const std::string & | GetMethod () |
| Get http method from incoming request, ie GET/POST/PUT etc. | |
| void | SetMethod (const std::string &x) |
| Set http method to be used in request. | |
| const std::string & | GetUrl () |
| Get url from request. | |
| void | SetUrl (const std::string &x) |
| Set url to be used in outgoing request. | |
| const std::string & | GetUri () |
| Get part of url before '?' character. | |
| void | SetUri (const std::string &x) |
| Now why would I need this when there is a SetUrl method? | |
| const std::string & | GetQueryString () |
| Get part of url after '?' character. | |
| const std::string & | GetHttpVersion () |
| Get http version from incoming request/response. | |
| const std::string & | GetStatus () |
| Get http status from incoming response. | |
| const std::string & | GetStatusText () |
| Get http statustext from incoming response. | |
| bool | IsRequest () |
| Incoming header has been identified as a request (method url http_version ). | |
| bool | IsResponse () |
| Incoming header has been identified as a response (http_version status status_text ). | |
| void | SetHttpVersion (const std::string &x) |
| Set http version to be used in outgoing request/response. | |
| void | SetStatus (const std::string &x) |
| Set http status for outgoing response. | |
| void | SetStatusText (const std::string &x) |
| Set http statustext for outgoing response. | |
| void | AddResponseHeader (const std::string &x, const std::string &y) |
| Add (and replace if exists) http header. | |
| void | AddResponseHeader (const std::string &x, const char *format,...) |
| Add (and replace if exists) http header. | |
| void | AppendResponseHeader (const std::string &x, const std::string &y) |
| Add http header. | |
| bool | ResponseHeaderIsSet (const std::string &name) |
| See if http header 'name' has been set. | |
| void | SendResponse () |
| Send response prepared with calls to methods SetHttpVersion, SetStatus, SetStatusText, and AddResponseHeader. | |
| void | SendRequest () |
| Send request prepared with calls to methods SetMethod, SetUrl, SetHttpVersion, and AddResponseHeader. | |
| virtual std::string | MyUseragent () |
| Implement this to return your own User-agent string. | |
| void | url_this (const std::string &url_in, std::string &protocol, std::string &host, port_t &port, std::string &url, std::string &file) |
| Parse url. | |
| bool | IsChunked () |
| Transfer coding 'chunked'. | |
Protected Member Functions | |
| HTTPSocket (const HTTPSocket &s) | |
| virtual void | Reset () |
| Reset state of socket to sucessfully implement keep-alive. | |
| void | SetMaxHeaderCount (int x) |
Private Types | |
| typedef Utility::ncmap < std::string > | string_m |
| map to hold http header values. | |
Private Member Functions | |
| HTTPSocket & | operator= (const HTTPSocket &) |
Private Attributes | |
| bool | m_first |
| bool | m_header |
| std::string | m_line |
| Current line in line protocol mode. | |
| std::string | m_method |
| std::string | m_url |
| std::string | m_uri |
| std::string | m_query_string |
| std::string | m_http_version |
| std::string | m_status |
| std::string | m_status_text |
| bool | m_request |
| bool | m_response |
| string_m | m_response_header |
| size_t | m_body_size_left |
| bool | m_b_http_1_1 |
| bool | m_b_keepalive |
| std::list< std::pair < std::string, std::string > > | m_response_header_append |
| bool | m_b_chunked |
| size_t | m_chunk_size |
| int | m_chunk_state |
| std::string | m_chunk_line |
| int | m_header_count |
| int | m_max_header_count |
Definition at line 47 of file HTTPSocket.h.
typedef Utility::ncmap<std::string> HTTPSocket::string_m [private] |
| HTTPSocket::HTTPSocket | ( | ISocketHandler & | h | ) |
Definition at line 45 of file HTTPSocket.cpp.
References TcpSocket::DisableInputBuffer(), and TcpSocket::SetLineProtocol().
00046 :TcpSocket(h) 00047 ,m_first(true) 00048 ,m_header(true) 00049 ,m_http_version("HTTP/1.0") 00050 ,m_request(false) 00051 ,m_response(false) 00052 ,m_body_size_left(0) 00053 ,m_b_http_1_1(false) 00054 ,m_b_keepalive(false) 00055 ,m_b_chunked(false) 00056 ,m_chunk_size(0) 00057 ,m_chunk_state(0) 00058 ,m_header_count(0) 00059 ,m_max_header_count(MAX_HTTP_HEADER_COUNT) 00060 { 00061 SetLineProtocol(); 00062 DisableInputBuffer(); 00063 }
| HTTPSocket::~HTTPSocket | ( | ) |
| HTTPSocket::HTTPSocket | ( | const HTTPSocket & | s | ) | [inline, protected] |
| void HTTPSocket::OnRawData | ( | const char * | buf, | |
| size_t | len | |||
| ) | [virtual] |
This callback is executed after a successful read from the socket.
| buf | Pointer to the data | |
| len | Length of the data |
Reimplemented from TcpSocket.
Definition at line 71 of file HTTPSocket.cpp.
References Parse::getword(), Utility::hex2unsigned(), m_b_chunked, m_b_http_1_1, m_b_keepalive, m_body_size_left, m_chunk_line, m_chunk_size, m_chunk_state, m_first, m_header, OnData(), OnDataComplete(), TcpSocket::OnRead(), TcpSocket::SetLineProtocol(), and TCP_BUFSIZE_READ.
00072 { 00073 if (!m_header) 00074 { 00075 if (m_b_chunked) 00076 { 00077 size_t ptr = 0; 00078 while (ptr < len) 00079 { 00080 switch (m_chunk_state) 00081 { 00082 case 4: 00083 while (ptr < len && (m_chunk_line.size() < 2 || m_chunk_line.substr(m_chunk_line.size() - 2) != "\r\n")) 00084 m_chunk_line += buf[ptr++]; 00085 if (m_chunk_line.size() > 1 && m_chunk_line.substr(m_chunk_line.size() - 2) == "\r\n") 00086 { 00087 OnDataComplete(); 00088 // prepare for next request(or response) 00089 m_b_chunked = false; 00090 SetLineProtocol( true ); 00091 m_first = true; 00092 m_header = true; 00093 m_body_size_left = 0; 00094 if (len - ptr > 0) 00095 { 00096 char tmp[TCP_BUFSIZE_READ]; 00097 memcpy(tmp, buf + ptr, len - ptr); 00098 tmp[len - ptr] = 0; 00099 OnRead( tmp, len - ptr ); 00100 ptr = len; 00101 } 00102 } 00103 break; 00104 case 0: 00105 while (ptr < len && (m_chunk_line.size() < 2 || m_chunk_line.substr(m_chunk_line.size() - 2) != "\r\n")) 00106 m_chunk_line += buf[ptr++]; 00107 if (m_chunk_line.size() > 1 && m_chunk_line.substr(m_chunk_line.size() - 2) == "\r\n") 00108 { 00109 m_chunk_line.resize(m_chunk_line.size() - 2); 00110 Parse pa(m_chunk_line, ";"); 00111 std::string size_str = pa.getword(); 00112 m_chunk_size = Utility::hex2unsigned(size_str); 00113 if (!m_chunk_size) 00114 { 00115 m_chunk_state = 4; 00116 m_chunk_line = ""; 00117 } 00118 else 00119 { 00120 m_chunk_state = 1; 00121 m_chunk_line = ""; 00122 } 00123 } 00124 break; 00125 case 1: 00126 { 00127 size_t left = len - ptr; 00128 size_t sz = m_chunk_size < left ? m_chunk_size : left; 00129 OnData(buf + ptr, sz); 00130 m_chunk_size -= sz; 00131 ptr += sz; 00132 if (!m_chunk_size) 00133 { 00134 m_chunk_state = 2; 00135 } 00136 } 00137 break; 00138 case 2: // skip CR 00139 ptr++; 00140 m_chunk_state = 3; 00141 break; 00142 case 3: // skip LF 00143 ptr++; 00144 m_chunk_state = 0; 00145 break; 00146 } 00147 } 00148 } 00149 else 00150 if (!m_b_http_1_1 || !m_b_keepalive) 00151 { 00152 OnData(buf, len); 00153 /* 00154 request is HTTP/1.0 _or_ HTTP/1.1 and not keep-alive 00155 00156 This means we destroy the connection after the response has been delivered, 00157 hence no need to reset all internal state variables for a new incoming 00158 request. 00159 */ 00160 m_body_size_left -= len; 00161 if (!m_body_size_left) 00162 { 00163 OnDataComplete(); 00164 } 00165 } 00166 else 00167 { 00168 size_t sz = m_body_size_left < len ? m_body_size_left : len; 00169 OnData(buf, sz); 00170 m_body_size_left -= sz; 00171 if (!m_body_size_left) 00172 { 00173 OnDataComplete(); 00174 // prepare for next request(or response) 00175 SetLineProtocol( true ); 00176 m_first = true; 00177 m_header = true; 00178 m_body_size_left = 0; 00179 if (len - sz > 0) 00180 { 00181 char tmp[TCP_BUFSIZE_READ]; 00182 memcpy(tmp, buf + sz, len - sz); 00183 tmp[len - sz] = 0; 00184 OnRead( tmp, len - sz ); 00185 } 00186 } 00187 } 00188 } 00189 }
| void HTTPSocket::OnLine | ( | const std::string & | line | ) | [virtual] |
Callback fires when a socket in line protocol has read one full line.
| line | Line read |
Reimplemented from TcpSocket.
Definition at line 192 of file HTTPSocket.cpp.
References Parse::getrest(), Parse::getword(), Socket::Handler(), Utility::l2string(), LOG_LEVEL_FATAL, ISocketHandler::LogError(), m_b_chunked, m_b_http_1_1, m_b_keepalive, m_body_size_left, m_first, m_header, m_header_count, m_http_version, m_max_header_count, m_method, m_query_string, m_request, m_response, m_status, m_status_text, m_uri, m_url, OnDataComplete(), OnFirst(), OnHeader(), OnHeaderComplete(), Socket::SetCloseAndDelete(), TcpSocket::SetLineProtocol(), Socket::SetRetain(), and Utility::ToLower().
00193 { 00194 if (m_first) 00195 { 00196 Parse pa(line); 00197 std::string str = pa.getword(); 00198 if (str.size() > 4 && Utility::ToLower(str.substr(0,5)) == "http/") // response 00199 { 00200 m_http_version = str; 00201 m_status = pa.getword(); 00202 m_status_text = pa.getrest(); 00203 m_response = true; 00204 } 00205 else // request 00206 { 00207 m_method = str; 00208 m_url = pa.getword(); 00209 size_t spl = m_url.find("?"); 00210 if (spl != std::string::npos) 00211 { 00212 m_uri = m_url.substr(0,spl); 00213 m_query_string = m_url.substr(spl + 1); 00214 } 00215 else 00216 { 00217 m_uri = m_url; 00218 m_query_string = ""; 00219 } 00220 m_http_version = pa.getword(); 00221 m_b_http_1_1 = m_http_version.size() > 4 && m_http_version.substr(4) == "/1.1"; 00222 m_b_keepalive = m_b_http_1_1; 00223 m_request = true; 00224 } 00225 m_first = false; 00226 OnFirst(); 00227 return; 00228 } 00229 if (!line.size()) 00230 { 00231 if (m_body_size_left || !m_b_http_1_1 || !m_b_keepalive || m_b_chunked) 00232 { 00233 SetLineProtocol(false); 00234 m_header = false; 00235 } 00236 OnHeaderComplete(); 00237 if (!m_body_size_left && !m_b_chunked) 00238 { 00239 OnDataComplete(); 00240 } 00241 return; 00242 } 00243 Parse pa(line,":"); 00244 std::string key = pa.getword(); 00245 std::string value = pa.getrest(); 00246 OnHeader(key,value); 00247 if (Utility::ToLower(key) == "content-length") 00248 { 00249 m_body_size_left = atol(value.c_str()); 00250 } 00251 if (m_b_http_1_1 && Utility::ToLower(key) == "connection") 00252 { 00253 m_b_keepalive = Utility::ToLower(value) != "close"; 00254 } 00255 if (Utility::ToLower(key) == "transfer-encoding" && Utility::ToLower(value) == "chunked") 00256 { 00257 m_b_chunked = true; 00258 } 00259 /* If remote end tells us to keep connection alive, and we're operating 00260 in http/1.1 mode (not http/1.0 mode), then we mark the socket to be 00261 retained. */ 00262 #ifdef ENABLE_POOL 00263 if (m_b_http_1_1 && m_b_keepalive) 00264 { 00265 SetRetain(); 00266 } 00267 #endif 00268 if (m_header_count++ > m_max_header_count) 00269 { 00270 SetCloseAndDelete(); 00271 Handler().LogError(this, "OnLine", m_header_count, "http header count exceeds builtin limit of (" + Utility::l2string(m_max_header_count) + ")", LOG_LEVEL_FATAL); 00272 } 00273 }
| virtual void HTTPSocket::OnFirst | ( | ) | [pure virtual] |
Callback executes when first line has been received.
GetMethod, GetUrl/GetUri, and GetHttpVersion are valid when this callback is executed.
Implemented in HttpBaseSocket, HttpClientSocket, HttpDebugSocket, and HttpdSocket.
Referenced by OnLine().
| virtual void HTTPSocket::OnHeader | ( | const std::string & | key, | |
| const std::string & | value | |||
| ) | [pure virtual] |
For each header line this callback is executed.
| key | Http header name | |
| value | Http header value |
Implemented in HttpBaseSocket, HttpClientSocket, HttpDebugSocket, and HttpdSocket.
Referenced by OnLine().
| virtual void HTTPSocket::OnHeaderComplete | ( | ) | [pure virtual] |
Callback fires when all http headers have been received.
Implemented in HttpBaseSocket, HttpClientSocket, HttpDebugSocket, and HttpdSocket.
Referenced by OnLine().
| virtual void HTTPSocket::OnData | ( | const char * | , | |
| size_t | ||||
| ) | [pure virtual] |
Chunk of http body data recevied.
Implemented in HttpBaseSocket, HttpClientSocket, HttpDebugSocket, and HttpdSocket.
Referenced by OnRawData().
| virtual void HTTPSocket::OnDataComplete | ( | ) | [inline, virtual] |
The full request/response body has been received.
Reimplemented in HttpDebugSocket.
Definition at line 70 of file HTTPSocket.h.
Referenced by OnLine(), and OnRawData().
| const std::string & HTTPSocket::GetMethod | ( | ) |
Get http method from incoming request, ie GET/POST/PUT etc.
Definition at line 359 of file HTTPSocket.cpp.
References m_method.
Referenced by HttpDebugSocket::OnFirst(), HttpBaseSocket::OnFirst(), and HttpdSocket::OnHeaderComplete().
00360 { 00361 return m_method; 00362 }
| void HTTPSocket::SetMethod | ( | const std::string & | x | ) |
Set http method to be used in request.
Definition at line 365 of file HTTPSocket.cpp.
References m_method.
Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), and HttpGetSocket::OnConnect().
00366 { 00367 m_method = x; 00368 }
| const std::string & HTTPSocket::GetUrl | ( | ) |
Get url from request.
Definition at line 371 of file HTTPSocket.cpp.
References m_url.
Referenced by HttpDebugSocket::OnFirst().
00372 { 00373 return m_url; 00374 }
| void HTTPSocket::SetUrl | ( | const std::string & | x | ) |
Set url to be used in outgoing request.
Definition at line 377 of file HTTPSocket.cpp.
References m_url.
Referenced by HttpClientSocket::HttpClientSocket(), HttpGetSocket::HttpGetSocket(), and HttpClientSocket::Url().
00378 { 00379 m_url = x; 00380 }
| const std::string & HTTPSocket::GetUri | ( | ) |
Get part of url before '?' character.
Definition at line 383 of file HTTPSocket.cpp.
References m_uri.
Referenced by HttpdSocket::OnData(), HttpBaseSocket::OnFirst(), and HttpdSocket::OnHeaderComplete().
00384 { 00385 return m_uri; 00386 }
| void HTTPSocket::SetUri | ( | const std::string & | x | ) |
Now why would I need this when there is a SetUrl method?
Definition at line 455 of file HTTPSocket.cpp.
References m_uri.
00456 { 00457 m_uri = x; 00458 }
| const std::string & HTTPSocket::GetQueryString | ( | ) |
Get part of url after '?' character.
Definition at line 389 of file HTTPSocket.cpp.
References m_query_string.
Referenced by HttpBaseSocket::OnFirst(), and HttpdSocket::OnHeaderComplete().
00390 { 00391 return m_query_string; 00392 }
| const std::string & HTTPSocket::GetHttpVersion | ( | ) |
Get http version from incoming request/response.
Definition at line 395 of file HTTPSocket.cpp.
References m_http_version.
Referenced by HttpDebugSocket::OnFirst(), HttpClientSocket::OnFirst(), and HttpBaseSocket::OnFirst().
00396 { 00397 return m_http_version; 00398 }
| const std::string & HTTPSocket::GetStatus | ( | ) |
Get http status from incoming response.
Definition at line 401 of file HTTPSocket.cpp.
References m_status.
Referenced by HttpClientSocket::OnFirst().
00402 { 00403 return m_status; 00404 }
| const std::string & HTTPSocket::GetStatusText | ( | ) |
Get http statustext from incoming response.
Definition at line 407 of file HTTPSocket.cpp.
References m_status_text.
Referenced by HttpClientSocket::OnFirst().
00408 { 00409 return m_status_text; 00410 }
| bool HTTPSocket::IsRequest | ( | ) |
Incoming header has been identified as a request (method url http_version
).
Definition at line 413 of file HTTPSocket.cpp.
References m_request.
00414 { 00415 return m_request; 00416 }
| bool HTTPSocket::IsResponse | ( | ) |
Incoming header has been identified as a response (http_version status status_text
).
Definition at line 419 of file HTTPSocket.cpp.
References m_response.
Referenced by HttpClientSocket::OnFirst().
00420 { 00421 return m_response; 00422 }
| void HTTPSocket::SetHttpVersion | ( | const std::string & | x | ) |
Set http version to be used in outgoing request/response.
Definition at line 425 of file HTTPSocket.cpp.
References m_http_version.
Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), and HttpBaseSocket::Respond().
00426 { 00427 m_http_version = x; 00428 }
| void HTTPSocket::SetStatus | ( | const std::string & | x | ) |
Set http status for outgoing response.
Definition at line 431 of file HTTPSocket.cpp.
References m_status.
Referenced by HttpdSocket::OnHeaderComplete(), HttpBaseSocket::Respond(), and HttpdSocket::Send64().
00432 { 00433 m_status = x; 00434 }
| void HTTPSocket::SetStatusText | ( | const std::string & | x | ) |
Set http statustext for outgoing response.
Definition at line 437 of file HTTPSocket.cpp.
References m_status_text.
Referenced by HttpdSocket::OnHeaderComplete(), HttpBaseSocket::Respond(), and HttpdSocket::Send64().
00438 { 00439 m_status_text = x; 00440 }
| void HTTPSocket::AddResponseHeader | ( | const std::string & | x, | |
| const std::string & | y | |||
| ) |
Add (and replace if exists) http header.
Definition at line 443 of file HTTPSocket.cpp.
References m_response_header.
Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), HttpGetSocket::OnConnect(), HttpdSocket::OnData(), HttpdSocket::OnHeaderComplete(), HttpBaseSocket::Respond(), and HttpdSocket::Send64().
00444 { 00445 m_response_header[x] = y; 00446 }
| void HTTPSocket::AddResponseHeader | ( | const std::string & | x, | |
| const char * | format, | |||
| ... | ||||
| ) |
Add (and replace if exists) http header.
Definition at line 295 of file HTTPSocket.cpp.
References m_response_header.
00296 { 00297 char slask[8192]; // temporary for vsprintf / vsnprintf 00298 va_list ap; 00299 00300 va_start(ap, format); 00301 #ifdef _WIN32 00302 vsprintf_s(slask, sizeof(slask), format, ap); 00303 #else 00304 vsnprintf(slask, sizeof(slask), format, ap); 00305 #endif 00306 va_end(ap); 00307 00308 m_response_header[header] = slask; 00309 }
| void HTTPSocket::AppendResponseHeader | ( | const std::string & | x, | |
| const std::string & | y | |||
| ) |
Add http header.
Definition at line 449 of file HTTPSocket.cpp.
References m_response_header_append.
Referenced by HttpBaseSocket::Respond().
00450 { 00451 m_response_header_append.push_back(std::pair<std::string, std::string>(x,y)); 00452 }
| bool HTTPSocket::ResponseHeaderIsSet | ( | const std::string & | name | ) |
See if http header 'name' has been set.
Definition at line 498 of file HTTPSocket.cpp.
References m_response_header, and m_response_header_append.
Referenced by HttpBaseSocket::Respond().
00499 { 00500 string_m::iterator it = m_response_header.find( name ); 00501 if (it != m_response_header.end()) 00502 { 00503 return true; 00504 } 00505 std::list<std::pair<std::string, std::string> >::iterator it2; 00506 for (it2 = m_response_header_append.begin(); it2 != m_response_header_append.end(); ++it2) 00507 { 00508 std::pair<std::string, std::string>& ref = *it2; 00509 if (!strcasecmp(ref.first.c_str(), name.c_str()) ) 00510 { 00511 return true; 00512 } 00513 } 00514 return false; 00515 }
| void HTTPSocket::SendResponse | ( | ) |
Send response prepared with calls to methods SetHttpVersion, SetStatus, SetStatusText, and AddResponseHeader.
Definition at line 276 of file HTTPSocket.cpp.
References m_http_version, m_response_header, m_response_header_append, m_status, m_status_text, and TcpSocket::Send().
Referenced by HttpdSocket::OnHeaderComplete(), HttpBaseSocket::Respond(), and HttpdSocket::Send64().
00277 { 00278 std::string msg; 00279 msg = m_http_version + " " + m_status + " " + m_status_text + "\r\n"; 00280 for (string_m::iterator it = m_response_header.begin(); it != m_response_header.end(); ++it) 00281 { 00282 std::string key = (*it).first; 00283 std::string val = (*it).second; 00284 msg += key + ": " + val + "\r\n"; 00285 } 00286 for (std::list<std::pair<std::string, std::string> >::iterator it2 = m_response_header_append.begin(); it2 != m_response_header_append.end(); ++it2) 00287 { 00288 msg += it2 -> first + ": " + it2 -> second + "\r\n"; 00289 } 00290 msg += "\r\n"; 00291 Send( msg ); 00292 }
| void HTTPSocket::SendRequest | ( | ) |
Send request prepared with calls to methods SetMethod, SetUrl, SetHttpVersion, and AddResponseHeader.
Definition at line 312 of file HTTPSocket.cpp.
References m_http_version, m_method, m_response_header, m_url, and TcpSocket::Send().
Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), and HttpGetSocket::OnConnect().
00313 { 00314 std::string msg; 00315 msg = m_method + " " + m_url + " " + m_http_version + "\r\n"; 00316 for (string_m::iterator it = m_response_header.begin(); it != m_response_header.end(); ++it) 00317 { 00318 std::string key = (*it).first; 00319 std::string val = (*it).second; 00320 msg += key + ": " + val + "\r\n"; 00321 } 00322 msg += "\r\n"; 00323 Send( msg ); 00324 }
| std::string HTTPSocket::MyUseragent | ( | ) | [virtual] |
Implement this to return your own User-agent string.
Definition at line 327 of file HTTPSocket.cpp.
Referenced by HttpPostSocket::DoMultipartPost(), HttpPutSocket::OnConnect(), HttpPostSocket::OnConnect(), and HttpGetSocket::OnConnect().
00328 { 00329 std::string version = "C++Sockets/"; 00330 #ifdef _VERSION 00331 version += _VERSION; 00332 #endif 00333 return version; 00334 }
| void HTTPSocket::url_this | ( | const std::string & | url_in, | |
| std::string & | protocol, | |||
| std::string & | host, | |||
| port_t & | port, | |||
| std::string & | url, | |||
| std::string & | file | |||
| ) |
Parse url.
If protocol is https, EnableSSL() will be called.
Definition at line 461 of file HTTPSocket.cpp.
References Socket::EnableSSL(), Parse::getrest(), Parse::getvalue(), Parse::getword(), Socket::Handler(), LOG_LEVEL_WARNING, and ISocketHandler::LogError().
Referenced by HttpClientSocket::HttpClientSocket(), and HttpClientSocket::Url().
00462 { 00463 Parse pa(url_in,"/"); 00464 protocol = pa.getword(); // http 00465 if (!strcasecmp(protocol.c_str(), "https:")) 00466 { 00467 #ifdef HAVE_OPENSSL 00468 EnableSSL(); 00469 #else 00470 Handler().LogError(this, "url_this", -1, "SSL not available", LOG_LEVEL_WARNING); 00471 #endif 00472 port = 443; 00473 } 00474 else 00475 { 00476 port = 80; 00477 } 00478 host = pa.getword(); 00479 if (strstr(host.c_str(),":")) 00480 { 00481 Parse pa(host,":"); 00482 pa.getword(host); 00483 port = static_cast<port_t>(pa.getvalue()); 00484 } 00485 url = "/" + pa.getrest(); 00486 { 00487 Parse pa(url,"/"); 00488 std::string tmp = pa.getword(); 00489 while (tmp.size()) 00490 { 00491 file = tmp; 00492 tmp = pa.getword(); 00493 } 00494 } 00495 } // url_this
| bool HTTPSocket::IsChunked | ( | ) | [inline] |
Transfer coding 'chunked'.
Definition at line 124 of file HTTPSocket.h.
Referenced by HttpDebugSocket::OnHeaderComplete().
00124 { return m_b_chunked; }
| void HTTPSocket::Reset | ( | ) | [protected, virtual] |
Reset state of socket to sucessfully implement keep-alive.
Reimplemented in HttpBaseSocket, and HttpdSocket.
Definition at line 337 of file HTTPSocket.cpp.
References m_first, m_header, m_header_count, m_request, m_response, m_response_header, m_response_header_append, and TcpSocket::SetLineProtocol().
Referenced by HttpdSocket::Reset(), and HttpBaseSocket::Reset().
00338 { 00339 m_first = true; 00340 m_header = true; 00341 m_request = false; 00342 m_response = false; 00343 SetLineProtocol(true); 00344 while (m_response_header.size()) 00345 { 00346 string_m::iterator it = m_response_header.begin(); 00347 m_response_header.erase(it); 00348 } 00349 while (m_response_header_append.size()) 00350 { 00351 std::list<std::pair<std::string, std::string> >::iterator it = m_response_header_append.begin(); 00352 m_response_header_append.erase(it); 00353 } 00354 m_header_count = 0; 00355 00356 }
| void HTTPSocket::SetMaxHeaderCount | ( | int | x | ) | [inline, protected] |
| HTTPSocket& HTTPSocket::operator= | ( | const HTTPSocket & | ) | [inline, private] |
bool HTTPSocket::m_first [private] |
bool HTTPSocket::m_header [private] |
std::string HTTPSocket::m_line [private] |
Current line in line protocol mode.
Reimplemented from TcpSocket.
Definition at line 137 of file HTTPSocket.h.
std::string HTTPSocket::m_method [private] |
Definition at line 138 of file HTTPSocket.h.
Referenced by GetMethod(), OnLine(), SendRequest(), and SetMethod().
std::string HTTPSocket::m_url [private] |
Definition at line 139 of file HTTPSocket.h.
Referenced by GetUrl(), OnLine(), SendRequest(), and SetUrl().
std::string HTTPSocket::m_uri [private] |
std::string HTTPSocket::m_query_string [private] |
std::string HTTPSocket::m_http_version [private] |
Definition at line 142 of file HTTPSocket.h.
Referenced by GetHttpVersion(), OnLine(), SendRequest(), SendResponse(), and SetHttpVersion().
std::string HTTPSocket::m_status [private] |
Definition at line 143 of file HTTPSocket.h.
Referenced by GetStatus(), OnLine(), SendResponse(), and SetStatus().
std::string HTTPSocket::m_status_text [private] |
Definition at line 144 of file HTTPSocket.h.
Referenced by GetStatusText(), OnLine(), SendResponse(), and SetStatusText().
bool HTTPSocket::m_request [private] |
bool HTTPSocket::m_response [private] |
string_m HTTPSocket::m_response_header [private] |
Definition at line 147 of file HTTPSocket.h.
Referenced by AddResponseHeader(), Reset(), ResponseHeaderIsSet(), SendRequest(), and SendResponse().
size_t HTTPSocket::m_body_size_left [private] |
Reimplemented in HttpBaseSocket.
Definition at line 148 of file HTTPSocket.h.
Referenced by OnLine(), and OnRawData().
bool HTTPSocket::m_b_http_1_1 [private] |
bool HTTPSocket::m_b_keepalive [private] |
Reimplemented in HttpBaseSocket.
Definition at line 150 of file HTTPSocket.h.
Referenced by OnLine(), and OnRawData().
std::list<std::pair<std::string, std::string> > HTTPSocket::m_response_header_append [private] |
Definition at line 151 of file HTTPSocket.h.
Referenced by AppendResponseHeader(), Reset(), ResponseHeaderIsSet(), and SendResponse().
bool HTTPSocket::m_b_chunked [private] |
size_t HTTPSocket::m_chunk_size [private] |
int HTTPSocket::m_chunk_state [private] |
std::string HTTPSocket::m_chunk_line [private] |
int HTTPSocket::m_header_count [private] |
int HTTPSocket::m_max_header_count [private] |
1.4.4