Skip to content

Commit

Permalink
modify for http status
Browse files Browse the repository at this point in the history
  • Loading branch information
puritys committed Jan 20, 2015
1 parent 25eb16f commit d6b5241
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Binary file modified binary/linux_ia32/phplike.node
Binary file not shown.
12 changes: 11 additions & 1 deletion nodejs/curl.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,22 @@ function responseHeaderToHash(str) {
if (!str || !casting.is_string(str)) {
return "";
}
var res = {};
var res = {}, regStatus, regRes;
var strAy = str.split(/[\n\r]+/);
var i, text, key, value, pos;
regStatus = /HTTPs?\/[^\s]+[\s]([0-9]+)[\s].*/i;
for (i in strAy) {
text = strAy[i];
pos = text.indexOf(": ");
if (pos === -1) {
regRes = text.match(regStatus);
if (regRes && regRes[1]) {
res['status'] = regRes[1];
res['httpCode'] = regRes[0];
continue;
}
}

key = text.substring(0, pos);
value = text.substring(pos + 2, text.length);
if (!core.empty(key)) {
Expand Down
5 changes: 4 additions & 1 deletion system/curl/src/phplikeCppCurl_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ void phplikeCppCurl::request(string method, string url, string paramStr , map<st
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resC);

res = curl_easy_perform(curl);
long http_code = 0;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);

if(!res) {
resHeader = resH.ptr;
resHeader = http_code + "\r\n";
resHeader += resH.ptr;
resContent = resC.ptr;
size_t foundPos = resContent.find("\r\n\r\n");
if (foundPos != std::string::npos) {
Expand Down
14 changes: 13 additions & 1 deletion tests/curl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@ describe('Test method: HTTP GET ', function() {
var param = {"q": "unit test"};
var header = {};
var res = phplikeMod.request("GET", url, param, {});

header = phplikeMod.getResponseHeader();
//console.log(res);
var match = res.match(/unit/);
assert.equal("unit", match[0]);
assert.equal("200", header['status']);
});
});

describe('Test method: HTTP GET 404', function() {
it('Fetch google response with query string', function() {
var url = "https://www.google.com.tw/notfound";
var param = {"q": "unit test"};
var header = {};
var res = phplikeMod.request("GET", url, param, {});
header = phplikeMod.getResponseHeader();
assert.equal("404", header['status']);
});
});

Expand Down

0 comments on commit d6b5241

Please sign in to comment.