1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/* <!-- 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 "HttpResponseCommand.h"
#include "DlAbortEx.h"
#include "DlRetryEx.h"
#include "HttpDownloadCommand.h"
#include "HttpInitiateConnectionCommand.h"
#include "message.h"
#include "Util.h"
#include "TrackerDownloadCommand.h"
// TODO
#include "TorrentDownloadEngine.h"
HttpResponseCommand::HttpResponseCommand(int cuid, Request* req, DownloadEngine* e, const Socket* s):
AbstractCommand(cuid, req, e, s) {
http = new HttpConnection(cuid, socket, req, e->option, e->logger);
}
HttpResponseCommand::~HttpResponseCommand() {
delete http;
}
bool HttpResponseCommand::executeInternal(Segment seg) {
if(req->seg.sp != seg.sp) {
e->logger->info(MSG_SEGMENT_CHANGED, cuid);
return prepareForRetry(0);
}
HttpHeader headers;
int status = http->receiveResponse(headers);
if(status == 0) {
// didn't receive header fully
e->commands.push(this);
return false;
}
// check HTTP status number
checkResponse(status, seg);
retrieveCookie(headers);
// check whether Location header exists. If it does, update request object
// with redirected URL.
// then establish a connection to the new host and port
if(headers.defined("Location")) {
return handleRedirect(headers.getFirst("Location"), headers);
}
if(!e->segmentMan->downloadStarted) {
string transferEncoding;
if(headers.defined("Transfer-Encoding")) {
return handleOtherEncoding(headers.getFirst("Transfer-Encoding"), headers);
} else {
return handleDefaultEncoding(headers);
}
} else {
if(req->getFile() != e->segmentMan->filename) {
throw new DlAbortEx(EX_FILENAME_MISMATCH, req->getFile().c_str(), e->segmentMan->filename.c_str());
}
createHttpDownloadCommand();
return true;
}
}
void HttpResponseCommand::checkResponse(int status, const Segment& segment) {
if(!(status < 400 && status >= 300 ||
(segment.sp+segment.ds == 0 && status == 200)
|| (segment.sp+segment.ds > 0 && status == 206))) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
}
bool HttpResponseCommand::handleRedirect(string url, const HttpHeader& headers) {
req->redirectUrl(url);
e->logger->info(MSG_REDIRECT, cuid, url.c_str());
e->noWait = true;
return prepareForRetry(0);
}
bool HttpResponseCommand::handleDefaultEncoding(const HttpHeader& headers) {
// TODO quick and dirty way
if(req->isTorrent) {
long long int size = headers.getFirstAsLLInt("Content-Length");
e->segmentMan->totalSize = size;
e->segmentMan->isSplittable = false;
createHttpDownloadCommand();
return true;
}
long long int size = headers.getFirstAsLLInt("Content-Length");
if(size == LONG_LONG_MAX || size < 0) {
throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
}
e->segmentMan->isSplittable = !(size == 0);
e->segmentMan->filename = req->getFile();
bool segFileExists = e->segmentMan->segmentFileExists();
e->segmentMan->downloadStarted = true;
if(segFileExists) {
e->segmentMan->load();
e->diskWriter->openExistingFile(e->segmentMan->getFilePath());
// send request again to the server with Range header
return prepareForRetry(0);
} else {
e->segmentMan->totalSize = size;
Segment seg;
e->segmentMan->getSegment(seg, cuid);
e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
createHttpDownloadCommand();
return true;
}
}
bool HttpResponseCommand::handleOtherEncoding(string transferEncoding, const HttpHeader& headers) {
// we ignore content-length when transfer-encoding is set
e->segmentMan->downloadStarted = true;
e->segmentMan->isSplittable = false;
e->segmentMan->filename = req->getFile();
e->segmentMan->totalSize = 0;
Segment seg;
e->segmentMan->getSegment(seg, cuid);
e->diskWriter->initAndOpenFile(e->segmentMan->getFilePath());
createHttpDownloadCommand(transferEncoding);
return true;
}
void HttpResponseCommand::createHttpDownloadCommand(string transferEncoding) {
if(!req->isTorrent) {
HttpDownloadCommand* command = new HttpDownloadCommand(cuid, req, e, socket);
TransferEncoding* enc = NULL;
if(transferEncoding.size() && (enc = command->getTransferEncoding(transferEncoding)) == NULL) {
delete(command);
throw new DlAbortEx(EX_TRANSFER_ENCODING_NOT_SUPPORTED, transferEncoding.c_str());
} else {
if(enc != NULL) {
command->transferEncoding = transferEncoding;
enc->init();
}
e->commands.push(command);
}
} else {
// TODO
TrackerDownloadCommand* command = new TrackerDownloadCommand(cuid, req, (TorrentDownloadEngine*)e, socket);
e->commands.push(command);
}
}
void HttpResponseCommand::retrieveCookie(const HttpHeader& headers) {
Strings v = headers.get("Set-Cookie");
for(Strings::const_iterator itr = v.begin(); itr != v.end(); itr++) {
Cookie c;
req->cookieBox->parse(c, *itr);
req->cookieBox->add(c);
}
}
|