forked from griddb/griddb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.h
499 lines (367 loc) · 11.9 KB
/
http.h
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Copyright (c) 2017 TOSHIBA Digital Solutions Corporation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
@file
@brief Definitions of http message classes
*/
#ifndef HTTP_H_
#define HTTP_H_
#include "util/container.h"
#ifndef HTTP_JSON_ENABLD
#define HTTP_JSON_ENABLD 1
#endif
namespace picojson {
class value;
}
class HttpMessage {
public:
static const int32_t DEFAULT_VERSION_MAJOR;
static const int32_t DEFAULT_VERSION_MINOR;
static const char8_t HEADER_ACCEPT[];
static const char8_t HEADER_ACCEPT_ENCODING[];
static const char8_t HEADER_CONNECTION[];
static const char8_t HEADER_CONTENT_ENCODING[];
static const char8_t HEADER_CONTENT_TYPE[];
static const char8_t HEADER_CONTENT_LENGTH[];
static const char8_t HEADER_HOST[];
static const char8_t CONNECTION_CLOSE[];
static const char8_t CONTENT_TYPE_OCTET_STREAM[];
static const char8_t CONTENT_TYPE_JSON[];
static const char8_t CONTENT_TYPE_FORM[];
static const char8_t CONTENT_TYPE_X_FORM[];
static const char8_t CONTENT_CODING_IDENTITY[];
static const char8_t CRLF[];
struct HeaderField;
class FieldParser;
class FieldBuilder;
class Formatter;
typedef util::StdAllocator<void, void> Allocator;
typedef util::BasicString< char8_t, std::char_traits<char8_t>,
util::StdAllocator<char8_t, void> > String;
typedef std::pair<int32_t, int32_t> Version;
typedef std::vector<
HeaderField, util::StdAllocator<HeaderField, void> > HeaderList;
typedef util::XArray<
uint8_t, util::StdAllocator<uint8_t, void> > Buffer;
typedef std::pair<const char8_t*, const char8_t*> StringRange;
explicit HttpMessage(const Allocator &alloc);
HttpMessage(const HttpMessage &another);
HttpMessage& operator=(const HttpMessage &another);
void clear();
void build();
Version& getVersion();
HeaderList& getHeaderList();
HeaderField* findHeader(const char8_t *name);
void addHeader(
const char8_t *name, const char8_t *value, bool overwriting);
void removeHeader(const char8_t *name);
void setStartLine(const char8_t *line);
bool isContentEncodingCustomized() const;
void setContentEncodingCustomized(bool customized);
bool isKeepAlive() const;
void setKeepAlive(bool keepAlive);
bool matchContentTypeName(const char8_t *type, bool failOnUnmatch = false);
bool isJsonType();
picojson::value toJsonValue();
void setJsonValue(const picojson::value &value);
bool acceptChunkedContent(const void *data, size_t size, bool eof);
const void* getMessageData() const;
size_t getMessageSize() const;
const void* getContentData() const;
size_t getContentSize() const;
void setMessageRef(const void *data, size_t size);
void setContentRef(const void *data, size_t size);
Buffer& prepareMessageBuffer();
Buffer& prepareContentBuffer();
bool readFrom(util::File &file, size_t *readSize);
bool writeTo(util::File &file);
bool isWrote() const;
size_t& getWroteSize();
bool isHttp10OrLess() const;
Formatter formatter() const;
private:
static const char8_t CHARS_HTAB[2];
static const char8_t CHARS_SP[2];
static const char8_t CHARS_EQ[2];
static const char8_t CHARS_COMMA[2];
static const char8_t CHARS_SEMI[2];
static const char8_t CHARS_DQUOTE[2];
static const char8_t CHARS_BSLASH[2];
static const char8_t CHARS_SEPARATOR[];
static const char8_t CHARS_TOKEN68_SYMBOL[];
static const std::pair<uint8_t, uint8_t> CHARS_VCHAR;
static const std::pair<uint8_t, uint8_t> CHARS_OBS;
static const std::pair<uint8_t, uint8_t> CHARS_UPPER_ALPHA;
static const std::pair<uint8_t, uint8_t> CHARS_LOWER_ALPHA;
static const std::pair<uint8_t, uint8_t> CHARS_DIGIT;
typedef std::pair<const void*, size_t> BufferRef;
size_t getHeaderSize() const;
static void appendBuffer(Buffer &buf, const void *data, size_t size);
static void appendBuffer(Buffer &buf, const String &str);
static void appendBuffer(Buffer &buf, const char8_t *str);
Version version_;
HeaderList headerList_;
String startLine_;
bool contentEncodingCustomized_;
bool keepAlive_;
BufferRef messageRef_;
BufferRef contentRef_;
Buffer messageBuf_;
Buffer contentBuf_;
size_t wroteSize_;
};
struct HttpMessage::HeaderField {
HeaderField(const String &name, const String &value);
String name_;
String value_;
};
class HttpMessage::FieldParser {
public:
FieldParser(const char8_t *begin, const char8_t *end);
bool nextSeparatedParameter(
StringRange &name, StringRange &value, String &valueStorage,
bool optional = false);
bool nextParameter(
StringRange &name, StringRange &value, String &valueStorage,
bool optional = false);
bool nextQuotedString(
StringRange &value, String &storage, bool optional = false);
bool nextToken(StringRange &token, bool optional = false);
bool nextToken68(StringRange &token, bool optional = false);
bool nextLiteral(const char8_t *str, bool optional = false);
bool nextElement(bool optional = false);
bool nextSpace(
StringRange *space = NULL, bool optional = false);
bool checkEnd(bool optional = false);
const char8_t* getError() const;
static int32_t compareToken(const char8_t *value1, const char8_t *value2);
static void normalizeToken(String &value);
static char8_t normalizeTokenChar(const char8_t ch);
static bool isDigit(const char8_t ch);
static bool charMatches(
const char8_t ch, const std::pair<uint8_t, uint8_t> &range);
private:
bool matches(const char8_t (&ch)[2]) const;
bool matches(const char8_t ch) const;
bool matches(const std::pair<uint8_t, uint8_t> &range) const;
bool checkResult(bool matched, bool optional, const char8_t *last);
const StringRange src_;
const char8_t *cur_;
const char8_t *error_;
};
class HttpMessage::FieldBuilder {
public:
explicit FieldBuilder(String &dest);
void prepareElement();
void addSeparatedParameter(const char8_t *name, const char8_t *value);
void addParameter(const char8_t *name, const char8_t *value);
void addQuotable(const char8_t *value);
void addString(const char8_t *value);
void addSpace();
const String& toString() const;
private:
String &dest_;
bool elementPrepared_;
};
class HttpMessage::Formatter {
public:
explicit Formatter(const HttpMessage &message);
void setFirstLine(const char8_t *firstLine);
void setLimit(size_t limit);
void format(std::ostream &s) const;
private:
const HttpMessage &message_;
const char8_t *firstLine_;
size_t limit_;
};
std::ostream& operator<<(
std::ostream &s, const HttpMessage::Formatter &formatter);
struct HttpAuth {
public:
typedef HttpMessage::Allocator Allocator;
typedef HttpMessage::String String;
enum Type {
TYPE_BASIC,
TYPE_DIGEST,
MAX_TYPE
};
enum Param {
PARAM_REALM,
PARAM_QOP,
PARAM_NONCE,
PARAM_OPAQUE,
PARAM_USERNAME,
PARAM_URI,
PARAM_ALGORITHM,
PARAM_NC,
PARAM_CNONCE,
PARAM_RESPONSE,
PARAM_PASSWORD,
MAX_PARAM
};
enum Qop {
QOP_NONE = 1 << 0,
QOP_AUTH = 1 << 1,
QOP_AUTH_INT = 1 << 2,
MAX_QOP = 1 << 3
};
enum Algorithm {
ALGORITHM_MD5,
ALGORITHM_MD5_SESS,
MAX_ALGORITHM
};
static const char8_t HEADER_REQUEST[];
static const char8_t HEADER_RESPONSE[];
typedef void (HashFunc)(const String&, String*);
explicit HttpAuth(const Allocator &alloc);
void initialize(Type type);
void setHashFunc(HashFunc *hashFunc);
void make(HttpMessage &message, bool requesting) const;
String make(
bool requesting,
HttpMessage::FieldBuilder *builder = NULL) const;
bool accept(HttpMessage &message, bool requesting);
bool accept(const char8_t *value, bool requesting);
void validate(bool requesting) const;
Type getType() const;
void setSecret(
const char8_t *method, const char8_t *secret,
const char8_t *bodyHash = NULL);
int32_t getQopFlags() const;
void setQopFlags(int32_t value);
Algorithm getAlgorithm() const;
void setAlgorithm(Algorithm value);
const char8_t* getParam(Param param) const;
const char8_t* findParam(Param param) const;
void setParam(Param param, const char8_t *value);
static bool resolveParamName(const char8_t *str, Param *value);
static bool resolveTypeName(const char8_t *str, Type *value);
static const char8_t* getHeaderName(bool requesting);
static const char8_t* getTypeName(Type type, bool failOnUnknown);
static const char8_t* getParamName(Param param, bool failOnUnknown);
static const char8_t* getQopName(Qop qop, bool failOnUnknown);
static const char8_t* getAlgorithmName(
Algorithm Algorithm, bool failOnUnknown);
private:
typedef std::pair<bool, String> Value;
typedef std::vector<Value, util::StdAllocator<Value, void> > ParamList;
static ParamList newParamList(const Allocator &alloc);
Type type_;
ParamList paramList_;
HashFunc *hashFunc_;
};
class HttpRequest {
public:
class Parser;
class Formatter;
typedef HttpMessage::Allocator Allocator;
typedef HttpMessage::String String;
typedef std::vector<
String, util::StdAllocator<String, void> > PathElements;
typedef std::map< String, String, std::less<String>, util::StdAllocator<
std::pair<const String, String>, void> > ParameterMap;
typedef int32_t Method;
static const uint16_t DEFAULT_HTTP_PORT;
static const Method METHOD_GET;
static const Method METHOD_POST;
explicit HttpRequest(const Allocator &alloc);
HttpMessage& getMessage();
const HttpMessage& getMessage() const;
void clear();
HttpMessage* parse(bool eof);
HttpMessage& build();
PathElements& getPathElements();
ParameterMap& getParameterMap();
String& getFragment();
bool isDirectory() const;
void setDirectory(bool directory);
Method getMethod() const;
void setMethod(Method method);
void acceptURL(const char8_t *url);
void acceptPath(const char8_t *path);
const char8_t* getScheme() const;
const char8_t* getHost() const;
uint16_t getPort() const;
static const char8_t* methodToString(Method method, bool failOnUnknown);
Formatter formatter() const;
private:
void encodeQueryString(std::ostream &os);
static void encodeURL(std::ostream &os, const char8_t *str);
HttpMessage message_;
PathElements pathElements_;
ParameterMap parameterMap_;
String fragment_;
bool directory_;
String scheme_;
Method method_;
String host_;
uint16_t port_;
};
class HttpRequest::Formatter {
public:
typedef HttpMessage::Formatter Base;
explicit Formatter(const HttpRequest &request);
Base& base();
void format(std::ostream &s) const;
private:
Base base_;
};
std::ostream& operator<<(
std::ostream &s, const HttpRequest::Formatter &formatter);
class HttpResponse {
public:
class Formatter;
typedef HttpMessage::Allocator Allocator;
typedef HttpMessage::String String;
typedef int32_t Status;
explicit HttpResponse(const Allocator &alloc);
HttpMessage& getMessage();
const HttpMessage& getMessage() const;
void clear();
HttpMessage* parse(bool eof);
HttpMessage& build();
Status getStatus() const;
void setStatus(Status status);
const char8_t* getFirstLine() const;
bool isSuccess() const;
void checkSuccess() const;
bool isRedirection() const;
const char8_t* getLocation();
void setBadRequestError();
void setAuthError();
void setMethodError();
void setInternalServerError();
Formatter formatter() const;
private:
enum {
HTTP_STATUS_SUCCESS = 200,
HTTP_STATUS_REDIRECTION = 300
};
Allocator alloc_;
HttpMessage message_;
Status status_;
String firstLine_;
};
class HttpResponse::Formatter {
public:
typedef HttpMessage::Formatter Base;
explicit Formatter(const HttpResponse &response);
Base& base();
void format(std::ostream &s) const;
private:
Base base_;
};
std::ostream& operator<<(
std::ostream &s, const HttpResponse::Formatter &formatter);
#endif