HttpRequest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <numeric>
  48. namespace aria2 {
  49. std::string HttpRequest::USER_AGENT = "aria2";
  50. HttpRequest::HttpRequest():entityLength(0),
  51. authEnabled(false),
  52. proxyEnabled(false),
  53. proxyAuthEnabled(false),
  54. userAgent(USER_AGENT)
  55. {}
  56. SharedHandle<Segment> HttpRequest::getSegment() const
  57. {
  58. return segment;
  59. }
  60. void HttpRequest::setSegment(const SharedHandle<Segment>& segment)
  61. {
  62. this->segment = segment;
  63. }
  64. void HttpRequest::setRequest(const SharedHandle<Request>& request)
  65. {
  66. this->request = request;
  67. }
  68. SharedHandle<Request> HttpRequest::getRequest() const
  69. {
  70. return request;
  71. }
  72. off_t HttpRequest::getStartByte() const
  73. {
  74. if(segment.isNull()) {
  75. return 0;
  76. } else {
  77. return segment->getPositionToWrite();
  78. }
  79. }
  80. off_t HttpRequest::getEndByte() const
  81. {
  82. if(segment.isNull() || request.isNull()) {
  83. return 0;
  84. } else {
  85. if(request->isPipeliningEnabled()) {
  86. return segment->getPosition()+segment->getLength()-1;
  87. } else {
  88. return 0;
  89. }
  90. }
  91. }
  92. RangeHandle HttpRequest::getRange() const
  93. {
  94. // content-length is always 0
  95. if(segment.isNull()) {
  96. return SharedHandle<Range>(new Range());
  97. } else {
  98. return SharedHandle<Range>(new Range(getStartByte(), getEndByte(), entityLength));
  99. }
  100. }
  101. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  102. {
  103. if(segment.isNull()) {
  104. return true;
  105. }
  106. if((getStartByte() == range->getStartByte()) &&
  107. ((getEndByte() == 0) ||
  108. ((getEndByte() > 0) && (getEndByte() == range->getEndByte()))) &&
  109. ((entityLength == 0) ||
  110. ((entityLength > 0) && (entityLength == range->getEntityLength())))) {
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. std::string HttpRequest::getHostText(const std::string& host, uint16_t port) const
  117. {
  118. return host+(port == 80 || port == 443 ? "" : ":"+Util::uitos(port));
  119. }
  120. std::string HttpRequest::createRequest() const
  121. {
  122. std::string requestLine = "GET ";
  123. if(getProtocol() == "ftp" || proxyEnabled) {
  124. requestLine += getCurrentURI();
  125. } else {
  126. if(getDir() == "/") {
  127. requestLine += getDir();
  128. } else {
  129. requestLine += getDir()+"/";
  130. }
  131. requestLine += getFile();
  132. }
  133. requestLine +=
  134. std::string(" HTTP/1.1\r\n")+
  135. "User-Agent: "+userAgent+"\r\n"+
  136. "Accept: */*\r\n"+ /* */
  137. "Host: "+getHostText(getHost(), getPort())+"\r\n"+
  138. "Pragma: no-cache\r\n"+
  139. "Cache-Control: no-cache\r\n";
  140. if(!request->isKeepAliveEnabled() && !request->isPipeliningEnabled()) {
  141. requestLine += "Connection: close\r\n";
  142. }
  143. if(!segment.isNull() && segment->getLength() > 0 &&
  144. (request->isPipeliningEnabled() || getStartByte() > 0)) {
  145. requestLine += "Range: bytes="+Util::itos(getStartByte());
  146. requestLine += "-";
  147. if(request->isPipeliningEnabled()) {
  148. requestLine += Util::itos(getEndByte());
  149. }
  150. requestLine += "\r\n";
  151. }
  152. if(proxyEnabled) {
  153. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  154. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  155. } else {
  156. requestLine += "Proxy-Connection: close\r\n";
  157. }
  158. }
  159. if(proxyEnabled && proxyAuthEnabled) {
  160. requestLine += getProxyAuthString();
  161. }
  162. if(authEnabled) {
  163. requestLine += "Authorization: Basic "+
  164. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfig(request)->getAuthText())+"\r\n";
  165. }
  166. if(getPreviousURI().size()) {
  167. requestLine += "Referer: "+getPreviousURI()+"\r\n";
  168. }
  169. std::string cookiesValue;
  170. Cookies cookies = request->cookieBox->criteriaFind(getHost(),
  171. getDir(),
  172. time(0),
  173. getProtocol() == "https" ?
  174. true : false);
  175. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  176. cookiesValue += (*itr).toString()+";";
  177. }
  178. if(cookiesValue.size()) {
  179. requestLine += std::string("Cookie: ")+cookiesValue+"\r\n";
  180. }
  181. // append additional headers given by user.
  182. for(std::deque<std::string>::const_iterator i = _userHeaders.begin();
  183. i != _userHeaders.end(); ++i) {
  184. requestLine += (*i)+"\r\n";
  185. }
  186. requestLine += "\r\n";
  187. return requestLine;
  188. }
  189. std::string HttpRequest::createProxyRequest() const
  190. {
  191. std::string requestLine =
  192. std::string("CONNECT ")+getHost()+":"+Util::uitos(getPort())+
  193. std::string(" HTTP/1.1\r\n")+
  194. "User-Agent: "+userAgent+"\r\n"+
  195. "Host: "+getHost()+":"+Util::uitos(getPort())+"\r\n";
  196. if(request->isKeepAliveEnabled() || request->isPipeliningEnabled()) {
  197. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  198. }else {
  199. requestLine += "Proxy-Connection: close\r\n";
  200. }
  201. if(proxyAuthEnabled) {
  202. requestLine += getProxyAuthString();
  203. }
  204. requestLine += "\r\n";
  205. return requestLine;
  206. }
  207. std::string HttpRequest::getProxyAuthString() const {
  208. return "Proxy-Authorization: Basic "+
  209. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfigForHttpProxy(request)->getAuthText())+"\r\n";
  210. }
  211. void HttpRequest::setUserHeaders(const std::string& userHeadersString)
  212. {
  213. std::deque<std::string> headers;
  214. Util::slice(headers, userHeadersString, '\n', true);
  215. _userHeaders = headers;
  216. }
  217. void HttpRequest::configure(const Option* option)
  218. {
  219. authEnabled = option->get(PREF_HTTP_AUTH_ENABLED) == V_TRUE;
  220. proxyEnabled =
  221. option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE &&
  222. option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  223. proxyAuthEnabled = option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
  224. }
  225. std::string HttpRequest::getPreviousURI() const
  226. {
  227. return request->getPreviousUrl();
  228. }
  229. std::string HttpRequest::getHost() const
  230. {
  231. return request->getHost();
  232. }
  233. uint16_t HttpRequest::getPort() const
  234. {
  235. return request->getPort();
  236. }
  237. std::string HttpRequest::getMethod() const
  238. {
  239. return request->getMethod();
  240. }
  241. std::string HttpRequest::getProtocol() const
  242. {
  243. return request->getProtocol();
  244. }
  245. std::string HttpRequest::getCurrentURI() const
  246. {
  247. return request->getCurrentUrl();
  248. }
  249. std::string HttpRequest::getDir() const
  250. {
  251. return request->getDir();
  252. }
  253. std::string HttpRequest::getFile() const
  254. {
  255. return request->getFile();
  256. }
  257. } // namespace aria2