Electroneum
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 <boost/lexical_cast.hpp>
31#include <boost/regex.hpp>
32#include <boost/utility/string_ref.hpp>
33#include <string>
34#include <utility>
35
36#include "string_tools.h"
37
38#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
39#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net.http"
40
41namespace epee
42{
43namespace net_utils
44{
45 namespace http
46 {
47
57
65
66 typedef std::list<std::pair<std::string, std::string> > fields_list;
67
68 inline
69 std::string get_value_from_fields_list(const std::string& param_name, const net_utils::http::fields_list& fields)
70 {
71 fields_list::const_iterator it = fields.begin();
72 for(; it != fields.end(); it++)
73 if(!string_tools::compare_no_case(param_name, it->first))
74 break;
75
76 if(it==fields.end())
77 return std::string();
78
79 return it->second;
80 }
81
82
83 inline
84 std::string get_value_from_uri_line(const std::string& param_name, const std::string& uri)
85 {
86 std::string buff = "([\\?|&])";
87 buff += param_name + "=([^&]*)";
88 boost::regex match_param(buff.c_str(), boost::regex::icase | boost::regex::normal);
89 boost::smatch result;
90 if(boost::regex_search(uri, result, match_param, boost::match_default) && result[0].matched)
91 {
92 return result[2];
93 }
94 return std::string();
95 }
96
97 static inline void add_field(std::string& out, const boost::string_ref name, const boost::string_ref value)
98 {
99 out.append(name.data(), name.size()).append(": ");
100 out.append(value.data(), value.size()).append("\r\n");
101 }
102 static inline void add_field(std::string& out, const std::pair<std::string, std::string>& field)
103 {
104 add_field(out, field.first, field.second);
105 }
106
107
109 {
110 std::string m_connection; //"Connection:"
111 std::string m_referer; //"Referer:"
112 std::string m_content_length; //"Content-Length:"
113 std::string m_content_type; //"Content-Type:"
114 std::string m_transfer_encoding;//"Transfer-Encoding:"
115 std::string m_content_encoding; //"Content-Encoding:"
116 std::string m_host; //"Host:"
117 std::string m_cookie; //"Cookie:"
118 std::string m_user_agent; //"User-Agent:"
119 std::string m_origin; //"Origin:"
121
122 void clear()
123 {
124 m_connection.clear();
125 m_referer.clear();
126 m_content_length.clear();
127 m_content_type.clear();
128 m_transfer_encoding.clear();
129 m_content_encoding.clear();
130 m_host.clear();
131 m_cookie.clear();
132 m_user_agent.clear();
133 m_origin.clear();
134 m_etc_fields.clear();
135 }
136 };
137
139 {
140 std::string m_path;
141 std::string m_query;
142 std::string m_fragment;
143 std::list<std::pair<std::string, std::string> > m_query_params;
144 };
145
147 {
148 std::string schema;
149 std::string host;
150 std::string uri;
153 };
154
155
185
186
188 {
192 std::string m_body;
193 std::string m_mime_tipe;
195 int m_http_ver_hi;// OUT paramter only
196 int m_http_ver_lo;// OUT paramter only
197
198 void clear()
199 {
200 this->~http_response_info();
201 new(this) http_response_info();
202 }
203 };
204 }
205}
206}
std::list< std::pair< std::string, std::string > > fields_list
Definition http_base.h:66
std::string get_value_from_fields_list(const std::string &param_name, const net_utils::http::fields_list &fields)
Definition http_base.h:69
std::string get_value_from_uri_line(const std::string &param_name, const std::string &uri)
Definition http_base.h:84
bool compare_no_case(const std::string &str1, const std::string &str2)
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
#define false
unsigned __int64 uint64_t
Definition stdint.h:136
std::list< std::pair< std::string, std::string > > m_query_params
Definition http_base.h:143