-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.cpp
More file actions
55 lines (44 loc) · 1.46 KB
/
request.cpp
File metadata and controls
55 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "request.h"
#include <sstream>
#include <atlutil.h>
namespace forceinline {
void http::request::set_hostname( std::string_view hostname ) {
m_hostname = hostname;
}
void http::request::set_argument( std::string_view arg_name, std::string_view arg_value ) {
if ( m_method == methods::GET )
m_arguments += m_arguments.length( ) > 0 ? '&' : '?';
else if ( m_arguments.length( ) )
m_arguments += '&';
m_arguments += url_escape( arg_name ) + '=' + url_escape( arg_value );
}
std::string http::request::get( ) {
std::string request = m_method == methods::GET ? "GET " : "POST ";
request += m_request_url;
if ( m_method == methods::GET )
request += m_arguments;
request += " HTTP/1.1\r\n";
request += "Host: " + m_hostname + "\r\n";
request += m_method == methods::GET ? "Accept: text/*\r\n\r\n" : "Content-Type: application/x-www-form-urlencoded\r\n";
if ( m_method == methods::POST ) {
request += "Content-Length: " + std::to_string( m_arguments.length( ) ) + "\r\n\r\n";
request += m_arguments;
}
return request;
}
std::string http::request::url_escape( std::string_view to_escape ) {
auto http_escape_char = [ ]( char c ) {
std::stringstream ss;
ss << '%' << std::hex << int( c );
return ss.str( );
};
std::string escaped = { };
for ( auto& c : to_escape ) {
if ( !AtlIsUnsafeUrlChar( c ) )
escaped += c;
else
escaped += http_escape_char( c );
}
return escaped;
}
} // namespace forceinline