Monero
Loading...
Searching...
No Matches
http_base.h
Go to the documentation of this file.
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_view.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
42namespace epee
43{
44namespace net_utils
45{
46 namespace http
47 {
48
58
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, boost::string_view name, boost::string_view 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 namespace detail
84 {
85 inline bool parse_header_line(boost::string_view line, boost::string_view& name, boost::string_view& value)
86 {
87 if(!line.empty() && line.back() == '\r')
88 line.remove_suffix(1);
89 if(line.empty())
90 return false;
91 if(line.front() == ' ' || line.front() == '\t')
92 return false;
93
94 const size_t colon = line.find(':');
95 if(colon == boost::string_view::npos || colon == 0)
96 return false;
97
98 name = line.substr(0, colon);
99 value = line.substr(colon + 1);
100 while(!value.empty() && (value.front() == ' ' || value.front() == '\t'))
101 value.remove_prefix(1);
102 while(!value.empty() && (value.back() == ' ' || value.back() == '\t'))
103 value.remove_suffix(1);
104
105 for(char c : name)
106 {
107 const bool alnum = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
108 const bool allowed = alnum ||
109 c == '!' || c == '#' || c == '$' || c == '%' || c == '&' ||
110 c == '\'' || c == '*' || c == '+' || c == '-' || c == '.' ||
111 c == '^' || c == '_' || c == '`' || c == '|' || c == '~';
112 if(!allowed)
113 return false;
114 }
115
116 return true;
117 }
118 }
119
121 {
122 std::string m_connection; //"Connection:"
123 std::string m_referer; //"Referer:"
124 std::string m_content_length; //"Content-Length:"
125 std::string m_content_type; //"Content-Type:"
126 std::string m_transfer_encoding;//"Transfer-Encoding:"
127 std::string m_content_encoding; //"Content-Encoding:"
128 std::string m_host; //"Host:"
129 std::string m_cookie; //"Cookie:"
130 std::string m_user_agent; //"User-Agent:"
131 std::string m_origin; //"Origin:"
133
134 void clear()
135 {
136 m_connection.clear();
137 m_referer.clear();
138 m_content_length.clear();
139 m_content_type.clear();
140 m_transfer_encoding.clear();
141 m_content_encoding.clear();
142 m_host.clear();
143 m_cookie.clear();
144 m_user_agent.clear();
145 m_origin.clear();
146 m_etc_fields.clear();
147 }
148 };
149
151 {
152 std::string m_path;
153 std::string m_query;
154 std::string m_fragment;
155 std::list<std::pair<std::string, std::string> > m_query_params;
156 };
157
159 {
160 std::string schema;
161 std::string host;
162 std::string uri;
165 };
166
167
197
198
200 {
204 std::string m_body;
205 std::string m_mime_tipe;
207 int m_http_ver_hi;// OUT paramter only
208 int m_http_ver_lo;// OUT paramter only
209
210 void clear()
211 {
212 this->~http_response_info();
213 new(this) http_response_info();
214 }
215
216 void wipe()
217 {
218 memwipe(&m_body[0], m_body.size());
219 }
220 };
221 }
222}
223}
#define false
void * memwipe(void *src, size_t n)
Definition memwipe.c:107
declaration and default definition for the functions used the API
Definition expect.cpp:34
bool parse_header_line(boost::string_view line, boost::string_view &name, boost::string_view &value)
Definition http_base.h:85
http_method
Definition http_base.h:49
@ http_method_head
Definition http_base.h:54
@ http_method_etc
Definition http_base.h:55
@ http_method_put
Definition http_base.h:53
@ http_method_get
Definition http_base.h:51
@ http_method_post
Definition http_base.h:52
@ http_method_options
Definition http_base.h:50
@ http_method_unknown
Definition http_base.h:56
http_content_type
Definition http_base.h:60
@ http_content_type_text_html
Definition http_base.h:61
@ http_content_type_other
Definition http_base.h:63
@ http_content_type_not_set
Definition http_base.h:64
@ http_content_type_image_gif
Definition http_base.h:62
static void add_field(std::string &out, boost::string_view name, boost::string_view value)
Definition http_base.h:73
std::list< std::pair< std::string, std::string > > fields_list
Definition http_base.h:67
std::string get_value_from_fields_list(const std::string &param_name, const net_utils::http::fields_list &fields)
Definition http_base.cpp:44
std::string get_value_from_uri_line(const std::string &param_name, const std::string &uri)
Definition http_base.cpp:57
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition uri.py:1
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
unsigned __int64 uint64_t
Definition stdint.h:136
Definition http_base.h:121
fields_list m_etc_fields
Definition http_base.h:132
std::string m_content_length
Definition http_base.h:124
std::string m_content_encoding
Definition http_base.h:127
std::string m_origin
Definition http_base.h:131
std::string m_referer
Definition http_base.h:123
std::string m_cookie
Definition http_base.h:129
void clear()
Definition http_base.h:134
std::string m_connection
Definition http_base.h:122
std::string m_user_agent
Definition http_base.h:130
std::string m_content_type
Definition http_base.h:125
std::string m_transfer_encoding
Definition http_base.h:126
std::string m_host
Definition http_base.h:128
http_request_info()
Definition http_base.h:170
bool m_have_to_block
Definition http_base.h:185
std::string m_full_request_str
Definition http_base.h:180
size_t m_full_request_buf_size
Definition http_base.h:188
uri_content m_uri_content
Definition http_base.h:187
int m_http_ver_hi
Definition http_base.h:183
std::string m_body
Definition http_base.h:189
std::string m_http_method_str
Definition http_base.h:179
http_header_info m_header_info
Definition http_base.h:186
std::string m_replace_html
Definition http_base.h:181
http_method m_http_method
Definition http_base.h:177
std::string m_request_head
Definition http_base.h:182
std::string m_URI
Definition http_base.h:178
void clear()
Definition http_base.h:191
int m_http_ver_lo
Definition http_base.h:184
int m_http_ver_lo
Definition http_base.h:208
void clear()
Definition http_base.h:210
int m_http_ver_hi
Definition http_base.h:207
void wipe()
Definition http_base.h:216
std::string m_response_comment
Definition http_base.h:202
std::string m_mime_tipe
Definition http_base.h:205
int m_response_code
Definition http_base.h:201
http_header_info m_header_info
Definition http_base.h:206
std::string m_body
Definition http_base.h:204
fields_list m_additional_fields
Definition http_base.h:203
Definition http_base.h:151
std::string m_path
Definition http_base.h:152
std::string m_fragment
Definition http_base.h:154
std::list< std::pair< std::string, std::string > > m_query_params
Definition http_base.h:155
std::string m_query
Definition http_base.h:153
Definition http_base.h:159
uri_content m_uri_content
Definition http_base.h:164
std::string schema
Definition http_base.h:160
std::string host
Definition http_base.h:161
uint64_t port
Definition http_base.h:163