]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/http/request.hh
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / include / seastar / http / request.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright 2015 Cloudius Systems
20 */
21
22 //
23 // request.hpp
24 // ~~~~~~~~~~~
25 //
26 // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
27 //
28 // Distributed under the Boost Software License, Version 1.0. (See accompanying
29 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
30 //
31 #pragma once
32
33 #include <seastar/core/sstring.hh>
34 #include <string>
35 #include <vector>
36 #include <strings.h>
37 #include <seastar/http/common.hh>
38
39 namespace seastar {
40
41 namespace httpd {
42 class connection;
43
44 /**
45 * A request received from a client.
46 */
47 struct request {
48 enum class ctclass
49 : char {
50 other, multipart, app_x_www_urlencoded,
51 };
52
53 struct case_insensitive_cmp {
54 bool operator()(const sstring& s1, const sstring& s2) const {
55 return std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(),
56 [](char a, char b) { return ::tolower(a) == ::tolower(b); });
57 }
58 };
59
60 struct case_insensitive_hash {
61 size_t operator()(sstring s) const {
62 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
63 return std::hash<sstring>()(s);
64 }
65 };
66
67 sstring _method;
68 sstring _url;
69 sstring _version;
70 int http_version_major;
71 int http_version_minor;
72 ctclass content_type_class;
73 size_t content_length = 0;
74 std::unordered_map<sstring, sstring, case_insensitive_hash, case_insensitive_cmp> _headers;
75 std::unordered_map<sstring, sstring> query_parameters;
76 connection* connection_ptr;
77 parameters param;
78 sstring content;
79 sstring protocol_name = "http";
80
81 /**
82 * Search for the first header of a given name
83 * @param name the header name
84 * @return a pointer to the header value, if it exists or empty string
85 */
86 sstring get_header(const sstring& name) const {
87 auto res = _headers.find(name);
88 if (res == _headers.end()) {
89 return "";
90 }
91 return res->second;
92 }
93
94 /**
95 * Search for the first header of a given name
96 * @param name the header name
97 * @return a pointer to the header value, if it exists or empty string
98 */
99 sstring get_query_param(const sstring& name) const {
100 auto res = query_parameters.find(name);
101 if (res == query_parameters.end()) {
102 return "";
103 }
104 return res->second;
105 }
106
107 /**
108 * Get the request protocol name. Can be either "http" or "https".
109 */
110 sstring get_protocol_name() const {
111 return protocol_name;
112 }
113
114 /**
115 * Get the request url.
116 * @return the request url
117 */
118 sstring get_url() const {
119 return get_protocol_name() + "://" + get_header("Host") + _url;
120 }
121
122 bool is_multi_part() const {
123 return content_type_class == ctclass::multipart;
124 }
125
126 bool is_form_post() const {
127 return content_type_class == ctclass::app_x_www_urlencoded;
128 }
129
130 };
131
132 } // namespace httpd
133
134 }