acl  3.5.3.0
http_servlet_impl.hpp
浏览该文件的文档.
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 #include <sstream>
6 #include <functional>
7 
8 namespace acl {
9 
10 class HttpServletRequest;
12 
15 
16 typedef std::function<bool(HttpRequest&, HttpResponse&)> http_handler_t;
17 typedef std::map<acl::string, http_handler_t> http_handlers_t;
18 
19 enum {
34 };
35 
37 public:
40  : HttpServlet(stream, session), handlers_(handlers) {}
41 
42  virtual ~http_servlet_impl(void) {}
43 
44 protected:
45  // override
46  bool doGet(HttpRequest& req, HttpResponse& res) {
47  return doService(http_handler_get, req, res);
48  }
49 
50  // override
51  bool doPost(HttpRequest& req, HttpResponse& res) {
52  return doService(http_handler_post, req, res);
53  }
54 
55  // override
56  bool doHead(HttpRequest& req, HttpResponse& res) {
57  return doService(http_handler_head, req, res);
58  }
59 
60  // override
61  bool doPut(HttpRequest& req, HttpResponse& res) {
62  return doService(http_handler_put, req, res);
63  }
64 
65  // override
66  bool doPatch(HttpRequest& req, HttpResponse& res) {
67  return doService(http_handler_patch, req, res);
68  }
69 
70  // override
71  bool doConnect(HttpRequest& req, HttpResponse& res) {
72  return doService(http_handler_connect, req, res);
73  }
74 
75  // override
76  bool doPurge(HttpRequest& req, HttpResponse& res) {
77  return doService(http_handler_purge, req, res);
78  }
79 
80  // override
81  bool doDelete(HttpRequest& req, HttpResponse& res) {
82  return doService(http_handler_delete, req, res);
83  }
84 
85  // override
86  bool doOptions(HttpRequest& req, HttpResponse& res) {
87  return doService(http_handler_options, req, res);
88  }
89 
90  // override
91  bool doProfind(HttpRequest& req, HttpResponse& res) {
92  return doService(http_handler_profind, req, res);
93  }
94 
95  // override
97  return doService(http_handler_websocket, req, res);
98  }
99 
100  // override
101  bool doUnknown(HttpRequest& req, HttpResponse& res) {
102  return doService(http_handler_unknown, req, res);
103  }
104 
105  // override
106  bool doError(HttpRequest& req, HttpResponse& res) {
107  return doService(http_handler_error, req, res);
108  }
109 
110 private:
111  bool doService(int type, HttpRequest& req, HttpResponse& res) {
112  if (type < http_handler_get || type >= http_handler_max) {
113  return false;
114  }
115 
116  res.setKeepAlive(req.isKeepAlive());
117  bool keep = req.isKeepAlive();
118 
119  const char* path = req.getPathInfo();
120  if (path == NULL || *path == 0) {
121  res.setStatus(400);
122  acl::string buf("404 bad request\r\n");
123  res.setContentLength(buf.size());
124  return res.write(buf.c_str(), buf.size()) && keep;
125  }
126 
127  size_t len = strlen(path);
128  acl::string buf(path);
129  if (path[len - 1] != '/') {
130  buf += '/';
131  }
132  buf.lower();
133 
134  std::map<acl::string, http_handler_t>::iterator it
135  = handlers_[type].find(buf);
136 
137  if (it != handlers_[type].end()) {
138  return it->second(req, res) && keep;
139  }
140 
141  res.setStatus(404);
142  buf = "404 ";
143  buf += path;
144  buf += " not found\r\n";
145  res.setContentLength(buf.size());
146  return res.write(buf.c_str(), buf.size()) && keep;
147  }
148 
149 private:
150  http_handlers_t* handlers_;
151 };
152 
153 } // namespace acl
bool doPurge(HttpRequest &req, HttpResponse &res)
HttpServletResponse & setKeepAlive(bool on)
bool write(const void *data, size_t len)
bool doPut(HttpRequest &req, HttpResponse &res)
bool doPost(HttpRequest &req, HttpResponse &res)
bool doDelete(HttpRequest &req, HttpResponse &res)
bool doProfind(HttpRequest &req, HttpResponse &res)
HttpServletResponse & setStatus(int status)
std::function< bool(HttpRequest &, HttpResponse &)> http_handler_t
std::map< acl::string, http_handler_t > http_handlers_t
bool doGet(HttpRequest &req, HttpResponse &res)
const char * getPathInfo(void) const
bool doOptions(HttpRequest &req, HttpResponse &res)
bool doHead(HttpRequest &req, HttpResponse &res)
HttpServletRequest HttpRequest
bool doPatch(HttpRequest &req, HttpResponse &res)
bool doUnknown(HttpRequest &req, HttpResponse &res)
HttpServletResponse & setContentLength(long long int n)
bool doWebsocket(HttpRequest &req, HttpResponse &res)
bool isKeepAlive(void) const
http_servlet_impl(http_handlers_t *handlers, socket_stream *stream, session *session)
bool doConnect(HttpRequest &req, HttpResponse &res)
virtual ~http_servlet_impl(void)
bool doError(HttpRequest &req, HttpResponse &res)
HttpServletResponse HttpResponse