aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/fetch/httpparser.h
blob: 078eb5f99d164d68c87c760e90f9410cff73df9f (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
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
#pragma once

#include "httpfsm.h"
#include "httpheader.h"

#include <library/cpp/mime/types/mime.h>
#include <util/system/yassert.h>
#include <library/cpp/http/misc/httpcodes.h>

template <size_t headermax = 100 << 10, size_t bodymax = 1 << 20>
struct TFakeCheck {
    bool Check(THttpHeader* /*header*/) {
        return false;
    }
    void CheckDocPart(void* /*buf*/, size_t /*len*/, THttpHeader* /*header*/) {
    } //for every part of DocumentBody will be called
    void CheckEndDoc(THttpHeader* /*header*/) {
    }
    size_t GetMaxHeaderSize() {
        return headermax;
    }
    size_t GetMaxBodySize(THttpHeader*) {
        return bodymax;
    }
};

class THttpParserBase {
public:
    enum States {
        hp_error,
        hp_eof,
        hp_in_header,
        hp_read_alive,
        hp_read_closed,
        hp_begin_chunk_header,
        hp_chunk_header,
        hp_read_chunk
    };

    States GetState() {
        return State;
    }

    void setAssumeConnectionClosed(int value) {
        AssumeConnectionClosed = value;
    }

    THttpHeader* GetHttpHeader() const {
        return Header;
    }

protected:
    int CheckHeaders() {
        if (Header->http_status < HTTP_OK || Header->http_status == HTTP_NO_CONTENT || Header->http_status == HTTP_NOT_MODIFIED) {
            Header->content_length = 0;
            Header->transfer_chunked = 0;
        }
        if (Header->transfer_chunked < -1) {
            Header->error = HTTP_BAD_ENCODING;
            return 1;
        } else if (Header->transfer_chunked == -1) {
            Header->transfer_chunked = 0;
        }
        if (!Header->transfer_chunked && Header->content_length < -1) {
            Header->error = HTTP_BAD_CONTENT_LENGTH;
            return 1;
        }
        if (Header->http_status == HTTP_OK) {
            if (Header->compression_method != HTTP_COMPRESSION_UNSET &&
                Header->compression_method != HTTP_COMPRESSION_IDENTITY &&
                Header->compression_method != HTTP_COMPRESSION_GZIP &&
                Header->compression_method != HTTP_COMPRESSION_DEFLATE)
            {
                Header->error = HTTP_BAD_CONTENT_ENCODING;
                return 1;
            }
        }
        if (Header->connection_closed == -1)
            Header->connection_closed = (Header->http_minor == 0 ||
                                         AssumeConnectionClosed);
        if (!Header->transfer_chunked && !Header->connection_closed && Header->content_length < 0 && !HeadRequest) {
            Header->error = HTTP_LENGTH_UNKNOWN;
            return 1;
        }
        if (Header->http_time < 0)
            Header->http_time = 0;
        if (Header->mime_type < 0)
            Header->mime_type = MIME_UNKNOWN;
        return 0;
    }

    THttpHeaderParser HeaderParser;
    THttpChunkParser ChunkParser;
    States State;
    long ChunkSize;
    THttpHeader* Header;
    int AssumeConnectionClosed;
    bool HeadRequest;
};

template <int isReader, typename TCheck = TFakeCheck<>>
class THttpParserGeneric: public THttpParserBase, public TCheck {
protected:
    long ParseGeneric(void*& buf, long& size) {
        if (!size) {
            switch (State) {
                case hp_error:
                case hp_eof:
                    break;
                case hp_read_closed:
                    State = hp_eof;
                    break;
                case hp_in_header:
                    Header->error = HTTP_HEADER_EOF;
                    State = hp_error;
                    break;
                case hp_read_alive:
                case hp_read_chunk:
                    if (HeadRequest)
                        State = hp_eof;
                    else {
                        Header->error = HTTP_MESSAGE_EOF;
                        State = hp_error;
                    }
                    break;
                case hp_begin_chunk_header:
                case hp_chunk_header:
                    if (HeadRequest)
                        State = hp_eof;
                    else {
                        Header->error = HTTP_CHUNK_EOF;
                        State = hp_error;
                    }
                    break;
            }
            return 0;
        }
        while (size) {
            int ret;

            switch (State) {
                case hp_error:
                    return 0;

                case hp_eof:
                    return 0;

                case hp_in_header:
                    if ((ret = HeaderParser.Execute(buf, size)) < 0) {
                        Header->error = HTTP_BAD_HEADER_STRING;
                        State = hp_error;
                        return 0;
                    } else if (ret == 2) {
                        Header->header_size += i32(HeaderParser.lastchar - (char*)buf + 1);
                        size -= long(HeaderParser.lastchar - (char*)buf + 1);
                        buf = HeaderParser.lastchar + 1;
                        State = CheckHeaders() ? hp_error
                                               : Header->transfer_chunked ? hp_begin_chunk_header
                                                                          : Header->content_length == 0 ? hp_eof
                                                                                                        : Header->content_length > 0 ? hp_read_alive
                                                                                                                                     : hp_read_closed;
                        if (State == hp_begin_chunk_header) {
                            // unget \n for chunk reader
                            buf = (char*)buf - 1;
                            size++;
                        }
                        if (isReader)
                            return size;
                    } else {
                        Header->header_size += size;
                        size = 0;
                    }
                    break;

                case hp_read_alive:
                    Header->entity_size += size;
                    if (Header->entity_size >= Header->content_length) {
                        State = hp_eof;
                    }

                    TCheck::CheckDocPart(buf, size, Header);
                    if (isReader)
                        return size;
                    size = 0;
                    break;

                case hp_read_closed:
                    Header->entity_size += size;
                    TCheck::CheckDocPart(buf, size, Header);
                    if (isReader)
                        return size;
                    size = 0;
                    break;

                case hp_begin_chunk_header:
                    ChunkParser.Init();
                    State = hp_chunk_header;
                    [[fallthrough]];

                case hp_chunk_header:
                    if ((ret = ChunkParser.Execute(buf, size)) < 0) {
                        Header->error = i16(ret == -2 ? HTTP_CHUNK_TOO_LARGE : HTTP_BAD_CHUNK);
                        State = hp_error;
                        return 0;
                    } else if (ret == 2) {
                        Header->entity_size += i32(ChunkParser.lastchar - (char*)buf + 1);
                        size -= long(ChunkParser.lastchar - (char*)buf + 1);
                        buf = ChunkParser.lastchar + 1;
                        ChunkSize = ChunkParser.chunk_length;
                        Y_ASSERT(ChunkSize >= 0); 
                        State = ChunkSize ? hp_read_chunk : hp_eof;
                    } else {
                        Header->entity_size += size;
                        size = 0;
                    }
                    break;

                case hp_read_chunk:
                    if (size >= ChunkSize) {
                        Header->entity_size += ChunkSize;
                        State = hp_begin_chunk_header;
                        TCheck::CheckDocPart(buf, ChunkSize, Header);
                        if (isReader)
                            return ChunkSize;
                        size -= ChunkSize;
                        buf = (char*)buf + ChunkSize;
                    } else {
                        Header->entity_size += size;
                        ChunkSize -= size;
                        TCheck::CheckDocPart(buf, size, Header);
                        if (isReader)
                            return size;
                        size = 0;
                    }
                    break;
            }
        }
        return size;
    }
};

template <class TCheck = TFakeCheck<>>
class THttpParser: public THttpParserGeneric<0, TCheck> {
    typedef THttpParserGeneric<0, TCheck> TBaseT; //sorry avoiding gcc 3.4.6 BUG!
public:
    void Init(THttpHeader* H, bool head_request = false) {
        TBaseT::Header = H;
        TBaseT::HeaderParser.Init(TBaseT::Header);
        TBaseT::State = TBaseT::hp_in_header;
        TBaseT::AssumeConnectionClosed = 0;
        TBaseT::HeadRequest = head_request;
    }

    void Parse(void* buf, long size) {
        TBaseT::ParseGeneric(buf, size);
    }
};

class TMemoReader {
public:
    int Init(void* buf, long bufsize) {
        Buf = buf;
        Bufsize = bufsize;
        return 0;
    }
    long Read(void*& buf) {
        Y_ASSERT(Bufsize >= 0); 
        if (!Bufsize) {
            Bufsize = -1;
            return 0;
        }
        buf = Buf;
        long ret = Bufsize;
        Bufsize = 0;
        return ret;
    }

protected:
    long Bufsize;
    void* Buf;
};

template <class Reader>
class THttpReader: public THttpParserGeneric<1>, public Reader {
    typedef THttpParserGeneric<1> TBaseT;

public:
    using TBaseT::AssumeConnectionClosed;
    using TBaseT::Header;
    using TBaseT::ParseGeneric;
    using TBaseT::State;

    int Init(THttpHeader* H, int parsHeader, int assumeConnectionClosed = 0, bool headRequest = false) {
        Header = H;
        Eoferr = 1;
        Size = 0;
        AssumeConnectionClosed = assumeConnectionClosed;
        HeadRequest = headRequest;
        return parsHeader ? ParseHeader() : SkipHeader();
    }

    long Read(void*& buf) {
        long Chunk;
        do {
            if (!Size) {
                if (Eoferr != 1)
                    return Eoferr;
                else if ((Size = (long)Reader::Read(Ptr)) < 0) {
                    Header->error = HTTP_CONNECTION_LOST;
                    return Eoferr = -1;
                }
            }
            Chunk = ParseGeneric(Ptr, Size);
            buf = Ptr;
            Ptr = (char*)Ptr + Chunk;
            Size -= Chunk;
            if (State == hp_eof) {
                Size = 0;
                Eoferr = 0;
            } else if (State == hp_error)
                return Eoferr = -1;
        } while (!Chunk);
        return Chunk;
    }

protected:
    int ParseHeader() {
        HeaderParser.Init(Header);
        State = hp_in_header;
        while (State == hp_in_header) {
            if ((Size = (long)Reader::Read(Ptr)) < 0)
                return Eoferr = -1;
            ParseGeneric(Ptr, Size);
        }
        if (State == hp_error)
            return Eoferr = -1;
        if (State == hp_eof)
            Eoferr = 0;
        return 0;
    }

    int SkipHeader() {
        long hdrsize = Header->header_size;
        while (hdrsize) {
            if ((Size = (long)Reader::Read(Ptr)) <= 0)
                return Eoferr = -1;
            if (Size >= hdrsize) {
                Size -= hdrsize;
                Ptr = (char*)Ptr + hdrsize;
                break;
            }
            hdrsize -= Size;
        }
        State = Header->transfer_chunked ? hp_begin_chunk_header
                                         : Header->content_length == 0 ? hp_eof
                                                                       : Header->content_length > 0 ? hp_read_alive
                                                                                                    : hp_read_closed;
        Header->entity_size = 0;
        if (State == hp_eof)
            Eoferr = 0;
        else if (State == hp_begin_chunk_header) {
            // unget \n for chunk reader
            Ptr = (char*)Ptr - 1;
            ++Size;
        }
        return 0;
    }

    void* Ptr;
    long Size;
    int Eoferr;
};