src/​examples/​cpp03/​http/​server/​request_parser.​hpp | src/​examples/​cpp11/​http/​server/​request_parser.​hpp
--- | ---
⋮ | ⋮
1 | /​/​ | 1 | /​/​
2 | /​/​·​request_parser.​hpp | 2 | /​/​·​request_parser.​hpp
3 | /​/​·​~~~~~~~~~~~~~~~~~~ | 3 | /​/​·​~~~~~~~~~~~~~~~~~~
4 | /​/​ | 4 | /​/​
5 | /​/​·​Copyright·​(c)​·​2003-​2020·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​ | 5 | /​/​·​Copyright·​(c)​·​2003-​2020·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 | /​/​ | 6 | /​/​
7 | /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying | 7 | /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying
8 | /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​ | 8 | /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 | /​/​ | 9 | /​/​
10 |  | 10 |
11 | #ifndef·​HTTP_REQUEST_PARSER_H​PP | 11 | #ifndef·​HTTP_REQUEST_PARSER_H​PP
12 | #define·​HTTP_REQUEST_PARSER_H​PP | 12 | #define·​HTTP_REQUEST_PARSER_H​PP
13 |  | 13 |
14 | #include·​<b o o s t/​l o g i c/​tr i b o ol.​h p p> | 14 | #include·​<tu ple>
15 | #i n c l u d e·​<b o o s t/​t u p l e/​t u p l e.​h p p> |
16 |  | 15 |
17 | namespace·​http·​{ | 16 | namespace·​http·​{
18 | namespace·​server·​{ | 17 | namespace·​server·​{
19 |  | 18 |
20 | struct·​request;​ | 19 | struct·​request;​
21 |  | 20 |
22 | /​/​/​·​Parser·​for·​incoming·​requests.​ | 21 | /​/​/​·​Parser·​for·​incoming·​requests.​
23 | class·​request_parser | 22 | class·​request_parser
24 | { | 23 | {
25 | public:​ | 24 | public:​
26 | ·​·​/​/​/​·​Construct·​ready·​to·​parse·​the·​request·​method.​ | 25 | ·​·​/​/​/​·​Construct·​ready·​to·​parse·​the·​request·​method.​
27 | ·​·​request_parser()​;​ | 26 | ·​·​request_parser()​;​
28 |  | 27 |
29 | ·​·​/​/​/​·​Reset·​to·​initial·​parser·​state.​ | 28 | ·​·​/​/​/​·​Reset·​to·​initial·​parser·​state.​
30 | ·​·​void·​reset()​;​ | 29 | ·​·​void·​reset()​;​
31 |  | 30 |
32 | ·​·​/​/​/​·​P a r se·​so m e·​d a t a.​·​T h e·​t r i b o o l·​r e tur n·​v alu e·​i s·​tr u e·​w h e n·​a·​c o mpl e t e·​re q ues t | 31 | ·​·​/​/​/​·​Result·​o f·​parse.​
33 | ·​·​/​/​/​·​h a s·​b een·​p arsed,​·​f a lse·​i f·​t he·​d a t a·​i s·​i n val id,​·​indeterminate·​w hen·​m o r e | 32 | ·​·​enu m·​resu l t_t y pe·​{·​g o o d,​·​bad,​·​indeterminate·​};​
34 | ·​·​/​/​/​·​d a t a·​i s·​r e q u i r e d.​·​T h e·​I n p u t I t e r a t o r·​r e t u r n·​v a l u e·​i n d i c a t e s·​h o w·​m u c h·​o f·​t h e | 33 |
35 | ·​·​/​/​/​·​input·​has·​b een·​consu m e d.​ | 34 | ·​·​/​/​/​·​P a r s e·​s o m e·​d a t a.​·​T h e·​enu m·​r e tur n·​val u e·​is·​g o o d·​w hen·​a·​com p l e t e·​r e q u est·​h a s
| 35 | ·​·​/​/​/​·​been·​parsed,​·​bad·​if·​the·​data·​is·​invalid,​·​indeterminate·​when·​more·​data·​is
| 36 | ·​·​/​/​/​·​required.​·​The·​InputIterator·​return·​value·​indicates·​how·​much·​of·​the·​input
| 37 | ·​·​/​/​/​·​has·​been·​consumed.​
36 | ·​·​template·​<typename·​InputIterator> | 38 | ·​·​template·​<typename·​InputIterator>
37 | ·​·​b o ost:​:​tuple<b o ost:​:​tr i b o o l,​·​InputIterator>·​parse(request&·​req,​ | 39 | ·​·​std:​:​tuple<r esu lt_t y p e,​·​InputIterator>·​parse(request&·​req,​
38 | ·​·​·​·​·​·​InputIterator·​begin,​·​InputIterator·​end)​ | 40 | ·​·​·​·​·​·​InputIterator·​begin,​·​InputIterator·​end)​
39 | ·​·​{ | 41 | ·​·​{
40 | ·​·​·​·​while·​(begin·​!=·​end)​ | 42 | ·​·​·​·​while·​(begin·​!=·​end)​
41 | ·​·​·​·​{ | 43 | ·​·​·​·​{
42 | ·​·​·​·​·​·​b o ost:​:​tr i b o o l·​result·​=·​consume(req,​·​*begin++)​;​ | 44 | ·​·​·​·​·​·​r esu lt_t y p e·​result·​=·​consume(req,​·​*begin++)​;​
43 | ·​·​·​·​·​·​if·​(result·​||·​!result)​ | 45 | ·​·​·​·​·​·​if·​(result·​==·​g o o d·​||·​result·​==·​b a d)​
44 | ·​·​·​·​·​·​·​·​return·​b o ost:​:​make_tuple(result,​·​begin)​;​ | 46 | ·​·​·​·​·​·​·​·​return·​std:​:​make_tuple(result,​·​begin)​;​
45 | ·​·​·​·​} | 47 | ·​·​·​·​}
46 | ·​·​·​·​b o o st:​:​tri b o o l·​r e s u l t·​=·​b o o s t:​:​indeterminate;​ | 48 | ·​·​·​·​r eturn·​s t d:​:​m a k e_t u p l e(indetermin​ate,​·​beg i n)​;​
47 | ·​·​·​·​r e t u r n·​b o o s t:​:​m a k e_t u p l e(r e s u l t,​·​b e g i n)​;​ |
48 | ·​·​} | 49 | ·​·​}
49 |  | 50 |
50 | private:​ | 51 | private:​
51 | ·​·​/​/​/​·​Handle·​the·​next·​character·​of·​input.​ | 52 | ·​·​/​/​/​·​Handle·​the·​next·​character·​of·​input.​
52 | ·​·​b o ost:​:​tr i b o o l·​consume(request&·​req,​·​char·​input)​;​ | 53 | ·​·​r esu lt_t y p e·​consume(request&·​req,​·​char·​input)​;​
53 |  | 54 |
54 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​an·​HTTP·​character.​ | 55 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​an·​HTTP·​character.​
55 | ·​·​static·​bool·​is_char(int·​c)​;​ | 56 | ·​·​static·​bool·​is_char(int·​c)​;​
56 |  | 57 |
57 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​an·​HTTP·​control·​character.​ | 58 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​an·​HTTP·​control·​character.​
58 | ·​·​static·​bool·​is_ctl(int·​c)​;​ | 59 | ·​·​static·​bool·​is_ctl(int·​c)​;​
59 |  | 60 |
60 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​defined·​as·​an·​HTTP·​tspecial·​character.​ | 61 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​defined·​as·​an·​HTTP·​tspecial·​character.​
61 | ·​·​static·​bool·​is_tspecial(int·​c)​;​ | 62 | ·​·​static·​bool·​is_tspecial(int·​c)​;​
62 |  | 63 |
63 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​a·​digit.​ | 64 | ·​·​/​/​/​·​Check·​if·​a·​byte·​is·​a·​digit.​
64 | ·​·​static·​bool·​is_digit(int·​c)​;​ | 65 | ·​·​static·​bool·​is_digit(int·​c)​;​
65 |  | 66 |
66 | ·​·​/​/​/​·​The·​current·​state·​of·​the·​parser.​ | 67 | ·​·​/​/​/​·​The·​current·​state·​of·​the·​parser.​
67 | ·​·​enum·​state | 68 | ·​·​enum·​state
68 | ·​·​{ | 69 | ·​·​{
69 | ·​·​·​·​method_start,​ | 70 | ·​·​·​·​method_start,​
70 | ·​·​·​·​method,​ | 71 | ·​·​·​·​method,​
71 | ·​·​·​·​uri,​ | 72 | ·​·​·​·​uri,​
72 | ·​·​·​·​http_version_h,​ | 73 | ·​·​·​·​http_version_h,​
73 | ·​·​·​·​http_version_t_1,​ | 74 | ·​·​·​·​http_version_t_1,​
74 | ·​·​·​·​http_version_t_2,​ | 75 | ·​·​·​·​http_version_t_2,​
75 | ·​·​·​·​http_version_p,​ | 76 | ·​·​·​·​http_version_p,​
76 | ·​·​·​·​http_version_slash,​ | 77 | ·​·​·​·​http_version_slash,​
77 | ·​·​·​·​http_version_major_st​art,​ | 78 | ·​·​·​·​http_version_major_st​art,​
78 | ·​·​·​·​http_version_major,​ | 79 | ·​·​·​·​http_version_major,​
79 | ·​·​·​·​http_version_minor_st​art,​ | 80 | ·​·​·​·​http_version_minor_st​art,​
80 | ·​·​·​·​http_version_minor,​ | 81 | ·​·​·​·​http_version_minor,​
81 | ·​·​·​·​expecting_newline_1,​ | 82 | ·​·​·​·​expecting_newline_1,​
82 | ·​·​·​·​header_line_start,​ | 83 | ·​·​·​·​header_line_start,​
83 | ·​·​·​·​header_lws,​ | 84 | ·​·​·​·​header_lws,​
84 | ·​·​·​·​header_name,​ | 85 | ·​·​·​·​header_name,​
85 | ·​·​·​·​space_before_header_v​alue,​ | 86 | ·​·​·​·​space_before_header_v​alue,​
86 | ·​·​·​·​header_value,​ | 87 | ·​·​·​·​header_value,​
87 | ·​·​·​·​expecting_newline_2,​ | 88 | ·​·​·​·​expecting_newline_2,​
88 | ·​·​·​·​expecting_newline_3 | 89 | ·​·​·​·​expecting_newline_3
89 | ·​·​}·​state_;​ | 90 | ·​·​}·​state_;​
90 | };​ | 91 | };​
91 |  | 92 |
92 | }·​/​/​·​namespace·​server | 93 | }·​/​/​·​namespace·​server
93 | }·​/​/​·​namespace·​http | 94 | }·​/​/​·​namespace·​http
94 |  | 95 |
95 | #endif·​/​/​·​HTTP_REQUEST_PARSER_H​PP | 96 | #endif·​/​/​·​HTTP_REQUEST_PARSER_H​PP