2015-05-29 15:54:00 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/net/url_request_fetch_job.h"
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2015-05-29 15:54:00 +00:00
|
|
|
|
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "net/base/io_buffer.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2015-06-17 01:31:33 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
|
|
|
#include "net/url_request/url_fetcher.h"
|
|
|
|
#include "net/url_request/url_fetcher_response_writer.h"
|
2015-05-29 15:54:00 +00:00
|
|
|
#include "net/url_request/url_request_status.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Pipe the response writer back to URLRequestFetchJob.
|
|
|
|
class ResponsePiper : public net::URLFetcherResponseWriter {
|
|
|
|
public:
|
|
|
|
explicit ResponsePiper(URLRequestFetchJob* job)
|
|
|
|
: first_write_(true), job_(job) {}
|
|
|
|
|
|
|
|
// net::URLFetcherResponseWriter:
|
|
|
|
int Initialize(const net::CompletionCallback& callback) override {
|
|
|
|
return net::OK;
|
|
|
|
}
|
|
|
|
int Write(net::IOBuffer* buffer,
|
|
|
|
int num_bytes,
|
|
|
|
const net::CompletionCallback& callback) override {
|
|
|
|
job_->DataAvailable(buffer, num_bytes);
|
|
|
|
if (first_write_) {
|
|
|
|
// The URLFetcherResponseWriter doesn't have an event when headers have
|
|
|
|
// been read, so we have to emulate by hooking to first write event.
|
|
|
|
job_->HeadersCompleted();
|
|
|
|
first_write_ = false;
|
|
|
|
}
|
|
|
|
return num_bytes;
|
|
|
|
}
|
|
|
|
int Finish(const net::CompletionCallback& callback) override {
|
|
|
|
return net::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool first_write_;
|
|
|
|
URLRequestFetchJob* job_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ResponsePiper);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-05-29 15:54:00 +00:00
|
|
|
URLRequestFetchJob::URLRequestFetchJob(
|
|
|
|
net::URLRequest* request,
|
|
|
|
net::NetworkDelegate* network_delegate,
|
|
|
|
const GURL& url)
|
|
|
|
: net::URLRequestJob(request, network_delegate),
|
|
|
|
url_(url),
|
2015-06-17 01:31:33 +00:00
|
|
|
finished_(false),
|
|
|
|
weak_factory_(this) {}
|
|
|
|
|
|
|
|
void URLRequestFetchJob::HeadersCompleted() {
|
|
|
|
response_info_.reset(new net::HttpResponseInfo);
|
|
|
|
response_info_->headers = fetcher_->GetResponseHeaders();
|
|
|
|
NotifyHeadersComplete();
|
|
|
|
}
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
void URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) {
|
|
|
|
buffer_.resize(buffer_.size() + num_bytes);
|
|
|
|
memcpy(buffer_.data() + buffer_.size() - num_bytes,
|
|
|
|
buffer->data(),
|
|
|
|
num_bytes);
|
|
|
|
SetStatus(net::URLRequestStatus());
|
|
|
|
}
|
2015-05-29 15:54:00 +00:00
|
|
|
|
|
|
|
void URLRequestFetchJob::Start() {
|
2015-06-16 09:08:53 +00:00
|
|
|
fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this));
|
2015-05-29 15:54:00 +00:00
|
|
|
auto context = AtomBrowserContext::Get()->url_request_context_getter();
|
2015-06-16 09:08:53 +00:00
|
|
|
fetcher_->SetRequestContext(context);
|
2015-06-17 01:31:33 +00:00
|
|
|
fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this)));
|
2015-06-16 09:08:53 +00:00
|
|
|
fetcher_->Start();
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void URLRequestFetchJob::Kill() {
|
|
|
|
URLRequestJob::Kill();
|
2015-06-17 01:31:33 +00:00
|
|
|
fetcher_.reset();
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool URLRequestFetchJob::ReadRawData(net::IOBuffer* dest,
|
|
|
|
int dest_size,
|
|
|
|
int* bytes_read) {
|
2015-06-17 01:31:33 +00:00
|
|
|
if (finished_) {
|
2015-05-29 15:54:00 +00:00
|
|
|
*bytes_read = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
if (buffer_.size() == 0) {
|
|
|
|
SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
|
|
|
|
return false;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
if (static_cast<size_t>(dest_size) >= buffer_.size()) {
|
|
|
|
// Copy all data at once (quick).
|
|
|
|
memcpy(dest->data(), buffer_.data(), buffer_.size());
|
|
|
|
*bytes_read = buffer_.size();
|
|
|
|
buffer_.clear();
|
|
|
|
} else {
|
|
|
|
// Can not fit all data, strip them (slow).
|
|
|
|
memcpy(dest->data(), buffer_.data(), dest_size);
|
|
|
|
*bytes_read = dest_size;
|
|
|
|
std::rotate(buffer_.begin(), buffer_.begin() + dest_size, buffer_.end());
|
|
|
|
buffer_.resize(buffer_.size() - dest_size);
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
bool URLRequestFetchJob::GetMimeType(std::string* mime_type) const {
|
|
|
|
if (!response_info_)
|
|
|
|
return false;
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
return response_info_->headers->GetMimeType(mime_type);
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
void URLRequestFetchJob::GetResponseInfo(net::HttpResponseInfo* info) {
|
|
|
|
if (response_info_)
|
|
|
|
*info = *response_info_;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
int URLRequestFetchJob::GetResponseCode() const {
|
|
|
|
if (!response_info_)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return response_info_->headers->response_code();
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 01:31:33 +00:00
|
|
|
void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
|
|
|
|
finished_ = true;
|
2015-05-29 15:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|