HttpRequest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "Request.h"
  37. #include "Segment.h"
  38. #include "Range.h"
  39. #include "Cookie.h"
  40. #include "CookieBox.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 <numeric>
  49. namespace aria2 {
  50. const std::string HttpRequest::USER_AGENT("aria2");
  51. HttpRequest::HttpRequest():entityLength(0),
  52. authEnabled(false),
  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. std::string requestLine = "GET ";
  125. if(getProtocol() == Request::PROTO_FTP || proxyEnabled) {
  126. requestLine += getCurrentURI();
  127. } else {
  128. if(getDir() == A2STR::SLASH_C) {
  129. requestLine += getDir();
  130. } else {
  131. requestLine += getDir()+A2STR::SLASH_C;
  132. }
  133. requestLine += getFile()+getQuery();
  134. }
  135. requestLine +=
  136. std::string(" HTTP/1.1\r\n")+
  137. "User-Agent: "+userAgent+"\r\n";
  138. requestLine += "Accept: */*"; /* */
  139. for(std::deque<std::string>::const_iterator i = _acceptTypes.begin();
  140. i != _acceptTypes.end(); ++i) {
  141. requestLine += ","+(*i);
  142. }
  143. requestLine += "\r\n";
  144. if(_contentEncodingEnabled) {
  145. std::string acceptableEncodings;
  146. #ifdef HAVE_LIBZ
  147. acceptableEncodings += "deflate, gzip";
  148. #endif // HAVE_LIBZ
  149. if(!acceptableEncodings.empty()) {
  150. requestLine += "Accept-Encoding: "+acceptableEncodings+"\r\n";
  151. }
  152. }
  153. requestLine +=
  154. "Host: "+getHostText(getHost(), getPort())+"\r\n"+
  155. "Pragma: no-cache\r\n"+
  156. "Cache-Control: no-cache\r\n";
  157. if(!request->isKeepAliveEnabled() && !request->isPipeliningEnabled()) {
  158. requestLine += "Connection: close\r\n";
  159. }
  160. if(!segment.isNull() && segment->getLength() > 0 &&
  161. (request->isPipeliningEnabled() || getStartByte() > 0)) {
  162. requestLine += "Range: bytes="+Util::itos(getStartByte());
  163. requestLine += "-";
  164. if(request->isPipeliningEnabled()) {
  165. requestLine += Util::itos(getEndByte());
  166. }
  167. requestLine += "\r\n";
  168. }
  169. if(proxyEnabled) {
  170. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  171. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  172. } else {
  173. requestLine += "Proxy-Connection: close\r\n";
  174. }
  175. }
  176. if(proxyEnabled && proxyAuthEnabled) {
  177. requestLine += getProxyAuthString();
  178. }
  179. if(authEnabled) {
  180. requestLine += "Authorization: Basic "+
  181. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfig(request)->getAuthText())+"\r\n";
  182. }
  183. if(getPreviousURI().size()) {
  184. requestLine += "Referer: "+getPreviousURI()+"\r\n";
  185. }
  186. std::string cookiesValue;
  187. Cookies cookies = request->cookieBox->criteriaFind(getHost(),
  188. getDir(),
  189. time(0),
  190. getProtocol() == Request::PROTO_HTTPS ?
  191. true : false);
  192. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  193. cookiesValue += (*itr).toString()+";";
  194. }
  195. if(cookiesValue.size()) {
  196. requestLine += std::string("Cookie: ")+cookiesValue+"\r\n";
  197. }
  198. // append additional headers given by user.
  199. for(std::deque<std::string>::const_iterator i = _headers.begin();
  200. i != _headers.end(); ++i) {
  201. requestLine += (*i)+"\r\n";
  202. }
  203. requestLine += "\r\n";
  204. return requestLine;
  205. }
  206. std::string HttpRequest::createProxyRequest() const
  207. {
  208. std::string requestLine =
  209. std::string("CONNECT ")+getHost()+":"+Util::uitos(getPort())+
  210. std::string(" HTTP/1.1\r\n")+
  211. "User-Agent: "+userAgent+"\r\n"+
  212. "Host: "+getHost()+":"+Util::uitos(getPort())+"\r\n";
  213. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  214. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  215. }else {
  216. requestLine += "Proxy-Connection: close\r\n";
  217. }
  218. if(proxyAuthEnabled) {
  219. requestLine += getProxyAuthString();
  220. }
  221. requestLine += "\r\n";
  222. return requestLine;
  223. }
  224. std::string HttpRequest::getProxyAuthString() const {
  225. return "Proxy-Authorization: Basic "+
  226. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfigForHttpProxy(request)->getAuthText())+"\r\n";
  227. }
  228. void HttpRequest::enableContentEncoding()
  229. {
  230. _contentEncodingEnabled = true;
  231. }
  232. void HttpRequest::disableContentEncoding()
  233. {
  234. _contentEncodingEnabled = false;
  235. }
  236. void HttpRequest::addHeader(const std::string& headersString)
  237. {
  238. std::deque<std::string> headers;
  239. Util::slice(headers, headersString, '\n', true);
  240. _headers.insert(_headers.end(), headers.begin(), headers.end());
  241. }
  242. void HttpRequest::addAcceptType(const std::string& type)
  243. {
  244. _acceptTypes.push_back(type);
  245. }
  246. void HttpRequest::configure(const Option* option)
  247. {
  248. authEnabled = option->getAsBool(PREF_HTTP_AUTH_ENABLED);
  249. proxyEnabled =
  250. option->getAsBool(PREF_HTTP_PROXY_ENABLED) &&
  251. option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  252. proxyAuthEnabled = option->getAsBool(PREF_HTTP_PROXY_AUTH_ENABLED);
  253. }
  254. const std::string& HttpRequest::getPreviousURI() const
  255. {
  256. return request->getPreviousUrl();
  257. }
  258. const std::string& HttpRequest::getHost() const
  259. {
  260. return request->getHost();
  261. }
  262. uint16_t HttpRequest::getPort() const
  263. {
  264. return request->getPort();
  265. }
  266. const std::string& HttpRequest::getMethod() const
  267. {
  268. return request->getMethod();
  269. }
  270. const std::string& HttpRequest::getProtocol() const
  271. {
  272. return request->getProtocol();
  273. }
  274. const std::string& HttpRequest::getCurrentURI() const
  275. {
  276. return request->getCurrentUrl();
  277. }
  278. const std::string& HttpRequest::getDir() const
  279. {
  280. return request->getDir();
  281. }
  282. const std::string& HttpRequest::getFile() const
  283. {
  284. return request->getFile();
  285. }
  286. const std::string& HttpRequest::getQuery() const
  287. {
  288. return request->getQuery();
  289. }
  290. } // namespace aria2