From 274854876c54d21806cf0324486350544da489cb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Jun 2015 10:57:26 +0800 Subject: [PATCH] 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 48c4f48..186e36a 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 b1084cf..2f6617a 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 6b493bb..ff5d4de 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 a5d591c..4c173f0 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 4dc0b5a..ad5f2ae 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 b608b98..23a4a9f 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_; -- 2.7.4