HttpRequest.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "Request.h"
  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():entityLength(0),
  53. proxyEnabled(false),
  54. proxyAuthEnabled(false),
  55. _contentEncodingEnabled(true),
  56. userAgent(USER_AGENT)
  57. {}
  58. SharedHandle<Segment> HttpRequest::getSegment() const
  59. {
  60. return segment;
  61. }
  62. void HttpRequest::setSegment(const SharedHandle<Segment>& segment)
  63. {
  64. this->segment = segment;
  65. }
  66. void HttpRequest::setRequest(const SharedHandle<Request>& request)
  67. {
  68. this->request = request;
  69. }
  70. SharedHandle<Request> HttpRequest::getRequest() const
  71. {
  72. return request;
  73. }
  74. off_t HttpRequest::getStartByte() const
  75. {
  76. if(segment.isNull()) {
  77. return 0;
  78. } else {
  79. return segment->getPositionToWrite();
  80. }
  81. }
  82. off_t HttpRequest::getEndByte() const
  83. {
  84. if(segment.isNull() || request.isNull()) {
  85. return 0;
  86. } else {
  87. if(request->isPipeliningEnabled()) {
  88. return segment->getPosition()+segment->getLength()-1;
  89. } else {
  90. return 0;
  91. }
  92. }
  93. }
  94. RangeHandle HttpRequest::getRange() const
  95. {
  96. // content-length is always 0
  97. if(segment.isNull()) {
  98. return SharedHandle<Range>(new Range());
  99. } else {
  100. return SharedHandle<Range>(new Range(getStartByte(), getEndByte(), entityLength));
  101. }
  102. }
  103. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  104. {
  105. if(segment.isNull()) {
  106. return true;
  107. }
  108. if((getStartByte() == range->getStartByte()) &&
  109. ((getEndByte() == 0) ||
  110. ((getEndByte() > 0) && (getEndByte() == range->getEndByte()))) &&
  111. ((entityLength == 0) ||
  112. ((entityLength > 0) && (entityLength == range->getEntityLength())))) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. std::string HttpRequest::getHostText(const std::string& host, uint16_t port) const
  119. {
  120. return host+(port == 80 || port == 443 ? "" : ":"+Util::uitos(port));
  121. }
  122. std::string HttpRequest::createRequest() const
  123. {
  124. SharedHandle<AuthConfig> authConfig =
  125. _authConfigFactory->createAuthConfig(request);
  126. std::string requestLine = "GET ";
  127. if(getProtocol() == Request::PROTO_FTP || proxyEnabled) {
  128. if(getProtocol() == Request::PROTO_FTP &&
  129. request->getUsername().empty() && !authConfig->getUser().empty()) {
  130. // Insert user into URI, like ftp://USER@host/
  131. std::string uri = getCurrentURI();
  132. assert(uri.size() >= 6);
  133. uri.insert(6, Util::urlencode(authConfig->getUser())+"@");
  134. requestLine += uri;
  135. } else {
  136. requestLine += getCurrentURI();
  137. }
  138. } else {
  139. if(getDir() == A2STR::SLASH_C) {
  140. requestLine += getDir();
  141. } else {
  142. requestLine += getDir()+A2STR::SLASH_C;
  143. }
  144. requestLine += getFile()+getQuery();
  145. }
  146. requestLine +=
  147. std::string(" HTTP/1.1\r\n")+
  148. "User-Agent: "+userAgent+"\r\n";
  149. requestLine += "Accept: */*"; /* */
  150. for(std::deque<std::string>::const_iterator i = _acceptTypes.begin();
  151. i != _acceptTypes.end(); ++i) {
  152. requestLine += ","+(*i);
  153. }
  154. requestLine += "\r\n";
  155. if(_contentEncodingEnabled) {
  156. std::string acceptableEncodings;
  157. #ifdef HAVE_LIBZ
  158. acceptableEncodings += "deflate, gzip";
  159. #endif // HAVE_LIBZ
  160. if(!acceptableEncodings.empty()) {
  161. requestLine += "Accept-Encoding: "+acceptableEncodings+"\r\n";
  162. }
  163. }
  164. requestLine +=
  165. "Host: "+getHostText(getHost(), getPort())+"\r\n"+
  166. "Pragma: no-cache\r\n"+
  167. "Cache-Control: no-cache\r\n";
  168. if(!request->isKeepAliveEnabled() && !request->isPipeliningEnabled()) {
  169. requestLine += "Connection: close\r\n";
  170. }
  171. if(!segment.isNull() && segment->getLength() > 0 &&
  172. (request->isPipeliningEnabled() || getStartByte() > 0)) {
  173. requestLine += "Range: bytes="+Util::itos(getStartByte());
  174. requestLine += "-";
  175. if(request->isPipeliningEnabled()) {
  176. requestLine += Util::itos(getEndByte());
  177. }
  178. requestLine += "\r\n";
  179. }
  180. if(proxyEnabled) {
  181. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  182. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  183. } else {
  184. requestLine += "Proxy-Connection: close\r\n";
  185. }
  186. }
  187. if(proxyEnabled && proxyAuthEnabled) {
  188. requestLine += getProxyAuthString();
  189. }
  190. if(!authConfig->getUser().empty()) {
  191. requestLine += "Authorization: Basic "+
  192. Base64::encode(authConfig->getAuthText())+"\r\n";
  193. }
  194. if(getPreviousURI().size()) {
  195. requestLine += "Referer: "+getPreviousURI()+"\r\n";
  196. }
  197. if(!_cookieStorage.isNull()) {
  198. std::string cookiesValue;
  199. std::deque<Cookie> cookies =
  200. _cookieStorage->criteriaFind(getHost(),
  201. getDir(),
  202. Time().getTime(),
  203. getProtocol() == Request::PROTO_HTTPS ?
  204. true : false);
  205. for(std::deque<Cookie>::const_iterator itr = cookies.begin();
  206. itr != cookies.end(); ++itr) {
  207. cookiesValue += (*itr).toString()+";";
  208. }
  209. if(!cookiesValue.empty()) {
  210. requestLine += std::string("Cookie: ")+cookiesValue+"\r\n";
  211. }
  212. }
  213. // append additional headers given by user.
  214. for(std::deque<std::string>::const_iterator i = _headers.begin();
  215. i != _headers.end(); ++i) {
  216. requestLine += (*i)+"\r\n";
  217. }
  218. requestLine += "\r\n";
  219. return requestLine;
  220. }
  221. std::string HttpRequest::createProxyRequest() const
  222. {
  223. std::string requestLine =
  224. std::string("CONNECT ")+getHost()+":"+Util::uitos(getPort())+
  225. std::string(" HTTP/1.1\r\n")+
  226. "User-Agent: "+userAgent+"\r\n"+
  227. "Host: "+getHost()+":"+Util::uitos(getPort())+"\r\n";
  228. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  229. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  230. }else {
  231. requestLine += "Proxy-Connection: close\r\n";
  232. }
  233. if(proxyAuthEnabled) {
  234. requestLine += getProxyAuthString();
  235. }
  236. requestLine += "\r\n";
  237. return requestLine;
  238. }
  239. std::string HttpRequest::getProxyAuthString() const
  240. {
  241. return "Proxy-Authorization: Basic "+
  242. Base64::encode(_authConfigFactory->createAuthConfigForHttpProxy(request)->
  243. getAuthText())+"\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::configure(const Option* option)
  264. {
  265. proxyEnabled =
  266. option->getAsBool(PREF_HTTP_PROXY_ENABLED) &&
  267. option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  268. proxyAuthEnabled = option->getAsBool(PREF_HTTP_PROXY_AUTH_ENABLED);
  269. }
  270. const std::string& HttpRequest::getPreviousURI() const
  271. {
  272. return request->getPreviousUrl();
  273. }
  274. const std::string& HttpRequest::getHost() const
  275. {
  276. return request->getHost();
  277. }
  278. uint16_t HttpRequest::getPort() const
  279. {
  280. return request->getPort();
  281. }
  282. const std::string& HttpRequest::getMethod() const
  283. {
  284. return request->getMethod();
  285. }
  286. const std::string& HttpRequest::getProtocol() const
  287. {
  288. return request->getProtocol();
  289. }
  290. const std::string& HttpRequest::getCurrentURI() const
  291. {
  292. return request->getCurrentUrl();
  293. }
  294. const std::string& HttpRequest::getDir() const
  295. {
  296. return request->getDir();
  297. }
  298. const std::string& HttpRequest::getFile() const
  299. {
  300. return request->getFile();
  301. }
  302. const std::string& HttpRequest::getQuery() const
  303. {
  304. return request->getQuery();
  305. }
  306. void HttpRequest::setCookieStorage
  307. (const SharedHandle<CookieStorage>& cookieStorage)
  308. {
  309. _cookieStorage = cookieStorage;
  310. }
  311. SharedHandle<CookieStorage> HttpRequest::getCookieStorage() const
  312. {
  313. return _cookieStorage;
  314. }
  315. void HttpRequest::setAuthConfigFactory
  316. (const SharedHandle<AuthConfigFactory>& factory)
  317. {
  318. _authConfigFactory = factory;
  319. }
  320. } // namespace aria2