Caching response headers so that AtomURLRequest can be freed after the close event.

This commit is contained in:
ali.ibrahim 2016-10-04 17:33:34 +02:00
parent ec1fc5a17b
commit e472d11761
4 changed files with 28 additions and 29 deletions

View file

@ -51,7 +51,7 @@ struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
auto size = node::Buffer::Length(val);
if (size == 0) {
// Support conversoin from empty buffer. A use case is
// Support conversion from empty buffer. A use case is
// a GET request without body.
// Since zero-sized IOBuffer(s) are not supported, we set the
// out pointer to null.
@ -60,7 +60,7 @@ struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
}
auto data = node::Buffer::Data(val);
if (!data) {
// This is an error as size is positif but data is null.
// This is an error as size is positive but data is null.
return false;
}
@ -333,13 +333,15 @@ void URLRequest::OnAuthenticationRequired(
base::Bind(&AtomURLRequest::PassLoginInformation, atom_request_));
}
void URLRequest::OnResponseStarted() {
void URLRequest::OnResponseStarted(
scoped_refptr<const net::HttpResponseHeaders> response_headers) {
if (request_state_.Canceled() ||
request_state_.Failed() ||
request_state_.Closed()) {
// Don't emit any event after request cancel.
return;
}
response_headers_ = response_headers;
response_state_.SetFlag(ResponseStateFlags::kStarted);
Emit("response");
}
@ -386,35 +388,35 @@ void URLRequest::OnResponseError(const std::string& error) {
int URLRequest::StatusCode() const {
if (auto response_headers = atom_request_->GetResponseHeaders()) {
return response_headers->response_code();
if (response_headers_) {
return response_headers_->response_code();
}
return -1;
}
std::string URLRequest::StatusMessage() const {
std::string result;
if (auto response_headers = atom_request_->GetResponseHeaders()) {
result = response_headers->GetStatusText();
if (response_headers_) {
result = response_headers_->GetStatusText();
}
return result;
}
scoped_refptr<const net::HttpResponseHeaders>
URLRequest::RawResponseHeaders() const {
return atom_request_->GetResponseHeaders();
return response_headers_;
}
uint32_t URLRequest::ResponseHttpVersionMajor() const {
if (auto response_headers = atom_request_->GetResponseHeaders()) {
return response_headers->GetHttpVersion().major_value();
if (response_headers_) {
return response_headers_->GetHttpVersion().major_value();
}
return 0;
}
uint32_t URLRequest::ResponseHttpVersionMinor() const {
if (auto response_headers = atom_request_->GetResponseHeaders()) {
return response_headers->GetHttpVersion().minor_value();
if (response_headers_) {
return response_headers_->GetHttpVersion().minor_value();
}
return 0;
}

View file

@ -100,7 +100,8 @@ class URLRequest : public mate::EventEmitter<URLRequest> {
// Methods for reporting events into JavaScript.
void OnAuthenticationRequired(
scoped_refptr<const net::AuthChallengeInfo> auth_info);
void OnResponseStarted();
void OnResponseStarted(
scoped_refptr<const net::HttpResponseHeaders> response_headers);
void OnResponseData(scoped_refptr<const net::IOBufferWithSize> data);
void OnResponseCompleted();
void OnRequestError(const std::string& error);
@ -202,7 +203,7 @@ class URLRequest : public mate::EventEmitter<URLRequest> {
// Used to implement pin/unpin.
v8::Global<v8::Object> wrapper_;
scoped_refptr<const net::HttpResponseHeaders> response_headers_;
base::WeakPtrFactory<URLRequest> weak_ptr_factory_;

View file

@ -126,15 +126,6 @@ void AtomURLRequest::RemoveExtraHeader(const std::string& name) const {
request_->RemoveRequestHeaderByName(name);
}
scoped_refptr<const net::HttpResponseHeaders>
AtomURLRequest::GetResponseHeaders() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return request_->response_headers();
}
void AtomURLRequest::PassLoginInformation(const base::string16& username,
const base::string16& password) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
@ -173,7 +164,7 @@ void AtomURLRequest::DoWriteBuffer(
buffer->size(),
is_last);
else if (is_last)
// Empty buffer and last chunck, i.e. request.end().
// Empty buffer and last chunk, i.e. request.end().
auto write_result = chunked_stream_writer_->AppendData(
nullptr,
0,
@ -233,12 +224,16 @@ void AtomURLRequest::OnResponseStarted(net::URLRequest* request) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK_EQ(request, request_.get());
scoped_refptr<const net::HttpResponseHeaders> response_headers =
request->response_headers();
const auto& status = request_->status();
if (status.is_success()) {
// Success or pending trigger a Read.
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&AtomURLRequest::InformDelegateResponseStarted, this));
base::Bind(&AtomURLRequest::InformDelegateResponseStarted,
this,
response_headers));
ReadResponse();
} else if (status.status() == net::URLRequestStatus::Status::FAILED) {
@ -337,10 +332,11 @@ void AtomURLRequest::InformDelegateAuthenticationRequired(
delegate_->OnAuthenticationRequired(auth_info);
}
void AtomURLRequest::InformDelegateResponseStarted() const {
void AtomURLRequest::InformDelegateResponseStarted(
scoped_refptr<const net::HttpResponseHeaders> response_headers) const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (delegate_)
delegate_->OnResponseStarted();
delegate_->OnResponseStarted(response_headers);
}
void AtomURLRequest::InformDelegateResponseData(

View file

@ -38,7 +38,6 @@ class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
void RemoveExtraHeader(const std::string& name) const;
void PassLoginInformation(const base::string16& username,
const base::string16& password) const;
scoped_refptr<const net::HttpResponseHeaders> GetResponseHeaders() const;
protected:
// Overrides of net::URLRequest::Delegate
@ -66,7 +65,8 @@ class AtomURLRequest : public base::RefCountedThreadSafe<AtomURLRequest>,
void InformDelegateAuthenticationRequired(
scoped_refptr<net::AuthChallengeInfo> auth_info) const;
void InformDelegateResponseStarted() const;
void InformDelegateResponseStarted(
scoped_refptr<const net::HttpResponseHeaders>) const;
void InformDelegateResponseData(
scoped_refptr<net::IOBufferWithSize> data) const;
void InformDelegateResponseCompleted() const;