From: visor Date: Tue, 24 May 2011 15:48:03 +0000 (+0900) Subject: fix http-get function bug. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=fbc6736976ef9c8ef237e5b96b1200ecc7943221;p=hmh%2Fhhml.git fix http-get function bug. --- diff --git a/lib/http.cc b/lib/http.cc index 0bab613..a273e8b 100644 --- a/lib/http.cc +++ b/lib/http.cc @@ -328,8 +328,8 @@ int HTTPSend::post (TcpClient& client, TcpBuf& buf, ustring& ans) { } void HTTPSend::makeHeader (ustring& q, const ustring& key, const ustring& val) { - static uregex re1 ("[^a-zA-Z_0-9]"); - static uregex re2 ("[\\x00-\\x1f;]"); + static uregex re1 ("[\\x00-\\x1f: ]"); + 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); @@ -341,10 +341,15 @@ ustring HTTPSend::query () { if (fpost) { q.assign (CharConst ("POST ")).append (path); + if (getparams.length () > 0) { + q.append (CharConst ("?")).append (getparams); + } q.append (CharConst (" HTTP/1.0\r\n")); } else { q.assign (CharConst ("GET ")).append (path); - if (params.length () > 0) { + if (getparams.length () > 0) { + q.append (CharConst ("?")).append (getparams); + } else if (params.length () > 0) { q.append (CharConst ("?")).append (params); } q.append (CharConst (" HTTP/1.0\r\n")); @@ -402,6 +407,7 @@ int HTTPSend::readReplyHead (TcpClient& client, TcpBuf& buf) { while (rc = buf.getln (client, line)) { if (line.empty ()) break; + line = omitCtrl (fixUTF8 (line)); b = line.begin (); e = line.end (); if (splitChar (b, e, ':', m1)) { @@ -437,7 +443,7 @@ void HTTPSend::readReplyBody (TcpClient& client, TcpBuf& buf, ustring& ans) { std::stringstream ostr; MotorOutputOStream out (&ostr); readReplyBody (client, buf, &out); - ans = ostr.str (); + ans = fixUTF8 (ostr.str ()); } const ustring* HTTPSend::findHeader (const ustring& name) { diff --git a/lib/http.h b/lib/http.h index dc3b888..3f1e5d7 100644 --- a/lib/http.h +++ b/lib/http.h @@ -70,6 +70,7 @@ class HTTPSend { ustring reqhost; ustring path; ustring params; + ustring getparams; ustring id; ustring pw; ustring cookie; diff --git a/modules/ml-http.cc b/modules/ml-http.cc index a62cb5f..0626c74 100644 --- a/modules/ml-http.cc +++ b/modules/ml-http.cc @@ -98,21 +98,27 @@ static void url_sub (const ustring& url, HTTPSend& http) { } } -static void request_query (MNode* query, HTTPSend& http) { +static void request_query (MNode* query, HTTPSend& http, bool getmethod = false) { MNode* a; + ustring* pp; + if (getmethod) { + pp = &http.getparams; + } else { + pp = &http.params; + } if (query && query->isCons ()) { while (query) { if (a = query->car ()) { - if (http.params.size () > 0) - http.params.append (uAmp); + if (pp->size () > 0) + pp->append (uAmp); if (a->isCons ()) { - http.params.append (urlencode (to_string (a->car ()))).append (uEq).append (urlencode (to_string (a->cdr ()))); + pp->append (urlencode (to_string (a->car ()))).append (uEq).append (urlencode (to_string (a->cdr ()))); } else { - http.params.append (urlencode (to_string (a))).append (uEq); + pp->append (urlencode (to_string (a))).append (uEq); nextNode (query); if (query) - http.params.append (urlencode (to_string (query->car ()))); + pp->append (urlencode (to_string (query->car ()))); } } nextNode (query); @@ -319,6 +325,7 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { MLHttpGet obj (mlenv); ustring url; MNodePtr query; + MNodePtr getquery; MNodePtr cookie; MNodePtr headerquery; @@ -339,6 +346,7 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { {CharConst ("proxy-password"), false}, {CharConst ("proxy-pw"), false}, {CharConst ("header"), false}, + {CharConst ("get-query"), false}, {NULL, 0, 0} }; @@ -376,12 +384,16 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { if (keywords[11]) { // header headerquery = eval (keywords[11], mlenv); } + if (keywords[12]) { // get-query + getquery = eval (keywords[12], mlenv); + } url_sub (url, obj.http); if (obj.http.proto.empty ()) { throw (obj.http.path + ": bad URL."); } request_query (query (), obj.http); + request_query (getquery (), obj.http, true); request_cookie (cookie (), obj.http); request_headerquery (headerquery (), obj.http);