Skip to content

Commit

Permalink
http, feat: support pack option for msgpack. (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhkzh committed Apr 9, 2021
1 parent b09f506 commit 4007c40
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
20 changes: 16 additions & 4 deletions fibjs/src/http/HttpClient.cpp
Expand Up @@ -14,6 +14,7 @@
#include "ifs/net.h"
#include "ifs/zlib.h"
#include "ifs/json.h"
#include "ifs/msgpack.h"
#include "ifs/querystring.h"
#include <string.h>

Expand Down Expand Up @@ -764,7 +765,7 @@ result_t HttpClient::request(exlib::string method, exlib::string url,
v8::Local<v8::Object> opts, obj_ptr<HttpResponse_base>& retVal, AsyncEvent* ac)
{
static const char* s_keys[] = {
"query", "headers", "body", "json", "response_body", NULL
"query", "headers", "body", "json", "pack", "response_body", NULL
};

if (ac->isSync()) {
Expand Down Expand Up @@ -854,11 +855,22 @@ result_t HttpClient::request(exlib::string method, exlib::string url,
}
} else {
v = opts->Get(isolate->NewString("json", 4));
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;
if (!v->IsUndefined()) {
if (v.IsEmpty()) {
v = opts->Get(isolate->NewString("pack", 4));
if (v.IsEmpty())
return CALL_E_JAVASCRIPT;

obj_ptr<Buffer_base> buf;
stm = new MemoryStream();

hr = msgpack_base::encode(v, buf);
if (hr < 0)
return hr;

stm->cc_write(buf);
map->add("Content-Type", "application/msgpack");
} else {
obj_ptr<Buffer_base> buf;
stm = new MemoryStream();

exlib::string s;
Expand Down
18 changes: 12 additions & 6 deletions idl/zh-cn/HttpClient.idl
Expand Up @@ -75,11 +75,12 @@ interface HttpClient : object
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {},
"response_body": SeekableStream // 指定接受 resposne 数据的流
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param method 指定 http 请求方法:GET, POST 等
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
Expand All @@ -94,10 +95,11 @@ interface HttpClient : object
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -111,10 +113,11 @@ interface HttpClient : object
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -128,10 +131,11 @@ interface HttpClient : object
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -145,10 +149,11 @@ interface HttpClient : object
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -162,10 +167,11 @@ interface HttpClient : object
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand Down
18 changes: 12 additions & 6 deletions idl/zh-cn/http.idl
Expand Up @@ -97,11 +97,12 @@ module http
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {},
"response_body": SeekableStream //指定接受 resposne 数据的流
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param method 指定 http 请求方法:GET, POST 等
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
Expand All @@ -116,10 +117,11 @@ module http
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -133,10 +135,11 @@ module http
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -150,10 +153,11 @@ module http
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -167,10 +171,11 @@ module http
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand All @@ -184,10 +189,11 @@ module http
"query": {},
"body": SeekableStream | Buffer | String | {},
"json": {},
"pack": {},
"headers": {}
}
```
其中 body,json 不得同时出现。缺省为 {},不包含任何附加信息
其中 body,json,pack 不得同时出现。缺省为 {},不包含任何附加信息
@param url 指定 url,必须是包含主机的完整 url
@param opts 指定附加信息
@return 返回服务器响应
Expand Down
12 changes: 12 additions & 0 deletions test/http_test.js
Expand Up @@ -1640,6 +1640,9 @@ describe("http", () => {
} else if (r.address == "/request_json:") {
r.response.write(r.address);
r.response.write(r.json().test_field);
} else if (r.address == "/request_pack:") {
r.response.write(r.address);
r.response.write(r.pack().test_field);
} else if (r.address == "/host:") {
r.response.write(r.address);
r.response.write(r.firstHeader('host'));
Expand Down Expand Up @@ -1730,6 +1733,15 @@ describe("http", () => {
"/request_json:field");
});

it("pack", () => {
assert.equal(http.request("GET", "http://127.0.0.1:" + (8882 + base_port) + "/request_pack:", {
pack: {
test_field: "field"
}
}).body.read().toString(),
"/request_pack:field");
});

it("header", () => {
assert.equal(http.request("GET", "http://127.0.0.1:" + (8882 + base_port) + "/request:", {
headers: {
Expand Down

0 comments on commit 4007c40

Please sign in to comment.