Skip to content

Commit

Permalink
http, feat: agent in custum headers will replace default agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Aug 29, 2017
1 parent 81d5221 commit 456cd3e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
5 changes: 3 additions & 2 deletions fibjs/include/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ class HttpClient : public HttpClient_base {
public:
result_t update_cookies(exlib::string url, obj_ptr<List_base> cookies);
result_t get_cookie(exlib::string url, exlib::string& retVal);
void setAgent(obj_ptr<HttpRequest_base> req)

exlib::string agent()
{
req->addHeader("User-Agent", m_userAgent);
return m_userAgent;
}

private:
Expand Down
17 changes: 9 additions & 8 deletions fibjs/src/http/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ result_t HttpClient::request(Stream_base* conn, HttpRequest_base* req,

Variant hdr;

if (pThis->m_retVal->firstHeader("Content-Encoding",
hdr)
!= CALL_RETURN_NULL) {
if (pThis->m_retVal->firstHeader("Content-Encoding", hdr) != CALL_RETURN_NULL) {
pThis->m_retVal->removeHeader("Content-Encoding");

pThis->m_retVal->get_body(pThis->m_body);
Expand All @@ -299,11 +297,9 @@ result_t HttpClient::request(Stream_base* conn, HttpRequest_base* req,
exlib::string str = hdr.string();

if (str == "gzip")
return zlib_base::gunzipTo(pThis->m_body,
pThis->m_unzip, pThis);
return zlib_base::gunzipTo(pThis->m_body, pThis->m_unzip, pThis);
else if (str == "deflate")
return zlib_base::inflateRawTo(pThis->m_body,
pThis->m_unzip, pThis);
return zlib_base::inflateRawTo(pThis->m_body, pThis->m_unzip, pThis);
}

return 0;
Expand Down Expand Up @@ -405,8 +401,13 @@ result_t HttpClient::request(exlib::string method, exlib::string url, SeekableSt
pThis->m_hc->get_cookie(pThis->m_url, cookie);
if (cookie.length() > 0)
pThis->m_req->setHeader("Cookie", cookie);

pThis->m_req->addHeader(pThis->m_headers);
pThis->m_hc->setAgent(pThis->m_req);

bool bCheck = false;
pThis->m_req->hasHeader("User-Agent", bCheck);
if (!bCheck)
pThis->m_req->addHeader("User-Agent", pThis->m_hc->agent());

if (pThis->m_body)
pThis->m_req->set_body(pThis->m_body);
Expand Down
20 changes: 20 additions & 0 deletions test/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,9 @@ describe("http", () => {
r.response.redirect("http://127.0.0.1:" + (8882 + base_port) + "/request");
} else if (r.address == "/redirect1") {
r.response.redirect("http://127.0.0.1:" + (8882 + base_port) + "/redirect1");
} else if (r.address == "/agent") {
if (r.allHeader("user-agent").length == 1)
r.response.body.write(r.firstHeader("user-agent"));
} else if (r.address != "/gzip_test") {
r.response.addHeader("set-cookie", "request=value; domain=127.0.0.1; path=/request");
r.response.addHeader("set-cookie", "request1=value; domain=127.0.0.1; path=/request");
Expand Down Expand Up @@ -1271,6 +1274,23 @@ describe("http", () => {
assert.equal(cookie, "root=value2");
});

it("agent", () => {
assert.equal(http.request("GET", "http://127.0.0.1:" + (8882 + base_port) + "/agent").body.read().toString(),
"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36");

http.userAgent = 'test agent';
assert.equal(http.request("GET", "http://127.0.0.1:" + (8882 + base_port) + "/agent").body.read().toString(),
"test agent");

assert.equal(http.request("GET", "http://127.0.0.1:" + (8882 + base_port) + "/agent", {
"user-agent": "agent in headers"
}).body.read().toString(),
"agent in headers");

assert.equal(http.request("GET", "http://127.0.0.1:" + (8882 + base_port) + "/agent").body.read().toString(),
"test agent");
});

it("gzip", () => {
assert.equal(http.get("http://127.0.0.1:" + (8882 + base_port) + "/gzip_test").body.read().toString(),
"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
Expand Down

0 comments on commit 456cd3e

Please sign in to comment.