From: visor Date: Sun, 16 May 2010 13:26:09 +0000 (+0900) Subject: SSL mode of http-get function. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=5ff02ffdb8fdafc3a85062dcaad2a3a949ed7669;p=hmh%2Fhhml.git SSL mode of http-get function. fix add-on function. --- diff --git a/config.h b/config.h index 265c555..45b544c 100644 --- a/config.h +++ b/config.h @@ -15,6 +15,7 @@ #define cmd_qmailinject "/var/qmail/bin/qmail-inject" #define cmd_rm "/bin/rm" #define cmd_rmdir "/bin/rmdir" +#define cmd_httpsclient "/export/web/htvar/bin/httpsclient" #define path_devnull "/dev/null" #define SHARE_AUTHDB 1 diff --git a/lib/util_tcp.cc b/lib/util_tcp.cc index 9ecb170..109b1a9 100644 --- a/lib/util_tcp.cc +++ b/lib/util_tcp.cc @@ -1,5 +1,7 @@ #include "util_tcp.h" +#include "config.h" #include "util_const.h" +#include "util_check.h" #include "ustring.h" #include #include @@ -8,12 +10,12 @@ #include #include -void TcpClient::init () { +void BasicTcpClient::init () { addrInfo = NULL; fd = -1; } -void TcpClient::destruct () { +void BasicTcpClient::destruct () { if (addrInfo) { freeaddrinfo (addrInfo); addrInfo = NULL; @@ -21,7 +23,7 @@ void TcpClient::destruct () { close (); } -void TcpClient::setAddr (const ustring& host, const ustring& port) { +void BasicTcpClient::setAddr (const ustring& host, const ustring& port) { assert (addrInfo == NULL); if (! getaddrinfo (host.c_str (), port.c_str (), NULL, &addrInfo)) { } else { @@ -29,7 +31,7 @@ void TcpClient::setAddr (const ustring& host, const ustring& port) { } } -bool TcpClient::connect () { +bool BasicTcpClient::connect () { struct addrinfo* ai; for (ai = addrInfo; ai; ai = ai->ai_next) { @@ -39,14 +41,14 @@ bool TcpClient::connect () { return false; } -void TcpClient::close () { +void BasicTcpClient::close () { if (fd >= 0) { ::close (fd); fd = -1; } } -ssize_t TcpClient::read (void* buf, size_t nbytes) { +ssize_t BasicTcpClient::read (void* buf, size_t nbytes) { ssize_t ans = 0; fd_set rfds; struct timeval tm; @@ -65,7 +67,7 @@ ssize_t TcpClient::read (void* buf, size_t nbytes) { return ans; } -ssize_t TcpClient::write (const void* buf, size_t nbytes) { +ssize_t BasicTcpClient::write (const void* buf, size_t nbytes) { ssize_t ans = 0; ssize_t s; fd_set wfds; @@ -94,7 +96,7 @@ ssize_t TcpClient::write (const void* buf, size_t nbytes) { return ans; } -bool TcpClient::connect_1 (struct addrinfo* ai) { +bool BasicTcpClient::connect_1 (struct addrinfo* ai) { // struct sockaddr_in* in; int rc; @@ -112,6 +114,7 @@ bool TcpClient::connect_1 (struct addrinfo* ai) { return true; } +//============================================================ bool TcpBuf::empty () { return start == tail; } @@ -163,3 +166,39 @@ size_t TcpBuf::size () { void TcpBuf::consume () { start = tail = buf.begin (); } + +//============================================================ +void SslClient::setAddr (const ustring& _host, const ustring& _port) { + if (checkHostname (_host)) + hostname = _host; + if (checkNum (_port)) + port = _port; +} + +bool SslClient::connect () { + char* argv[8]; + int rc; + + if (port.length () == 0) + port = ustring (CharConst ("443")); + argv[0] = (char*)cmd_httpsclient; + argv[1] = (char*)hostname.c_str (); + argv[2] = (char*)port.c_str (); + argv[3] = NULL; + rc = proc.open (argv); + return rc == 1; +} + +void SslClient::close () { + proc.close (); +} + +ssize_t SslClient::read (void* buf, size_t nbytes) { + return proc.read ((char*)buf, nbytes); +} + +ssize_t SslClient::write (const void* buf, size_t nbytes) { + proc.write ((const char*)buf, nbytes); + return nbytes; +} + diff --git a/lib/util_tcp.h b/lib/util_tcp.h index d19e5d1..c5ed935 100644 --- a/lib/util_tcp.h +++ b/lib/util_tcp.h @@ -1,17 +1,30 @@ #ifndef UTIL_TCP_H #define UTIL_TCP_H +#include "util_proc.h" #include "ustring.h" #include #include class TcpClient { public: + TcpClient () {}; + virtual ~TcpClient () {}; + + virtual void setAddr (const ustring& host, const ustring& port) = 0; + virtual bool connect () = 0; + virtual void close () = 0; + virtual ssize_t read (void* buf, size_t nbytes) = 0; + virtual ssize_t write (const void* buf, size_t nbytes) = 0; +}; + +class BasicTcpClient: public TcpClient { + public: struct addrinfo* addrInfo; int fd; time_t limit; - TcpClient () { + BasicTcpClient () { #ifdef DEBUG limit = 10; #else @@ -19,7 +32,7 @@ class TcpClient { #endif /* DEBUG */ init (); }; - virtual ~TcpClient () { + virtual ~BasicTcpClient () { destruct (); }; virtual void init (); @@ -51,4 +64,20 @@ class TcpBuf { virtual void consume (); }; +class SslClient: public TcpClient { + public: + ustring hostname; + ustring port; + ProcRW proc; + + SslClient () {}; + virtual ~SslClient () {}; + + virtual void setAddr (const ustring& _host, const ustring& _port); + virtual bool connect (); + virtual void close (); + virtual ssize_t read (void* buf, size_t nbytes); + virtual ssize_t write (const void* buf, size_t nbytes); +}; + #endif /* UTIL_TCP_H */ diff --git a/modules/ml-addon.cc b/modules/ml-addon.cc index 2178c9f..6d6c68d 100644 --- a/modules/ml-addon.cc +++ b/modules/ml-addon.cc @@ -5,6 +5,7 @@ #include "motorconst.h" #include "motorenv.h" #include "motoroutput.h" +#include "utf8.h" #include "util_const.h" #include "util_check.h" #include "util_string.h" @@ -79,14 +80,16 @@ static void addon_sub2 (MlEnv* mlenv, AddonParams& o) { MNode* ml_addon (MNode* cell, MlEnv* mlenv) { MNode* arg = cell->cdr (); AddonParams o; + ustring* ans = NULL; setParams (arg, 1, &o.params, NULL, &o.keywords, &o.rest); o.cmd = eval_str (o.params[0], mlenv); addon_sub2 (mlenv, o); - ustring* a = new ustring; - o.proc.read (*a); + ans = new ustring; + o.proc.read (*ans); + *ans = fixUTF8 (*ans); - return newMNode_str (a); + return newMNode_str (ans); } //#AFUNC add-on-tab ml_addon_tab @@ -104,6 +107,7 @@ MNode* ml_addon_tab (MNode* cell, MlEnv* mlenv) { MNodeList ans; o.proc.read (a); + a = fixUTF8 (a); b = a.begin (); e = a.end (); while (usearch (b, e, m, re_tab)) { @@ -154,6 +158,7 @@ MNode* ml_addon_array_tab_nl (MNode* cell, MlEnv* mlenv) { int i; o.proc.read (a); + a = fixUTF8 (a); b = a.begin (); e = a.end (); while (usearch (b, e, m, re_lf)) { diff --git a/modules/ml-http.cc b/modules/ml-http.cc index ad841ba..c5f79fe 100644 --- a/modules/ml-http.cc +++ b/modules/ml-http.cc @@ -314,13 +314,17 @@ MNode* ml_http_get (MNode* cell, MlEnv* mlenv) { request_cookie (cookie (), obj.http); if (obj.http.proto == uHttp) { - obj.http.submit (obj.client, obj.buf); + if (! obj.client) + obj.client = new BasicTcpClient; + obj.http.submit (*obj.client, obj.buf); } else if (obj.http.proto == uHttps) { - throw (obj.http.proto + ": protocol not supported."); + if (! obj.client) + obj.client = new SslClient; + obj.http.submit (*obj.client, obj.buf); } else { throw (obj.http.proto + ": protocol not supported."); } - obj.http.readReplyHead (obj.client, obj.buf); + obj.http.readReplyHead (*obj.client, obj.buf); mlenv->setMStack (&obj); ans = progn (rest, mlenv); @@ -366,7 +370,7 @@ MNode* ml_http_get_http_response (MNode* cell, MlEnv* mlenv, MLFunc* mobj) { if (arg) throw (uErrorWrongNumber); - obj->http.readReplyBody (obj->client, obj->buf, ans); + obj->http.readReplyBody (*obj->client, obj->buf, ans); return newMNode_str (new ustring (ans)); } @@ -396,7 +400,7 @@ MNode* ml_http_get_http_response_file (MNode* cell, MlEnv* mlenv, MLFunc* mobj) std::ofstream stream; MotorOutputOStream out (&stream); stream.open (tmp.c_str (), std::ios_base::out | std::ios_base::binary); - obj->http.readReplyBody (obj->client, obj->buf, &out); + obj->http.readReplyBody (*obj->client, obj->buf, &out); stream.close (); rename (tmp.c_str (), tgt.c_str ()); } @@ -425,8 +429,8 @@ MNode* ml_http_get_http_response_output (MNode* cell, MlEnv* mlenv, MLFunc* mob if (keywords[0] && eval_bool (keywords[0], mlenv)) cflag = true; -// obj->http.readReplyBody (obj->client, obj->buf, std::cout); - obj->http.readReplyBody (obj->client, obj->buf, mlenv->env->output); +// obj->http.readReplyBody (*obj->client, obj->buf, std::cout); + obj->http.readReplyBody (*obj->client, obj->buf, mlenv->env->output); if (! cflag) mlenv->breakProg (); diff --git a/modules/ml-http.h b/modules/ml-http.h index 2ee1c36..91b81aa 100644 --- a/modules/ml-http.h +++ b/modules/ml-http.h @@ -12,11 +12,20 @@ class MlEnv; class MLHttpGet: public MLFunc { public: HTTPSend http; - TcpClient client; +// TcpClient client; + TcpClient* client; TcpBuf buf; - MLHttpGet (): MLFunc (cMLHttpGetID) {}; - virtual ~MLHttpGet () {}; + MLHttpGet (): MLFunc (cMLHttpGetID) { +// client = new BasicTcpClient; + client = NULL; + }; + virtual ~MLHttpGet () { + if (client) { + delete client; + client = NULL; + } + }; }; MNode* ml_error (MNode* cell, MlEnv* mlenv); diff --git a/modules/ml-store.cc b/modules/ml-store.cc index 361b4ee..c4318da 100644 --- a/modules/ml-store.cc +++ b/modules/ml-store.cc @@ -478,9 +478,6 @@ MNode* ml_read_file (MNode* cell, MlEnv* mlenv) { if (eval_bool (keywords[2], mlenv)) // named storetype = F_NAMED; -// if (mlenv->env->storedir.empty ()) -// throw (uErrorNoStore); -// src = mlenv->env->path_store_file (name); switch (storetype) { case F_SERIAL: src = mlenv->env->path_store_file (name);