fix add-on function.
#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
#include "util_tcp.h"
+#include "config.h"
#include "util_const.h"
+#include "util_check.h"
#include "ustring.h"
#include <boost/regex.hpp>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
-void TcpClient::init () {
+void BasicTcpClient::init () {
addrInfo = NULL;
fd = -1;
}
-void TcpClient::destruct () {
+void BasicTcpClient::destruct () {
if (addrInfo) {
freeaddrinfo (addrInfo);
addrInfo = NULL;
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 {
}
}
-bool TcpClient::connect () {
+bool BasicTcpClient::connect () {
struct addrinfo* ai;
for (ai = addrInfo; ai; ai = ai->ai_next) {
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;
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;
return ans;
}
-bool TcpClient::connect_1 (struct addrinfo* ai) {
+bool BasicTcpClient::connect_1 (struct addrinfo* ai) {
// struct sockaddr_in* in;
int rc;
return true;
}
+//============================================================
bool TcpBuf::empty () {
return start == tail;
}
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;
+}
+
#ifndef UTIL_TCP_H
#define UTIL_TCP_H
+#include "util_proc.h"
#include "ustring.h"
#include <sys/types.h>
#include <netinet/in.h>
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
#endif /* DEBUG */
init ();
};
- virtual ~TcpClient () {
+ virtual ~BasicTcpClient () {
destruct ();
};
virtual void init ();
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 */
#include "motorconst.h"
#include "motorenv.h"
#include "motoroutput.h"
+#include "utf8.h"
#include "util_const.h"
#include "util_check.h"
#include "util_string.h"
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
MNodeList ans;
o.proc.read (a);
+ a = fixUTF8 (a);
b = a.begin ();
e = a.end ();
while (usearch (b, e, m, re_tab)) {
int i;
o.proc.read (a);
+ a = fixUTF8 (a);
b = a.begin ();
e = a.end ();
while (usearch (b, e, m, re_lf)) {
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);
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));
}
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 ());
}
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 ();
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);
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);