HttpRequest.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "HttpRequest.h"
  36. #include <cassert>
  37. #include <numeric>
  38. #include "Segment.h"
  39. #include "Range.h"
  40. #include "CookieStorage.h"
  41. #include "Option.h"
  42. #include "Util.h"
  43. #include "Base64.h"
  44. #include "prefs.h"
  45. #include "AuthConfigFactory.h"
  46. #include "AuthConfig.h"
  47. #include "a2functional.h"
  48. #include "TimeA2.h"
  49. namespace aria2 {
  50. const std::string HttpRequest::USER_AGENT("aria2");
  51. HttpRequest::HttpRequest():_contentEncodingEnabled(true),
  52. userAgent(USER_AGENT)
  53. {}
  54. void HttpRequest::setSegment(const SharedHandle<Segment>& segment)
  55. {
  56. this->segment = segment;
  57. }
  58. void HttpRequest::setRequest(const SharedHandle<Request>& request)
  59. {
  60. this->request = request;
  61. }
  62. off_t HttpRequest::getStartByte() const
  63. {
  64. if(segment.isNull()) {
  65. return 0;
  66. } else {
  67. return _fileEntry->gtoloff(segment->getPositionToWrite());
  68. }
  69. }
  70. off_t HttpRequest::getEndByte() const
  71. {
  72. if(segment.isNull() || request.isNull()) {
  73. return 0;
  74. } else {
  75. if(request->isPipeliningEnabled()) {
  76. off_t endByte = _fileEntry->gtoloff(segment->getPosition()+segment->getLength()-1);
  77. return std::min(endByte, static_cast<off_t>(_fileEntry->getLength()-1));
  78. } else {
  79. return 0;
  80. }
  81. }
  82. }
  83. RangeHandle HttpRequest::getRange() const
  84. {
  85. // content-length is always 0
  86. if(segment.isNull()) {
  87. return SharedHandle<Range>(new Range());
  88. } else {
  89. return SharedHandle<Range>(new Range(getStartByte(), getEndByte(),
  90. _fileEntry->getLength()));
  91. }
  92. }
  93. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  94. {
  95. if(segment.isNull()) {
  96. return true;
  97. }
  98. if((getStartByte() == range->getStartByte()) &&
  99. ((getEndByte() == 0) ||
  100. (getEndByte() == range->getEndByte())) &&
  101. ((_fileEntry->getLength() == 0) ||
  102. (_fileEntry->getLength() == range->getEntityLength()))) {
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. }
  108. std::string HttpRequest::getHostText(const std::string& host, uint16_t port) const
  109. {
  110. std::string hosttext = host;
  111. if(!(port == 80 || port == 443)) {
  112. strappend(hosttext, ":", Util::uitos(port));
  113. }
  114. return hosttext;
  115. }
  116. std::string HttpRequest::createRequest()
  117. {
  118. _authConfig = _authConfigFactory->createAuthConfig(request);
  119. std::string requestLine = request->getMethod();
  120. requestLine += " ";
  121. if(!_proxyRequest.isNull()) {
  122. if(getProtocol() == Request::PROTO_FTP &&
  123. request->getUsername().empty() && !_authConfig.isNull()) {
  124. // Insert user into URI, like ftp://USER@host/
  125. std::string uri = getCurrentURI();
  126. assert(uri.size() >= 6);
  127. uri.insert(6, Util::urlencode(_authConfig->getUser())+"@");
  128. requestLine += uri;
  129. } else {
  130. requestLine += getCurrentURI();
  131. }
  132. } else {
  133. if(getDir() == A2STR::SLASH_C) {
  134. requestLine += getDir();
  135. } else {
  136. requestLine += getDir();
  137. requestLine += A2STR::SLASH_C;
  138. }
  139. requestLine += getFile();
  140. requestLine += getQuery();
  141. }
  142. requestLine += " HTTP/1.1\r\n";
  143. strappend(requestLine, "User-Agent: ", userAgent, "\r\n");
  144. requestLine += "Accept: */*"; /* */
  145. for(std::deque<std::string>::const_iterator i = _acceptTypes.begin();
  146. i != _acceptTypes.end(); ++i) {
  147. strappend(requestLine, ",", (*i));
  148. }
  149. requestLine += "\r\n";
  150. if(_contentEncodingEnabled) {
  151. std::string acceptableEncodings;
  152. #ifdef HAVE_LIBZ
  153. acceptableEncodings += "deflate, gzip";
  154. #endif // HAVE_LIBZ
  155. if(!acceptableEncodings.empty()) {
  156. strappend(requestLine, "Accept-Encoding: ", acceptableEncodings, "\r\n");
  157. }
  158. }
  159. strappend(requestLine, "Host: ", getHostText(getHost(), getPort()), "\r\n");
  160. requestLine += "Pragma: no-cache\r\n";
  161. requestLine += "Cache-Control: no-cache\r\n";
  162. if(!request->isKeepAliveEnabled() && !request->isPipeliningEnabled()) {
  163. requestLine += "Connection: close\r\n";
  164. }
  165. if(!segment.isNull() && segment->getLength() > 0 &&
  166. (request->isPipeliningEnabled() || getStartByte() > 0)) {
  167. requestLine += "Range: bytes=";
  168. requestLine += Util::itos(getStartByte());
  169. requestLine += "-";
  170. if(request->isPipeliningEnabled()) {
  171. requestLine += Util::itos(getEndByte());
  172. }
  173. requestLine += "\r\n";
  174. }
  175. if(!_proxyRequest.isNull()) {
  176. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  177. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  178. } else {
  179. requestLine += "Proxy-Connection: close\r\n";
  180. }
  181. }
  182. if(!_proxyRequest.isNull() && !_proxyRequest->getUsername().empty()) {
  183. requestLine += getProxyAuthString();
  184. }
  185. if(!_authConfig.isNull()) {
  186. strappend(requestLine, "Authorization: Basic ",
  187. Base64::encode(_authConfig->getAuthText()), "\r\n");
  188. }
  189. if(getPreviousURI().size()) {
  190. strappend(requestLine, "Referer: ", getPreviousURI(), "\r\n");
  191. }
  192. if(!_cookieStorage.isNull()) {
  193. std::string cookiesValue;
  194. std::deque<Cookie> cookies =
  195. _cookieStorage->criteriaFind(getHost(),
  196. getDir(),
  197. Time().getTime(),
  198. getProtocol() == Request::PROTO_HTTPS ?
  199. true : false);
  200. for(std::deque<Cookie>::const_iterator itr = cookies.begin();
  201. itr != cookies.end(); ++itr) {
  202. strappend(cookiesValue, (*itr).toString(), ";");
  203. }
  204. if(!cookiesValue.empty()) {
  205. strappend(requestLine, "Cookie: ", cookiesValue, "\r\n");
  206. }
  207. }
  208. // append additional headers given by user.
  209. for(std::deque<std::string>::const_iterator i = _headers.begin();
  210. i != _headers.end(); ++i) {
  211. strappend(requestLine, (*i), "\r\n");
  212. }
  213. requestLine += "\r\n";
  214. return requestLine;
  215. }
  216. std::string HttpRequest::createProxyRequest() const
  217. {
  218. assert(!_proxyRequest.isNull());
  219. std::string hostport = getHost();
  220. strappend(hostport, ":", Util::uitos(getPort()));
  221. std::string requestLine = "CONNECT ";
  222. strappend(requestLine, hostport, " HTTP/1.1\r\n");
  223. strappend(requestLine, "User-Agent: ", userAgent, "\r\n");
  224. strappend(requestLine, "Host: ", hostport, "\r\n");
  225. // TODO Is "Proxy-Connection" needed here?
  226. // if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  227. // requestLine += "Proxy-Connection: Keep-Alive\r\n";
  228. // }else {
  229. // requestLine += "Proxy-Connection: close\r\n";
  230. // }
  231. if(!_proxyRequest->getUsername().empty()) {
  232. requestLine += getProxyAuthString();
  233. }
  234. requestLine += "\r\n";
  235. return requestLine;
  236. }
  237. std::string HttpRequest::getProxyAuthString() const
  238. {
  239. return strconcat("Proxy-Authorization: Basic ",
  240. Base64::encode(strconcat(_proxyRequest->getUsername(),
  241. ":",
  242. _proxyRequest->getPassword())),
  243. "\r\n");
  244. }
  245. void HttpRequest::enableContentEncoding()
  246. {
  247. _contentEncodingEnabled = true;
  248. }
  249. void HttpRequest::disableContentEncoding()
  250. {
  251. _contentEncodingEnabled = false;
  252. }
  253. void HttpRequest::addHeader(const std::string& headersString)
  254. {
  255. std::deque<std::string> headers;
  256. Util::slice(headers, headersString, '\n', true);
  257. _headers.insert(_headers.end(), headers.begin(), headers.end());
  258. }
  259. void HttpRequest::addAcceptType(const std::string& type)
  260. {
  261. _acceptTypes.push_back(type);
  262. }
  263. void HttpRequest::setCookieStorage
  264. (const SharedHandle<CookieStorage>& cookieStorage)
  265. {
  266. _cookieStorage = cookieStorage;
  267. }
  268. void HttpRequest::setAuthConfigFactory
  269. (const SharedHandle<AuthConfigFactory>& factory)
  270. {
  271. _authConfigFactory = factory;
  272. }
  273. void HttpRequest::setProxyRequest(const SharedHandle<Request>& proxyRequest)
  274. {
  275. _proxyRequest = proxyRequest;
  276. }
  277. bool HttpRequest::isProxyRequestSet() const
  278. {
  279. return !_proxyRequest.isNull();
  280. }
  281. bool HttpRequest::authenticationUsed() const
  282. {
  283. return !_authConfig.isNull();
  284. }
  285. const SharedHandle<AuthConfig>& HttpRequest::getAuthConfig() const
  286. {
  287. return _authConfig;
  288. }
  289. } // namespace aria2