HttpRequest.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "Util.h"
  37. #include "Base64.h"
  38. #include "prefs.h"
  39. #include "AuthConfigFactory.h"
  40. #include "AuthConfig.h"
  41. RangeHandle HttpRequest::getRange() const
  42. {
  43. // content-length is always 0
  44. if(segment.isNull()) {
  45. return new Range(0, 0, 0);
  46. } else {
  47. return new Range(getStartByte(), getEndByte(), entityLength);
  48. }
  49. }
  50. bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
  51. {
  52. if(segment.isNull()) {
  53. return true;
  54. }
  55. if(getStartByte() == range->getStartByte() &&
  56. (getEndByte() == 0 ||
  57. getEndByte() > 0 && getEndByte() == range->getEndByte()) &&
  58. (entityLength == 0 ||
  59. entityLength > 0 && entityLength == range->getEntityLength())) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. string HttpRequest::getHostText(const string& host, int32_t port) const
  66. {
  67. return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port));
  68. }
  69. string HttpRequest::createRequest() const
  70. {
  71. string requestLine = "GET ";
  72. if(getProtocol() == "ftp" || proxyEnabled) {
  73. requestLine += getCurrentURI();
  74. } else {
  75. if(getDir() == "/") {
  76. requestLine += getDir();
  77. } else {
  78. requestLine += getDir()+"/";
  79. }
  80. requestLine += getFile();
  81. }
  82. requestLine +=
  83. string(" HTTP/1.1\r\n")+
  84. "User-Agent: "+userAgent+"\r\n"+
  85. "Accept: */*\r\n"+ /* */
  86. "Host: "+getHostText(getHost(), getPort())+"\r\n"+
  87. "Pragma: no-cache\r\n"+
  88. "Cache-Control: no-cache\r\n";
  89. if(!request->isKeepAlive()) {
  90. requestLine += "Connection: close\r\n";
  91. }
  92. if(!segment.isNull() && segment->getLength() > 0 &&
  93. (request->isKeepAlive() || getStartByte() > 0)) {
  94. requestLine += "Range: bytes="+Util::llitos(getStartByte());
  95. requestLine += "-";
  96. if(request->isKeepAlive()) {
  97. requestLine += Util::llitos(getEndByte());
  98. }
  99. requestLine += "\r\n";
  100. }
  101. if(proxyEnabled) {
  102. if(request->isKeepAlive()) {
  103. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  104. } else {
  105. requestLine += "Proxy-Connection: close\r\n";
  106. }
  107. }
  108. if(proxyEnabled && proxyAuthEnabled) {
  109. requestLine += getProxyAuthString();
  110. }
  111. if(authEnabled) {
  112. requestLine += "Authorization: Basic "+
  113. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfig(request)->getAuthText())+"\r\n";
  114. }
  115. if(getPreviousURI().size()) {
  116. requestLine += "Referer: "+getPreviousURI()+"\r\n";
  117. }
  118. string cookiesValue;
  119. Cookies cookies = request->cookieBox->criteriaFind(getHost(),
  120. getDir(),
  121. time(0),
  122. getProtocol() == "https" ?
  123. true : false);
  124. for(Cookies::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  125. cookiesValue += (*itr).toString()+";";
  126. }
  127. if(cookiesValue.size()) {
  128. requestLine += string("Cookie: ")+cookiesValue+"\r\n";
  129. }
  130. requestLine += "\r\n";
  131. return requestLine;
  132. }
  133. string HttpRequest::createProxyRequest() const
  134. {
  135. string requestLine =
  136. string("CONNECT ")+getHost()+":"+Util::itos(getPort())+
  137. string(" HTTP/1.1\r\n")+
  138. "User-Agent: "+userAgent+"\r\n"+
  139. "Host: "+getHost()+":"+Util::itos(getPort())+"\r\n";
  140. if(request->isKeepAlive()) {
  141. requestLine += "Proxy-Connection: Keep-Alive\r\n";
  142. }else {
  143. requestLine += "Proxy-Connection: close\r\n";
  144. }
  145. if(proxyAuthEnabled) {
  146. requestLine += getProxyAuthString();
  147. }
  148. requestLine += "\r\n";
  149. return requestLine;
  150. }
  151. string HttpRequest::getProxyAuthString() const {
  152. return "Proxy-Authorization: Basic "+
  153. Base64::encode(AuthConfigFactorySingleton::instance()->createAuthConfigForHttpProxy(request)->getAuthText())+"\r\n";
  154. }
  155. void HttpRequest::configure(const Option* option)
  156. {
  157. authEnabled = option->get(PREF_HTTP_AUTH_ENABLED) == V_TRUE;
  158. proxyEnabled =
  159. option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE &&
  160. option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  161. proxyAuthEnabled = option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
  162. }