HttpRequest.cc 9.1 KB

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