http_server.h

00001 /*
00002  * http_server.h
00003  * This file is part of dbPager Classes Library (DCL)
00004  *
00005  * Copyright (c) 2008 Dennis Prochko <wolfsoft@mail.ru>
00006  *
00007  * DCL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation version 3.
00010  *
00011  * DCL is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with DCL; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor,
00019  * Boston, MA  02110-1301  USA
00020  */
00021 
00022 #ifndef _HTTP_SERVER_H_
00023 #define _HTTP_SERVER_H_
00024 
00025 #include <map>
00026 #include <string>
00027 #include <sstream>
00028 
00029 #include <dcl/delegate.h>
00030 #include <dcl/http_header.h>
00031 #include <dcl/mutex.h>
00032 #include <dcl/tcp_server.h>
00033 
00034 namespace dbp {
00035 
00037 
00040 class http_server: public tcp_server {
00041 public:
00043         typedef delegate1<const http_request&, http_response> on_request_handler;
00045         http_server(size_t worker_threads = 2, size_t queue_size = 32768);
00047         virtual ~http_server() { }
00049         void on_request(on_request_handler handler) {
00050                 request_handler = handler;
00051         }
00052 protected:
00053         void on_process_data(on_process_data_handler handler) {
00054                 tcp_server::on_process_data(handler);
00055         }
00056 private:
00057         // processing requests
00058         struct request {
00059                 enum states {
00060                         WAIT_COMMAND,
00061                         WAIT_HEADER,
00062                         RECEIVING_DATA,
00063                         PROCESS_REQUEST,
00064                         CLOSING
00065                 };
00066                 request(): state(WAIT_COMMAND), data_size(0), data_readed(0) { }
00067                 states state;
00068                 size_t data_size;
00069                 size_t data_readed;
00070                 http_request req;
00071         };
00072         typedef std::map<int, request*> requests;
00073         requests reqs;
00074         mutex lock;
00075         // Handlers
00076         on_request_handler request_handler;
00077         // Custom handlers
00078         bool process_data(const socket&, std::istream&, std::ostream&);
00079 };
00080 
00081 } // namespace
00082 
00083 #endif /*_HTTP_SERVER_H_*/

 
Support This Project
SourceForge.net Logo