HttpHeaderProcessor.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2012 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "HttpHeaderProcessor.h"
  36. #include <vector>
  37. #include "HttpHeader.h"
  38. #include "message.h"
  39. #include "util.h"
  40. #include "DlRetryEx.h"
  41. #include "DlAbortEx.h"
  42. #include "A2STR.h"
  43. #include "error_code.h"
  44. namespace aria2 {
  45. namespace {
  46. enum {
  47. // Server mode
  48. PREV_METHOD,
  49. METHOD,
  50. PREV_PATH,
  51. PATH,
  52. PREV_REQ_VERSION,
  53. REQ_VERSION,
  54. // Client mode,
  55. PREV_RES_VERSION,
  56. RES_VERSION,
  57. PREV_STATUS_CODE,
  58. STATUS_CODE,
  59. PREV_REASON_PHRASE,
  60. REASON_PHRASE,
  61. // name/value header fields
  62. PREV_EOL,
  63. PREV_FIELD_NAME,
  64. FIELD_NAME,
  65. PREV_FIELD_VALUE,
  66. FIELD_VALUE,
  67. // End of header
  68. PREV_EOH,
  69. HEADERS_COMPLETE
  70. };
  71. } // namespace
  72. HttpHeaderProcessor::HttpHeaderProcessor(ParserMode mode)
  73. : mode_(mode),
  74. state_(mode == CLIENT_PARSER ? PREV_RES_VERSION : PREV_METHOD),
  75. lastBytesProcessed_(0),
  76. lastFieldHdKey_(HttpHeader::MAX_INTERESTING_HEADER),
  77. result_(new HttpHeader())
  78. {
  79. }
  80. HttpHeaderProcessor::~HttpHeaderProcessor() {}
  81. namespace {
  82. size_t getToken(std::string& buf, const unsigned char* data, size_t length,
  83. size_t off)
  84. {
  85. size_t j = off;
  86. while (j < length && !util::isLws(data[j]) && !util::isCRLF(data[j])) {
  87. ++j;
  88. }
  89. buf.append(&data[off], &data[j]);
  90. return j - 1;
  91. }
  92. } // namespace
  93. namespace {
  94. size_t getFieldNameToken(std::string& buf, const unsigned char* data,
  95. size_t length, size_t off)
  96. {
  97. size_t j = off;
  98. while (j < length && data[j] != ':' && !util::isLws(data[j]) &&
  99. !util::isCRLF(data[j])) {
  100. ++j;
  101. }
  102. buf.append(&data[off], &data[j]);
  103. return j - 1;
  104. }
  105. } // namespace
  106. namespace {
  107. size_t getText(std::string& buf, const unsigned char* data, size_t length,
  108. size_t off)
  109. {
  110. size_t j = off;
  111. while (j < length && !util::isCRLF(data[j])) {
  112. ++j;
  113. }
  114. buf.append(&data[off], &data[j]);
  115. return j - 1;
  116. }
  117. } // namespace
  118. namespace {
  119. size_t ignoreText(std::string& buf, const unsigned char* data, size_t length,
  120. size_t off)
  121. {
  122. size_t j = off;
  123. while (j < length && !util::isCRLF(data[j])) {
  124. ++j;
  125. }
  126. return j - 1;
  127. }
  128. } // namespace
  129. bool HttpHeaderProcessor::parse(const unsigned char* data, size_t length)
  130. {
  131. size_t i;
  132. lastBytesProcessed_ = 0;
  133. for (i = 0; i < length; ++i) {
  134. unsigned char c = data[i];
  135. switch (state_) {
  136. case PREV_METHOD:
  137. if (util::isLws(c) || util::isCRLF(c)) {
  138. throw DL_ABORT_EX("Bad Request-Line: missing method");
  139. }
  140. i = getToken(buf_, data, length, i);
  141. state_ = METHOD;
  142. break;
  143. case METHOD:
  144. if (util::isLws(c)) {
  145. result_->setMethod(buf_);
  146. buf_.clear();
  147. state_ = PREV_PATH;
  148. break;
  149. }
  150. if (util::isCRLF(c)) {
  151. throw DL_ABORT_EX("Bad Request-Line: missing request-target");
  152. }
  153. i = getToken(buf_, data, length, i);
  154. break;
  155. case PREV_PATH:
  156. if (util::isCRLF(c)) {
  157. throw DL_ABORT_EX("Bad Request-Line: missing request-target");
  158. }
  159. if (util::isLws(c)) {
  160. break;
  161. }
  162. i = getToken(buf_, data, length, i);
  163. state_ = PATH;
  164. break;
  165. case PATH:
  166. if (util::isLws(c)) {
  167. result_->setRequestPath(buf_);
  168. buf_.clear();
  169. state_ = PREV_REQ_VERSION;
  170. break;
  171. }
  172. if (util::isCRLF(c)) {
  173. throw DL_ABORT_EX("Bad Request-Line: missing HTTP-version");
  174. }
  175. i = getToken(buf_, data, length, i);
  176. break;
  177. case PREV_REQ_VERSION:
  178. if (util::isCRLF(c)) {
  179. throw DL_ABORT_EX("Bad Request-Line: missing HTTP-version");
  180. }
  181. if (util::isLws(c)) {
  182. break;
  183. }
  184. i = getToken(buf_, data, length, i);
  185. state_ = REQ_VERSION;
  186. break;
  187. case REQ_VERSION:
  188. if (util::isCRLF(c)) {
  189. result_->setVersion(buf_);
  190. buf_.clear();
  191. state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
  192. break;
  193. }
  194. if (util::isLws(c)) {
  195. throw DL_ABORT_EX("Bad Request-Line: LWS after HTTP-version");
  196. }
  197. i = getToken(buf_, data, length, i);
  198. break;
  199. case PREV_RES_VERSION:
  200. if (util::isLws(c) || util::isCRLF(c)) {
  201. throw DL_ABORT_EX("Bad Status-Line: missing HTTP-version");
  202. }
  203. i = getToken(buf_, data, length, i);
  204. state_ = RES_VERSION;
  205. break;
  206. case RES_VERSION:
  207. if (util::isLws(c)) {
  208. result_->setVersion(buf_);
  209. buf_.clear();
  210. state_ = PREV_STATUS_CODE;
  211. break;
  212. }
  213. if (util::isCRLF(c)) {
  214. throw DL_ABORT_EX("Bad Status-Line: missing status-code");
  215. }
  216. break;
  217. case PREV_STATUS_CODE:
  218. if (util::isCRLF(c)) {
  219. throw DL_ABORT_EX("Bad Status-Line: missing status-code");
  220. }
  221. if (!util::isLws(c)) {
  222. state_ = STATUS_CODE;
  223. i = getToken(buf_, data, length, i);
  224. }
  225. break;
  226. case STATUS_CODE:
  227. if (!util::isLws(c) && !util::isCRLF(c)) {
  228. i = getToken(buf_, data, length, i);
  229. break;
  230. }
  231. {
  232. int statusCode = -1;
  233. if (buf_.size() == 3 && util::isNumber(buf_.begin(), buf_.end())) {
  234. statusCode =
  235. (buf_[0] - '0') * 100 + (buf_[1] - '0') * 10 + (buf_[2] - '0');
  236. }
  237. if (statusCode < 100) {
  238. throw DL_ABORT_EX("Bad status code: bad status-code");
  239. }
  240. result_->setStatusCode(statusCode);
  241. buf_.clear();
  242. }
  243. if (c == '\r') {
  244. state_ = PREV_EOL;
  245. break;
  246. }
  247. if (c == '\n') {
  248. state_ = PREV_FIELD_NAME;
  249. break;
  250. }
  251. state_ = PREV_REASON_PHRASE;
  252. break;
  253. case PREV_REASON_PHRASE:
  254. if (util::isCRLF(c)) {
  255. // The reason-phrase is completely optional.
  256. state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
  257. break;
  258. }
  259. if (util::isLws(c)) {
  260. break;
  261. }
  262. state_ = REASON_PHRASE;
  263. i = getText(buf_, data, length, i);
  264. break;
  265. case REASON_PHRASE:
  266. if (util::isCRLF(c)) {
  267. result_->setReasonPhrase(buf_);
  268. buf_.clear();
  269. state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
  270. break;
  271. }
  272. i = getText(buf_, data, length, i);
  273. break;
  274. case PREV_EOL:
  275. if (c != '\n') {
  276. throw DL_ABORT_EX("Bad HTTP header: missing LF");
  277. }
  278. state_ = PREV_FIELD_NAME;
  279. break;
  280. case PREV_FIELD_NAME:
  281. if (util::isLws(c)) {
  282. if (lastFieldName_.empty()) {
  283. throw DL_ABORT_EX("Bad HTTP header: field name starts with LWS");
  284. }
  285. // Evil Multi-line header field
  286. state_ = FIELD_VALUE;
  287. break;
  288. }
  289. if (!lastFieldName_.empty()) {
  290. if (lastFieldHdKey_ != HttpHeader::MAX_INTERESTING_HEADER) {
  291. result_->put(lastFieldHdKey_, util::strip(buf_));
  292. }
  293. lastFieldName_.clear();
  294. lastFieldHdKey_ = HttpHeader::MAX_INTERESTING_HEADER;
  295. buf_.clear();
  296. }
  297. if (c == '\n') {
  298. state_ = HEADERS_COMPLETE;
  299. break;
  300. }
  301. if (c == '\r') {
  302. state_ = PREV_EOH;
  303. break;
  304. }
  305. if (c == ':') {
  306. throw DL_ABORT_EX("Bad HTTP header: field name starts with ':'");
  307. }
  308. state_ = FIELD_NAME;
  309. i = getFieldNameToken(lastFieldName_, data, length, i);
  310. break;
  311. case FIELD_NAME:
  312. if (util::isLws(c) || util::isCRLF(c)) {
  313. throw DL_ABORT_EX("Bad HTTP header: missing ':'");
  314. }
  315. if (c == ':') {
  316. util::lowercase(lastFieldName_);
  317. lastFieldHdKey_ = idInterestingHeader(lastFieldName_.c_str());
  318. state_ = PREV_FIELD_VALUE;
  319. break;
  320. }
  321. i = getFieldNameToken(lastFieldName_, data, length, i);
  322. break;
  323. case PREV_FIELD_VALUE:
  324. if (c == '\r') {
  325. state_ = PREV_EOL;
  326. break;
  327. }
  328. if (c == '\n') {
  329. state_ = PREV_FIELD_NAME;
  330. break;
  331. }
  332. if (util::isLws(c)) {
  333. break;
  334. }
  335. state_ = FIELD_VALUE;
  336. if (lastFieldHdKey_ == HttpHeader::MAX_INTERESTING_HEADER) {
  337. i = ignoreText(buf_, data, length, i);
  338. break;
  339. }
  340. i = getText(buf_, data, length, i);
  341. break;
  342. case FIELD_VALUE:
  343. if (c == '\r') {
  344. state_ = PREV_EOL;
  345. break;
  346. }
  347. if (c == '\n') {
  348. state_ = PREV_FIELD_NAME;
  349. break;
  350. }
  351. if (lastFieldHdKey_ == HttpHeader::MAX_INTERESTING_HEADER) {
  352. i = ignoreText(buf_, data, length, i);
  353. break;
  354. }
  355. i = getText(buf_, data, length, i);
  356. break;
  357. case PREV_EOH:
  358. if (c != '\n') {
  359. throw DL_ABORT_EX("Bad HTTP header: "
  360. "missing LF at the end of the header");
  361. }
  362. state_ = HEADERS_COMPLETE;
  363. break;
  364. case HEADERS_COMPLETE:
  365. goto fin;
  366. }
  367. }
  368. fin:
  369. // See Apache's documentation
  370. // http://httpd.apache.org/docs/2.2/en/mod/core.html about size
  371. // limit of HTTP headers. The page states that the number of request
  372. // fields rarely exceeds 20.
  373. if (lastFieldName_.size() > 1024 || buf_.size() > 8_k) {
  374. throw DL_ABORT_EX("Too large HTTP header");
  375. }
  376. lastBytesProcessed_ = i;
  377. headers_.append(&data[0], &data[i]);
  378. if (state_ != HEADERS_COMPLETE) {
  379. return false;
  380. }
  381. // If both transfer-encoding and (content-length or content-range)
  382. // are present, delete content-length and content-range. RFC 7230
  383. // says that sender must not send both transfer-encoding and
  384. // content-length. If both present, transfer-encoding overrides
  385. // content-length. There is no text about transfer-encoding and
  386. // content-range. But there is no reason to send transfer-encoding
  387. // when range is set.
  388. if (result_->defined(HttpHeader::TRANSFER_ENCODING)) {
  389. result_->remove(HttpHeader::CONTENT_LENGTH);
  390. result_->remove(HttpHeader::CONTENT_RANGE);
  391. }
  392. return true;
  393. }
  394. bool HttpHeaderProcessor::parse(const std::string& data)
  395. {
  396. return parse(reinterpret_cast<const unsigned char*>(data.c_str()),
  397. data.size());
  398. }
  399. size_t HttpHeaderProcessor::getLastBytesProcessed() const
  400. {
  401. return lastBytesProcessed_;
  402. }
  403. void HttpHeaderProcessor::clear()
  404. {
  405. state_ = (mode_ == CLIENT_PARSER ? PREV_RES_VERSION : PREV_METHOD);
  406. lastBytesProcessed_ = 0;
  407. buf_.clear();
  408. lastFieldName_.clear();
  409. lastFieldHdKey_ = HttpHeader::MAX_INTERESTING_HEADER;
  410. result_.reset(new HttpHeader());
  411. headers_.clear();
  412. }
  413. std::unique_ptr<HttpHeader> HttpHeaderProcessor::getResult()
  414. {
  415. return std::move(result_);
  416. }
  417. std::string HttpHeaderProcessor::getHeaderString() const { return headers_; }
  418. } // namespace aria2