blob: af3ce46dbdb8d6234a39be05a543af989e32bcf1 (
plain) (
blame)
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
|
#pragma once
#include <util/generic/string.h>
#include <util/generic/strbuf.h>
#include <util/generic/yexception.h>
#include <util/generic/hash_set.h>
#include <util/string/cast.h>
#include <library/cpp/http/io/stream.h>
struct THttpVersion {
unsigned Major = 1;
unsigned Minor = 0;
};
//http requests parser for async/callbacks arch. (uggly state-machine)
//usage, - call Parse(...), if returned 'true' - all message parsed,
//external (non entered in message) bytes in input data counted by GetExtraDataSize()
class THttpParser {
public:
enum TMessageType {
Request,
Response
};
THttpParser(TMessageType mt = Response)
: Parser_(&THttpParser::FirstLineParser)
, MessageType_(mt)
{
}
inline void DisableCollectingHeaders() noexcept {
CollectHeaders_ = false;
}
inline void SetGzipAllowMultipleStreams(bool allow) noexcept {
GzipAllowMultipleStreams_ = allow;
}
inline void DisableDecodeContent() noexcept {
DecodeContent_ = false;
}
/*
* Disable message-body parsing.
* Useful for parse HEAD method responses
*/
inline void BodyNotExpected() {
BodyNotExpected_ = true;
}
/// @return true on end parsing (GetExtraDataSize() return amount not used bytes)
/// throw exception on bad http format (unsupported encoding, etc)
/// sz == 0 signaling end of input stream
bool Parse(const char* data, size_t sz) {
if (ParseImpl(data, sz)) {
if (DecodeContent_) {
DecodeContent(DecodedContent_);
}
return true;
}
return false;
}
const char* Data() const noexcept {
return Data_;
}
size_t GetExtraDataSize() const noexcept {
return ExtraDataSize_;
}
const TString& FirstLine() const noexcept {
return FirstLine_;
}
unsigned RetCode() const noexcept {
return RetCode_;
}
const THttpVersion& HttpVersion() const noexcept {
return HttpVersion_;
}
const THttpHeaders& Headers() const noexcept {
return Headers_;
}
bool IsKeepAlive() const noexcept {
return KeepAlive_;
}
bool GetContentLength(ui64& value) const noexcept {
if (!HasContentLength_) {
return false;
}
value = ContentLength_;
return true;
}
TString GetBestCompressionScheme() const;
const TString& Content() const noexcept {
return Content_;
}
const TString& DecodedContent() const noexcept {
return DecodedContent_;
}
void Prepare() {
HeaderLine_.reserve(128);
FirstLine_.reserve(128);
}
bool DecodeContent(TString& decodedContent) const;
private:
bool ParseImpl(const char* data, size_t sz) {
Data_ = data;
DataEnd_ = data + sz;
if (sz == 0) {
OnEof();
return true;
}
return (this->*Parser_)();
}
// stage parsers
bool FirstLineParser();
bool HeadersParser();
bool ContentParser();
bool ChunkedContentParser();
bool OnEndParsing();
// continue read to CurrentLine_
bool ReadLine();
void ParseHttpVersion(TStringBuf httpVersion);
void ParseHeaderLine();
void OnEof();
void ApplyHeaderLine(const TStringBuf& name, const TStringBuf& val);
typedef bool (THttpParser::*TParser)();
TParser Parser_; //current parser (stage)
TMessageType MessageType_ = Response;
bool CollectHeaders_ = true;
bool GzipAllowMultipleStreams_ = true;
bool DecodeContent_ = true;
bool BodyNotExpected_ = false;
// parsed data
const char* Data_ = nullptr;
const char* DataEnd_ = nullptr;
TString CurrentLine_;
TString HeaderLine_;
size_t ExtraDataSize_ = 0;
// headers
TString FirstLine_;
THttpVersion HttpVersion_;
unsigned RetCode_ = 0;
THttpHeaders Headers_;
bool KeepAlive_ = false;
THashSet<TString> AcceptEncodings_;
TString ContentEncoding_;
bool HasContentLength_ = false;
ui64 ContentLength_ = 0;
struct TChunkInputState {
size_t LeftBytes_ = 0;
bool ReadLastChunk_ = false;
};
TAutoPtr<TChunkInputState> ChunkInputState_;
TString Content_;
TString DecodedContent_;
};
|