HttpConnection.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "HttpConnection.h"
  23. #include "DlRetryEx.h"
  24. #include "Util.h"
  25. #include "Base64.h"
  26. #include "message.h"
  27. #include "prefs.h"
  28. HttpConnection::HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger):
  29. cuid(cuid), socket(socket), req(req), option(op), logger(logger) {}
  30. void HttpConnection::sendRequest(const Segment& segment) const {
  31. string request = createRequest(segment);
  32. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  33. socket->writeData(request.c_str(), request.size());
  34. }
  35. void HttpConnection::sendProxyRequest() const {
  36. string request =
  37. string("CONNECT ")+req->getHost()+":"+Util::llitos(req->getPort())+
  38. string(" HTTP/1.1\r\n")+
  39. "Host: "+getHost(req->getHost(), req->getPort())+"\r\n";
  40. if(useProxyAuth()) {
  41. request += getProxyAuthString();
  42. }
  43. request += "\r\n";
  44. logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
  45. socket->writeData(request.c_str(), request.size());
  46. }
  47. string HttpConnection::getProxyAuthString() const {
  48. return "Proxy-Authorization: Basic "+
  49. Base64::encode(option->get(PREF_HTTP_PROXY_USER)+":"+
  50. option->get(PREF_HTTP_PROXY_PORT))+"\r\n";
  51. }
  52. string HttpConnection::getHost(const string& host, int port) const {
  53. return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port));
  54. }
  55. string HttpConnection::createRequest(const Segment& segment) const {
  56. string request = string("GET ")+
  57. (req->getProtocol() == "ftp" || useProxy() && useProxyGet() ?
  58. req->getCurrentUrl() :
  59. ((req->getDir() == "/" ? "/" : req->getDir()+"/")+req->getFile()))+
  60. string(" HTTP/1.1\r\n")+
  61. "User-Agent: aria2\r\n"+
  62. "Connection: close\r\n"+
  63. "Accept: */*\r\n"+ /* */
  64. "Host: "+getHost(req->getHost(), req->getPort())+"\r\n"+
  65. "Pragma: no-cache\r\n"+
  66. "Cache-Control: no-cache\r\n";
  67. if(segment.sp+segment.ds > 0) {
  68. request += "Range: bytes="+
  69. Util::llitos(segment.sp+segment.ds)+"-"+Util::llitos(segment.ep)+"\r\n";
  70. }
  71. if(useProxy() && useProxyAuth() && useProxyGet()) {
  72. request += getProxyAuthString();
  73. }
  74. if(option->get(PREF_HTTP_AUTH_SCHEME) == V_BASIC) {
  75. request += "Authorization: Basic "+
  76. Base64::encode(option->get(PREF_HTTP_USER)+":"+
  77. option->get(PREF_HTTP_PASSWD))+"\r\n";
  78. }
  79. if(req->getPreviousUrl().size()) {
  80. request += "Referer: "+req->getPreviousUrl()+"\r\n";
  81. }
  82. string cookiesValue;
  83. vector<Cookie> cookies = req->cookieBox->criteriaFind(req->getHost(), req->getDir(), req->getProtocol() == "https" ? true : false);
  84. for(vector<Cookie>::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
  85. cookiesValue += (*itr).toString()+";";
  86. }
  87. if(cookiesValue.size()) {
  88. request += string("Cookie: ")+cookiesValue+"\r\n";
  89. }
  90. request += "\r\n";
  91. return request;
  92. }
  93. int HttpConnection::receiveResponse(HttpHeader& headers) {
  94. char buf[512];
  95. while(socket->isReadable(0)) {
  96. int size = sizeof(buf)-1;
  97. socket->peekData(buf, size);
  98. if(size == 0) {
  99. throw new DlRetryEx(EX_INVALID_RESPONSE);
  100. }
  101. buf[size] = '\0';
  102. int hlenTemp = header.size();
  103. header += buf;
  104. string::size_type p;
  105. if((p = header.find("\r\n\r\n")) == string::npos) {
  106. socket->readData(buf, size);
  107. } else {
  108. if(Util::endsWith(header, "\r\n\r\n")) {
  109. socket->readData(buf, size);
  110. } else {
  111. header.erase(p+4);
  112. size = p+4-hlenTemp;
  113. socket->readData(buf, size);
  114. }
  115. break;
  116. }
  117. }
  118. if(!Util::endsWith(header, "\r\n\r\n")) {
  119. return 0;
  120. }
  121. // OK, we got all headers.
  122. logger->info(MSG_RECEIVE_RESPONSE, cuid, header.c_str());
  123. string::size_type p, np;
  124. p = np = 0;
  125. np = header.find("\r\n", p);
  126. if(np == string::npos) {
  127. throw new DlRetryEx(EX_NO_STATUS_HEADER);
  128. }
  129. // check HTTP status value
  130. string status = header.substr(9, 3);
  131. p = np+2;
  132. // retreive status name-value pairs, then push these into map
  133. while((np = header.find("\r\n", p)) != string::npos && np != p) {
  134. string line = header.substr(p, np-p);
  135. p = np+2;
  136. pair<string, string> hp;
  137. Util::split(hp, line, ':');
  138. headers.put(hp.first, hp.second);
  139. }
  140. return (int)strtol(status.c_str(), NULL, 10);
  141. }
  142. bool HttpConnection::useProxy() const {
  143. return option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE;
  144. }
  145. bool HttpConnection::useProxyAuth() const {
  146. return option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
  147. }
  148. bool HttpConnection::useProxyGet() const {
  149. return option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
  150. }