HttpHeaderProcessor.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. HttpHeaderProcessor::~HttpHeaderProcessor() {}
  80. namespace {
  81. size_t getToken(std::string& buf,
  82. const unsigned char* data, size_t length, size_t off)
  83. {
  84. size_t j;
  85. for(j = off; j < length && !util::isLws(data[j]) && !util::isCRLF(data[j]);
  86. ++j);
  87. buf.append(&data[off], &data[j]);
  88. return j-1;
  89. }
  90. } // namespace
  91. namespace {
  92. size_t getFieldNameToken(std::string& buf,
  93. const unsigned char* data, size_t length, size_t off)
  94. {
  95. size_t j;
  96. for(j = off; j < length && data[j] != ':' &&
  97. !util::isLws(data[j]) && !util::isCRLF(data[j]); ++j);
  98. buf.append(&data[off], &data[j]);
  99. return j-1;
  100. }
  101. } // namespace
  102. namespace {
  103. size_t getText(std::string& buf,
  104. const unsigned char* data, size_t length, size_t off)
  105. {
  106. size_t j;
  107. for(j = off; j < length && !util::isCRLF(data[j]); ++j);
  108. buf.append(&data[off], &data[j]);
  109. return j-1;
  110. }
  111. } // namespace
  112. namespace {
  113. size_t ignoreText(std::string& buf,
  114. const unsigned char* data, size_t length, size_t off)
  115. {
  116. size_t j;
  117. for(j = off; j < length && !util::isCRLF(data[j]); ++j);
  118. return j-1;
  119. }
  120. } // namespace
  121. bool HttpHeaderProcessor::parse(const unsigned char* data, size_t length)
  122. {
  123. size_t i;
  124. lastBytesProcessed_ = 0;
  125. for (i = 0; i < length; ++i) {
  126. unsigned char c = data[i];
  127. switch (state_) {
  128. case PREV_METHOD:
  129. if (util::isLws(c) || util::isCRLF(c)) {
  130. throw DL_ABORT_EX("Bad Request-Line: missing method");
  131. }
  132. i = getToken(buf_, data, length, i);
  133. state_ = METHOD;
  134. break;
  135. case METHOD:
  136. if (util::isLws(c)) {
  137. result_->setMethod(buf_);
  138. buf_.clear();
  139. state_ = PREV_PATH;
  140. break;
  141. }
  142. if (util::isCRLF(c)) {
  143. throw DL_ABORT_EX("Bad Request-Line: missing request-target");
  144. }
  145. i = getToken(buf_, data, length, i);
  146. break;
  147. case PREV_PATH:
  148. if (util::isCRLF(c)) {
  149. throw DL_ABORT_EX("Bad Request-Line: missing request-target");
  150. }
  151. if (util::isLws(c)) {
  152. break;
  153. }
  154. i = getToken(buf_, data, length, i);
  155. state_ = PATH;
  156. break;
  157. case PATH:
  158. if (util::isLws(c)) {
  159. result_->setRequestPath(buf_);
  160. buf_.clear();
  161. state_ = PREV_REQ_VERSION;
  162. break;
  163. }
  164. if (util::isCRLF(c)) {
  165. throw DL_ABORT_EX("Bad Request-Line: missing HTTP-version");
  166. }
  167. i = getToken(buf_, data, length, i);
  168. break;
  169. case PREV_REQ_VERSION:
  170. if (util::isCRLF(c)) {
  171. throw DL_ABORT_EX("Bad Request-Line: missing HTTP-version");
  172. }
  173. if (util::isLws(c)) {
  174. break;
  175. }
  176. i = getToken(buf_, data, length, i);
  177. state_ = REQ_VERSION;
  178. break;
  179. case REQ_VERSION:
  180. if (util::isCRLF(c)) {
  181. result_->setVersion(buf_);
  182. buf_.clear();
  183. state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
  184. break;
  185. }
  186. if (util::isLws(c)) {
  187. throw DL_ABORT_EX("Bad Request-Line: LWS after HTTP-version");
  188. }
  189. i = getToken(buf_, data, length, i);
  190. break;
  191. case PREV_RES_VERSION:
  192. if (util::isLws(c) || util::isCRLF(c)) {
  193. throw DL_ABORT_EX("Bad Status-Line: missing HTTP-version");
  194. }
  195. i = getToken(buf_, data, length, i);
  196. state_ = RES_VERSION;
  197. break;
  198. case RES_VERSION:
  199. if (util::isLws(c)) {
  200. result_->setVersion(buf_);
  201. buf_.clear();
  202. state_ = PREV_STATUS_CODE;
  203. break;
  204. }
  205. if (util::isCRLF(c)) {
  206. throw DL_ABORT_EX("Bad Status-Line: missing status-code");
  207. }
  208. break;
  209. case PREV_STATUS_CODE:
  210. if (util::isCRLF(c)) {
  211. throw DL_ABORT_EX("Bad Status-Line: missing status-code");
  212. }
  213. if (!util::isLws(c)) {
  214. state_ = STATUS_CODE;
  215. i = getToken(buf_, data, length, i);
  216. }
  217. break;
  218. case STATUS_CODE:
  219. if (!util::isLws(c) && !util::isCRLF(c)) {
  220. i = getToken(buf_, data, length, i);
  221. break;
  222. }
  223. {
  224. int statusCode = -1;
  225. if(buf_.size() == 3 && util::isNumber(buf_.begin(), buf_.end())) {
  226. statusCode = (buf_[0]-'0')*100 + (buf_[1]-'0')*10 + (buf_[2]-'0');
  227. }
  228. if (statusCode < 100) {
  229. throw DL_ABORT_EX("Bad status code: bad status-code");
  230. }
  231. result_->setStatusCode(statusCode);
  232. buf_.clear();
  233. }
  234. if (c == '\r') {
  235. state_ = PREV_EOL;
  236. break;
  237. }
  238. if (c == '\n') {
  239. state_ = PREV_FIELD_NAME;
  240. break;
  241. }
  242. state_ = PREV_REASON_PHRASE;
  243. break;
  244. case PREV_REASON_PHRASE:
  245. if (util::isCRLF(c)) {
  246. // The reason-phrase is completely optional.
  247. state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
  248. break;
  249. }
  250. if (util::isLws(c)) {
  251. break;
  252. }
  253. state_ = REASON_PHRASE;
  254. i = getText(buf_, data, length, i);
  255. break;
  256. case REASON_PHRASE:
  257. if (util::isCRLF(c)) {
  258. result_->setReasonPhrase(buf_);
  259. buf_.clear();
  260. state_ = c == '\n' ? PREV_FIELD_NAME : PREV_EOL;
  261. break;
  262. }
  263. i = getText(buf_, data, length, i);
  264. break;
  265. case PREV_EOL:
  266. if (c != '\n') {
  267. throw DL_ABORT_EX("Bad HTTP header: missing LF");
  268. }
  269. state_ = PREV_FIELD_NAME;
  270. break;
  271. case PREV_FIELD_NAME:
  272. if (util::isLws(c)) {
  273. if( lastFieldName_.empty()) {
  274. throw DL_ABORT_EX("Bad HTTP header: field name starts with LWS");
  275. }
  276. // Evil Multi-line header field
  277. state_ = FIELD_VALUE;
  278. break;
  279. }
  280. if (!lastFieldName_.empty()) {
  281. if(lastFieldHdKey_ != HttpHeader::MAX_INTERESTING_HEADER) {
  282. result_->put(lastFieldHdKey_, util::strip(buf_));
  283. }
  284. lastFieldName_.clear();
  285. lastFieldHdKey_ = HttpHeader::MAX_INTERESTING_HEADER;
  286. buf_.clear();
  287. }
  288. if (c == '\n') {
  289. state_ = HEADERS_COMPLETE;
  290. break;
  291. }
  292. if (c == '\r') {
  293. state_ = PREV_EOH;
  294. break;
  295. }
  296. if (c == ':') {
  297. throw DL_ABORT_EX("Bad HTTP header: field name starts with ':'");
  298. }
  299. state_ = FIELD_NAME;
  300. i = getFieldNameToken(lastFieldName_, data, length, i);
  301. break;
  302. case FIELD_NAME:
  303. if (util::isLws(c) || util::isCRLF(c)) {
  304. throw DL_ABORT_EX("Bad HTTP header: missing ':'");
  305. }
  306. if (c == ':') {
  307. util::lowercase(lastFieldName_);
  308. lastFieldHdKey_ = idInterestingHeader(lastFieldName_.c_str());
  309. state_ = PREV_FIELD_VALUE;
  310. break;
  311. }
  312. i = getFieldNameToken(lastFieldName_, data, length, i);
  313. break;
  314. case PREV_FIELD_VALUE:
  315. if (c == '\r') {
  316. state_ = PREV_EOL;
  317. break;
  318. }
  319. if (c == '\n') {
  320. state_ = PREV_FIELD_NAME;
  321. break;
  322. }
  323. if (util::isLws(c)) {
  324. break;
  325. }
  326. state_ = FIELD_VALUE;
  327. if (lastFieldHdKey_ == HttpHeader::MAX_INTERESTING_HEADER) {
  328. i = ignoreText(buf_, data, length, i);
  329. break;
  330. }
  331. i = getText(buf_, data, length, i);
  332. break;
  333. case FIELD_VALUE:
  334. if (c == '\r') {
  335. state_ = PREV_EOL;
  336. break;
  337. }
  338. if (c == '\n') {
  339. state_ = PREV_FIELD_NAME;
  340. break;
  341. }
  342. if (lastFieldHdKey_ == HttpHeader::MAX_INTERESTING_HEADER) {
  343. i = ignoreText(buf_, data, length, i);
  344. break;
  345. }
  346. i = getText(buf_, data, length, i);
  347. break;
  348. case PREV_EOH:
  349. if (c != '\n') {
  350. throw DL_ABORT_EX("Bad HTTP header: "
  351. "missing LF at the end of the header");
  352. }
  353. state_ = HEADERS_COMPLETE;
  354. break;
  355. case HEADERS_COMPLETE:
  356. goto fin;
  357. }
  358. }
  359. fin:
  360. // See Apache's documentation
  361. // http://httpd.apache.org/docs/2.2/en/mod/core.html about size
  362. // limit of HTTP headers. The page states that the number of request
  363. // fields rarely exceeds 20.
  364. if (lastFieldName_.size() > 1024 || buf_.size() > 8192) {
  365. throw DL_ABORT_EX("Too large HTTP header");
  366. }
  367. lastBytesProcessed_ = i;
  368. headers_.append(&data[0], &data[i]);
  369. return state_ == HEADERS_COMPLETE;
  370. }
  371. bool HttpHeaderProcessor::parse(const std::string& data)
  372. {
  373. return parse(reinterpret_cast<const unsigned char*>(data.c_str()),
  374. data.size());
  375. }
  376. size_t HttpHeaderProcessor::getLastBytesProcessed() const
  377. {
  378. return lastBytesProcessed_;
  379. }
  380. void HttpHeaderProcessor::clear()
  381. {
  382. state_ = (mode_ == CLIENT_PARSER ? PREV_RES_VERSION : PREV_METHOD);
  383. lastBytesProcessed_ = 0;
  384. buf_.clear();
  385. lastFieldName_.clear();
  386. lastFieldHdKey_ = HttpHeader::MAX_INTERESTING_HEADER;
  387. result_.reset(new HttpHeader());
  388. headers_.clear();
  389. }
  390. std::unique_ptr<HttpHeader> HttpHeaderProcessor::getResult()
  391. {
  392. return std::move(result_);
  393. }
  394. std::string HttpHeaderProcessor::getHeaderString() const
  395. {
  396. return headers_;
  397. }
  398. } // namespace aria2