Skip to content

Commit

Permalink
Don't use null-terminated strings for request body. This tends to fai…
Browse files Browse the repository at this point in the history
…l with JSON since JSON isn't a subset of javascript, and allows some whitespace characters. See http://timelessrepo.com/json-isnt-a-javascript-subset for details. Fixes issue #26. Bump up version to 0.0.5.
  • Loading branch information
dhruvbird committed Feb 1, 2014
1 parent d2354bb commit 2ef8090
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions curllib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,27 @@ class CurlLib : ObjectWrap {

static size_t write_data(void *ptr, size_t size,
size_t nmemb, void *userdata) {
buffer += std::string((char*)ptr, size*nmemb);
//std::cerr<<"Wrote: "<<size*nmemb<<" bytes"<<std::endl;
//std::cerr<<"Buffer size: "<<buffer.size()<<" bytes"<<std::endl;
buffer.append(static_cast<char*>(ptr), size * nmemb);
// std::cerr<<"Wrote: "<<size*nmemb<<" bytes"<<std::endl;
// std::cerr<<"Buffer size: "<<buffer.size()<<" bytes"<<std::endl;
return size * nmemb;
}

static size_t write_headers(void *ptr, size_t size, size_t nmemb, void *userdata)
{
std::string header((char*)ptr, size*nmemb);
std::string header(static_cast<char*>(ptr), size * nmemb);
headers.push_back(header);
return size * nmemb;
}

/**
* Copy src->Length() bytes from src to dest. Resize dest before
* copying the data.
*/
static void copy_to_buffer(buff_t &dest, Local<String> &src) {
//std::cerr<<"copy_to_buffer::Length::"<<src->Length()<<std::endl;

// std::cerr<<"copy_to_buffer::Length::"<<src->Length()<<std::endl;
if (src->Length() > 0) {
dest.resize(src->Length() + 1);
dest.resize(src->Length());
src->WriteAscii(&dest[0], 0, src->Length());
}
}
Expand All @@ -102,7 +105,7 @@ class CurlLib : ObjectWrap {
}

if (!buffer.empty()) {
memcpy(buffer_data, &buffer[0], buffer.size());
memcpy(buffer_data, buffer.data(), buffer.size());
}
buffer.clear();
return scope.Close(buffer_obj);
Expand Down Expand Up @@ -171,17 +174,16 @@ class CurlLib : ObjectWrap {
std::vector<buff_t> _reqh;

copy_to_buffer(_body, body);
if (!_body.empty()) {
_body.resize(_body.size() - 1);
}

copy_to_buffer(_method, method);
_method.push_back('\0');
copy_to_buffer(_url, url);
_url.push_back('\0');

for (size_t i = 0; i < reqh->Length(); ++i) {
buff_t _tmp;
Local<String> _src = reqh->Get(i)->ToString();
copy_to_buffer(_tmp, _src);
_tmp.push_back('\0');
_reqh.push_back(_tmp);
}

Expand All @@ -203,11 +205,11 @@ class CurlLib : ObjectWrap {
// curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, &_method[0]);
if (_body.size()) {
if (!_body.empty()) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
reinterpret_cast<char*> (&_body[0]));
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
(curl_off_t)-1);
reinterpret_cast<char*> (&_body[0]));
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
(curl_off_t)_body.size());
}
curl_easy_setopt(curl, CURLOPT_URL, &_url[0]);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
Expand Down Expand Up @@ -258,9 +260,7 @@ class CurlLib : ObjectWrap {
result->Set(sym_error, String::New(curl_easy_strerror(res)));
}

// buffer.clear();
headers.clear();

return scope.Close(result);
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-sync",
"version": "0.0.4",
"version": "0.0.5",
"description": "A synchronous HTTP Client library for node.js",
"main": "http-sync.js",
"scripts": {
Expand Down

0 comments on commit 2ef8090

Please sign in to comment.