From 63340d9585d2041ebb22309ff772b29ffb093ebf Mon Sep 17 00:00:00 2001 From: visor Date: Mon, 25 Apr 2011 22:26:41 +0900 Subject: [PATCH] header option of http-get function. --- lib/http.cc | 21 ++++++++++++++++++--- lib/http.h | 2 ++ modules/ml-http.cc | 29 ++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/http.cc b/lib/http.cc index 8657505..0bab613 100644 --- a/lib/http.cc +++ b/lib/http.cc @@ -327,6 +327,15 @@ int HTTPSend::post (TcpClient& client, TcpBuf& buf, ustring& ans) { return responseCode; } +void HTTPSend::makeHeader (ustring& q, const ustring& key, const ustring& val) { + static uregex re1 ("[^a-zA-Z_0-9]"); + static uregex re2 ("[\\x00-\\x1f;]"); + + if (! checkRe (key, re1) && ! checkRe (val, re2) && key.length () + val.length () < 2048) { + q.append (key).append (CharConst (": ")).append (val).append (uCRLF); + } +} + ustring HTTPSend::query () { ustring q; @@ -341,13 +350,16 @@ ustring HTTPSend::query () { q.append (CharConst (" HTTP/1.0\r\n")); } if (reqhost.length () > 0) { - q.append (CharConst ("Host: ")).append (reqhost).append (uCRLF); +// q.append (CharConst ("Host: ")).append (reqhost).append (uCRLF); + makeHeader (q, ustring (CharConst ("Host")), reqhost); } else { - q.append (CharConst ("Host: ")).append (host).append (uCRLF); +// q.append (CharConst ("Host: ")).append (host).append (uCRLF); + makeHeader (q, ustring (CharConst ("Host")), host); } q.append (CharConst ("Accept: */*\r\n")); if (cookie.length () > 0) { - q.append (CharConst ("Cookie: ")).append (cookie).append (uCRLF); +// q.append (CharConst ("Cookie: ")).append (cookie).append (uCRLF); + makeHeader (q, ustring (CharConst ("Cookie")), cookie); } if (id.length () > 0) { ustring idpw; @@ -359,6 +371,9 @@ ustring HTTPSend::query () { idpw.assign (proxyid).append (uColon).append (proxypw); q.append (CharConst ("Proxy-Authorization: Basic ")).append (base64Encode (idpw.begin (), idpw.end ())).append (uCRLF); } + for (std::vector::iterator i = header_req.begin (); i < header_req.end (); i ++) { + makeHeader (q, (*i).first, (*i).second); + } if (fpost) q.append (CharConst ("Content-Length: ")).append (to_ustring (params.length ())).append (uCRLF); q.append (CharConst ("Connection: close\r\n\r\n")); diff --git a/lib/http.h b/lib/http.h index 07657cf..dc3b888 100644 --- a/lib/http.h +++ b/lib/http.h @@ -76,6 +76,7 @@ class HTTPSend { bool useproxy; ustring proxyid; ustring proxypw; + std::vector header_req; std::vector header_reply; std::vector cookie_reply; int responseCode; @@ -90,6 +91,7 @@ class HTTPSend { virtual bool submit (TcpClient& client, TcpBuf& buf); virtual int post (TcpClient& client, TcpBuf& buf, ustring& ans); + virtual void makeHeader (ustring& q, const ustring& key, const ustring& val); virtual ustring query (); virtual int readReplyHead (TcpClient& client, TcpBuf& buf); virtual void readReplyBody (TcpClient& client, TcpBuf& buf, MotorOutput* out); diff --git a/modules/ml-http.cc b/modules/ml-http.cc index c76a7d2..a62cb5f 100644 --- a/modules/ml-http.cc +++ b/modules/ml-http.cc @@ -143,6 +143,26 @@ static void request_cookie (MNode* cookie, HTTPSend& http) { } } +static void request_headerquery (MNode* headerquery, HTTPSend& http) { + MNode* a; + + if (headerquery && headerquery->isCons ()) { + while (headerquery) { + if (a = headerquery->car ()) { + if (a->isCons ()) { + http.header_req.push_back (HTTPSend::mapelem (to_string (a->car ()), to_string (a->cdr ()))); + } else { + nextNode (headerquery); + if (headerquery) + http.header_req.push_back (HTTPSend::mapelem (to_string (a), to_string (headerquery->car ()))); + } + } + nextNode (headerquery); + } + } + return; +} + /*DOC: ==http client== @@ -290,7 +310,7 @@ MNode* ml_response_no_cache (MNode* cell, MlEnv* mlenv) { /*DOC: ===$http-get== - ($http-get [#post] [:id STRING] [:password STRING | :pw STRING] URL [:query '((NAME . VALUE) ...)] [:cookie '((NAME . VALUE) ...)] [SUBFUNCTION...]) + ($http-get [#post] [:id STRING] [:password STRING | :pw STRING] URL [:query '((NAME . VALUE) ...)] [:cookie '((NAME . VALUE) ...)] [:header '((NAME . VALUE) ...)] [SUBFUNCTION...]) */ //#MFUNC $http-get ml_http_get cMLHttpGetID @@ -300,6 +320,8 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { ustring url; MNodePtr query; MNodePtr cookie; + MNodePtr headerquery; + std::vector params; std::vector keywords; MNode* rest; @@ -316,6 +338,7 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { {CharConst ("proxy-id"), false}, {CharConst ("proxy-password"), false}, {CharConst ("proxy-pw"), false}, + {CharConst ("header"), false}, {NULL, 0, 0} }; @@ -350,6 +373,9 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { obj.http.proxypw = omitCtrl (eval_str (keywords[10], mlenv)); } } + if (keywords[11]) { // header + headerquery = eval (keywords[11], mlenv); + } url_sub (url, obj.http); if (obj.http.proto.empty ()) { @@ -357,6 +383,7 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { } request_query (query (), obj.http); request_cookie (cookie (), obj.http); + request_headerquery (headerquery (), obj.http); if (obj.http.proto == uHttp) { if (! obj.client) -- 2.11.0