HttpHeader.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <istream>
  37. #include "Range.h"
  38. #include "util.h"
  39. #include "A2STR.h"
  40. namespace aria2 {
  41. const std::string HttpHeader::LOCATION("Location");
  42. const std::string HttpHeader::TRANSFER_ENCODING("Transfer-Encoding");
  43. const std::string HttpHeader::CONTENT_ENCODING("Content-Encoding");
  44. const std::string HttpHeader::CONTENT_DISPOSITION("Content-Disposition");
  45. const std::string HttpHeader::SET_COOKIE("Set-Cookie");
  46. const std::string HttpHeader::CHUNKED("chunked");
  47. const std::string HttpHeader::GZIP("gzip");
  48. const std::string HttpHeader::DEFLATE("deflate");
  49. const std::string HttpHeader::CONTENT_TYPE("Content-Type");
  50. const std::string HttpHeader::RETRY_AFTER("Retry-After");
  51. const std::string HttpHeader::CONNECTION("Connection");
  52. const std::string HttpHeader::CLOSE("close");
  53. const std::string HttpHeader::CONTENT_LENGTH("Content-Length");
  54. const std::string HttpHeader::CONTENT_RANGE("Content-Range");
  55. const std::string HttpHeader::LAST_MODIFIED("Last-Modified");
  56. const std::string HttpHeader::ACCEPT_ENCODING("Accept-Encoding");
  57. const std::string HttpHeader::HTTP_1_1("HTTP/1.1");
  58. const std::string HttpHeader::S200("200");
  59. const std::string HttpHeader::S206("206");
  60. const std::string HttpHeader::S300("300");
  61. const std::string HttpHeader::S301("301");
  62. const std::string HttpHeader::S302("302");
  63. const std::string HttpHeader::S303("303");
  64. const std::string HttpHeader::S304("304");
  65. const std::string HttpHeader::S307("307");
  66. const std::string HttpHeader::S400("400");
  67. const std::string HttpHeader::S401("401");
  68. const std::string HttpHeader::S404("404");
  69. void HttpHeader::put(const std::string& name, const std::string& value) {
  70. std::multimap<std::string, std::string>::value_type vt
  71. (util::toLower(name), value);
  72. table_.insert(vt);
  73. }
  74. bool HttpHeader::defined(const std::string& name) const {
  75. return table_.count(util::toLower(name)) >= 1;
  76. }
  77. const std::string& HttpHeader::getFirst(const std::string& name) const {
  78. std::multimap<std::string, std::string>::const_iterator itr =
  79. table_.find(util::toLower(name));
  80. if(itr == table_.end()) {
  81. return A2STR::NIL;
  82. } else {
  83. return (*itr).second;
  84. }
  85. }
  86. std::vector<std::string> HttpHeader::get(const std::string& name) const
  87. {
  88. std::vector<std::string> v;
  89. std::string n(util::toLower(name));
  90. std::pair<std::multimap<std::string, std::string>::const_iterator,
  91. std::multimap<std::string, std::string>::const_iterator> itrpair =
  92. table_.equal_range(n);
  93. std::multimap<std::string, std::string>::const_iterator first = itrpair.first;
  94. while(first != itrpair.second) {
  95. v.push_back((*first).second);
  96. ++first;
  97. }
  98. return v;
  99. }
  100. unsigned int HttpHeader::getFirstAsUInt(const std::string& name) const {
  101. return getFirstAsULLInt(name);
  102. }
  103. uint64_t HttpHeader::getFirstAsULLInt(const std::string& name) const {
  104. const std::string& value = getFirst(name);
  105. if(value.empty()) {
  106. return 0;
  107. } else {
  108. return util::parseULLInt(value);
  109. }
  110. }
  111. RangeHandle HttpHeader::getRange() const
  112. {
  113. const std::string& rangeStr = getFirst(CONTENT_RANGE);
  114. if(rangeStr.empty()) {
  115. const std::string& contentLengthStr = getFirst(CONTENT_LENGTH);
  116. if(contentLengthStr.empty()) {
  117. return SharedHandle<Range>(new Range());
  118. } else {
  119. uint64_t contentLength = util::parseULLInt(contentLengthStr);
  120. if(contentLength == 0) {
  121. return SharedHandle<Range>(new Range());
  122. } else {
  123. return SharedHandle<Range>
  124. (new Range(0, contentLength-1, contentLength));
  125. }
  126. }
  127. }
  128. std::string byteRangeSpec;
  129. {
  130. // we expect that rangeStr looks like 'bytes 100-199/100'
  131. // but some server returns '100-199/100', omitting bytes-unit sepcifier
  132. // 'bytes'.
  133. std::pair<std::string, std::string> splist;
  134. util::divide(splist, rangeStr, ' ');
  135. if(splist.second.empty()) {
  136. // we assume bytes-unit specifier omitted.
  137. byteRangeSpec = splist.first;
  138. } else {
  139. byteRangeSpec = splist.second;
  140. }
  141. }
  142. std::pair<std::string, std::string> byteRangeSpecPair;
  143. util::divide(byteRangeSpecPair, byteRangeSpec, '/');
  144. if(util::strip(byteRangeSpecPair.first) == "*" ||
  145. util::strip(byteRangeSpecPair.second) == "*") {
  146. // If byte-range-resp-spec or instance-length is "*", we returns
  147. // empty Range. The former is usually sent with 416 (Request range
  148. // not satisfiable) status.
  149. return SharedHandle<Range>(new Range());
  150. }
  151. std::pair<std::string, std::string> byteRangeRespSpecPair;
  152. util::divide(byteRangeRespSpecPair, byteRangeSpecPair.first, '-');
  153. off_t startByte = util::parseLLInt(byteRangeRespSpecPair.first);
  154. off_t endByte = util::parseLLInt(byteRangeRespSpecPair.second);
  155. uint64_t entityLength = util::parseULLInt(byteRangeSpecPair.second);
  156. return SharedHandle<Range>(new Range(startByte, endByte, entityLength));
  157. }
  158. void HttpHeader::setResponseStatus(const std::string& responseStatus)
  159. {
  160. responseStatus_ = responseStatus;
  161. }
  162. void HttpHeader::setVersion(const std::string& version)
  163. {
  164. version_ = version;
  165. }
  166. void HttpHeader::setMethod(const std::string& method)
  167. {
  168. method_ = method;
  169. }
  170. void HttpHeader::setRequestPath(const std::string& requestPath)
  171. {
  172. requestPath_ = requestPath;
  173. }
  174. void HttpHeader::fill(std::istream& in)
  175. {
  176. std::string line;
  177. while(std::getline(in, line)) {
  178. line = util::strip(line);
  179. if(line.empty()) {
  180. continue;
  181. }
  182. std::pair<std::string, std::string> hp;
  183. util::divide(hp, line, ':');
  184. put(hp.first, hp.second);
  185. }
  186. }
  187. void HttpHeader::clearField()
  188. {
  189. table_.clear();
  190. }
  191. } // namespace aria2