aria2/src/HttpConnection.cc

162 lines
5.2 KiB
C++
Raw Normal View History

2006-02-17 13:35:04 +00:00
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "HttpConnection.h"
#include "DlRetryEx.h"
#include "Util.h"
#include "Base64.h"
#include "message.h"
2006-02-21 14:00:58 +00:00
#include "prefs.h"
2006-02-17 13:35:04 +00:00
2006-02-21 12:27:17 +00:00
HttpConnection::HttpConnection(int cuid, const Socket* socket, const Request* req, const Option* op, const Logger* logger):
cuid(cuid), socket(socket), req(req), option(op), logger(logger) {}
2006-02-17 13:35:04 +00:00
2006-02-21 12:27:17 +00:00
void HttpConnection::sendRequest(const Segment& segment) const {
string request = createRequest(segment);
2006-02-21 15:01:05 +00:00
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
2006-02-17 13:35:04 +00:00
socket->writeData(request.c_str(), request.size());
}
2006-02-21 12:27:17 +00:00
void HttpConnection::sendProxyRequest() const {
2006-02-21 14:00:58 +00:00
string request =
string("CONNECT ")+req->getHost()+":"+Util::llitos(req->getPort())+
2006-02-17 13:35:04 +00:00
string(" HTTP/1.1\r\n")+
"Host: "+getHost(req->getHost(), req->getPort())+"\r\n";
if(useProxyAuth()) {
2006-02-22 15:40:04 +00:00
request += getProxyAuthString();
2006-02-17 13:35:04 +00:00
}
request += "\r\n";
2006-02-21 15:01:05 +00:00
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
2006-02-17 13:35:04 +00:00
socket->writeData(request.c_str(), request.size());
}
2006-02-22 15:40:04 +00:00
string HttpConnection::getProxyAuthString() const {
return "Proxy-Authorization: Basic "+
Base64::encode(option->get(PREF_HTTP_PROXY_USER)+":"+
option->get(PREF_HTTP_PROXY_PORT))+"\r\n";
}
2006-02-21 12:27:17 +00:00
string HttpConnection::getHost(const string& host, int port) const {
return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port));
2006-02-17 13:35:04 +00:00
}
2006-02-21 12:27:17 +00:00
string HttpConnection::createRequest(const Segment& segment) const {
string request = string("GET ")+
2006-02-22 15:40:04 +00:00
(req->getProtocol() == "ftp" || useProxy() && useProxyGet() ?
2006-02-21 12:27:17 +00:00
req->getCurrentUrl() :
((req->getDir() == "/" ? "/" : req->getDir()+"/")+req->getFile()))+
string(" HTTP/1.1\r\n")+
2006-02-17 13:35:04 +00:00
"User-Agent: aria2\r\n"+
"Connection: close\r\n"+
2006-02-21 12:27:17 +00:00
"Accept: */*\r\n"+ /* */
2006-02-17 13:35:04 +00:00
"Host: "+getHost(req->getHost(), req->getPort())+"\r\n"+
"Pragma: no-cache\r\n"+
"Cache-Control: no-cache\r\n";
if(segment.sp+segment.ds > 0) {
2006-02-21 14:00:58 +00:00
request += "Range: bytes="+
Util::llitos(segment.sp+segment.ds)+"-"+Util::llitos(segment.ep)+"\r\n";
2006-02-17 13:35:04 +00:00
}
2006-02-22 15:40:04 +00:00
if(useProxy() && useProxyAuth() && useProxyGet()) {
request += getProxyAuthString();
}
2006-02-21 14:00:58 +00:00
if(option->get(PREF_HTTP_AUTH_SCHEME) == V_BASIC) {
2006-02-17 13:35:04 +00:00
request += "Authorization: Basic "+
2006-02-21 14:00:58 +00:00
Base64::encode(option->get(PREF_HTTP_USER)+":"+
option->get(PREF_HTTP_PASSWD))+"\r\n";
2006-02-17 13:35:04 +00:00
}
if(req->getPreviousUrl().size()) {
request += "Referer: "+req->getPreviousUrl()+"\r\n";
}
2006-02-17 13:35:04 +00:00
string cookiesValue;
vector<Cookie> cookies = req->cookieBox->criteriaFind(req->getHost(), req->getDir(), req->getProtocol() == "https" ? true : false);
for(vector<Cookie>::const_iterator itr = cookies.begin(); itr != cookies.end(); itr++) {
cookiesValue += (*itr).toString()+";";
}
if(cookiesValue.size()) {
request += string("Cookie: ")+cookiesValue+"\r\n";
}
request += "\r\n";
return request;
}
int HttpConnection::receiveResponse(HttpHeader& headers) {
2006-02-21 12:27:17 +00:00
char buf[512];
while(socket->isReadable(0)) {
int size = sizeof(buf)-1;
socket->peekData(buf, size);
2006-02-22 15:40:04 +00:00
if(size == 0) {
throw new DlRetryEx(EX_INVALID_RESPONSE);
}
2006-02-21 12:27:17 +00:00
buf[size] = '\0';
int hlenTemp = header.size();
header += buf;
string::size_type p;
if((p = header.find("\r\n\r\n")) == string::npos) {
socket->readData(buf, size);
} else {
if(Util::endsWith(header, "\r\n\r\n")) {
socket->readData(buf, size);
2006-02-17 13:35:04 +00:00
} else {
2006-02-21 12:27:17 +00:00
header.erase(p+4);
size = p+4-hlenTemp;
socket->readData(buf, size);
2006-02-17 13:35:04 +00:00
}
2006-02-21 12:27:17 +00:00
break;
2006-02-17 13:35:04 +00:00
}
}
2006-02-21 12:27:17 +00:00
if(!Util::endsWith(header, "\r\n\r\n")) {
return 0;
}
// OK, we got all headers.
2006-02-17 13:35:04 +00:00
logger->info(MSG_RECEIVE_RESPONSE, cuid, header.c_str());
string::size_type p, np;
2006-02-17 13:35:04 +00:00
p = np = 0;
np = header.find("\r\n", p);
if(np == string::npos) {
throw new DlRetryEx(EX_NO_STATUS_HEADER);
}
// check HTTP status value
string status = header.substr(9, 3);
p = np+2;
// retreive status name-value pairs, then push these into map
while((np = header.find("\r\n", p)) != string::npos && np != p) {
string line = header.substr(p, np-p);
p = np+2;
pair<string, string> hp;
Util::split(hp, line, ':');
2006-02-21 12:27:17 +00:00
headers.put(hp.first, hp.second);
2006-02-17 13:35:04 +00:00
}
return (int)strtol(status.c_str(), NULL, 10);
}
2006-02-21 12:27:17 +00:00
bool HttpConnection::useProxy() const {
2006-02-21 14:00:58 +00:00
return option->get(PREF_HTTP_PROXY_ENABLED) == V_TRUE;
2006-02-17 13:35:04 +00:00
}
2006-02-21 12:27:17 +00:00
bool HttpConnection::useProxyAuth() const {
2006-02-21 14:00:58 +00:00
return option->get(PREF_HTTP_PROXY_AUTH_ENABLED) == V_TRUE;
2006-02-17 13:35:04 +00:00
}
2006-02-22 15:40:04 +00:00
bool HttpConnection::useProxyGet() const {
return option->get(PREF_HTTP_PROXY_METHOD) == V_GET;
}