forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_message.cc
199 lines (160 loc) · 4.98 KB
/
http_message.cc
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "http_message.hh"
#include "exception.hh"
#include "http_record.pb.h"
using namespace std;
/* methods called by an external parser */
void HTTPMessage::set_first_line( const string & str )
{
assert( state_ == FIRST_LINE_PENDING );
first_line_ = str;
state_ = HEADERS_PENDING;
}
void HTTPMessage::add_header( const std::string & str )
{
assert( state_ == HEADERS_PENDING );
headers_.emplace_back( str );
}
void HTTPMessage::done_with_headers( void )
{
assert( state_ == HEADERS_PENDING );
state_ = BODY_PENDING;
calculate_expected_body_size();
}
void HTTPMessage::set_expected_body_size( const bool is_known, const size_t value )
{
assert( state_ == BODY_PENDING );
expected_body_size_ = make_pair( is_known, value );
}
size_t HTTPMessage::read_in_body( const std::string & str )
{
assert( state_ == BODY_PENDING );
if ( body_size_is_known() ) {
/* body size known in advance */
assert( body_.size() <= expected_body_size() );
const size_t amount_to_append = min( expected_body_size() - body_.size(),
str.size() );
body_.append( str.substr( 0, amount_to_append ) );
if ( body_.size() == expected_body_size() ) {
state_ = COMPLETE;
}
return amount_to_append;
} else {
/* body size not known in advance */
return read_in_complex_body( str );
}
}
void HTTPMessage::eof( void )
{
switch ( state() ) {
case FIRST_LINE_PENDING:
return;
case HEADERS_PENDING:
throw Exception( "HTTPMessage", "EOF received in middle of headers" );
case BODY_PENDING:
if ( eof_in_body() ) {
state_ = COMPLETE;
}
break;
case COMPLETE:
assert( false ); /* nobody should be calling methods on a complete message */
return;
}
}
bool HTTPMessage::body_size_is_known( void ) const
{
assert( state_ > HEADERS_PENDING );
return expected_body_size_.first;
}
size_t HTTPMessage::expected_body_size( void ) const
{
assert( body_size_is_known() );
return expected_body_size_.second;
}
/* locale-insensitive ASCII conversion */
static char http_to_lower( char c )
{
const char diff = 'A' - 'a';
if ( c >= 'A' and c <= 'Z' ) {
c -= diff;
}
return c;
}
static string strip_initial_whitespace( const string & str )
{
size_t first_nonspace = str.find_first_not_of( " " );
if ( first_nonspace == std::string::npos ) {
return "";
} else {
return str.substr( first_nonspace );
}
}
/* check if two strings are equivalent per HTTP 1.1 comparison (case-insensitive) */
bool HTTPMessage::equivalent_strings( const string & a, const string & b )
{
const string new_a = strip_initial_whitespace( a ),
new_b = strip_initial_whitespace( b );
if ( new_a.size() != new_b.size() ) {
return false;
}
for ( auto it_a = new_a.begin(), it_b = new_b.begin(); it_a < new_a.end(); it_a++, it_b++ ) {
if ( http_to_lower( *it_a ) != http_to_lower( *it_b ) ) {
return false;
}
}
return true;
}
bool HTTPMessage::has_header( const string & header_name ) const
{
for ( const auto & header : headers_ ) {
/* canonicalize header name per RFC 2616 section 2.1 */
if ( equivalent_strings( header.key(), header_name ) ) {
return true;
}
}
return false;
}
const string & HTTPMessage::get_header_value( const std::string & header_name ) const
{
for ( const auto & header : headers_ ) {
/* canonicalize header name per RFC 2616 section 2.1 */
if ( equivalent_strings( header.key(), header_name ) ) {
return header.value();
}
}
throw Exception( "HTTPMessage header not found", header_name );
}
const std::string & HTTPMessage::get_first_line( void ) const
{
return first_line_;
}
/* serialize the request or response as one string */
std::string HTTPMessage::str( void ) const
{
assert( state_ == COMPLETE );
/* start with first line */
string ret( first_line_ + CRLF );
/* iterate through headers and add "key: value\r\n" to request */
for ( const auto & header : headers_ ) {
ret.append( header.str() + CRLF );
}
/* blank line between headers and body */
ret.append( CRLF );
/* add body to request */
ret.append( body_ );
return ret;
}
HTTP_Record::http_message HTTPMessage::toprotobuf( void ) const
{
assert( state_ == COMPLETE );
HTTP_Record::http_message message;
/* add first line to protobuf http_message */
message.set_first_line( first_line_ + CRLF );
/* iterate through headers and add "key: value\r\n" to protobuf */
for ( const auto & header : headers_ ) {
message.add_headers( header.str() + CRLF );
}
/* add blank line to separate headers and body, and then add body */
message.set_body( CRLF + body_ );
return message;
}