NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
parser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Peter Thorson. 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 WebSocket++ Project 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"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON 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 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE http_parser
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
36 
38  // Valid characters
39 
40  // misc
41  BOOST_CHECK( websocketpp::http::is_token_char('!') );
42  BOOST_CHECK( websocketpp::http::is_token_char('#') );
43  BOOST_CHECK( websocketpp::http::is_token_char('$') );
44  BOOST_CHECK( websocketpp::http::is_token_char('%') );
45  BOOST_CHECK( websocketpp::http::is_token_char('&') );
46  BOOST_CHECK( websocketpp::http::is_token_char('\'') );
47  BOOST_CHECK( websocketpp::http::is_token_char('*') );
48  BOOST_CHECK( websocketpp::http::is_token_char('+') );
49  BOOST_CHECK( websocketpp::http::is_token_char('-') );
50  BOOST_CHECK( websocketpp::http::is_token_char('.') );
51  BOOST_CHECK( websocketpp::http::is_token_char('^') );
52  BOOST_CHECK( websocketpp::http::is_token_char('_') );
53  BOOST_CHECK( websocketpp::http::is_token_char('`') );
54  BOOST_CHECK( websocketpp::http::is_token_char('~') );
55 
56  // numbers
57  for (int i = 0x30; i < 0x3a; i++) {
58  BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
59  }
60 
61  // upper
62  for (int i = 0x41; i < 0x5b; i++) {
63  BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
64  }
65 
66  // lower
67  for (int i = 0x61; i < 0x7b; i++) {
68  BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
69  }
70 
71  // invalid characters
72 
73  // lower unprintable
74  for (int i = 0; i < 33; i++) {
75  BOOST_CHECK( !websocketpp::http::is_token_char((unsigned char)(i)) );
76  }
77 
78  // misc
79  BOOST_CHECK( !websocketpp::http::is_token_char('(') );
80  BOOST_CHECK( !websocketpp::http::is_token_char(')') );
81  BOOST_CHECK( !websocketpp::http::is_token_char('<') );
82  BOOST_CHECK( !websocketpp::http::is_token_char('>') );
83  BOOST_CHECK( !websocketpp::http::is_token_char('@') );
84  BOOST_CHECK( !websocketpp::http::is_token_char(',') );
85  BOOST_CHECK( !websocketpp::http::is_token_char(';') );
86  BOOST_CHECK( !websocketpp::http::is_token_char(':') );
87  BOOST_CHECK( !websocketpp::http::is_token_char('\\') );
88  BOOST_CHECK( !websocketpp::http::is_token_char('"') );
89  BOOST_CHECK( !websocketpp::http::is_token_char('/') );
90  BOOST_CHECK( !websocketpp::http::is_token_char('[') );
91  BOOST_CHECK( !websocketpp::http::is_token_char(']') );
92  BOOST_CHECK( !websocketpp::http::is_token_char('?') );
93  BOOST_CHECK( !websocketpp::http::is_token_char('=') );
94  BOOST_CHECK( !websocketpp::http::is_token_char('{') );
95  BOOST_CHECK( !websocketpp::http::is_token_char('}') );
96 
97  // upper unprintable and out of ascii range
98  for (int i = 127; i < 256; i++) {
99  BOOST_CHECK( !websocketpp::http::is_token_char((unsigned char)(i)) );
100  }
101 
102  // is not
103  BOOST_CHECK( !websocketpp::http::is_not_token_char('!') );
104  BOOST_CHECK( websocketpp::http::is_not_token_char('(') );
105 }
106 
108  std::string d1 = "foo";
109  std::string d2 = " foo ";
110 
111  std::pair<std::string,std::string::const_iterator> ret;
112 
113  ret = websocketpp::http::parser::extract_token(d1.begin(),d1.end());
114  BOOST_CHECK( ret.first == "foo" );
115  BOOST_CHECK( ret.second == d1.begin()+3 );
116 
117  ret = websocketpp::http::parser::extract_token(d2.begin(),d2.end());
118  BOOST_CHECK( ret.first.empty() );
119  BOOST_CHECK( ret.second == d2.begin()+0 );
120 
121  ret = websocketpp::http::parser::extract_token(d2.begin()+1,d2.end());
122  BOOST_CHECK( ret.first == "foo" );
123  BOOST_CHECK( ret.second == d2.begin()+4 );
124 }
125 
127  std::string d1 = "\"foo\"";
128  std::string d2 = "\"foo\\\"bar\\\"baz\"";
129  std::string d3 = "\"foo\" ";
130  std::string d4;
131  std::string d5 = "foo";
132 
133  std::pair<std::string,std::string::const_iterator> ret;
134 
136 
137  ret = extract_quoted_string(d1.begin(),d1.end());
138  BOOST_CHECK( ret.first == "foo" );
139  BOOST_CHECK( ret.second == d1.end() );
140 
141  ret = extract_quoted_string(d2.begin(),d2.end());
142  BOOST_CHECK( ret.first == "foo\"bar\"baz" );
143  BOOST_CHECK( ret.second == d2.end() );
144 
145  ret = extract_quoted_string(d3.begin(),d3.end());
146  BOOST_CHECK( ret.first == "foo" );
147  BOOST_CHECK( ret.second == d3.begin()+5 );
148 
149  ret = extract_quoted_string(d4.begin(),d4.end());
150  BOOST_CHECK( ret.first.empty() );
151  BOOST_CHECK( ret.second == d4.begin() );
152 
153  ret = extract_quoted_string(d5.begin(),d5.end());
154  BOOST_CHECK( ret.first.empty() );
155  BOOST_CHECK( ret.second == d5.begin() );
156 }
157 
159  std::string d1 = " foo bar";
160  d1.append(1,char(9));
161  d1.append("baz\r\n d\r\n \r\n e\r\nf");
162 
163  std::string::const_iterator ret;
164 
165  ret = websocketpp::http::parser::extract_all_lws(d1.begin(),d1.end());
166  BOOST_CHECK( ret == d1.begin()+1 );
167 
168  ret = websocketpp::http::parser::extract_all_lws(d1.begin()+1,d1.end());
169  BOOST_CHECK( ret == d1.begin()+1 );
170 
171  ret = websocketpp::http::parser::extract_all_lws(d1.begin()+4,d1.end());
172  BOOST_CHECK( ret == d1.begin()+9 );
173 
174  ret = websocketpp::http::parser::extract_all_lws(d1.begin()+12,d1.end());
175  BOOST_CHECK( ret == d1.begin()+13 );
176 
177  ret = websocketpp::http::parser::extract_all_lws(d1.begin()+16,d1.end());
178  BOOST_CHECK( ret == d1.begin()+19 );
179 
180  ret = websocketpp::http::parser::extract_all_lws(d1.begin()+20,d1.end());
181  BOOST_CHECK( ret == d1.begin()+28 );
182 
183  ret = websocketpp::http::parser::extract_all_lws(d1.begin()+29,d1.end());
184  BOOST_CHECK( ret == d1.begin()+29 );
185 }
186 
187 BOOST_AUTO_TEST_CASE( extract_attributes_blank ) {
188  std::string s;
189 
191  std::string::const_iterator it;
192 
193  it = websocketpp::http::parser::extract_attributes(s.begin(),s.end(),a);
194  BOOST_CHECK( it == s.begin() );
195  BOOST_CHECK_EQUAL( a.size(), 0 );
196 }
197 
198 BOOST_AUTO_TEST_CASE( extract_attributes_simple ) {
199  std::string s = "foo";
200 
202  std::string::const_iterator it;
203 
204  it = websocketpp::http::parser::extract_attributes(s.begin(),s.end(),a);
205  BOOST_CHECK( it == s.end() );
206  BOOST_CHECK_EQUAL( a.size(), 1 );
207  BOOST_CHECK( a.find("foo") != a.end() );
208  BOOST_CHECK_EQUAL( a.find("foo")->second, "" );
209 }
210 
212  std::string s1;
213  std::string s2 = "foo";
214  std::string s3 = " foo \r\nAbc";
215  std::string s4 = " \r\n foo ";
216  std::string s5 = "foo,bar";
217  std::string s6 = "foo;bar";
218  std::string s7 = "foo;baz,bar";
219  std::string s8 = "foo;bar;baz";
220  std::string s9 = "foo;bar=baz";
221  std::string s10 = "foo;bar=baz;boo";
222  std::string s11 = "foo;bar=baz;boo,bob";
223  std::string s12 = "foo;bar=\"a b c\"";
224  std::string s13 = "foo;bar=\"a \\\"b\\\" c\"";
225 
226 
227  std::string sx = "foo;bar=\"a \\\"b\\\" c\"";
230  std::string::const_iterator it;
231 
233 
234  it = extract_parameters(s1.begin(),s1.end(),p);
235  BOOST_CHECK( it == s1.begin() );
236 
237  p.clear();
238  it = extract_parameters(s2.begin(),s2.end(),p);
239  BOOST_CHECK( it == s2.end() );
240  BOOST_CHECK_EQUAL( p.size(), 1 );
241  BOOST_CHECK( p[0].first == "foo" );
242  BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
243 
244  p.clear();
245  it = extract_parameters(s3.begin(),s3.end(),p);
246  BOOST_CHECK( it == s3.begin()+5 );
247  BOOST_CHECK_EQUAL( p.size(), 1 );
248  BOOST_CHECK( p[0].first == "foo" );
249  BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
250 
251  p.clear();
252  it = extract_parameters(s4.begin(),s4.end(),p);
253  BOOST_CHECK( it == s4.end() );
254  BOOST_CHECK_EQUAL( p.size(), 1 );
255  BOOST_CHECK( p[0].first == "foo" );
256  BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
257 
258  p.clear();
259  it = extract_parameters(s5.begin(),s5.end(),p);
260  BOOST_CHECK( it == s5.end() );
261  BOOST_CHECK_EQUAL( p.size(), 2 );
262  BOOST_CHECK( p[0].first == "foo" );
263  BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
264  BOOST_CHECK( p[1].first == "bar" );
265  BOOST_CHECK_EQUAL( p[1].second.size(), 0 );
266 
267  p.clear();
268  it = extract_parameters(s6.begin(),s6.end(),p);
269  BOOST_CHECK( it == s6.end() );
270  BOOST_CHECK_EQUAL( p.size(), 1 );
271  BOOST_CHECK( p[0].first == "foo" );
272  a = p[0].second;
273  BOOST_CHECK_EQUAL( a.size(), 1 );
274  BOOST_CHECK( a.find("bar") != a.end() );
275  BOOST_CHECK_EQUAL( a.find("bar")->second, "" );
276 
277  p.clear();
278  it = extract_parameters(s7.begin(),s7.end(),p);
279  BOOST_CHECK( it == s7.end() );
280  BOOST_CHECK_EQUAL( p.size(), 2 );
281  BOOST_CHECK( p[0].first == "foo" );
282  a = p[0].second;
283  BOOST_CHECK_EQUAL( a.size(), 1 );
284  BOOST_CHECK( a.find("baz") != a.end() );
285  BOOST_CHECK_EQUAL( a.find("baz")->second, "" );
286  BOOST_CHECK( p[1].first == "bar" );
287  a = p[1].second;
288  BOOST_CHECK_EQUAL( a.size(), 0 );
289 
290  p.clear();
291  it = extract_parameters(s8.begin(),s8.end(),p);
292  BOOST_CHECK( it == s8.end() );
293  BOOST_CHECK_EQUAL( p.size(), 1 );
294  BOOST_CHECK( p[0].first == "foo" );
295  a = p[0].second;
296  BOOST_CHECK_EQUAL( a.size(), 2 );
297  BOOST_CHECK( a.find("bar") != a.end() );
298  BOOST_CHECK_EQUAL( a.find("bar")->second, "" );
299  BOOST_CHECK( a.find("baz") != a.end() );
300  BOOST_CHECK_EQUAL( a.find("baz")->second, "" );
301 
302  p.clear();
303  it = extract_parameters(s9.begin(),s9.end(),p);
304  BOOST_CHECK( it == s9.end() );
305  BOOST_CHECK_EQUAL( p.size(), 1 );
306  BOOST_CHECK( p[0].first == "foo" );
307  a = p[0].second;
308  BOOST_CHECK_EQUAL( a.size(), 1 );
309  BOOST_CHECK( a.find("bar") != a.end() );
310  BOOST_CHECK_EQUAL( a.find("bar")->second, "baz" );
311 
312  p.clear();
313  it = extract_parameters(s10.begin(),s10.end(),p);
314  BOOST_CHECK( it == s10.end() );
315  BOOST_CHECK_EQUAL( p.size(), 1 );
316  BOOST_CHECK( p[0].first == "foo" );
317  a = p[0].second;
318  BOOST_CHECK_EQUAL( a.size(), 2 );
319  BOOST_CHECK( a.find("bar") != a.end() );
320  BOOST_CHECK_EQUAL( a.find("bar")->second, "baz" );
321  BOOST_CHECK( a.find("boo") != a.end() );
322  BOOST_CHECK_EQUAL( a.find("boo")->second, "" );
323 
324  p.clear();
325  it = extract_parameters(s11.begin(),s11.end(),p);
326  BOOST_CHECK( it == s11.end() );
327  BOOST_CHECK_EQUAL( p.size(), 2 );
328  BOOST_CHECK( p[0].first == "foo" );
329  a = p[0].second;
330  BOOST_CHECK_EQUAL( a.size(), 2 );
331  BOOST_CHECK( a.find("bar") != a.end() );
332  BOOST_CHECK_EQUAL( a.find("bar")->second, "baz" );
333  BOOST_CHECK( a.find("boo") != a.end() );
334  BOOST_CHECK_EQUAL( a.find("boo")->second, "" );
335  a = p[1].second;
336  BOOST_CHECK_EQUAL( a.size(), 0 );
337 
338  p.clear();
339  it = extract_parameters(s12.begin(),s12.end(),p);
340  BOOST_CHECK( it == s12.end() );
341  BOOST_CHECK_EQUAL( p.size(), 1 );
342  BOOST_CHECK( p[0].first == "foo" );
343  a = p[0].second;
344  BOOST_CHECK_EQUAL( a.size(), 1 );
345  BOOST_CHECK( a.find("bar") != a.end() );
346  BOOST_CHECK_EQUAL( a.find("bar")->second, "a b c" );
347 
348  p.clear();
349  it = extract_parameters(s13.begin(),s13.end(),p);
350  BOOST_CHECK( it == s13.end() );
351  BOOST_CHECK_EQUAL( p.size(), 1 );
352  BOOST_CHECK( p[0].first == "foo" );
353  a = p[0].second;
354  BOOST_CHECK_EQUAL( a.size(), 1 );
355  BOOST_CHECK( a.find("bar") != a.end() );
356  BOOST_CHECK_EQUAL( a.find("bar")->second, "a \"b\" c" );
357 }
358 
360  std::string test1 = "foo";
361  std::string test2 = " foo ";
362  std::string test3 = "foo ";
363  std::string test4 = " foo";
364  std::string test5 = " foo ";
365  std::string test6 = " \r\n foo ";
366  std::string test7 = " \t foo ";
367  std::string test8 = " \t ";
368  std::string test9 = " \n\r";
369 
370  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test1), "foo" );
371  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test2), "foo" );
372  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test3), "foo" );
373  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test4), "foo" );
374  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test5), "foo" );
375  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test6), "foo" );
376  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test7), "foo" );
377  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test8), "" );
378  BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test9), "" );
379 }
380 
381 BOOST_AUTO_TEST_CASE( case_insensitive_headers ) {
383 
384  r.replace_header("foo","bar");
385 
386  BOOST_CHECK_EQUAL( r.get_header("foo"), "bar" );
387  BOOST_CHECK_EQUAL( r.get_header("FOO"), "bar" );
388  BOOST_CHECK_EQUAL( r.get_header("Foo"), "bar" );
389 }
390 
391 BOOST_AUTO_TEST_CASE( case_insensitive_headers_overwrite ) {
393 
394  r.replace_header("foo","bar");
395 
396  BOOST_CHECK_EQUAL( r.get_header("foo"), "bar" );
397  BOOST_CHECK_EQUAL( r.get_header("Foo"), "bar" );
398 
399  r.replace_header("Foo","baz");
400 
401  BOOST_CHECK_EQUAL( r.get_header("foo"), "baz" );
402  BOOST_CHECK_EQUAL( r.get_header("Foo"), "baz" );
403 
404  r.remove_header("FoO");
405 
406  BOOST_CHECK_EQUAL( r.get_header("foo"), "" );
407  BOOST_CHECK_EQUAL( r.get_header("Foo"), "" );
408 }
409 
410 BOOST_AUTO_TEST_CASE( blank_consume ) {
412 
413  std::string raw;
414 
415  bool exception = false;
416 
417  try {
418  r.consume(raw.c_str(),raw.size());
419  } catch (...) {
420  exception = true;
421  }
422 
423  BOOST_CHECK( exception == false );
424  BOOST_CHECK( r.ready() == false );
425 }
426 
427 BOOST_AUTO_TEST_CASE( blank_request ) {
429 
430  std::string raw = "\r\n\r\n";
431 
432  bool exception = false;
433 
434  try {
435  r.consume(raw.c_str(),raw.size());
436  } catch (...) {
437  exception = true;
438  }
439 
440  BOOST_CHECK( exception == true );
441  BOOST_CHECK( r.ready() == false );
442 }
443 
444 BOOST_AUTO_TEST_CASE( bad_request_no_host ) {
446 
447  std::string raw = "GET / HTTP/1.1\r\n\r\n";
448 
449  bool exception = false;
450 
451  try {
452  r.consume(raw.c_str(),raw.size());
453  } catch (...) {
454  exception = true;
455  }
456 
457  BOOST_CHECK( exception == true );
458  BOOST_CHECK( r.ready() == false );
459 }
460 
461 BOOST_AUTO_TEST_CASE( basic_request ) {
463 
464  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
465 
466  bool exception = false;
467  size_t pos = 0;
468 
469  try {
470  pos = r.consume(raw.c_str(),raw.size());
471  } catch (std::exception &e) {
472  exception = true;
473  std::cout << e.what() << std::endl;
474  }
475 
476  BOOST_CHECK( exception == false );
477  BOOST_CHECK( pos == 41 );
478  BOOST_CHECK( r.ready() == true );
479  BOOST_CHECK( r.get_version() == "HTTP/1.1" );
480  BOOST_CHECK( r.get_method() == "GET" );
481  BOOST_CHECK( r.get_uri() == "/" );
482  BOOST_CHECK( r.get_header("Host") == "www.example.com" );
483 }
484 
485 BOOST_AUTO_TEST_CASE( basic_request_with_body ) {
487 
488  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\nContent-Length: 5\r\n\r\nabcdef";
489 
490  bool exception = false;
491  size_t pos = 0;
492 
493  try {
494  pos = r.consume(raw.c_str(),raw.size());
495  } catch (std::exception &e) {
496  exception = true;
497  std::cout << e.what() << std::endl;
498  }
499 
500  BOOST_CHECK( exception == false );
501  BOOST_CHECK_EQUAL( pos, 65 );
502  BOOST_CHECK( r.ready() == true );
503  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
504  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
505  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
506  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
507  BOOST_CHECK_EQUAL( r.get_header("Content-Length"), "5" );
508  BOOST_CHECK_EQUAL( r.get_body(), "abcde" );
509 }
510 
511 BOOST_AUTO_TEST_CASE( basic_request_with_body_split ) {
513 
514  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\nContent-Length: 6\r\n\r\nabc";
515  std::string raw2 = "def";
516 
517  bool exception = false;
518  size_t pos = 0;
519 
520  try {
521  pos += r.consume(raw.c_str(),raw.size());
522  pos += r.consume(raw2.c_str(),raw2.size());
523  } catch (std::exception &e) {
524  exception = true;
525  std::cout << e.what() << std::endl;
526  }
527 
528  BOOST_CHECK( exception == false );
529  BOOST_CHECK_EQUAL( pos, 66 );
530  BOOST_CHECK( r.ready() == true );
531  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
532  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
533  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
534  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
535  BOOST_CHECK_EQUAL( r.get_header("Content-Length"), "6" );
536  BOOST_CHECK_EQUAL( r.get_body(), "abcdef" );
537 }
538 
539 
540 
541 BOOST_AUTO_TEST_CASE( trailing_body_characters ) {
543 
544  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\na";
545 
546  bool exception = false;
547  size_t pos = 0;
548 
549  try {
550  pos = r.consume(raw.c_str(),raw.size());
551  } catch (...) {
552  exception = true;
553  }
554 
555  BOOST_CHECK( exception == false );
556  BOOST_CHECK( pos == 41 );
557  BOOST_CHECK( r.ready() == true );
558  BOOST_CHECK( r.get_version() == "HTTP/1.1" );
559  BOOST_CHECK( r.get_method() == "GET" );
560  BOOST_CHECK( r.get_uri() == "/" );
561  BOOST_CHECK( r.get_header("Host") == "www.example.com" );
562 }
563 
564 BOOST_AUTO_TEST_CASE( trailing_body_characters_beyond_max_lenth ) {
566 
567  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
568  raw.append(websocketpp::http::max_header_size,'*');
569 
570  bool exception = false;
571  size_t pos = 0;
572 
573  try {
574  pos = r.consume(raw.c_str(),raw.size());
575  } catch (...) {
576  exception = true;
577  }
578 
579  BOOST_CHECK( exception == false );
580  BOOST_CHECK( pos == 41 );
581  BOOST_CHECK( r.ready() == true );
582  BOOST_CHECK( r.get_version() == "HTTP/1.1" );
583  BOOST_CHECK( r.get_method() == "GET" );
584  BOOST_CHECK( r.get_uri() == "/" );
585  BOOST_CHECK( r.get_header("Host") == "www.example.com" );
586 }
587 
588 BOOST_AUTO_TEST_CASE( basic_split1 ) {
590 
591  std::string raw = "GET / HTTP/1.1\r\n";
592  std::string raw2 = "Host: www.example.com\r\n\r\na";
593 
594  bool exception = false;
595  size_t pos = 0;
596 
597  try {
598  pos += r.consume(raw.c_str(),raw.size());
599  pos += r.consume(raw2.c_str(),raw2.size());
600  } catch (std::exception &e) {
601  exception = true;
602  std::cout << e.what() << std::endl;
603  }
604 
605  BOOST_CHECK( exception == false );
606  BOOST_CHECK( pos == 41 );
607  BOOST_CHECK( r.ready() == true );
608  BOOST_CHECK( r.get_version() == "HTTP/1.1" );
609  BOOST_CHECK( r.get_method() == "GET" );
610  BOOST_CHECK( r.get_uri() == "/" );
611  BOOST_CHECK( r.get_header("Host") == "www.example.com" );
612 }
613 
614 BOOST_AUTO_TEST_CASE( basic_split2 ) {
616 
617  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r";
618  std::string raw2 = "\n\r\na";
619 
620  bool exception = false;
621  size_t pos = 0;
622 
623  try {
624  pos += r.consume(raw.c_str(),raw.size());
625  pos += r.consume(raw2.c_str(),raw2.size());
626  } catch (std::exception &e) {
627  exception = true;
628  std::cout << e.what() << std::endl;
629  }
630 
631  BOOST_CHECK( exception == false );
632  BOOST_CHECK( pos == 41 );
633  BOOST_CHECK( r.ready() == true );
634  BOOST_CHECK( r.get_version() == "HTTP/1.1" );
635  BOOST_CHECK( r.get_method() == "GET" );
636  BOOST_CHECK( r.get_uri() == "/" );
637  BOOST_CHECK( r.get_header("Host") == "www.example.com" );
638 }
639 
640 BOOST_AUTO_TEST_CASE( max_header_len ) {
642 
643  std::string raw(websocketpp::http::max_header_size-1,'*');
644  raw += "\r\n";
645 
646  bool exception = false;
647  size_t pos = 0;
648 
649  try {
650  pos += r.consume(raw.c_str(),raw.size());
651  } catch (const websocketpp::http::exception& e) {
653  exception = true;
654  }
655  }
656 
657  BOOST_CHECK( exception == true );
658 }
659 
660 BOOST_AUTO_TEST_CASE( max_header_len_split ) {
662 
663  std::string raw(websocketpp::http::max_header_size-1,'*');
664  std::string raw2(2,'*');
665 
666  bool exception = false;
667  size_t pos = 0;
668 
669  try {
670  pos += r.consume(raw.c_str(),raw.size());
671  pos += r.consume(raw2.c_str(),raw2.size());
672  } catch (const websocketpp::http::exception& e) {
674  exception = true;
675  }
676  }
677 
678  BOOST_CHECK( exception == true );
679 }
680 
681 BOOST_AUTO_TEST_CASE( max_body_len ) {
683 
684  r.set_max_body_size(5);
685 
686  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\nContent-Length: 6\r\n\r\nabcdef";
687 
688  bool exception = false;
689  size_t pos = 0;
690 
691  try {
692  pos += r.consume(raw.c_str(),raw.size());
693  } catch (websocketpp::http::exception const & e) {
694  exception = true;
696  }
697 
698  BOOST_CHECK_EQUAL(r.get_max_body_size(),5);
699  BOOST_CHECK( exception == true );
700 }
701 
702 BOOST_AUTO_TEST_CASE( firefox_full_request ) {
704 
705  std::string raw = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
706 
707  bool exception = false;
708  size_t pos = 0;
709 
710  try {
711  pos += r.consume(raw.c_str(),raw.size());
712  } catch (...) {
713  exception = true;
714  }
715 
716  BOOST_CHECK( exception == false );
717  BOOST_CHECK( pos == 482 );
718  BOOST_CHECK( r.ready() == true );
719  BOOST_CHECK( r.get_version() == "HTTP/1.1" );
720  BOOST_CHECK( r.get_method() == "GET" );
721  BOOST_CHECK( r.get_uri() == "/" );
722  BOOST_CHECK( r.get_header("Host") == "localhost:5000" );
723  BOOST_CHECK( r.get_header("User-Agent") == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0" );
724  BOOST_CHECK( r.get_header("Accept") == "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" );
725  BOOST_CHECK( r.get_header("Accept-Language") == "en-us,en;q=0.5" );
726  BOOST_CHECK( r.get_header("Accept-Encoding") == "gzip, deflate" );
727  BOOST_CHECK( r.get_header("Connection") == "keep-alive, Upgrade" );
728  BOOST_CHECK( r.get_header("Sec-WebSocket-Version") == "8" );
729  BOOST_CHECK( r.get_header("Sec-WebSocket-Origin") == "http://zaphoyd.com" );
730  BOOST_CHECK( r.get_header("Sec-WebSocket-Key") == "pFik//FxwFk0riN4ZiPFjQ==" );
731  BOOST_CHECK( r.get_header("Pragma") == "no-cache" );
732  BOOST_CHECK( r.get_header("Cache-Control") == "no-cache" );
733  BOOST_CHECK( r.get_header("Upgrade") == "websocket" );
734 }
735 
736 BOOST_AUTO_TEST_CASE( bad_method ) {
738 
739  std::string raw = "GE]T / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
740 
741  bool exception = false;
742 
743  try {
744  r.consume(raw.c_str(),raw.size());
745  } catch (...) {
746  exception = true;
747  }
748 
749  BOOST_CHECK( exception == true );
750 }
751 
752 BOOST_AUTO_TEST_CASE( bad_header_name ) {
754 
755  std::string raw = "GET / HTTP/1.1\r\nHo]st: www.example.com\r\n\r\n";
756 
757  bool exception = false;
758 
759  try {
760  r.consume(raw.c_str(),raw.size());
761  } catch (...) {
762  exception = true;
763  }
764 
765  BOOST_CHECK( exception == true );
766 }
767 
768 BOOST_AUTO_TEST_CASE( old_http_version ) {
770 
771  std::string raw = "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n";
772 
773  bool exception = false;
774  size_t pos = 0;
775 
776  try {
777  pos = r.consume(raw.c_str(),raw.size());
778  } catch (...) {
779  exception = true;
780  }
781 
782  BOOST_CHECK( exception == false );
783  BOOST_CHECK_EQUAL( pos, 41 );
784  BOOST_CHECK( r.ready() == true );
785  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.0" );
786  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
787  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
788  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
789 }
790 
791 BOOST_AUTO_TEST_CASE( new_http_version1 ) {
793 
794  std::string raw = "GET / HTTP/1.12\r\nHost: www.example.com\r\n\r\n";
795 
796  bool exception = false;
797  size_t pos = 0;
798 
799  try {
800  pos = r.consume(raw.c_str(),raw.size());
801  } catch (...) {
802  exception = true;
803  }
804 
805  BOOST_CHECK( exception == false );
806  BOOST_CHECK_EQUAL( pos, 42 );
807  BOOST_CHECK( r.ready() == true );
808  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.12" );
809  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
810  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
811  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
812 }
813 
814 BOOST_AUTO_TEST_CASE( new_http_version2 ) {
816 
817  std::string raw = "GET / HTTP/12.12\r\nHost: www.example.com\r\n\r\n";
818 
819  bool exception = false;
820  size_t pos = 0;
821 
822  try {
823  pos = r.consume(raw.c_str(),raw.size());
824  } catch (...) {
825  exception = true;
826  }
827 
828  BOOST_CHECK( exception == false );
829  BOOST_CHECK_EQUAL( pos, 43 );
830  BOOST_CHECK( r.ready() == true );
831  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/12.12" );
832  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
833  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
834  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
835 }
836 
837 /* commented out due to not being implemented yet
838 
839 BOOST_AUTO_TEST_CASE( new_http_version3 ) {
840  websocketpp::http::parser::request r;
841 
842  std::string raw = "GET / HTTPS/12.12\r\nHost: www.example.com\r\n\r\n";
843 
844  bool exception = false;
845  size_t pos = 0;
846 
847  try {
848  pos = r.consume(raw.c_str(),raw.size());
849  } catch (...) {
850  exception = true;
851  }
852 
853  BOOST_CHECK( exception == true );
854 }*/
855 
856 BOOST_AUTO_TEST_CASE( header_whitespace1 ) {
858 
859  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com \r\n\r\n";
860 
861  bool exception = false;
862  size_t pos = 0;
863 
864  try {
865  pos = r.consume(raw.c_str(),raw.size());
866  } catch (...) {
867  exception = true;
868  }
869 
870  BOOST_CHECK( exception == false );
871  BOOST_CHECK_EQUAL( pos, 43 );
872  BOOST_CHECK( r.ready() == true );
873  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
874  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
875  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
876  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
877 }
878 
879 BOOST_AUTO_TEST_CASE( header_whitespace2 ) {
881 
882  std::string raw = "GET / HTTP/1.1\r\nHost:www.example.com\r\n\r\n";
883 
884  bool exception = false;
885  size_t pos = 0;
886 
887  try {
888  pos = r.consume(raw.c_str(),raw.size());
889  } catch (...) {
890  exception = true;
891  }
892 
893  BOOST_CHECK( exception == false );
894  BOOST_CHECK_EQUAL( pos, 40 );
895  BOOST_CHECK( r.ready() == true );
896  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
897  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
898  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
899  BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
900 }
901 
902 BOOST_AUTO_TEST_CASE( header_aggregation ) {
904 
905  std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\nFoo: bar\r\nFoo: bat\r\n\r\n";
906 
907  bool exception = false;
908  size_t pos = 0;
909 
910  try {
911  pos = r.consume(raw.c_str(),raw.size());
912  } catch (...) {
913  exception = true;
914  }
915 
916  BOOST_CHECK( exception == false );
917  BOOST_CHECK_EQUAL( pos, 61 );
918  BOOST_CHECK( r.ready() == true );
919  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
920  BOOST_CHECK_EQUAL( r.get_method(), "GET" );
921  BOOST_CHECK_EQUAL( r.get_uri(), "/" );
922  BOOST_CHECK_EQUAL( r.get_header("Foo"), "bar, bat" );
923 }
924 
925 BOOST_AUTO_TEST_CASE( wikipedia_example_response ) {
927 
928  std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
929 
930  bool exception = false;
931  size_t pos = 0;
932 
933  try {
934  pos += r.consume(raw.c_str(),raw.size());
935  } catch (std::exception &e) {
936  exception = true;
937  std::cout << e.what() << std::endl;
938  }
939 
940  BOOST_CHECK( exception == false );
941  BOOST_CHECK_EQUAL( pos, 159 );
942  BOOST_CHECK( r.headers_ready() == true );
943  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
945  BOOST_CHECK_EQUAL( r.get_status_msg(), "Switching Protocols" );
946  BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket" );
947  BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade" );
948  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Accept"), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
949  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "chat" );
950 }
951 
952 BOOST_AUTO_TEST_CASE( wikipedia_example_response_trailing ) {
954 
955  std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
956  raw += "a";
957 
958  bool exception = false;
959  size_t pos = 0;
960 
961  try {
962  pos += r.consume(raw.c_str(),raw.size());
963  } catch (std::exception &e) {
964  exception = true;
965  std::cout << e.what() << std::endl;
966  }
967 
968  BOOST_CHECK( exception == false );
969  BOOST_CHECK_EQUAL( pos, 159 );
970  BOOST_CHECK( r.headers_ready() == true );
971  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
973  BOOST_CHECK_EQUAL( r.get_status_msg(), "Switching Protocols" );
974  BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket" );
975  BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade" );
976  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Accept"), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
977  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "chat" );
978 }
979 
980 BOOST_AUTO_TEST_CASE( wikipedia_example_response_trailing_large ) {
982 
983  std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
984  raw.append(websocketpp::http::max_header_size,'*');
985 
986  bool exception = false;
987  size_t pos = 0;
988 
989  try {
990  pos += r.consume(raw.c_str(),raw.size());
991  } catch (std::exception &e) {
992  exception = true;
993  std::cout << e.what() << std::endl;
994  }
995 
996  BOOST_CHECK( exception == false );
997  BOOST_CHECK_EQUAL( pos, 159 );
998  BOOST_CHECK( r.headers_ready() == true );
999  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
1001  BOOST_CHECK_EQUAL( r.get_status_msg(), "Switching Protocols" );
1002  BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket" );
1003  BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade" );
1004  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Accept"), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
1005  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "chat" );
1006 }
1007 
1008 BOOST_AUTO_TEST_CASE( response_with_non_standard_lws ) {
1010 
1011  std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept:HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
1012 
1013  bool exception = false;
1014  size_t pos = 0;
1015 
1016  try {
1017  pos += r.consume(raw.c_str(),raw.size());
1018  } catch (std::exception &e) {
1019  exception = true;
1020  std::cout << e.what() << std::endl;
1021  }
1022 
1023  BOOST_CHECK( exception == false );
1024  BOOST_CHECK_EQUAL( pos, 158 );
1025  BOOST_CHECK( r.headers_ready() );
1026  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
1028  BOOST_CHECK_EQUAL( r.get_status_msg(), "Switching Protocols" );
1029  BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket" );
1030  BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade" );
1031  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Accept"), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
1032  BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "chat" );
1033 }
1034 
1035 BOOST_AUTO_TEST_CASE( plain_http_response ) {
1037 
1038  std::string raw = "HTTP/1.1 200 OK\r\nDate: Thu, 10 May 2012 11:59:25 GMT\r\nServer: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch\r\nLast-Modified: Tue, 30 Mar 2010 17:41:28 GMT\r\nETag: \"16799d-55-4830823a78200\"\r\nAccept-Ranges: bytes\r\nContent-Length: 85\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>";
1039 
1040  bool exception = false;
1041  size_t pos = 0;
1042 
1043  try {
1044  pos += r.consume(raw.c_str(),raw.size());
1045  } catch (std::exception &e) {
1046  exception = true;
1047  std::cout << e.what() << std::endl;
1048  }
1049 
1050  BOOST_CHECK( exception == false );
1051  BOOST_CHECK_EQUAL( pos, 405 );
1052  BOOST_CHECK( r.headers_ready() == true );
1053  BOOST_CHECK( r.ready() == true );
1054  BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
1055  BOOST_CHECK_EQUAL( r.get_status_code(), websocketpp::http::status_code::ok );
1056  BOOST_CHECK_EQUAL( r.get_status_msg(), "OK" );
1057  BOOST_CHECK_EQUAL( r.get_header("Date"), "Thu, 10 May 2012 11:59:25 GMT" );
1058  BOOST_CHECK_EQUAL( r.get_header("Server"), "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch" );
1059  BOOST_CHECK_EQUAL( r.get_header("Last-Modified"), "Tue, 30 Mar 2010 17:41:28 GMT" );
1060  BOOST_CHECK_EQUAL( r.get_header("ETag"), "\"16799d-55-4830823a78200\"" );
1061  BOOST_CHECK_EQUAL( r.get_header("Accept-Ranges"), "bytes" );
1062  BOOST_CHECK_EQUAL( r.get_header("Content-Length"), "85" );
1063  BOOST_CHECK_EQUAL( r.get_header("Vary"), "Accept-Encoding" );
1064  BOOST_CHECK_EQUAL( r.get_header("Content-Type"), "text/html" );
1065  BOOST_CHECK_EQUAL( r.get_body(), "<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>" );
1066 }
1067 
1068 BOOST_AUTO_TEST_CASE( parse_istream ) {
1070 
1071  std::stringstream s;
1072 
1073  s << "HTTP/1.1 200 OK\r\nDate: Thu, 10 May 2012 11:59:25 GMT\r\nServer: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch\r\nLast-Modified: Tue, 30 Mar 2010 17:41:28 GMT\r\nETag: \"16799d-55-4830823a78200\"\r\nAccept-Ranges: bytes\r\nContent-Length: 85\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>";
1074 
1075  bool exception = false;
1076  size_t pos = 0;
1077 
1078  try {
1079  pos += r.consume(s);
1080  } catch (std::exception &e) {
1081  exception = true;
1082  std::cout << e.what() << std::endl;
1083  }
1084 
1085  BOOST_CHECK_EQUAL( exception, false );
1086  BOOST_CHECK_EQUAL( pos, 405 );
1087  BOOST_CHECK_EQUAL( r.headers_ready(), true );
1088  BOOST_CHECK_EQUAL( r.ready(), true );
1089 }
1090 
1091 BOOST_AUTO_TEST_CASE( write_request_basic ) {
1093 
1094  std::string raw = "GET / HTTP/1.1\r\n\r\n";
1095 
1096  r.set_version("HTTP/1.1");
1097  r.set_method("GET");
1098  r.set_uri("/");
1099 
1100  BOOST_CHECK_EQUAL( r.raw(), raw );
1101 }
1102 
1103 BOOST_AUTO_TEST_CASE( write_request_with_header ) {
1105 
1106  std::string raw = "GET / HTTP/1.1\r\nHost: http://example.com\r\n\r\n";
1107 
1108  r.set_version("HTTP/1.1");
1109  r.set_method("GET");
1110  r.set_uri("/");
1111  r.replace_header("Host","http://example.com");
1112 
1113  BOOST_CHECK_EQUAL( r.raw(), raw );
1114 }
1115 
1116 BOOST_AUTO_TEST_CASE( write_request_with_body ) {
1118 
1119  std::string raw = "POST / HTTP/1.1\r\nContent-Length: 48\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: http://example.com\r\n\r\nlicenseID=string&content=string&paramsXML=string";
1120 
1121  r.set_version("HTTP/1.1");
1122  r.set_method("POST");
1123  r.set_uri("/");
1124  r.replace_header("Host","http://example.com");
1125  r.replace_header("Content-Type","application/x-www-form-urlencoded");
1126  r.set_body("licenseID=string&content=string&paramsXML=string");
1127 
1128  BOOST_CHECK_EQUAL( r.raw(), raw );
1129 }
size_t get_max_body_size() const
Get body size limit.
Definition: parser.hpp:519
BOOST_AUTO_TEST_CASE(is_token_char)
Definition: parser.cpp:37
std::string raw() const
Returns the full raw request (including the body)
Definition: request.hpp:131
status_code::value m_error_code
Definition: constants.hpp:303
std::string const & get_body() const
Get HTTP body.
Definition: parser.hpp:495
void set_body(std::string const &value)
Set body content.
Definition: parser.hpp:91
status_code::value get_status_code() const
Return the response status code.
Definition: response.hpp:152
std::vector< std::pair< std::string, attribute_list > > parameter_list
The type of an HTTP parameter list.
Definition: constants.hpp:53
std::string const & get_uri() const
Return the requested URI.
Definition: request.hpp:104
bool is_not_token_char(unsigned char c)
Is the character a non-token.
Definition: constants.hpp:103
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:57
bool ready() const
Returns true if the response is ready.
Definition: response.hpp:116
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition: parser.hpp:45
std::string const & get_version() const
Get the HTTP version string.
Definition: parser.hpp:410
InputIterator extract_parameters(InputIterator begin, InputIterator end, parameter_list &parameters)
Extract HTTP parameters.
Definition: parser.hpp:293
bool headers_ready() const
Returns true if the response headers are fully parsed.
Definition: response.hpp:121
InputIterator extract_attributes(InputIterator begin, InputIterator end, attribute_list &attributes)
Extract HTTP attributes.
Definition: parser.hpp:195
std::pair< std::string, InputIterator > extract_quoted_string(InputIterator begin, InputIterator end)
Read and return the next quoted string in the stream.
Definition: parser.hpp:92
size_t const max_header_size
Maximum size in bytes before rejecting an HTTP header as too big.
Definition: constants.hpp:65
std::map< std::string, std::string > attribute_list
The type of an HTTP attribute list.
Definition: constants.hpp:45
std::string strip_lws(std::string const &input)
Definition: parser.hpp:379
void replace_header(std::string const &key, std::string const &val)
Set a value for an HTTP header, replacing an existing value.
Definition: parser.hpp:81
void set_uri(std::string const &uri)
Set the HTTP uri. Must be a valid HTTP uri.
Definition: request.hpp:159
void set_version(std::string const &version)
Set HTTP parser Version.
Definition: parser.hpp:41
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
size_t consume(char const *buf, size_t len)
Process bytes in the input buffer.
Definition: request.hpp:41
void set_method(std::string const &method)
Set the HTTP method. Must be a valid HTTP token.
Definition: request.hpp:151
size_t consume(char const *buf, size_t len)
Process bytes in the input buffer.
Definition: response.hpp:42
InputIterator extract_all_lws(InputIterator begin, InputIterator end)
Read and discard linear whitespace.
Definition: parser.hpp:164
std::pair< std::string, InputIterator > extract_token(InputIterator begin, InputIterator end)
Read and return the next token in the stream.
Definition: parser.hpp:73
bool ready() const
Returns whether or not the request is ready for reading.
Definition: request.hpp:82
void remove_header(std::string const &key)
Remove a header from the parser.
Definition: parser.hpp:87
bool is_token_char(unsigned char c)
Is the character a token.
Definition: constants.hpp:98
const std::string & get_status_msg() const
Return the response status message.
Definition: response.hpp:157
std::string const & get_method() const
Return the request method.
Definition: request.hpp:96
void set_max_body_size(size_t value)
Set body size limit.
Definition: parser.hpp:532