| 1 | #ifndef _HTTP_CONNECTION_H
|
|---|
| 2 | #define _HTTP_CONNECTION_H
|
|---|
| 3 |
|
|---|
| 4 | #include <boost/shared_ptr.hpp>
|
|---|
| 5 | #include <boost/asio.hpp>
|
|---|
| 6 | #include <boost/enable_shared_from_this.hpp>
|
|---|
| 7 |
|
|---|
| 8 | #include <boost/random/mersenne_twister.hpp>
|
|---|
| 9 | #include <boost/random/uniform_int.hpp>
|
|---|
| 10 | #include <boost/random/variate_generator.hpp>
|
|---|
| 11 | #include <limits.h>
|
|---|
| 12 | #include "../../engine/estimate.h"
|
|---|
| 13 |
|
|---|
| 14 | namespace engine
|
|---|
| 15 | {
|
|---|
| 16 | class cSession;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | namespace http_server
|
|---|
| 21 | {
|
|---|
| 22 |
|
|---|
| 23 | class cResponse;
|
|---|
| 24 | class cRequest;
|
|---|
| 25 | class cConnectionManager;
|
|---|
| 26 |
|
|---|
| 27 | class cHttpConnection
|
|---|
| 28 | : public boost::enable_shared_from_this<cHttpConnection>
|
|---|
| 29 | {
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | typedef boost::shared_ptr<cHttpConnection> connection_ptr;
|
|---|
| 33 | typedef std::map<unsigned int, engine::cSession*> sessions_map;
|
|---|
| 34 |
|
|---|
| 35 | public:
|
|---|
| 36 |
|
|---|
| 37 | ~cHttpConnection();
|
|---|
| 38 |
|
|---|
| 39 | static connection_ptr Create(boost::asio::io_service& io_service, cConnectionManager &conn_manager)
|
|---|
| 40 | {
|
|---|
| 41 | return connection_ptr(new cHttpConnection(io_service, conn_manager));
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | boost::asio::ip::tcp::socket& GetSocket()
|
|---|
| 45 | {
|
|---|
| 46 | return m_Socket;
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 | void HandleClient();
|
|---|
| 50 | void HandleRequest(const boost::system::error_code& error);
|
|---|
| 51 | void HandleExistingSession(cResponse& response, const cRequest& _request, const unsigned int ses_id);
|
|---|
| 52 | void HandleWriteResponse(const boost::system::error_code& error);
|
|---|
| 53 | void StartTimeout();
|
|---|
| 54 | void HandleTimeout(const boost::system::error_code &e);
|
|---|
| 55 | void Stop();
|
|---|
| 56 | static void ClearSessions()
|
|---|
| 57 | {
|
|---|
| 58 | s_Sessions.clear();
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | private:
|
|---|
| 63 | cHttpConnection(boost::asio::io_service& io_service, cConnectionManager &conn_manager);
|
|---|
| 64 |
|
|---|
| 65 | static unsigned int GetRandUniqueId()
|
|---|
| 66 | {
|
|---|
| 67 | static boost::mt19937 gen;
|
|---|
| 68 | static boost::uniform_int<> dist(1, INT_MAX);
|
|---|
| 69 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rnd(gen, dist);
|
|---|
| 70 |
|
|---|
| 71 | unsigned int random_id = rnd();
|
|---|
| 72 |
|
|---|
| 73 | if(cHttpConnection::s_Sessions.empty())
|
|---|
| 74 | return random_id;
|
|---|
| 75 |
|
|---|
| 76 | if(cHttpConnection::s_Sessions.find(random_id) == cHttpConnection::s_Sessions.end())
|
|---|
| 77 | return random_id;
|
|---|
| 78 | else
|
|---|
| 79 | return GetRandUniqueId();
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | private:
|
|---|
| 83 | boost::asio::ip::tcp::socket m_Socket;
|
|---|
| 84 | boost::asio::streambuf m_RequestBuf;
|
|---|
| 85 | boost::asio::streambuf m_ResponseBuf;
|
|---|
| 86 | cConnectionManager &m_ConnectionManager;
|
|---|
| 87 | boost::asio::deadline_timer m_IoTimeout;
|
|---|
| 88 | bool m_HttpVersion;
|
|---|
| 89 | bool m_TimeoutStarted;
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 | static sessions_map s_Sessions;
|
|---|
| 93 | static engine::cEstimator s_Estimator;
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | typedef boost::shared_ptr<cHttpConnection> connection_ptr;
|
|---|
| 97 |
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | #endif
|
|---|