Allow setting method for RequestHttpJob
This commit is contained in:
parent
81db8e098e
commit
274854876c
6 changed files with 41 additions and 15 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,6 @@ class RequestErrorJob
|
|||
|
||||
protocol.RequestHttpJob =
|
||||
class RequestHttpJob
|
||||
constructor: ({@url}) ->
|
||||
constructor: ({@url, @method}) ->
|
||||
|
||||
module.exports = protocol
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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_;
|
||||
|
|
Loading…
Reference in a new issue