Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
RequestHeader.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
21#pragma once
22
24
25#ifdef DELETE
26#undef DELETE
27#endif
28
29namespace sese::net::http {
30
32enum class RequestType {
33 OPTIONS,
34 GET,
35 POST,
36 HEAD,
37 PUT,
38 DELETE,
39 TRACE,
40 CONNECT,
42};
43
44inline std::string requestTypeToString(RequestType request_type) {
45 switch (request_type) {
47 return "OPTIONS";
49 return "GET";
51 return "POST";
53 return "HEAD";
55 return "PUT";
57 return "DELETE";
59 return "TRACE";
61 return "CONNECT";
63 default:
64 return "ANOTHER";
65 }
66}
67
68inline RequestType stringToRequestType(const std::string &request_type_str) {
69 if (request_type_str == "OPTIONS") {
71 }
72 if (request_type_str == "GET") {
73 return RequestType::GET;
74 }
75 if (request_type_str == "POST") {
76 return RequestType::POST;
77 }
78 if (request_type_str == "HEAD") {
79 return RequestType::HEAD;
80 }
81 if (request_type_str == "PUT") {
82 return RequestType::PUT;
83 }
84 if (request_type_str == "DELETE") {
86 }
87 if (request_type_str == "TRACE") {
88 return RequestType::TRACE;
89 }
90 if (request_type_str == "CONNECT") {
92 }
94}
95// GCOVR_EXCL_START
96
101class RequestHeader : public Header {
102public:
103 using Ptr = std::unique_ptr<RequestHeader>;
104
105 RequestHeader() = default;
106
107 RequestHeader(const std::initializer_list<KeyValueType> &initializer_list)
108 : Header(initializer_list) {}
109
110 [[nodiscard]] RequestType getType() const { return type; }
111 void setType(RequestType request_type) { this->type = request_type; }
112
113 [[nodiscard]] const std::string &getUri() const { return uri; }
114 void setUri(const std::string &uri) { this->uri = uri; }
115
116 [[nodiscard]] const std::string &getQueryArg(const std::string &key, const std::string &default_value) const;
117 void setQueryArg(const std::string &key, const std::string &value);
118 [[nodiscard]] size_t queryArgsSize() const { return query_args.size(); }
119 [[nodiscard]] bool queryArgsEmpty() const { return query_args.empty(); }
120 void queryArgsClear() { return query_args.clear(); }
121 bool queryArgsExist(const std::string &key) { return query_args.find(key) != query_args.end(); }
126 const std::string &getQueryArg(const std::string &key) { return query_args.at(key); }
127
128 [[nodiscard]] HttpVersion getVersion() const { return version; }
129 void setVersion(HttpVersion new_version) { this->version = new_version; }
130
131 [[nodiscard]] std::string getUrl() const;
132 void setUrl(const std::string &request_url);
133
134protected:
136 std::string uri = "/";
138
139 std::map<std::string, std::string> query_args;
140};
141
142// GCOVR_EXCL_STOP
143
144} // namespace sese::net::http