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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/net/url_request_slow_download_job.h"
#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
#include "base/string_util.h"
#include "googleurl/src/gurl.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_status.h"
const int kFirstDownloadSize = 1024 * 35;
const int kSecondDownloadSize = 1024 * 10;
const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] =
"http://url.handled.by.slow.download/download-unknown-size";
const char URLRequestSlowDownloadJob::kKnownSizeUrl[] =
"http://url.handled.by.slow.download/download-known-size";
const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] =
"http://url.handled.by.slow.download/download-finish";
const char URLRequestSlowDownloadJob::kErrorFinishDownloadUrl[] =
"http://url.handled.by.slow.download/download-error";
std::vector<URLRequestSlowDownloadJob*>
URLRequestSlowDownloadJob::pending_requests_;
// Return whether this is the finish or error URL.
static bool IsCompletionUrl(const GURL& url) {
if (url.spec() == URLRequestSlowDownloadJob::kFinishDownloadUrl)
return true;
return (url.spec() == URLRequestSlowDownloadJob::kErrorFinishDownloadUrl);
}
void URLRequestSlowDownloadJob::Start() {
MessageLoop::current()->PostTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&URLRequestSlowDownloadJob::StartAsync));
}
// static
void URLRequestSlowDownloadJob::AddUrlHandler() {
net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
filter->AddUrlHandler(GURL(kUnknownSizeUrl),
&URLRequestSlowDownloadJob::Factory);
filter->AddUrlHandler(GURL(kKnownSizeUrl),
&URLRequestSlowDownloadJob::Factory);
filter->AddUrlHandler(GURL(kFinishDownloadUrl),
&URLRequestSlowDownloadJob::Factory);
filter->AddUrlHandler(GURL(kErrorFinishDownloadUrl),
&URLRequestSlowDownloadJob::Factory);
}
/*static */
net::URLRequestJob* URLRequestSlowDownloadJob::Factory(
net::URLRequest* request,
const std::string& scheme) {
URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob(request);
if (!IsCompletionUrl(request->url()))
URLRequestSlowDownloadJob::pending_requests_.push_back(job);
return job;
}
/* static */
void URLRequestSlowDownloadJob::FinishPendingRequests(bool error) {
typedef std::vector<URLRequestSlowDownloadJob*> JobList;
for (JobList::iterator it = pending_requests_.begin(); it !=
pending_requests_.end(); ++it) {
if (error) {
(*it)->set_should_error_download();
} else {
(*it)->set_should_finish_download();
}
}
pending_requests_.clear();
}
URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(net::URLRequest* request)
: net::URLRequestJob(request),
first_download_size_remaining_(kFirstDownloadSize),
should_finish_download_(false),
should_send_second_chunk_(false),
should_error_download_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {}
void URLRequestSlowDownloadJob::StartAsync() {
if (IsCompletionUrl(request_->url())) {
URLRequestSlowDownloadJob::FinishPendingRequests(
request_->url().spec() == kErrorFinishDownloadUrl);
}
NotifyHeadersComplete();
}
bool URLRequestSlowDownloadJob::ReadRawData(net::IOBuffer* buf, int buf_size,
int *bytes_read) {
if (IsCompletionUrl(request_->url())) {
*bytes_read = 0;
return true;
}
if (should_send_second_chunk_) {
DCHECK(buf_size > kSecondDownloadSize);
for (int i = 0; i < kSecondDownloadSize; ++i) {
buf->data()[i] = '*';
}
*bytes_read = kSecondDownloadSize;
should_send_second_chunk_ = false;
return true;
}
if (first_download_size_remaining_ > 0) {
int send_size = std::min(first_download_size_remaining_, buf_size);
for (int i = 0; i < send_size; ++i) {
buf->data()[i] = '*';
}
*bytes_read = send_size;
first_download_size_remaining_ -= send_size;
DCHECK(!is_done());
return true;
}
if (should_finish_download_) {
*bytes_read = 0;
return true;
}
// If we make it here, the first chunk has been sent and we need to wait
// until a request is made for kFinishDownloadUrl.
SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&URLRequestSlowDownloadJob::CheckDoneStatus),
100);
// Return false to signal there is pending data.
return false;
}
void URLRequestSlowDownloadJob::CheckDoneStatus() {
if (should_finish_download_) {
should_send_second_chunk_ = true;
SetStatus(net::URLRequestStatus());
NotifyReadComplete(kSecondDownloadSize);
} else if (should_error_download_) {
NotifyDone(
net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED));
} else {
MessageLoop::current()->PostDelayedTask(
FROM_HERE,
method_factory_.NewRunnableMethod(
&URLRequestSlowDownloadJob::CheckDoneStatus),
100);
}
}
// Public virtual version.
void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) {
// Forward to private const version.
GetResponseInfoConst(info);
}
URLRequestSlowDownloadJob::~URLRequestSlowDownloadJob() {}
// Private const version.
void URLRequestSlowDownloadJob::GetResponseInfoConst(
net::HttpResponseInfo* info) const {
// Send back mock headers.
std::string raw_headers;
if (IsCompletionUrl(request_->url())) {
raw_headers.append(
"HTTP/1.1 200 OK\n"
"Content-type: text/plain\n");
} else {
raw_headers.append(
"HTTP/1.1 200 OK\n"
"Content-type: application/octet-stream\n"
"Cache-Control: max-age=0\n");
if (request_->url().spec() == kKnownSizeUrl) {
raw_headers.append(base::StringPrintf(
"Content-Length: %d\n",
kFirstDownloadSize + kSecondDownloadSize));
}
}
// ParseRawHeaders expects \0 to end each header line.
ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1));
info->headers = new net::HttpResponseHeaders(raw_headers);
}
bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const {
net::HttpResponseInfo info;
GetResponseInfoConst(&info);
return info.headers && info.headers->GetMimeType(mime_type);
}
|