HttpHeader.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <istream>
  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::HTTP_1_1("HTTP/1.1");
  57. const std::string HttpHeader::S200("200");
  58. const std::string HttpHeader::S300("300");
  59. const std::string HttpHeader::S400("400");
  60. const std::string HttpHeader::S401("401");
  61. const std::string HttpHeader::S404("404");
  62. void HttpHeader::put(const std::string& name, const std::string& value) {
  63. std::multimap<std::string, std::string>::value_type vt(Util::toLower(name), value);
  64. table.insert(vt);
  65. }
  66. bool HttpHeader::defined(const std::string& name) const {
  67. return table.count(Util::toLower(name)) >= 1;
  68. }
  69. const std::string& HttpHeader::getFirst(const std::string& name) const {
  70. std::multimap<std::string, std::string>::const_iterator itr = table.find(Util::toLower(name));
  71. if(itr == table.end()) {
  72. return A2STR::NIL;
  73. } else {
  74. return (*itr).second;
  75. }
  76. }
  77. std::deque<std::string> HttpHeader::get(const std::string& name) const {
  78. std::deque<std::string> v;
  79. std::string n(Util::toLower(name));
  80. std::multimap<std::string, std::string>::const_iterator first =
  81. table.lower_bound(n);
  82. std::multimap<std::string, std::string>::const_iterator last =
  83. table.upper_bound(n);
  84. while(first != last) {
  85. v.push_back((*first).second);
  86. ++first;
  87. }
  88. return v;
  89. }
  90. unsigned int HttpHeader::getFirstAsUInt(const std::string& name) const {
  91. return getFirstAsULLInt(name);
  92. }
  93. uint64_t HttpHeader::getFirstAsULLInt(const std::string& name) const {
  94. const std::string& value = getFirst(name);
  95. if(value.empty()) {
  96. return 0;
  97. } else {
  98. return Util::parseULLInt(value);
  99. }
  100. }
  101. RangeHandle HttpHeader::getRange() const
  102. {
  103. const std::string& rangeStr = getFirst(CONTENT_RANGE);
  104. if(rangeStr.empty()) {
  105. const std::string& contentLengthStr = getFirst(CONTENT_LENGTH);
  106. if(contentLengthStr.empty()) {
  107. return SharedHandle<Range>(new Range());
  108. } else {
  109. uint64_t contentLength = Util::parseULLInt(contentLengthStr);
  110. if(contentLength == 0) {
  111. return SharedHandle<Range>(new Range());
  112. } else {
  113. return SharedHandle<Range>(new Range(0, contentLength-1, contentLength));
  114. }
  115. }
  116. }
  117. std::string byteRangeSpec;
  118. {
  119. // we expect that rangeStr looks like 'bytes 100-199/100'
  120. // but some server returns '100-199/100', omitting bytes-unit sepcifier
  121. // 'bytes'.
  122. std::pair<std::string, std::string> splist;
  123. Util::split(splist, rangeStr, ' ');
  124. if(splist.second.empty()) {
  125. // we assume bytes-unit specifier omitted.
  126. byteRangeSpec = splist.first;
  127. } else {
  128. byteRangeSpec = splist.second;
  129. }
  130. }
  131. std::pair<std::string, std::string> byteRangeSpecPair;
  132. Util::split(byteRangeSpecPair, byteRangeSpec, '/');
  133. std::pair<std::string, std::string> byteRangeRespSpecPair;
  134. Util::split(byteRangeRespSpecPair, byteRangeSpecPair.first, '-');
  135. off_t startByte = Util::parseLLInt(byteRangeRespSpecPair.first);
  136. off_t endByte = Util::parseLLInt(byteRangeRespSpecPair.second);
  137. uint64_t entityLength = Util::parseULLInt(byteRangeSpecPair.second);
  138. return SharedHandle<Range>(new Range(startByte, endByte, entityLength));
  139. }
  140. const std::string& HttpHeader::getResponseStatus() const
  141. {
  142. return _responseStatus;
  143. }
  144. void HttpHeader::setResponseStatus(const std::string& responseStatus)
  145. {
  146. _responseStatus = responseStatus;
  147. }
  148. const std::string& HttpHeader::getVersion() const
  149. {
  150. return _version;
  151. }
  152. void HttpHeader::setVersion(const std::string& version)
  153. {
  154. _version = version;
  155. }
  156. void HttpHeader::fill(std::istream& in)
  157. {
  158. std::string line;
  159. while(std::getline(in, line)) {
  160. line = Util::trim(line);
  161. if(line.empty()) {
  162. break;
  163. }
  164. std::pair<std::string, std::string> hp;
  165. Util::split(hp, line, ':');
  166. put(hp.first, hp.second);
  167. }
  168. }
  169. void HttpHeader::clearField()
  170. {
  171. table.clear();
  172. }
  173. } // namespace aria2