jle_cpp_tk  0.0 2015-04-03 sh1:"d699093732dd5f321606d0ff7a6b63b229f1922c"
A small, safe, selft contained, soft-realtime C++ toolkit
http_server.h
1 #ifndef JLE_NET_HTTP_SERVER_H
2 #define JLE_NET_HTTP_SERVER_H
3 
4 #include <string>
5 #include "core/cont/map.hpp"
6 #include "core/signal_slot.hpp"
7 #include "core/cont/list.hpp"
8 
9 namespace std { class thread; };
10 
12 
13 
14 
15 struct ns_mgr;
16 struct ns_connection;
17 struct http_message;
18 
19 
20 namespace jle { namespace net { namespace http {
21 
22 
23 struct Map {
24  std::string full;
25 
27 };
28 std::ostream& operator<<(std::ostream& out, const Map& m);
29 
30 struct List {
31  std::string full;
32 
34 };
35 std::ostream& operator<<(std::ostream& out, const List& m);
36 
37 struct Req_resp_line {
38  std::string full;
39 
40  std::string method;
41  std::string uri;
42  Map query_string;
43  std::string proto;
44 };
45 std::ostream& operator<<(std::ostream& out, const Req_resp_line& line);
46 
47 struct Request_msg {
48  Request_msg() = delete;
49 
50  std::string full;
51  Req_resp_line request_line;
52  Map headers;
53  List cookies;
54  std::string body;
55 };
56 std::ostream& operator<<(std::ostream& out, const Request_msg& rq);
57 
58 
59 struct Response_msg {
60  Response_msg()=delete;
61 
62  int code;
63  std::string phrase;
64  std::string type;
65  std::string date;
66  std::string body;
67 };
68 
69 
70 Response_msg response_ok(const std::string& body);
71 
72 
73 
74 class Server;
75 
76 class Request : public jle::signal_receptor {
77 public:
78  void send_response(const Response_msg& resp);
79  const Request_msg request_msg;
80  const std::string listen_port;
81 
82 private:
83  Request(struct ns_connection *_nc, const Request_msg& _rq, const std::string _listen_port);
84 
85 
86 
87  struct ns_connection *nc;
88  bool valid_server;
89 
90  void invalidated_server();
91 
92 friend class Server;
93 };
94 
95 
96 class Server : public jle::signal_receptor {
97 
98 public:
99  Server(const std::string& _port);
100 
101  ~Server();
102 
103 
104  jle::signal<const jle::shared_ptr<Request>&> signal_request_received;
105 
106 
107 private:
109  std::string port;
110 
111  void on_fossa_request(struct ns_connection* _nc, int /*ev*/, struct http_message& hm);
112  Request_msg parse_rq(struct http_message& hm);
113 
114  jle::signal<> signal_closing_server;
115 
116  void loop_check_messages(void);
117 
118  bool destroing=false;
119  jle::shared_ptr<std::thread> thread_check_messages;
120 };
121 
122 
123 
124 } // namespace http
125 } // namespace net
126 } // namespace jle
127 
128 #endif // JLE_NET_HTTP_SERVER_H
Definition: http_server.h:96
Definition: fossa.h:590
Definition: http_server.h:37
Definition: http_server.h:9
Definition: fossa.h:601
Definition: http_server.h:76
Definition: fossa.h:1178
Any object connected to signals, has to inherit from signal_receptor.
Definition: signal_slot.hpp:87
Definition: http_server.h:59
signal instance to connect and emit
Definition: signal_slot.hpp:232
Definition: http_server.h:47
Definition: http_server.h:23
Definition: http_server.h:30
generic namespace
Definition: alarm.cpp:12