From a5e2f8e79e5144e1a54353afdd1ea57c13abca00 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Fri, 29 May 2015 21:24:00 +0530 Subject: [PATCH 01/11] protocol: adding requestHttpJob method --- atom/browser/api/atom_api_protocol.cc | 9 +++ atom/browser/api/lib/protocol.coffee | 4 + atom/browser/net/adapter_request_job.cc | 7 ++ atom/browser/net/adapter_request_job.h | 2 + atom/browser/net/url_request_fetch_job.cc | 90 +++++++++++++++++++++++ atom/browser/net/url_request_fetch_job.h | 57 ++++++++++++++ filenames.gypi | 4 +- spec/api-protocol-spec.coffee | 23 ++++++ 8 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 atom/browser/net/url_request_fetch_job.cc create mode 100644 atom/browser/net/url_request_fetch_job.h diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 083ea32b2010..eede89233201 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -8,6 +8,7 @@ #include "atom/browser/net/adapter_request_job.h" #include "atom/browser/net/atom_url_request_job_factory.h" #include "atom/common/native_mate_converters/file_path_converter.h" +#include "atom/common/native_mate_converters/gurl_converter.h" #include "content/public/browser/browser_thread.h" #include "native_mate/callback.h" #include "native_mate/dictionary.h" @@ -123,6 +124,14 @@ class CustomProtocolRequestJob : public AdapterRequestJob { base::Bind(&AdapterRequestJob::CreateErrorJobAndStart, GetWeakPtr(), error)); return; + } else if (name == "RequestHttpJob") { + GURL url; + dict.Get("url", &url); + + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, + base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, + GetWeakPtr(), url)); + return; } } diff --git a/atom/browser/api/lib/protocol.coffee b/atom/browser/api/lib/protocol.coffee index c2db7800c08d..bbd2499a8371 100644 --- a/atom/browser/api/lib/protocol.coffee +++ b/atom/browser/api/lib/protocol.coffee @@ -34,4 +34,8 @@ protocol.RequestErrorJob = class RequestErrorJob constructor: (@error) -> +protocol.RequestHttpJob = +class RequestHttpJob + constructor: (@url) -> + module.exports = protocol diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index af3b02f150f4..258416e2f42a 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -4,8 +4,10 @@ #include "atom/browser/net/adapter_request_job.h" +#include "atom/browser/atom_browser_context.h" #include "base/threading/sequenced_worker_pool.h" #include "atom/browser/net/url_request_buffer_job.h" +#include "atom/browser/net/url_request_fetch_job.h" #include "atom/browser/net/url_request_string_job.h" #include "atom/browser/net/asar/url_request_asar_job.h" #include "atom/common/asar/asar_util.h" @@ -104,6 +106,11 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { real_job_->Start(); } +void AdapterRequestJob::CreateHttpJobAndStart(const GURL& url) { + real_job_ = new URLRequestFetchJob(request(), network_delegate(), url); + real_job_->Start(); +} + void AdapterRequestJob::CreateJobFromProtocolHandlerAndStart() { real_job_ = protocol_handler_->MaybeCreateJob(request(), network_delegate()); diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index d87207464c08..1ecc82230b76 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -9,6 +9,7 @@ #include "base/memory/ref_counted_memory.h" #include "base/memory/weak_ptr.h" +#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_job.h" #include "net/url_request/url_request_job_factory.h" #include "v8/include/v8.h" @@ -56,6 +57,7 @@ class AdapterRequestJob : public net::URLRequestJob { const std::string& charset, scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); + void CreateHttpJobAndStart(const GURL& url); void CreateJobFromProtocolHandlerAndStart(); private: diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc new file mode 100644 index 000000000000..081e85f28f32 --- /dev/null +++ b/atom/browser/net/url_request_fetch_job.cc @@ -0,0 +1,90 @@ +// 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" + +#include + +#include "atom/browser/atom_browser_context.h" +#include "net/base/io_buffer.h" +#include "net/base/net_errors.h" +#include "net/url_request/url_request_status.h" + +namespace atom { + +URLRequestFetchJob::URLRequestFetchJob( + net::URLRequest* request, + net::NetworkDelegate* network_delegate, + const GURL& url) + : net::URLRequestJob(request, network_delegate), + url_(url), + weak_ptr_factory_(this) {} + +URLRequestFetchJob::~URLRequestFetchJob() {} + +void URLRequestFetchJob::Start() { + net::URLFetcher* fetcher = net::URLFetcher::Create(url_, + net::URLFetcher::GET, + this); + auto context = AtomBrowserContext::Get()->url_request_context_getter(); + fetcher->SetRequestContext(context); + fetcher->SaveResponseWithWriter(scoped_ptr( + this)); + fetcher->Start(); +} + +void URLRequestFetchJob::Kill() { + weak_ptr_factory_.InvalidateWeakPtrs(); + URLRequestJob::Kill(); +} + +bool URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, + int dest_size, + int* bytes_read) { + if (!dest_size) { + *bytes_read = 0; + return true; + } + + int to_read = dest_size < buffer_->BytesRemaining() ? + dest_size : buffer_->BytesRemaining(); + memcpy(dest->data(), buffer_->data(), to_read); + buffer_->DidConsume(to_read); + if (!buffer_->BytesRemaining()) { + NotifyReadComplete(buffer_->size()); + return true; + } + + *bytes_read = to_read; + return false; +} + +void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) { + if (!source->GetStatus().is_success()) + NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, + source->GetResponseCode())); + + NotifyHeadersComplete(); +} + + +int URLRequestFetchJob::Initialize(const net::CompletionCallback& callback) { + if (buffer_) + buffer_->Release(); + return net::OK; +} + +int URLRequestFetchJob::Write(net::IOBuffer* buffer, + int num_bytes, + const net::CompletionCallback& calback) { + buffer_ = new net::DrainableIOBuffer(buffer, num_bytes); + set_expected_content_size(num_bytes); + return num_bytes; +} + +int URLRequestFetchJob::Finish(const net::CompletionCallback& callback) { + return net::OK; +} + +} // namespace atom diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h new file mode 100644 index 000000000000..e955341c7d50 --- /dev/null +++ b/atom/browser/net/url_request_fetch_job.h @@ -0,0 +1,57 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ +#define ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ + +#include "base/memory/ref_counted.h" +#include "base/memory/weak_ptr.h" +#include "net/base/io_buffer.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_fetcher_delegate.h" +#include "net/url_request/url_fetcher_response_writer.h" +#include "net/url_request/url_request_job.h" + +namespace atom { + +class URLRequestFetchJob : public net::URLRequestJob, + public net::URLFetcherDelegate, + public net::URLFetcherResponseWriter { + public: + URLRequestFetchJob(net::URLRequest* request, + net::NetworkDelegate* network_delegate, + const GURL& url); + + // net::URLRequestJob: + void Start() override; + void Kill() override; + bool ReadRawData(net::IOBuffer* buf, + int buf_size, + int* bytes_read) override; + + // net::URLFetcherDelegate: + void OnURLFetchComplete(const net::URLFetcher* source) override; + + // net::URLFetchResponseWriter: + int Initialize(const net::CompletionCallback& callback) override; + int Write(net::IOBuffer* buffer, + int num_bytes, + const net::CompletionCallback& callback) override; + int Finish(const net::CompletionCallback& callback) override; + + protected: + virtual ~URLRequestFetchJob(); + + private: + GURL url_; + scoped_refptr buffer_; + + base::WeakPtrFactory weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(URLRequestFetchJob); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ diff --git a/filenames.gypi b/filenames.gypi index 6ece2ba634c7..ab523c188beb 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -147,11 +147,13 @@ 'atom/browser/net/atom_url_request_job_factory.cc', 'atom/browser/net/atom_url_request_job_factory.h', 'atom/browser/net/http_protocol_handler.cc', - 'atom/browser/net/http_protocol_handler.h', + 'atom/browser/net/http_protocol_handler.h', 'atom/browser/net/url_request_string_job.cc', 'atom/browser/net/url_request_string_job.h', 'atom/browser/net/url_request_buffer_job.cc', 'atom/browser/net/url_request_buffer_job.h', + 'atom/browser/net/url_request_fetch_job.cc', + 'atom/browser/net/url_request_fetch_job.h', 'atom/browser/ui/accelerator_util.cc', 'atom/browser/ui/accelerator_util.h', 'atom/browser/ui/accelerator_util_mac.mm', diff --git a/spec/api-protocol-spec.coffee b/spec/api-protocol-spec.coffee index 5a7bda16555d..e3cc98cd7c4e 100644 --- a/spec/api-protocol-spec.coffee +++ b/spec/api-protocol-spec.coffee @@ -1,5 +1,6 @@ assert = require 'assert' ipc = require 'ipc' +http = require 'http' path = require 'path' remote = require 'remote' protocol = remote.require 'protocol' @@ -73,6 +74,28 @@ describe 'protocol module', -> protocol.unregisterProtocol 'atom-error-job' done() + it 'returns RequestHttpJob should send respone', (done) -> + server = http.createServer (req, res) -> + res.writeHead(200, {'Content-Type': 'text/plain'}) + res.end('hello') + server.close() + server.listen 0, '127.0.0.1', -> + {port} = server.address() + url = "http://127.0.0.1:#{port}" + job = new protocol.RequestHttpJob(url) + handler = remote.createFunctionWithReturnValue job + protocol.registerProtocol 'atom-http-job', handler + + $.ajax + url: 'atom-http-job://fake-host' + success: (data) -> + assert.equal data, 'hello' + protocol.unregisterProtocol 'atom-http-job' + done() + error: (xhr, errorType, error) -> + assert false, 'Got error: ' + errorType + ' ' + error + protocol.unregisterProtocol 'atom-http-job' + it 'returns RequestBufferJob should send buffer', (done) -> data = new Buffer("hello") job = new protocol.RequestBufferJob(data: data) From 44f8bfc550cb3fe0a103c2cdd3209a2741ebb2de Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 16 Jun 2015 17:08:53 +0800 Subject: [PATCH 02/11] Don't leak URLFetcher --- atom/browser/net/url_request_fetch_job.cc | 10 ++++------ atom/browser/net/url_request_fetch_job.h | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 081e85f28f32..3f4ef0694e70 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -24,14 +24,12 @@ URLRequestFetchJob::URLRequestFetchJob( URLRequestFetchJob::~URLRequestFetchJob() {} void URLRequestFetchJob::Start() { - net::URLFetcher* fetcher = net::URLFetcher::Create(url_, - net::URLFetcher::GET, - this); + fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); auto context = AtomBrowserContext::Get()->url_request_context_getter(); - fetcher->SetRequestContext(context); - fetcher->SaveResponseWithWriter(scoped_ptr( + fetcher_->SetRequestContext(context); + fetcher_->SaveResponseWithWriter(scoped_ptr( this)); - fetcher->Start(); + fetcher_->Start(); } void URLRequestFetchJob::Kill() { diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index e955341c7d50..69726f9db45b 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -45,6 +45,7 @@ class URLRequestFetchJob : public net::URLRequestJob, private: GURL url_; + scoped_ptr fetcher_; scoped_refptr buffer_; base::WeakPtrFactory weak_ptr_factory_; From af05f26a5f9b124cdf894483a1f35f4592b454be Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 09:31:33 +0800 Subject: [PATCH 03/11] Make URLRequestFetchJob actually work --- atom/browser/api/atom_api_protocol.cc | 3 +- atom/browser/api/lib/protocol.coffee | 2 +- atom/browser/net/adapter_request_job.cc | 13 +++ atom/browser/net/adapter_request_job.h | 2 + atom/browser/net/url_request_fetch_job.cc | 136 +++++++++++++++------- atom/browser/net/url_request_fetch_job.h | 35 +++--- 6 files changed, 130 insertions(+), 61 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index eede89233201..48c4f483c593 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -116,8 +116,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob { GetWeakPtr(), path)); return; } else if (name == "RequestErrorJob") { - // Default value net::ERR_NOT_IMPLEMENTED - int error = -11; + int error = net::ERR_NOT_IMPLEMENTED; dict.Get("error", &error); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, diff --git a/atom/browser/api/lib/protocol.coffee b/atom/browser/api/lib/protocol.coffee index bbd2499a8371..b1084cf86bd7 100644 --- a/atom/browser/api/lib/protocol.coffee +++ b/atom/browser/api/lib/protocol.coffee @@ -36,6 +36,6 @@ class RequestErrorJob protocol.RequestHttpJob = class RequestHttpJob - constructor: (@url) -> + constructor: ({@url}) -> module.exports = protocol diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index 258416e2f42a..6b493bbc496e 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -68,6 +68,14 @@ bool AdapterRequestJob::GetCharset(std::string* charset) { return real_job_->GetCharset(charset); } +void AdapterRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { + real_job_->GetResponseInfo(info); +} + +int AdapterRequestJob::GetResponseCode() const { + return real_job_->GetResponseCode(); +} + base::WeakPtr AdapterRequestJob::GetWeakPtr() { return weak_factory_.GetWeakPtr(); } @@ -107,6 +115,11 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { } void AdapterRequestJob::CreateHttpJobAndStart(const GURL& url) { + if (!url.is_valid()) { + CreateErrorJobAndStart(net::ERR_INVALID_URL); + return; + } + real_job_ = new URLRequestFetchJob(request(), network_delegate(), url); real_job_->Start(); } diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index 1ecc82230b76..a5d591cd3ce9 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -41,6 +41,8 @@ class AdapterRequestJob : public net::URLRequestJob { net::Filter* SetupFilter() const override; bool GetMimeType(std::string* mime_type) const override; bool GetCharset(std::string* charset) override; + void GetResponseInfo(net::HttpResponseInfo* info) override; + int GetResponseCode() const override; base::WeakPtr GetWeakPtr(); diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 3f4ef0694e70..3754a1538d49 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -4,85 +4,141 @@ #include "atom/browser/net/url_request_fetch_job.h" -#include +#include +#include #include "atom/browser/atom_browser_context.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_fetcher.h" +#include "net/url_request/url_fetcher_response_writer.h" #include "net/url_request/url_request_status.h" namespace atom { +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 + URLRequestFetchJob::URLRequestFetchJob( net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url) : net::URLRequestJob(request, network_delegate), url_(url), - weak_ptr_factory_(this) {} + finished_(false), + weak_factory_(this) {} -URLRequestFetchJob::~URLRequestFetchJob() {} +void URLRequestFetchJob::HeadersCompleted() { + response_info_.reset(new net::HttpResponseInfo); + response_info_->headers = fetcher_->GetResponseHeaders(); + NotifyHeadersComplete(); +} + +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()); +} void URLRequestFetchJob::Start() { fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); auto context = AtomBrowserContext::Get()->url_request_context_getter(); fetcher_->SetRequestContext(context); - fetcher_->SaveResponseWithWriter(scoped_ptr( - this)); + fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); fetcher_->Start(); } void URLRequestFetchJob::Kill() { - weak_ptr_factory_.InvalidateWeakPtrs(); URLRequestJob::Kill(); + fetcher_.reset(); } bool URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, int dest_size, int* bytes_read) { - if (!dest_size) { + if (finished_) { *bytes_read = 0; return true; } - int to_read = dest_size < buffer_->BytesRemaining() ? - dest_size : buffer_->BytesRemaining(); - memcpy(dest->data(), buffer_->data(), to_read); - buffer_->DidConsume(to_read); - if (!buffer_->BytesRemaining()) { - NotifyReadComplete(buffer_->size()); - return true; + if (buffer_.size() == 0) { + SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); + return false; } - *bytes_read = to_read; - return false; + if (static_cast(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; +} + +bool URLRequestFetchJob::GetMimeType(std::string* mime_type) const { + if (!response_info_) + return false; + + return response_info_->headers->GetMimeType(mime_type); +} + +void URLRequestFetchJob::GetResponseInfo(net::HttpResponseInfo* info) { + if (response_info_) + *info = *response_info_; +} + +int URLRequestFetchJob::GetResponseCode() const { + if (!response_info_) + return -1; + + return response_info_->headers->response_code(); } void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) { - if (!source->GetStatus().is_success()) - NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, - source->GetResponseCode())); - - NotifyHeadersComplete(); -} - - -int URLRequestFetchJob::Initialize(const net::CompletionCallback& callback) { - if (buffer_) - buffer_->Release(); - return net::OK; -} - -int URLRequestFetchJob::Write(net::IOBuffer* buffer, - int num_bytes, - const net::CompletionCallback& calback) { - buffer_ = new net::DrainableIOBuffer(buffer, num_bytes); - set_expected_content_size(num_bytes); - return num_bytes; -} - -int URLRequestFetchJob::Finish(const net::CompletionCallback& callback) { - return net::OK; + finished_ = true; } } // namespace atom diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index 69726f9db45b..df7ad54d5937 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -5,50 +5,49 @@ #ifndef ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ #define ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ -#include "base/memory/ref_counted.h" +#include + #include "base/memory/weak_ptr.h" -#include "net/base/io_buffer.h" -#include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher_delegate.h" -#include "net/url_request/url_fetcher_response_writer.h" #include "net/url_request/url_request_job.h" namespace atom { class URLRequestFetchJob : public net::URLRequestJob, - public net::URLFetcherDelegate, - public net::URLFetcherResponseWriter { + public net::URLFetcherDelegate { public: URLRequestFetchJob(net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url); + base::WeakPtr GetWeakPtr() { + return weak_factory_.GetWeakPtr(); + }; + + void HeadersCompleted(); + void DataAvailable(net::IOBuffer* buffer, int num_bytes); + // net::URLRequestJob: void Start() override; void Kill() override; bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; + bool GetMimeType(std::string* mime_type) const override; + void GetResponseInfo(net::HttpResponseInfo* info) override; + int GetResponseCode() const override; // net::URLFetcherDelegate: void OnURLFetchComplete(const net::URLFetcher* source) override; - // net::URLFetchResponseWriter: - int Initialize(const net::CompletionCallback& callback) override; - int Write(net::IOBuffer* buffer, - int num_bytes, - const net::CompletionCallback& callback) override; - int Finish(const net::CompletionCallback& callback) override; - - protected: - virtual ~URLRequestFetchJob(); - private: GURL url_; scoped_ptr fetcher_; - scoped_refptr buffer_; + std::vector buffer_; + scoped_ptr response_info_; + bool finished_; - base::WeakPtrFactory weak_ptr_factory_; + base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(URLRequestFetchJob); }; From 81db8e098ed4bbd28afc407fc6a9f1fea025bff9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 10:19:58 +0800 Subject: [PATCH 04/11] Don't need buffer for piping data --- atom/browser/net/url_request_fetch_job.cc | 56 +++++++++-------------- atom/browser/net/url_request_fetch_job.h | 15 ++---- spec/api-protocol-spec.coffee | 2 +- 3 files changed, 26 insertions(+), 47 deletions(-) diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 3754a1538d49..4dc0b5a7c9c5 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -5,7 +5,6 @@ #include "atom/browser/net/url_request_fetch_job.h" #include -#include #include "atom/browser/atom_browser_context.h" #include "net/base/io_buffer.h" @@ -32,14 +31,13 @@ class ResponsePiper : public net::URLFetcherResponseWriter { 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; + return job_->DataAvailable(buffer, num_bytes); } int Finish(const net::CompletionCallback& callback) override { return net::OK; @@ -60,8 +58,7 @@ URLRequestFetchJob::URLRequestFetchJob( const GURL& url) : net::URLRequestJob(request, network_delegate), url_(url), - finished_(false), - weak_factory_(this) {} + pending_buffer_size_(0) {} void URLRequestFetchJob::HeadersCompleted() { response_info_.reset(new net::HttpResponseInfo); @@ -69,12 +66,21 @@ void URLRequestFetchJob::HeadersCompleted() { NotifyHeadersComplete(); } -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); +int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) { + // Clear the IO_PENDING status. SetStatus(net::URLRequestStatus()); + // Do nothing if pending_buffer_ is empty, i.e. there's no ReadRawData() + // operation waiting for IO completion. + if (!pending_buffer_.get()) + return net::ERR_IO_PENDING;; + + // pending_buffer_ is set to the IOBuffer instance provided to ReadRawData() + // by URLRequestJob. + + int bytes_read = std::min(num_bytes, pending_buffer_size_); + memcpy(pending_buffer_->data(), buffer->data(), bytes_read); + NotifyReadComplete(bytes_read); + return bytes_read; } void URLRequestFetchJob::Start() { @@ -93,29 +99,10 @@ void URLRequestFetchJob::Kill() { bool URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, int dest_size, int* bytes_read) { - if (finished_) { - *bytes_read = 0; - return true; - } - - if (buffer_.size() == 0) { - SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); - return false; - } - - if (static_cast(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; + pending_buffer_ = dest; + pending_buffer_size_ = dest_size; + SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); + return false; } bool URLRequestFetchJob::GetMimeType(std::string* mime_type) const { @@ -138,7 +125,8 @@ int URLRequestFetchJob::GetResponseCode() const { } void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) { - finished_ = true; + NotifyDone(fetcher_->GetStatus()); + NotifyReadComplete(0); } } // namespace atom diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index df7ad54d5937..b608b980bc52 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -5,9 +5,6 @@ #ifndef ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ #define ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ -#include - -#include "base/memory/weak_ptr.h" #include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_request_job.h" @@ -20,12 +17,8 @@ class URLRequestFetchJob : public net::URLRequestJob, net::NetworkDelegate* network_delegate, const GURL& url); - base::WeakPtr GetWeakPtr() { - return weak_factory_.GetWeakPtr(); - }; - void HeadersCompleted(); - void DataAvailable(net::IOBuffer* buffer, int num_bytes); + int DataAvailable(net::IOBuffer* buffer, int num_bytes); // net::URLRequestJob: void Start() override; @@ -43,11 +36,9 @@ class URLRequestFetchJob : public net::URLRequestJob, private: GURL url_; scoped_ptr fetcher_; - std::vector buffer_; + scoped_refptr pending_buffer_; + int pending_buffer_size_; scoped_ptr response_info_; - bool finished_; - - base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(URLRequestFetchJob); }; diff --git a/spec/api-protocol-spec.coffee b/spec/api-protocol-spec.coffee index e3cc98cd7c4e..3ad1cbd91fad 100644 --- a/spec/api-protocol-spec.coffee +++ b/spec/api-protocol-spec.coffee @@ -82,7 +82,7 @@ describe 'protocol module', -> server.listen 0, '127.0.0.1', -> {port} = server.address() url = "http://127.0.0.1:#{port}" - job = new protocol.RequestHttpJob(url) + job = new protocol.RequestHttpJob({url}) handler = remote.createFunctionWithReturnValue job protocol.registerProtocol 'atom-http-job', handler From 274854876c54d21806cf0324486350544da489cb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 10:57:26 +0800 Subject: [PATCH 05/11] Allow setting method for RequestHttpJob --- atom/browser/api/atom_api_protocol.cc | 4 ++- atom/browser/api/lib/protocol.coffee | 2 +- atom/browser/net/adapter_request_job.cc | 6 ++-- atom/browser/net/adapter_request_job.h | 2 +- atom/browser/net/url_request_fetch_job.cc | 38 ++++++++++++++++++----- atom/browser/net/url_request_fetch_job.h | 4 +-- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 48c4f483c593..186e36a52210 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -125,11 +125,13 @@ class CustomProtocolRequestJob : public AdapterRequestJob { return; } else if (name == "RequestHttpJob") { GURL url; + std::string method; dict.Get("url", &url); + dict.Get("method", &method); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, - GetWeakPtr(), url)); + GetWeakPtr(), url, method)); return; } } diff --git a/atom/browser/api/lib/protocol.coffee b/atom/browser/api/lib/protocol.coffee index b1084cf86bd7..2f6617a173c7 100644 --- a/atom/browser/api/lib/protocol.coffee +++ b/atom/browser/api/lib/protocol.coffee @@ -36,6 +36,6 @@ class RequestErrorJob protocol.RequestHttpJob = class RequestHttpJob - constructor: ({@url}) -> + constructor: ({@url, @method}) -> module.exports = protocol diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index 6b493bbc496e..ff5d4deaa595 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -114,13 +114,15 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { real_job_->Start(); } -void AdapterRequestJob::CreateHttpJobAndStart(const GURL& url) { +void AdapterRequestJob::CreateHttpJobAndStart(const GURL& url, + const std::string& method) { if (!url.is_valid()) { CreateErrorJobAndStart(net::ERR_INVALID_URL); return; } - real_job_ = new URLRequestFetchJob(request(), network_delegate(), url); + real_job_ = new URLRequestFetchJob(request(), network_delegate(), url, + method); real_job_->Start(); } diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index a5d591cd3ce9..4c173f0de825 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -59,7 +59,7 @@ class AdapterRequestJob : public net::URLRequestJob { const std::string& charset, scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); - void CreateHttpJobAndStart(const GURL& url); + void CreateHttpJobAndStart(const GURL& url, const std::string& method); void CreateJobFromProtocolHandlerAndStart(); private: diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 4dc0b5a7c9c5..ad5f2aed62b7 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -7,6 +7,7 @@ #include #include "atom/browser/atom_browser_context.h" +#include "base/strings/string_util.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" #include "net/http/http_response_headers.h" @@ -18,6 +19,25 @@ namespace atom { namespace { +// Convert string to RequestType. +net::URLFetcher::RequestType GetRequestType(const std::string& raw) { + std::string method = StringToUpperASCII(raw); + if (method.empty() || method == "GET") + return net::URLFetcher::GET; + else if (method == "POST") + return net::URLFetcher::POST; + else if (method == "HEAD") + return net::URLFetcher::HEAD; + else if (method == "DELETE") + return net::URLFetcher::DELETE_REQUEST; + else if (method == "PUT") + return net::URLFetcher::PUT; + else if (method == "PATCH") + return net::URLFetcher::PATCH; + else // Use "GET" as fallback. + return net::URLFetcher::GET; +} + // Pipe the response writer back to URLRequestFetchJob. class ResponsePiper : public net::URLFetcherResponseWriter { public: @@ -55,10 +75,15 @@ class ResponsePiper : public net::URLFetcherResponseWriter { URLRequestFetchJob::URLRequestFetchJob( net::URLRequest* request, net::NetworkDelegate* network_delegate, - const GURL& url) + const GURL& url, + const std::string& method) : net::URLRequestJob(request, network_delegate), - url_(url), - pending_buffer_size_(0) {} + fetcher_(net::URLFetcher::Create(url, GetRequestType(method), this)), + pending_buffer_size_(0) { + auto context = AtomBrowserContext::Get()->url_request_context_getter(); + fetcher_->SetRequestContext(context); + fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); +} void URLRequestFetchJob::HeadersCompleted() { response_info_.reset(new net::HttpResponseInfo); @@ -84,10 +109,6 @@ int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) { } void URLRequestFetchJob::Start() { - fetcher_.reset(net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); - auto context = AtomBrowserContext::Get()->url_request_context_getter(); - fetcher_->SetRequestContext(context); - fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); fetcher_->Start(); } @@ -126,7 +147,8 @@ int URLRequestFetchJob::GetResponseCode() const { void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) { NotifyDone(fetcher_->GetStatus()); - NotifyReadComplete(0); + if (fetcher_->GetStatus().is_success()) + NotifyReadComplete(0); } } // namespace atom diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index b608b980bc52..23a4a9f8dd72 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -15,7 +15,8 @@ class URLRequestFetchJob : public net::URLRequestJob, public: URLRequestFetchJob(net::URLRequest* request, net::NetworkDelegate* network_delegate, - const GURL& url); + const GURL& url, + const std::string& method); void HeadersCompleted(); int DataAvailable(net::IOBuffer* buffer, int num_bytes); @@ -34,7 +35,6 @@ class URLRequestFetchJob : public net::URLRequestJob, void OnURLFetchComplete(const net::URLFetcher* source) override; private: - GURL url_; scoped_ptr fetcher_; scoped_refptr pending_buffer_; int pending_buffer_size_; From 66c4c7e77b39b45538c943b3f35449a810d2a117 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 11:04:15 +0800 Subject: [PATCH 06/11] Clear pending_buffer_ at correct time --- atom/browser/net/url_request_fetch_job.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index ad5f2aed62b7..7781f957cc1a 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -104,6 +104,12 @@ int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) { int bytes_read = std::min(num_bytes, pending_buffer_size_); memcpy(pending_buffer_->data(), buffer->data(), bytes_read); + + // Clear the buffers before notifying the read is complete, so that it is + // safe for the observer to read. + pending_buffer_ = nullptr; + pending_buffer_size_ = 0; + NotifyReadComplete(bytes_read); return bytes_read; } @@ -146,6 +152,8 @@ int URLRequestFetchJob::GetResponseCode() const { } void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) { + pending_buffer_ = nullptr; + pending_buffer_size_ = 0; NotifyDone(fetcher_->GetStatus()); if (fetcher_->GetStatus().is_success()) NotifyReadComplete(0); From e07f5cd53f0efb8997d2b0f983f3607ddb0c8312 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 11:11:13 +0800 Subject: [PATCH 07/11] Use |request|'s method if |method| is not specified --- atom/browser/net/url_request_fetch_job.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 7781f957cc1a..c064ee6d913a 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -78,8 +78,15 @@ URLRequestFetchJob::URLRequestFetchJob( const GURL& url, const std::string& method) : net::URLRequestJob(request, network_delegate), - fetcher_(net::URLFetcher::Create(url, GetRequestType(method), this)), pending_buffer_size_(0) { + // Use |request|'s method if |method| is not specified. + net::URLFetcher::RequestType request_type; + if (method.empty()) + request_type = GetRequestType(request->method()); + else + request_type = GetRequestType(method); + + fetcher_.reset(net::URLFetcher::Create(url, request_type, this)); auto context = AtomBrowserContext::Get()->url_request_context_getter(); fetcher_->SetRequestContext(context); fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); From 543c4d55972b7958807e20fa1b1d7f13b8ba168d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 11:20:09 +0800 Subject: [PATCH 08/11] Allow setting referrer --- atom/browser/api/atom_api_protocol.cc | 5 +++-- atom/browser/api/lib/protocol.coffee | 2 +- atom/browser/net/adapter_request_job.cc | 5 +++-- atom/browser/net/adapter_request_job.h | 4 +++- atom/browser/net/url_request_fetch_job.cc | 9 ++++++++- atom/browser/net/url_request_fetch_job.h | 3 ++- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index 186e36a52210..9bc2de4b42a9 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -125,13 +125,14 @@ class CustomProtocolRequestJob : public AdapterRequestJob { return; } else if (name == "RequestHttpJob") { GURL url; - std::string method; + std::string method, referrer; dict.Get("url", &url); dict.Get("method", &method); + dict.Get("referrer", &referrer); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(&AdapterRequestJob::CreateHttpJobAndStart, - GetWeakPtr(), url, method)); + GetWeakPtr(), url, method, referrer)); return; } } diff --git a/atom/browser/api/lib/protocol.coffee b/atom/browser/api/lib/protocol.coffee index 2f6617a173c7..ff4bc5ba9a66 100644 --- a/atom/browser/api/lib/protocol.coffee +++ b/atom/browser/api/lib/protocol.coffee @@ -36,6 +36,6 @@ class RequestErrorJob protocol.RequestHttpJob = class RequestHttpJob - constructor: ({@url, @method}) -> + constructor: ({@url, @method, @referrer}) -> module.exports = protocol diff --git a/atom/browser/net/adapter_request_job.cc b/atom/browser/net/adapter_request_job.cc index ff5d4deaa595..08331829b550 100644 --- a/atom/browser/net/adapter_request_job.cc +++ b/atom/browser/net/adapter_request_job.cc @@ -115,14 +115,15 @@ void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) { } void AdapterRequestJob::CreateHttpJobAndStart(const GURL& url, - const std::string& method) { + const std::string& method, + const std::string& referrer) { if (!url.is_valid()) { CreateErrorJobAndStart(net::ERR_INVALID_URL); return; } real_job_ = new URLRequestFetchJob(request(), network_delegate(), url, - method); + method, referrer); real_job_->Start(); } diff --git a/atom/browser/net/adapter_request_job.h b/atom/browser/net/adapter_request_job.h index 4c173f0de825..d5e814d214d4 100644 --- a/atom/browser/net/adapter_request_job.h +++ b/atom/browser/net/adapter_request_job.h @@ -59,7 +59,9 @@ class AdapterRequestJob : public net::URLRequestJob { const std::string& charset, scoped_refptr data); void CreateFileJobAndStart(const base::FilePath& path); - void CreateHttpJobAndStart(const GURL& url, const std::string& method); + void CreateHttpJobAndStart(const GURL& url, + const std::string& method, + const std::string& referrer); void CreateJobFromProtocolHandlerAndStart(); private: diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index c064ee6d913a..35e46a8f4f8d 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -76,7 +76,8 @@ URLRequestFetchJob::URLRequestFetchJob( net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, - const std::string& method) + const std::string& method, + const std::string& referrer) : net::URLRequestJob(request, network_delegate), pending_buffer_size_(0) { // Use |request|'s method if |method| is not specified. @@ -90,6 +91,12 @@ URLRequestFetchJob::URLRequestFetchJob( auto context = AtomBrowserContext::Get()->url_request_context_getter(); fetcher_->SetRequestContext(context); fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); + + // Use |request|'s referrer if |referrer| is not specified. + if (referrer.empty()) + fetcher_->SetReferrer(request->referrer()); + else + fetcher_->SetReferrer(referrer); } void URLRequestFetchJob::HeadersCompleted() { diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index 23a4a9f8dd72..affa7b331f3e 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -16,7 +16,8 @@ class URLRequestFetchJob : public net::URLRequestJob, URLRequestFetchJob(net::URLRequest* request, net::NetworkDelegate* network_delegate, const GURL& url, - const std::string& method); + const std::string& method, + const std::string& referrer); void HeadersCompleted(); int DataAvailable(net::IOBuffer* buffer, int num_bytes); From 92f3371118c39c1a22a1c72b734a50410c947ff7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 11:30:31 +0800 Subject: [PATCH 09/11] Use |request|'s headers if possible --- atom/browser/net/url_request_fetch_job.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 35e46a8f4f8d..bff3332a3f54 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -97,6 +97,12 @@ URLRequestFetchJob::URLRequestFetchJob( fetcher_->SetReferrer(request->referrer()); else fetcher_->SetReferrer(referrer); + + // Use |request|'s headers. + net::HttpRequestHeaders headers; + if (request->GetFullRequestHeaders(&headers)) { + fetcher_->SetExtraRequestHeaders(headers.ToString()); + } } void URLRequestFetchJob::HeadersCompleted() { From 1d4190377901ff5d6b462498de884374f1e4e339 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 11:32:39 +0800 Subject: [PATCH 10/11] docs: protocol.RequestHttpJob --- docs/api/protocol.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/api/protocol.md b/docs/api/protocol.md index 181a2e57f733..ba62bf301f41 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -84,12 +84,21 @@ Create a request job which sends a string as response. Create a request job which sends a buffer as response. +## Class: protocol.RequestHttpJob(options) + +* `options` Object + * `url` String + * `method` String - Default is `GET` + * `referrer` String + +Send a request to `url` and pipe the response back. + ## Class: protocol.RequestErrorJob(code) * `code` Integer Create a request job which sets appropriate network error message to console. -Default message is `net::ERR_NOT_IMPLEMENTED`. Code should be in the following +Default message is `net::ERR_NOT_IMPLEMENTED`. Code should be in the following range. * Ranges: From c2aa7d538fc56774164887da32041b08aa2d0cdf Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 11:34:47 +0800 Subject: [PATCH 11/11] Fix cpplint warnings --- atom/browser/net/url_request_fetch_job.cc | 8 +++++--- atom/browser/net/url_request_fetch_job.h | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index bff3332a3f54..f77379aed6c2 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -5,6 +5,7 @@ #include "atom/browser/net/url_request_fetch_job.h" #include +#include #include "atom/browser/atom_browser_context.h" #include "base/strings/string_util.h" @@ -93,10 +94,11 @@ URLRequestFetchJob::URLRequestFetchJob( fetcher_->SaveResponseWithWriter(make_scoped_ptr(new ResponsePiper(this))); // Use |request|'s referrer if |referrer| is not specified. - if (referrer.empty()) + if (referrer.empty()) { fetcher_->SetReferrer(request->referrer()); - else + } else { fetcher_->SetReferrer(referrer); + } // Use |request|'s headers. net::HttpRequestHeaders headers; @@ -117,7 +119,7 @@ int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer, int num_bytes) { // Do nothing if pending_buffer_ is empty, i.e. there's no ReadRawData() // operation waiting for IO completion. if (!pending_buffer_.get()) - return net::ERR_IO_PENDING;; + return net::ERR_IO_PENDING; // pending_buffer_ is set to the IOBuffer instance provided to ReadRawData() // by URLRequestJob. diff --git a/atom/browser/net/url_request_fetch_job.h b/atom/browser/net/url_request_fetch_job.h index affa7b331f3e..7975aa715eec 100644 --- a/atom/browser/net/url_request_fetch_job.h +++ b/atom/browser/net/url_request_fetch_job.h @@ -5,6 +5,8 @@ #ifndef ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ #define ATOM_BROWSER_NET_URL_REQUEST_FETCH_JOB_H_ +#include + #include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_request_job.h"