#include <httpsrv.h>
Public Member Functions | |
HttpServer () | |
~HttpServer () | |
void | enable_extensions (bool flag=true) |
void | register_url (const char *prefix) |
void | unregister_url (const char *prefix) |
void | receive_request (HttpRequest &request) |
void | send_response (HttpRequest &request, const string &str, const char *content_type) |
void | send_error (HttpRequest &request, u_short code, const char *status_text) |
void | send (u_short code, const char *status_text, const string &data, const char *content_type, bool connection_close) |
Protected Attributes | |
AddressList | _addr |
Socket | _listen_socket |
string | _url_prefix |
Socket | _conn_socket |
bool | _extensions |
Private Member Functions | |
bool | handle_request (HttpRequest &request) |
Definition at line 64 of file httpsrv.h.
HttpServer::HttpServer | ( | ) |
Definition at line 209 of file httpsrv.cpp.
: _extensions(false) { }
HttpServer::~HttpServer | ( | ) |
Definition at line 214 of file httpsrv.cpp.
{ }
void HttpServer::enable_extensions | ( | bool | flag = true ) |
[inline] |
bool HttpServer::handle_request | ( | HttpRequest & | request ) | [private] |
Definition at line 265 of file httpsrv.cpp.
References HttpRequest::_command, _conn_socket, HttpRequest::_data, _stricmp, _strnicmp, BUFFER_LEN, Socket::close(), HTTP_GET, HTTP_POST, HTTP_UNKNOWN, SocketReader::read(), SocketReader::read_line(), and sscanf_s.
Referenced by receive_request().
{ SocketReader reader(_conn_socket); // read the first request line char line[BUFFER_LEN]; if (reader.read_line(line, BUFFER_LEN) <= 0) { // The client connection has been closed. _conn_socket.close(); return false; } char command[32]; char path[BUFFER_LEN]; char version[12]; #ifdef __STDC_WANT_SECURE_LIB__ if (sscanf_s(line, "%s %s %s", command, _countof(command), path, _countof(path), version, _countof(version)) != 3) #else if (sscanf(line, "%s %s %s", command, path, version) != 3) #endif throw Exception("invalid HTML server response"); if (!_stricmp(command, "GET")) request._command = HTTP_GET; else if (!_stricmp(command, "POST")) request._command = HTTP_POST; else request._command = HTTP_UNKNOWN; // read the request header int content_length = 0; string content_type; // string host; for(;;) { if (reader.read_line(line, BUFFER_LEN) <= 0) break; if (!_strnicmp(line, "Content-length:", 15)) content_length = atoi(line+15); else if (!_strnicmp(line, "Content-type:", 13)) content_type = line+14; // else if (!_strnicmp(line, "Host:", 5)) // host = line+6; } if (!content_type.empty() && _strnicmp(content_type.c_str(),"text/xml",8) && _strnicmp(content_type.c_str(),"application/x-www-form-urlencoded",33)) { char b[BUFFER_LEN]; sprintf(b, "illegal content type: %s", content_type.c_str()); throw Exception(b); } if (content_length) { request._data.resize(content_length); reader.read(&request._data.at(0), content_length); } return true; }
void HttpServer::receive_request | ( | HttpRequest & | request ) |
Definition at line 247 of file httpsrv.cpp.
References _conn_socket, _listen_socket, handle_request(), Socket::init(), and INVALID_SOCKET.
Referenced by server_test().
{ do { if (_conn_socket == INVALID_SOCKET) { if (listen(_listen_socket, 4) < 0) throw LWSE(); SOCKADDR_IN sin; socklen_t sin_len = sizeof(sin); SOCKET accept_socket = accept(_listen_socket, (struct sockaddr*)&sin, &sin_len); if (accept_socket != INVALID_SOCKET) _conn_socket.init(accept_socket); } } while(!handle_request(request)); }
void HttpServer::register_url | ( | const char * | prefix ) |
Definition at line 219 of file httpsrv.cpp.
References _addr, URL::_hostname, _listen_socket, URL::_path, URL::_port, _url_prefix, Socket::bind(), and AddressList::resolve().
Referenced by server_test().
{ URL parsed_url(prefix); if (!parsed_url._hostname.empty() && parsed_url._hostname!="+") // initialize address with hostname and port _addr.resolve(parsed_url._hostname.c_str(), parsed_url._port.c_str(), 0); else // initialize address only with port assignment _addr.resolve(NULL, parsed_url._port.c_str(), AI_PASSIVE); _url_prefix = parsed_url._path; _listen_socket.bind(_addr); }
void HttpServer::send | ( | u_short | code, |
const char * | status_text, | ||
const string & | data, | ||
const char * | content_type, | ||
bool | connection_close | ||
) |
Definition at line 345 of file httpsrv.cpp.
References _conn_socket, Socket::close(), and Socket::send().
Referenced by send_error(), and send_response().
{ // send the HTML response header ostringstream buffer; buffer << "HTTP/1.1 200 OK\r\n"; buffer << "Server: XMLStorage XMLRPC\r\n"; buffer << "Content-length: " << data.length() << "\r\n"; buffer << "Content-type: " << content_type << "\r\n"; // buffer << "Date: ...\r\n"; if (connection_close) buffer << "Connection: close\r\n"; buffer << "\r\n"; const string& header = buffer.str(); _conn_socket.send(header.c_str(), header.length()); // send the response body _conn_socket.send(data.c_str(), data.length()); if (connection_close) _conn_socket.close(); }
void HttpServer::send_error | ( | HttpRequest & | request, |
u_short | code, | ||
const char * | status_text | ||
) |
Definition at line 335 of file httpsrv.cpp.
References BUFFER_LEN, and send().
Referenced by server_test().
{ char buffer[BUFFER_LEN]; sprintf(buffer, "%d - %s", code, status_text); string error_msg = buffer; send(code, status_text, error_msg, "text/plain", true); }
void HttpServer::send_response | ( | HttpRequest & | request, |
const string & | str, | ||
const char * | content_type | ||
) |
Definition at line 330 of file httpsrv.cpp.
References _extensions, and send().
Referenced by server_test().
{ send(200, "OK", str, content_type, !_extensions); }
void HttpServer::unregister_url | ( | const char * | prefix ) |
Definition at line 235 of file httpsrv.cpp.
References _addr, _listen_socket, URL::_path, _url_prefix, AddressList::clear(), and Socket::close().
{ URL parsed_url(prefix); if (parsed_url._path == _url_prefix) { _listen_socket.close(); _addr.clear(); _url_prefix.erase(); } }
AddressList HttpServer::_addr [protected] |
Definition at line 92 of file httpsrv.h.
Referenced by register_url(), and unregister_url().
Socket HttpServer::_conn_socket [protected] |
Definition at line 97 of file httpsrv.h.
Referenced by handle_request(), receive_request(), and send().
bool HttpServer::_extensions [protected] |
Definition at line 100 of file httpsrv.h.
Referenced by enable_extensions(), and send_response().
Socket HttpServer::_listen_socket [protected] |
Definition at line 93 of file httpsrv.h.
Referenced by receive_request(), register_url(), and unregister_url().
string HttpServer::_url_prefix [protected] |
Definition at line 94 of file httpsrv.h.
Referenced by register_url(), and unregister_url().