-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpHeader.cpp
More file actions
214 lines (181 loc) · 5.54 KB
/
Copy pathhttpHeader.cpp
File metadata and controls
214 lines (181 loc) · 5.54 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "httpHeader.hpp"
const String requestMethodString[]
{
"INVALID" , // Internally used value
"OPTIONS" ,
"GET" ,
"HEAD" ,
"POST" ,
"PUT" ,
"DELETE" ,
"TRACE" ,
"CONNECT"
} ;
const String responseString[]
{
"100 CONTINUE" ,
"101 SWITCHING PROTOCOLS " ,
"102 PROCESSING " ,
"200 OK" ,
"201 CREATED " ,
"202 ACCEPTED " ,
"203 UNOFFICIAL INFO " ,
"204 NO CONTENT " ,
"205 RESET CONTENT " ,
"206 PARTIAL CONTENT " ,
"207 MULTI STATUS " ,
"300 MULTIPLE CHOICES " ,
"301 MOVED PERMANENTLY " ,
"302 FOUND " ,
"303 SEE OTHER " ,
"304 NOT MODIFIED " ,
"305 USE PROXY " ,
"306 TEMPORARY REDIRECT " ,
"307 PERMANENT REDIRECT " ,
"400 BAD REQUEST " ,
"401 UNAUTHORIZED " ,
"402 PAYMENT REQUIRED " ,
"403 FORBIDDEN " ,
"404 NOT FOUND " ,
"405 METHOD NOT ALLOWED " ,
"406 NOT ACCEPTABLE " ,
"407 PROXY AUTH REQUIRED " ,
"408 REQUEST TIMEOUT " ,
"409 CONFLICT " ,
"410 GONE " ,
"411 LENGHT REQUIRED " ,
"412 PRECONDITION FAILED " ,
"413 ENTITY TOO LARGE " ,
"414 URI TOO LONG " ,
"415 UNSUPPORTED MEDIA TYPE " ,
"416 REQUESTED RANGE NOT SATISFIABLE " ,
"417 EXPECTATION FAILED " ,
"418 I AM A TEAPOT " ,
"420 ENHACE YOUR CALM" ,
"422 UNPROCESSABLE ENTITY" ,
"426 UPGRADE REQUIRED" ,
"429 RETRY WITH" ,
"451 UNAVAILABLE FOR LEGAL REASONS" ,
"500 INTERNAL SERVER ERROR" ,
"501 NOT IMPLEMENTED" ,
"502 BAD GATEWAY" ,
"503 SERVICE UNAVAILABLE" ,
"504 GATEWAY TIMEOUT" ,
"505 HTTP VERSION NOT SUPPORTED"
} ;
http::Request_t::operator String() const
{
return requestMethodString[ (uint8_t) requestId ] ;
}
http::Request_t::operator Request() const
{
return requestId ;
}
http::Request http::Request_t::operator = ( const Request & newRequest )
{
requestId = newRequest ;
return * this ;
}
http::Response_t::operator String() const
{
return responseString[ ( uint8_t ) responseId ] ;
}
http::Response_t::operator Response() const
{
return responseId ;
}
/**
* Main serialization function for HTTP header classes
* Here is where much of the job is done
*
* @param fieldN numeber of fields available in the HTTP header
* @param fieldArray pointer to the fieldArray member of the HTTP header object
*/
String headerToString( const uint8_t & fieldN, const http::Field * const * fieldArray )
{
// String that holds the serialized header
String header ;
/* NOTE:
* pragma is a special field that is used to store all the actual pragma paramaters
* It is known to be the first element of fieldArray
* This reference is made for convenience
*
* As it needs to be processed in a different way than other fields,
* all loops that iterate over fields start at index 1 to not touch it
*/
const http::Field & pragma = * ( fieldArray[ 0 ] ) ;
// Local function that serializes the header field at index n of fieldArray
auto serializeField = [ & ]( const uint8_t n )
{
const http::Field & field = * ( fieldArray[ n ] ) ;
return field.value.length() > 0 ?
field.tag + " " + field.value + "\n" : "" ;
} ;
// Begin header serialization
/* NOTE:
* The first row serialization method changes if this
* is for a response or a request, so delegate this
* to the proper function overloads.
*
* Instead, the other fields will be serialized here
*
* NOTE2:
* The serializeField function is used only in the following loop
* but in this way the whole thing is more expressive
*/
for( auto n = 1 ; n < fieldN ; ++ n )
header += serializeField( n ) ;
// Serialize and append pragma parameters to the header
{
uint8_t subStrCount = 0 ;
uint8_t newStrCount = 0 ;
/* NOTE: ( pragma field structure )
* The pragma field value is a concatenation of the values of
* all the actual pragma fields, each terminated with a \n
*/
// Count the \n terminated substrings and create an array that can hold them
for( auto ptr = pragma.value.begin() ; ptr < pragma.value.end() ; ++ptr )
if( * ptr == '\n' ) ++subStrCount ;
String pragmaArray[ subStrCount ] ;
// Extract the individual pragma field values
for( auto ptr = pragma.value.begin() ; ptr < pragma.value.end() ; ++ptr )
{
pragmaArray[ newStrCount ] += *ptr ;
if( *ptr == '\n' ) ++newStrCount ;
}
// Once extraction is complete, serialize them all
for( newStrCount = 0 ; newStrCount < subStrCount ; ++newStrCount )
header += pragma.tag + pragmaArray[ newStrCount ] ;
}
// Field serialization complete
// Append header termination sequence
header += "\r\n" ;
return header ;
}
http::ResponseHeader & http::ResponseHeader::operator = ( const ResponseHeader & other )
{
for( auto n = 0 ; n < fieldN ; ++ n )
fieldArray[ n ]->value = other.fieldArray[ n ]->value ;
return * this ;
}
/// Serialization method for HTTP response header classes
http::ResponseHeader::operator String() const
{
String partialHeader = headerToString( ( uint8_t ) fieldN, fieldArray ) ;
String response = "HTTP/" + version + " " + ( String ) responseCode + "\n" ;
return response + partialHeader ;
}
/// Serialization method for HTTP request header classes
http::RequestHeader::operator String() const
{
String partialHeader = headerToString( ( uint8_t ) fieldN, fieldArray ) ;
String request =
( String ) requestMethod + " " + requestTarget + " " + version + "\n" ;
return request + partialHeader ;
}
http::RequestHeader & http::RequestHeader::operator = ( const RequestHeader & other )
{
for( auto n = 0 ; n < fieldN ; ++ n )
fieldArray[ n ]->value = other.fieldArray[ n ]->value ;
return * this ;
}