Debian Codegraph @

contrib/epee/include/net/http_base.h

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28
29 #pragma once
30 #include "memwipe.h"
31
32 #include <boost/utility/string_ref.hpp>
33
34 #include <string>
35 #include <utility>
36 #include <list>
37 #include <cstdint>
38
39 #undef MONERO_DEFAULT_LOG_CATEGORY
40 #define MONERO_DEFAULT_LOG_CATEGORY "net.http"
41
42 namespace epee
43 {
44 namespace net_utils
45 {
46 namespace http
47 {
48
49 enum http_method{
50 http_method_options,
51 http_method_get,
52 http_method_post,
53 http_method_put,
54 http_method_head,
55 http_method_etc,
56 http_method_unknown
57 };
58
59 enum http_content_type
60 {
61 http_content_type_text_html,
62 http_content_type_image_gif,
63 http_content_type_other,
64 http_content_type_not_set
65 };
66
67 typedef std::list<std::pair<std::string, std::string> > fields_list;
68
69 std::string get_value_from_fields_list(const std::string& param_name, const net_utils::http::fields_list& fields);
70
71 std::string get_value_from_uri_line(const std::string& param_name, const std::string& uri);
72
73 static inline void add_field(std::string& out, const boost::string_ref name, const boost::string_ref value)
74 {
75 out.append(name.data(), name.size()).append(": ");
76 out.append(value.data(), value.size()).append("\r\n");
77 }
78 static inline void add_field(std::string& out, const std::pair<std::string, std::string>& field)
79 {
80 add_field(out, field.first, field.second);
81 }
82
83
84 struct http_header_info
85 {
86 std::string m_connection; //"Connection:"
87 std::string m_referer; //"Referer:"
88 std::string m_content_length; //"Content-Length:"
89 std::string m_content_type; //"Content-Type:"
90 std::string m_transfer_encoding;//"Transfer-Encoding:"
91 std::string m_content_encoding; //"Content-Encoding:"
92 std::string m_host; //"Host:"
93 std::string m_cookie; //"Cookie:"
94 std::string m_user_agent; //"User-Agent:"
95 std::string m_origin; //"Origin:"
96 fields_list m_etc_fields;
97
98 void clear()
99 {
100 m_connection.clear();
101 m_referer.clear();
102 m_content_length.clear();
103 m_content_type.clear();
104 m_transfer_encoding.clear();
105 m_content_encoding.clear();
106 m_host.clear();
107 m_cookie.clear();
108 m_user_agent.clear();
109 m_origin.clear();
110 m_etc_fields.clear();
111 }
112 };
113
114 struct uri_content
115 {
116 std::string m_path;
117 std::string m_query;
118 std::string m_fragment;
119 std::list<std::pair<std::string, std::string> > m_query_params;
120 };
121
122 struct url_content
123 {
124 std::string schema;
125 std::string host;
126 std::string uri;
127 uint64_t port;
128 uri_content m_uri_content;
129 };
130
131
132 struct http_request_info
133 {
134 http_request_info():m_http_method(http_method_unknown),
135 m_http_ver_hi(0),
136 m_http_ver_lo(0),
137 m_have_to_block(false),
138 m_full_request_buf_size(0)
139 {}
140
141 http_method m_http_method;
142 std::string m_URI;
143 std::string m_http_method_str;
144 std::string m_full_request_str;
145 std::string m_replace_html;
146 std::string m_request_head;
147 int m_http_ver_hi;
148 int m_http_ver_lo;
149 bool m_have_to_block;
150 http_header_info m_header_info;
151 uri_content m_uri_content;
152 size_t m_full_request_buf_size;
153 std::string m_body;
154
155 void clear()
156 {
157 this->~http_request_info();
158 new(this) http_request_info();
159 }
160 };
161
162
163 struct http_response_info
164 {
165 int m_response_code;
166 std::string m_response_comment;
167 fields_list m_additional_fields;
168 std::string m_body;
169 std::string m_mime_tipe;
170 http_header_info m_header_info;
171 int m_http_ver_hi;// OUT paramter only
172 int m_http_ver_lo;// OUT paramter only
173
174 void clear()
175 {
176 this->~http_response_info();
177 new(this) http_response_info();
178 }
179
180 void wipe()
181 {
182 memwipe(&m_body[0], m_body.size());
183 }
184 };
185 }
186 }
187 }