Allow setting method for RequestHttpJob
authorCheng Zhao <zcbenz@gmail.com>
Wed, 17 Jun 2015 02:57:26 +0000 (10:57 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 17 Jun 2015 02:57:26 +0000 (10:57 +0800)
atom/browser/api/atom_api_protocol.cc
atom/browser/api/lib/protocol.coffee
atom/browser/net/adapter_request_job.cc
atom/browser/net/adapter_request_job.h
atom/browser/net/url_request_fetch_job.cc
atom/browser/net/url_request_fetch_job.h

index 48c4f48..186e36a 100644 (file)
@@ -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;
       }
     }
index b1084cf..2f6617a 100644 (file)
@@ -36,6 +36,6 @@ class RequestErrorJob
 
 protocol.RequestHttpJob =
 class RequestHttpJob
-  constructor: ({@url}) ->
+  constructor: ({@url, @method}) ->
 
 module.exports = protocol
index 6b493bb..ff5d4de 100644 (file)
@@ -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();
 }
 
index a5d591c..4c173f0 100644 (file)
@@ -59,7 +59,7 @@ class AdapterRequestJob : public net::URLRequestJob {
                                const std::string& charset,
                                scoped_refptr<base::RefCountedBytes> data);
   void CreateFileJobAndStart(const base::FilePath& path);
-  void CreateHttpJobAndStart(const GURL& url);
+  void CreateHttpJobAndStart(const GURL& url, const std::string& method);
   void CreateJobFromProtocolHandlerAndStart();
 
  private:
index 4dc0b5a..ad5f2ae 100644 (file)
@@ -7,6 +7,7 @@
 #include <algorithm>
 
 #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
index b608b98..23a4a9f 100644 (file)
@@ -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<net::URLFetcher> fetcher_;
   scoped_refptr<net::IOBuffer> pending_buffer_;
   int pending_buffer_size_;