summaryrefslogtreecommitdiff
path: root/src/HttpHeaderProcessor.cc
blob: 53f061e6f56b815a304646b5c4c6ebdf26a871ec (plain)
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
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2012 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "HttpHeaderProcessor.h"

#include <vector>

#include "HttpHeader.h"
#include "message.h"
#include "util.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"
#include "A2STR.h"
#include "error_code.h"

namespace aria2 {

namespace {
enum {
  // Server mode
  PREV_METHOD,
  METHOD,
  PREV_PATH,
  PATH,
  PREV_REQ_VERSION,
  REQ_VERSION,
  // Client mode,
  PREV_RES_VERSION,
  RES_VERSION,
  PREV_STATUS_CODE,
  STATUS_CODE,
  PREV_REASON_PHRASE,
  REASON_PHRASE,
  // name/value header fields
  PREV_EOL,
  PREV_FIELD_NAME,
  FIELD_NAME,
  PREV_FIELD_VALUE,
  FIELD_VALUE,
  // End of header
  PREV_EOH,
  HEADERS_COMPLETE
};
} // namespace

HttpHeaderProcessor::HttpHeaderProcessor(ParserMode mode)
    : mode_(mode),
      state_(mode == CLIENT_PARSER ? PREV_RES_VERSION : PREV_METHOD),
      lastBytesProcessed_(0),
      lastFieldHdKey_(HttpHeader::MAX_INTERESTING_HEADER),
      result_(make_unique<HttpHeader>())
{
}

HttpHeaderProcessor::~HttpHeaderProcessor() = default;

namespace {
size_t getToken(std::string& buf, const unsigned char* data, size_t length,
                size_t off)
{
  size_t j = off;
  while (j < length && !util::isLws(data[j]) && !util::isCRLF(data[j])) {
    ++j;
  }
  buf.append(&data[off], &data[j]);
  return j - 1;
}
} // namespace

namespace {
size_t getFieldNameToken(std::string& buf, const unsigned char* data,
                         size_t length, size_t off)
{
  size_t j = off;
  while (j < length && data[j] != ':' && !util::isLws(data[j]) &&
         !util::isCRLF(data[j])) {
    ++j;
  }
  buf.append(&data[off], &data[j]);
  return j - 1;
}
} // namespace

namespace {
size_t getText(std::string& buf, const unsigned char* data, size_t length,
               size_t off)
{
  size_t j = off;
  while (j < length && !util::isCRLF(data[j])) {
    ++j;
  }
  buf.append(&data[off], &data[j]);
  return j - 1;
}
} // namespace

namespace {
size_t ignoreText(std::string& buf, const unsigned char* data, size_t length,
                  size_t off)
{
  size_t j = off;
  while (j < length && !util::isCRLF(data[j])) {
    ++j;
  }
  return j - 1;
}
} // namespace

bool HttpHeaderProcessor::parse(const unsigned char* data, size_t length)
{
  size_t i;
  lastBytesProcessed_ = 0;
  for (i = 0; i < length; ++i) {
    unsigned char c = data[i];
    switch (state_) {
    case PREV_METHOD:
      if (util::isLws(c) || util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Request-Line: missing method");
      }

      i = getToken(buf_, data, length, i);
      state_ = METHOD;
      break;

    case METHOD:
      if (util::isLws(c)) {
        result_->setMethod(buf_);
        buf_.clear();
        state_ = PREV_PATH;
        break;
      }

      if (util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Request-Line: missing request-target");
      }

      i = getToken(buf_, data, length, i);
      break;

    case PREV_PATH:
      if (util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Request-Line: missing request-target");
      }

      if (util::isLws(c)) {
        break;
      }

      i = getToken(buf_, data, length, i);
      state_ = PATH;
      break;

    case PATH:
      if (util::isLws(c)) {
        result_->setRequestPath(buf_);
        buf_.clear();
        state_ = PREV_REQ_VERSION;
        break;
      }

      if (util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Request-Line: missing HTTP-version");
      }

      i = getToken(buf_, data, length, i);
      break;

    case PREV_REQ_VERSION:
      if (util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Request-Line: missing HTTP-version");
      }

      if (util::isLws(c)) {
        break;
      }

      i = getToken(buf_, data, length, i);
      state_ = REQ_VERSION;
      break;

    case REQ_VERSION:
      if (util::isCRLF(c)) {
        result_->setVersion(buf_);
        buf_.clear();
        state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
        break;
      }

      if (util::isLws(c)) {
        throw DL_ABORT_EX("Bad Request-Line: LWS after HTTP-version");
      }

      i = getToken(buf_, data, length, i);
      break;

    case PREV_RES_VERSION:
      if (util::isLws(c) || util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Status-Line: missing HTTP-version");
      }

      i = getToken(buf_, data, length, i);
      state_ = RES_VERSION;
      break;

    case RES_VERSION:
      if (util::isLws(c)) {
        result_->setVersion(buf_);
        buf_.clear();
        state_ = PREV_STATUS_CODE;
        break;
      }

      if (util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Status-Line: missing status-code");
      }

      break;

    case PREV_STATUS_CODE:
      if (util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad Status-Line: missing status-code");
      }

      if (!util::isLws(c)) {
        state_ = STATUS_CODE;
        i = getToken(buf_, data, length, i);
      }

      break;

    case STATUS_CODE:
      if (!util::isLws(c) && !util::isCRLF(c)) {
        i = getToken(buf_, data, length, i);
        break;
      }

      {
        int statusCode = -1;
        if (buf_.size() == 3 && util::isNumber(buf_.begin(), buf_.end())) {
          statusCode =
              (buf_[0] - '0') * 100 + (buf_[1] - '0') * 10 + (buf_[2] - '0');
        }
        if (statusCode < 100) {
          throw DL_ABORT_EX("Bad status code: bad status-code");
        }
        result_->setStatusCode(statusCode);
        buf_.clear();
      }
      if (c == '\r') {
        state_ = PREV_EOL;
        break;
      }

      if (c == '\n') {
        state_ = PREV_FIELD_NAME;
        break;
      }

      state_ = PREV_REASON_PHRASE;
      break;

    case PREV_REASON_PHRASE:
      if (util::isCRLF(c)) {
        // The reason-phrase is completely optional.
        state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
        break;
      }

      if (util::isLws(c)) {
        break;
      }

      state_ = REASON_PHRASE;
      i = getText(buf_, data, length, i);
      break;

    case REASON_PHRASE:
      if (util::isCRLF(c)) {
        result_->setReasonPhrase(buf_);
        buf_.clear();
        state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
        break;
      }

      i = getText(buf_, data, length, i);
      break;

    case PREV_EOL:
      if (c != '\n') {
        throw DL_ABORT_EX("Bad HTTP header: missing LF");
      }

      state_ = PREV_FIELD_NAME;
      break;

    case PREV_FIELD_NAME:
      if (util::isLws(c)) {
        if (lastFieldName_.empty()) {
          throw DL_ABORT_EX("Bad HTTP header: field name starts with LWS");
        }
        // Evil Multi-line header field
        state_ = FIELD_VALUE;
        break;
      }

      if (!lastFieldName_.empty()) {
        if (lastFieldHdKey_ != HttpHeader::MAX_INTERESTING_HEADER) {
          result_->put(lastFieldHdKey_, util::strip(buf_));
        }
        lastFieldName_.clear();
        lastFieldHdKey_ = HttpHeader::MAX_INTERESTING_HEADER;
        buf_.clear();
      }
      if (c == '\n') {
        state_ = HEADERS_COMPLETE;
        break;
      }

      if (c == '\r') {
        state_ = PREV_EOH;
        break;
      }

      if (c == ':') {
        throw DL_ABORT_EX("Bad HTTP header: field name starts with ':'");
      }

      state_ = FIELD_NAME;
      i = getFieldNameToken(lastFieldName_, data, length, i);
      break;

    case FIELD_NAME:
      if (util::isLws(c) || util::isCRLF(c)) {
        throw DL_ABORT_EX("Bad HTTP header: missing ':'");
      }

      if (c == ':') {
        util::lowercase(lastFieldName_);
        lastFieldHdKey_ = idInterestingHeader(lastFieldName_.c_str());
        state_ = PREV_FIELD_VALUE;
        break;
      }

      i = getFieldNameToken(lastFieldName_, data, length, i);
      break;

    case PREV_FIELD_VALUE:
      if (c == '\r') {
        state_ = PREV_EOL;
        break;
      }

      if (c == '\n') {
        state_ = PREV_FIELD_NAME;
        break;
      }

      if (util::isLws(c)) {
        break;
      }

      state_ = FIELD_VALUE;
      if (lastFieldHdKey_ == HttpHeader::MAX_INTERESTING_HEADER) {
        i = ignoreText(buf_, data, length, i);
        break;
      }

      i = getText(buf_, data, length, i);
      break;

    case FIELD_VALUE:
      if (c == '\r') {
        state_ = PREV_EOL;
        break;
      }

      if (c == '\n') {
        state_ = PREV_FIELD_NAME;
        break;
      }

      if (lastFieldHdKey_ == HttpHeader::MAX_INTERESTING_HEADER) {
        i = ignoreText(buf_, data, length, i);
        break;
      }

      i = getText(buf_, data, length, i);
      break;

    case PREV_EOH:
      if (c != '\n') {
        throw DL_ABORT_EX("Bad HTTP header: "
                          "missing LF at the end of the header");
      }

      state_ = HEADERS_COMPLETE;
      break;

    case HEADERS_COMPLETE:
      goto fin;
    }
  }

fin:
  // See Apache's documentation
  // http://httpd.apache.org/docs/2.2/en/mod/core.html about size
  // limit of HTTP headers. The page states that the number of request
  // fields rarely exceeds 20.
  if (lastFieldName_.size() > 1024 || buf_.size() > 8_k) {
    throw DL_ABORT_EX("Too large HTTP header");
  }

  lastBytesProcessed_ = i;
  headers_.append(&data[0], &data[i]);

  if (state_ != HEADERS_COMPLETE) {
    return false;
  }

  // If both transfer-encoding and (content-length or content-range)
  // are present, delete content-length and content-range.  RFC 7230
  // says that sender must not send both transfer-encoding and
  // content-length.  If both present, transfer-encoding overrides
  // content-length.  There is no text about transfer-encoding and
  // content-range.  But there is no reason to send transfer-encoding
  // when range is set.
  if (result_->defined(HttpHeader::TRANSFER_ENCODING)) {
    result_->remove(HttpHeader::CONTENT_LENGTH);
    result_->remove(HttpHeader::CONTENT_RANGE);
  }

  return true;
}

bool HttpHeaderProcessor::parse(const std::string& data)
{
  return parse(reinterpret_cast<const unsigned char*>(data.c_str()),
               data.size());
}

size_t HttpHeaderProcessor::getLastBytesProcessed() const
{
  return lastBytesProcessed_;
}

void HttpHeaderProcessor::clear()
{
  state_ = (mode_ == CLIENT_PARSER ? PREV_RES_VERSION : PREV_METHOD);
  lastBytesProcessed_ = 0;
  buf_.clear();
  lastFieldName_.clear();
  lastFieldHdKey_ = HttpHeader::MAX_INTERESTING_HEADER;
  result_ = make_unique<HttpHeader>();
  headers_.clear();
}

std::unique_ptr<HttpHeader> HttpHeaderProcessor::getResult()
{
  return std::move(result_);
}

std::string HttpHeaderProcessor::getHeaderString() const { return headers_; }

} // namespace aria2