LIPH's C++ Codes
http.h
Go to the documentation of this file.
1#ifndef LIPH_NET_HTTP_H_
2#define LIPH_NET_HTTP_H_
3
4#include "liph/macros.h"
5
6#ifdef OS_UNIX
7
8#include <map>
9#include <string>
10
11#include "liph/net/tcp.h"
12
13namespace liph::net {
14
15class http_request {
16public:
17 http_request();
18
19 std::string method() const { return method_; }
20
21 void method(const std::string& method);
22
23 std::string url() const { return url_; }
24
25 void url(const std::string& url) { url_ = url; }
26
27 std::string host() const { return host_; }
28
29 void host(const std::string& host) { host_ = host; }
30
31private:
32 bool parse_header(std::string&);
33 bool parse_request_line(const std::string&);
34
35 void clear() {
36 method_.clear();
37 url_.clear();
38 version_.clear();
39 host_.clear();
40 header_.clear();
41 }
42
43 std::string method_;
44 std::string url_;
45 std::string version_;
46 std::string host_;
47 std::map<std::string, std::string> header_;
48};
49
50class http_response {
51public:
52 http_response();
53 void set_content(const std::string&, const std::string&);
54 std::string content() const;
55 void parse_header(const std::string&);
56 std::map<std::string, std::string> header_;
57 std::string content_;
58};
59
60class http_server {
61public:
62 typedef void (*http_handle)(const http_request& req, http_response& res);
63 http_server(int port = 80);
64 ~http_server();
65 void set_handle(const std::string& url, http_handle func);
66 void set_static_sever(const std::string& url, const std::string& path);
67 void start();
68
69private:
70 static void handle(std::shared_ptr<socket> client);
71 int port_;
72 std::map<std::string, http_handle> url_map_;
73};
74
75} // namespace liph::net
76
77#endif
78
79#endif // LIPH_NET_HTTP_H_
#define func(V)