HttpHeader.cc 8.2 KB

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