HttpHeader.cc 8.2 KB

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