Adding support for POST requests.

This commit is contained in:
ali.ibrahim 2016-09-26 14:03:49 +02:00
parent 2b3b41d5f9
commit f7525d7877
5 changed files with 230 additions and 97 deletions

View file

@ -41,11 +41,45 @@ struct Converter<scoped_refptr<const net::HttpResponseHeaders>> {
template<>
struct Converter<scoped_refptr<const net::IOBufferWithSize>> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
scoped_refptr<const net::IOBufferWithSize> buffer) {
return node::Buffer::Copy(isolate, buffer->data(), buffer->size()).ToLocalChecked();
}
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
scoped_refptr<const net::IOBufferWithSize>* out) {
auto size = node::Buffer::Length(val);
if (size == 0) {
// Support conversoin 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.
*out = nullptr;
return true;
}
auto data = node::Buffer::Data(val);
if (!data) {
// This is an error as size is positif but data is null.
return false;
}
auto io_buffer = new net::IOBufferWithSize(size);
if (!io_buffer) {
// Assuming allocation failed.
return false;
}
// We do a deep copy. We could have used Buffer's internal memory
// but that is much more complicated to be properly handled.
memcpy(io_buffer->data(), data, size);
*out = io_buffer;
return true;
}
};
}
@ -102,8 +136,7 @@ void URLRequest::BuildPrototype(v8::Isolate* isolate,
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
// Request API
.MakeDestroyable()
.SetMethod("_write", &URLRequest::Write)
.SetMethod("_end", &URLRequest::End)
.SetMethod("_writeBuffer", &URLRequest::WriteBuffer)
.SetMethod("abort", &URLRequest::Abort)
.SetMethod("_setHeader", &URLRequest::SetHeader)
.SetMethod("_getHeader", &URLRequest::GetHeader)
@ -118,21 +151,27 @@ void URLRequest::BuildPrototype(v8::Isolate* isolate,
}
void URLRequest::Write() {
atom_request_->Write();
bool URLRequest::WriteBuffer(scoped_refptr<const net::IOBufferWithSize> buffer, bool is_last) {
atom_request_->WriteBuffer(buffer, is_last);
return true;
}
void URLRequest::End() {
pin();
atom_request_->End();
}
void URLRequest::Abort() {
atom_request_->Abort();
}
void URLRequest::SetHeader(const std::string& name, const std::string& value) {
bool URLRequest::SetHeader(const std::string& name, const std::string& value) {
if (!net::HttpUtil::IsValidHeaderName(name)) {
return false;
}
if (!net::HttpUtil::IsValidHeaderValue(value)) {
return false;
}
atom_request_->SetHeader(name, value);
return true;
}
std::string URLRequest::GetHeader(const std::string& name) {
return atom_request_->GetHeader(name);
@ -165,6 +204,8 @@ void URLRequest::OnResponseData(
void URLRequest::OnResponseCompleted() {
EmitResponseEvent("end");
unpin();
atom_request_ = nullptr;
}

View file

@ -32,10 +32,9 @@ class URLRequest : public mate::EventEmitter<URLRequest> {
private:
void Write();
void End();
bool WriteBuffer(scoped_refptr<const net::IOBufferWithSize> buffer, bool is_last);
void Abort();
void SetHeader(const std::string& name, const std::string& value);
bool SetHeader(const std::string& name, const std::string& value);
std::string GetHeader(const std::string& name);
void RemoveHeader(const std::string& name);
@ -66,7 +65,7 @@ private:
void pin();
void unpin();
scoped_refptr<const AtomURLRequest> atom_request_;
scoped_refptr<AtomURLRequest> atom_request_;
v8::Global<v8::Object> wrapper_;
base::WeakPtrFactory<URLRequest> weak_ptr_factory_;