1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "asio_server_serve_mux.h"
26
27 #include "asio_server_request_impl.h"
28 #include "asio_server_request_handler.h"
29 #include "util.h"
30 #include "http2.h"
31
32 namespace nghttp2 {
33
34 namespace asio_http2 {
35
36 namespace server {
37
handle(std::string pattern,request_cb cb)38 bool serve_mux::handle(std::string pattern, request_cb cb) {
39 if (pattern.empty() || !cb) {
40 return false;
41 }
42
43 auto it = mux_.find(pattern);
44 if (it != std::end(mux_) && (*it).second.user_defined) {
45 return false;
46 }
47
48 // if pattern ends with '/' (e.g., /foo/), add implicit permanent
49 // redirect for '/foo'.
50 if (pattern.size() >= 2 && pattern.back() == '/') {
51 auto redirect_pattern = pattern.substr(0, pattern.size() - 1);
52 auto it = mux_.find(redirect_pattern);
53 if (it == std::end(mux_) || !(*it).second.user_defined) {
54 std::string path;
55 if (pattern[0] == '/') {
56 path = pattern;
57 } else {
58 // skip host part
59 path = pattern.substr(pattern.find('/'));
60 }
61 if (it == std::end(mux_)) {
62 mux_.emplace(std::move(redirect_pattern),
63 handler_entry{false,
64 redirect_handler(301, std::move(path)),
65 pattern});
66 } else {
67 (*it).second = handler_entry{
68 false, redirect_handler(301, std::move(path)), pattern};
69 }
70 }
71 }
72 mux_.emplace(pattern, handler_entry{true, std::move(cb), pattern});
73
74 return true;
75 }
76
handler(request_impl & req) const77 request_cb serve_mux::handler(request_impl &req) const {
78 auto &path = req.uri().path;
79 if (req.method() != "CONNECT") {
80 auto clean_path = ::nghttp2::http2::path_join(StringRef{}, StringRef{},
81 StringRef{path}, StringRef{});
82 if (clean_path != path) {
83 auto new_uri = util::percent_encode_path(clean_path);
84 auto &uref = req.uri();
85 if (!uref.raw_query.empty()) {
86 new_uri += '?';
87 new_uri += uref.raw_query;
88 }
89
90 return redirect_handler(301, std::move(new_uri));
91 }
92 }
93 auto &host = req.uri().host;
94
95 auto cb = match(host + path);
96 if (cb) {
97 return cb;
98 }
99 cb = match(path);
100 if (cb) {
101 return cb;
102 }
103 return status_handler(404);
104 }
105
106 namespace {
path_match(const std::string & pattern,const std::string & path)107 bool path_match(const std::string &pattern, const std::string &path) {
108 if (pattern.back() != '/') {
109 return pattern == path;
110 }
111 return util::starts_with(path, pattern);
112 }
113 } // namespace
114
match(const std::string & path) const115 request_cb serve_mux::match(const std::string &path) const {
116 const handler_entry *ent = nullptr;
117 size_t best = 0;
118 for (auto &kv : mux_) {
119 auto &pattern = kv.first;
120 if (!path_match(pattern, path)) {
121 continue;
122 }
123 if (!ent || best < pattern.size()) {
124 best = pattern.size();
125 ent = &kv.second;
126 }
127 }
128 if (ent) {
129 return ent->cb;
130 }
131 return request_cb();
132 }
133
134 } // namespace server
135
136 } // namespace asio_http2
137
138 } // namespace nghttp2
139