Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef __HTTP_SESSION_H__ 7 : #define __HTTP_SESSION_H__ 8 : 9 : #include <atomic> 10 : #include <mutex> 11 : 12 : #include <boost/scoped_ptr.hpp> 13 : #include <tbb/concurrent_queue.h> 14 : 15 : #include "base/util.h" 16 : #include "io/ssl_session.h" 17 : 18 : class HttpRequest; 19 : class HttpServer; 20 : 21 : class HttpSession: public SslSession { 22 : public: 23 : typedef boost::function<void(HttpSession *session, 24 : enum TcpSession::Event event)> SessionEventCb; 25 : 26 : HttpSession(HttpServer *server, SslSocket *sock, bool async_ready = true); 27 : virtual ~HttpSession(); 28 : const std::string get_context() { return context_str_; } 29 : 30 : static bool SendSession(std::string const& s, 31 : const uint8_t *data, size_t size, size_t *sent) { 32 : HttpSessionPtr hs = GetSession(s); 33 : if (!hs) return false; 34 : return hs->Send(data, size, sent); 35 : } 36 : static std::string get_client_context(std::string const& s) { 37 : HttpSessionPtr hs = GetSession(s); 38 : if (!hs) return ""; 39 : return hs->get_client_context(); 40 : } 41 : static bool set_client_context(std::string const& s, 42 : const std::string& ctx) { 43 : HttpSessionPtr hs = GetSession(s); 44 : if (!hs) return false; 45 : hs->set_client_context(ctx); 46 : return true; 47 : } 48 : static long GetPendingTaskCount() { 49 : return task_count_; 50 : } 51 : 52 : void AcceptSession(); 53 : void RegisterEventCb(SessionEventCb cb); 54 : 55 : protected: 56 : virtual void OnRead(Buffer buffer); 57 : 58 : private: 59 : class RequestBuilder; 60 : class RequestHandler; 61 : typedef boost::intrusive_ptr<HttpSession> HttpSessionPtr; 62 : typedef std::map<std::string, HttpSessionPtr> map_type; 63 : 64 : void OnSessionEvent(TcpSession *session, 65 : enum TcpSession::Event event); 66 : 67 0 : static map_type* GetMap() { 68 0 : if (!context_map_) { 69 0 : context_map_ = new map_type(); 70 : } 71 0 : return context_map_; 72 : } 73 : static HttpSessionPtr GetSession(std::string const& s) { 74 : std::scoped_lock lock(map_mutex_); 75 : map_type::iterator it = GetMap()->find(s); 76 : if (it == GetMap()->end()) { 77 : return 0; 78 : } 79 : return it->second; 80 : } 81 : const std::string get_client_context() { return client_context_str_; } 82 : void set_client_context(const std::string& client_ctx) 83 : { client_context_str_ = client_ctx; } 84 : boost::scoped_ptr<RequestBuilder> request_builder_; 85 : tbb::concurrent_queue<HttpRequest *> request_queue_; 86 : std::atomic<bool> req_queue_empty_; 87 : std::string context_str_; 88 : std::string client_context_str_; 89 : SessionEventCb event_cb_; 90 : 91 : static int req_handler_task_id_; 92 : static map_type* context_map_; 93 : static std::mutex map_mutex_; 94 : static std::atomic<long> task_count_; 95 : 96 : DISALLOW_COPY_AND_ASSIGN(HttpSession); 97 : }; 98 : 99 : #endif /* __HTTP_SESSION_H__ */