2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2015 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "asio_server_serve_mux.h"
27 #include "asio_server_request_impl.h"
28 #include "asio_server_request_handler.h"
34 namespace asio_http2 {
38 bool serve_mux::handle(std::string pattern, request_cb cb) {
39 if (pattern.empty() || !cb) {
43 auto it = mux_.find(pattern);
44 if (it != std::end(mux_) && (*it).second.user_defined) {
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) {
55 if (pattern[0] == '/') {
59 path = pattern.substr(pattern.find('/'));
61 if (it == std::end(mux_)) {
62 mux_.emplace(std::move(redirect_pattern),
64 redirect_handler(301, std::move(path)),
67 (*it).second = handler_entry{
68 false, redirect_handler(301, std::move(path)), pattern};
72 mux_.emplace(pattern, handler_entry{true, std::move(cb), pattern});
77 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(
81 nullptr, 0, nullptr, 0, path.c_str(), path.size(), nullptr, 0);
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()) {
87 new_uri += uref.raw_query;
90 return redirect_handler(301, std::move(new_uri));
93 auto &host = req.uri().host;
95 auto cb = match(host + path);
103 return status_handler(404);
107 bool path_match(const std::string &pattern, const std::string &path) {
108 if (pattern.back() != '/') {
109 return pattern == path;
111 return util::startsWith(path, pattern);
115 request_cb serve_mux::match(const std::string &path) const {
116 const handler_entry *ent = nullptr;
118 for (auto &kv : mux_) {
119 auto &pattern = kv.first;
120 if (!path_match(pattern, path)) {
123 if (!ent || best < pattern.size()) {
124 best = pattern.size();
134 } // namespace server
136 } // namespace asio_http2
138 } // namespace nghttp2