HttpHeader.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "HttpHeader.h"
  36. #include "Range.h"
  37. #include "util.h"
  38. #include "A2STR.h"
  39. #include "DownloadFailureException.h"
  40. #include "array_fun.h"
  41. namespace aria2 {
  42. HttpHeader::HttpHeader() : statusCode_(0) {}
  43. HttpHeader::~HttpHeader() = default;
  44. void HttpHeader::put(int hdKey, const std::string& value)
  45. {
  46. std::multimap<int, std::string>::value_type vt(hdKey, value);
  47. table_.insert(vt);
  48. }
  49. void HttpHeader::remove(int hdKey) { table_.erase(hdKey); }
  50. bool HttpHeader::defined(int hdKey) const { return table_.count(hdKey); }
  51. const std::string& HttpHeader::find(int hdKey) const
  52. {
  53. auto itr = table_.find(hdKey);
  54. if (itr == table_.end()) {
  55. return A2STR::NIL;
  56. }
  57. else {
  58. return (*itr).second;
  59. }
  60. }
  61. std::vector<std::string> HttpHeader::findAll(int hdKey) const
  62. {
  63. std::vector<std::string> v;
  64. auto itrpair = table_.equal_range(hdKey);
  65. while (itrpair.first != itrpair.second) {
  66. v.push_back((*itrpair.first).second);
  67. ++itrpair.first;
  68. }
  69. return v;
  70. }
  71. std::pair<std::multimap<int, std::string>::const_iterator,
  72. std::multimap<int, std::string>::const_iterator>
  73. HttpHeader::equalRange(int hdKey) const
  74. {
  75. return table_.equal_range(hdKey);
  76. }
  77. Range HttpHeader::getRange() const
  78. {
  79. const auto& rangeStr = find(CONTENT_RANGE);
  80. if (rangeStr.empty()) {
  81. const std::string& clenStr = find(CONTENT_LENGTH);
  82. if (clenStr.empty()) {
  83. return Range();
  84. }
  85. else {
  86. int64_t contentLength;
  87. if (!util::parseLLIntNoThrow(contentLength, clenStr) ||
  88. contentLength < 0) {
  89. throw DL_ABORT_EX("Content-Length must be positive integer");
  90. }
  91. else if (contentLength > std::numeric_limits<a2_off_t>::max()) {
  92. throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, contentLength));
  93. }
  94. else if (contentLength == 0) {
  95. return Range();
  96. }
  97. else {
  98. return Range(0, contentLength - 1, contentLength);
  99. }
  100. }
  101. }
  102. // we expect that rangeStr looks like 'bytes 100-199/200' but some
  103. // server returns '100-199/200', omitting bytes-unit specifier
  104. // 'bytes'. Moreover, some server may return like
  105. // 'bytes=100-199/200'.
  106. auto byteRangeSpec = std::find(rangeStr.begin(), rangeStr.end(), ' ');
  107. if (byteRangeSpec == rangeStr.end()) {
  108. // check for 'bytes=100-199/200' case
  109. byteRangeSpec = std::find(rangeStr.begin(), rangeStr.end(), '=');
  110. if (byteRangeSpec == rangeStr.end()) {
  111. // we assume bytes-unit specifier omitted.
  112. byteRangeSpec = rangeStr.begin();
  113. }
  114. else {
  115. ++byteRangeSpec;
  116. }
  117. }
  118. else {
  119. while (byteRangeSpec != rangeStr.end() &&
  120. (*byteRangeSpec == ' ' || *byteRangeSpec == '\t')) {
  121. ++byteRangeSpec;
  122. }
  123. }
  124. auto slash = std::find(byteRangeSpec, rangeStr.end(), '/');
  125. if (slash == rangeStr.end() || slash + 1 == rangeStr.end() ||
  126. (byteRangeSpec + 1 == slash && *byteRangeSpec == '*') ||
  127. (slash + 2 == rangeStr.end() && *(slash + 1) == '*')) {
  128. // If byte-range-resp-spec or instance-length is "*", we returns
  129. // empty Range. The former is usually sent with 416 (Request range
  130. // not satisfiable) status.
  131. return Range();
  132. }
  133. auto minus = std::find(byteRangeSpec, slash, '-');
  134. if (minus == slash) {
  135. return Range();
  136. }
  137. int64_t startByte, endByte, entityLength;
  138. if (!util::parseLLIntNoThrow(startByte, std::string(byteRangeSpec, minus)) ||
  139. !util::parseLLIntNoThrow(endByte, std::string(minus + 1, slash)) ||
  140. !util::parseLLIntNoThrow(entityLength,
  141. std::string(slash + 1, rangeStr.end())) ||
  142. startByte < 0 || endByte < 0 || entityLength < 0) {
  143. throw DL_ABORT_EX("byte-range-spec must be positive");
  144. }
  145. if (startByte > std::numeric_limits<a2_off_t>::max()) {
  146. throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, startByte));
  147. }
  148. if (endByte > std::numeric_limits<a2_off_t>::max()) {
  149. throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, endByte));
  150. }
  151. if (entityLength > std::numeric_limits<a2_off_t>::max()) {
  152. throw DOWNLOAD_FAILURE_EXCEPTION(fmt(EX_TOO_LARGE_FILE, entityLength));
  153. }
  154. return Range(startByte, endByte, entityLength);
  155. }
  156. void HttpHeader::setVersion(const std::string& version) { version_ = version; }
  157. void HttpHeader::setMethod(const std::string& method) { method_ = method; }
  158. void HttpHeader::setRequestPath(const std::string& requestPath)
  159. {
  160. requestPath_ = requestPath;
  161. }
  162. void HttpHeader::clearField() { table_.clear(); }
  163. int HttpHeader::getStatusCode() const { return statusCode_; }
  164. void HttpHeader::setStatusCode(int code) { statusCode_ = code; }
  165. const std::string& HttpHeader::getVersion() const { return version_; }
  166. const std::string& HttpHeader::getMethod() const { return method_; }
  167. const std::string& HttpHeader::getRequestPath() const { return requestPath_; }
  168. const std::string& HttpHeader::getReasonPhrase() const { return reasonPhrase_; }
  169. void HttpHeader::setReasonPhrase(const std::string& reasonPhrase)
  170. {
  171. reasonPhrase_ = reasonPhrase;
  172. }
  173. bool HttpHeader::fieldContains(int hdKey, const char* value)
  174. {
  175. std::pair<std::multimap<int, std::string>::const_iterator,
  176. std::multimap<int, std::string>::const_iterator>
  177. range = equalRange(hdKey);
  178. for (auto i = range.first; i != range.second; ++i) {
  179. std::vector<Scip> values;
  180. util::splitIter((*i).second.begin(), (*i).second.end(),
  181. std::back_inserter(values), ',',
  182. true // doStrip
  183. );
  184. for (const auto& v : values) {
  185. if (util::strieq(v.first, v.second, value)) {
  186. return true;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. bool HttpHeader::isKeepAlive() const
  193. {
  194. const std::string& connection = find(CONNECTION);
  195. return !util::strieq(connection, "close") &&
  196. (version_ == "HTTP/1.1" || util::strieq(connection, "keep-alive"));
  197. }
  198. namespace {
  199. const char* INTERESTING_HEADER_NAMES[] = {
  200. "accept-encoding",
  201. "access-control-request-headers",
  202. "access-control-request-method",
  203. "authorization",
  204. "connection",
  205. "content-disposition",
  206. "content-encoding",
  207. "content-length",
  208. "content-range",
  209. "content-type",
  210. "digest",
  211. "infohash",
  212. "last-modified",
  213. "link",
  214. "location",
  215. "origin",
  216. "port",
  217. "retry-after",
  218. "sec-websocket-key",
  219. "sec-websocket-version",
  220. "set-cookie",
  221. "transfer-encoding",
  222. "upgrade",
  223. };
  224. } // namespace
  225. int idInterestingHeader(const char* hdName)
  226. {
  227. const char** i = std::lower_bound(std::begin(INTERESTING_HEADER_NAMES),
  228. std::end(INTERESTING_HEADER_NAMES), hdName,
  229. util::strless);
  230. if (i != std::end(INTERESTING_HEADER_NAMES) && strcmp(*i, hdName) == 0) {
  231. return i - std::begin(INTERESTING_HEADER_NAMES);
  232. }
  233. else {
  234. return HttpHeader::MAX_INTERESTING_HEADER;
  235. }
  236. }
  237. } // namespace aria2