Example #1
0
int acl_unix_trigger(ACL_EVENT *event, const char *service,
	const char *buf, int len, int timeout)
{
	const char *myname = "acl_unix_trigger";
	struct ACL_UNIX_TRIGGER *up;
	ACL_SOCKET fd;

	if (acl_msg_verbose > 0)
		acl_msg_info("%s: service %s", myname, service);

	/*
	 * Connect...
	 */
	if ((fd = acl_unix_connect(service, ACL_BLOCKING, timeout)) < 0) {
		if (acl_msg_verbose)
			acl_msg_warn("%s: connect to %s: %s",
				myname, service, strerror(errno));
		return -1;
	}
	acl_close_on_exec(fd, ACL_CLOSE_ON_EXEC);

	/*
	 * Stash away context.
	 */
	up = (struct ACL_UNIX_TRIGGER *) acl_mymalloc(sizeof(*up));
	up->service = acl_mystrdup(service);
	up->stream = acl_vstream_fdopen(fd, O_RDWR, 4096,
			timeout, ACL_VSTREAM_TYPE_LISTEN_UNIX);

	/*
	 * Write the request...
	 */
	if (acl_vstream_writen(up->stream, buf, len) < 0
		|| acl_vstream_writen(up->stream, "", 1) < 0)
	{
		if (acl_msg_verbose)
			acl_msg_warn("%s: write to %s: %s",
				myname, service, strerror(errno));
	}

	/*
	 * Wakeup when the peer disconnects, or when we lose patience.
	 */
#ifdef	__USE_TIMER
	if (timeout > 0)
		acl_event_request_timer(event, acl_unix_trigger_timer,
			(void *) up, (timeout + 100) * 1000000);
	acl_event_enable_read(event, up->stream, 0,
		acl_unix_trigger_event, (void *) up);
#else
	if (timeout > 0)
		acl_event_enable_read(event, up->stream, timeout + 100,
			acl_unix_trigger_event, (void *) up);
	else
		acl_event_enable_read(event, up->stream, 0,
			acl_unix_trigger_event, (void *) up);
#endif

	return 0;
}
Example #2
0
int smtp_send_stream(SMTP_CLIENT *client, ACL_VSTREAM *in)
{
	int   n = 0, ret;

	while (1) {
		ret = acl_vstream_read(in, client->buf, client->size);
		if (ret == ACL_VSTREAM_EOF)
			break;
		if (acl_vstream_writen(client->conn, client->buf, ret)
			== ACL_VSTREAM_EOF)
		{
			acl_msg_error("%s(%d): write data error(%s)",
				__FUNCTION__, __LINE__, acl_last_serror());
			return -1;
		}
		n += ret;
	}

	if (n == 0) {
		acl_msg_error("%s(%d): in stream is empty",
			__FUNCTION__, __LINE__);
		return -1;
	}
	return 0;
}
Example #3
0
static int http_demo(ACL_VSTREAM *cstream, const char* res, size_t len)
{
	char  buf[8192];
	int   ret;

	cstream->rw_timeout = __rw_timeout;

	while (1) {
		ret = acl_vstream_gets_nonl(cstream, buf, sizeof(buf) - 1);
		if (ret == ACL_VSTREAM_EOF) {
			printf("gets error: %s\r\n", acl_last_serror());
			return -1;
		}

		buf[ret] = 0;
		if (strcasecmp(buf, "stop") == 0) {
			__stop = 1;
			printf("----stop now----\r\n");
			break;
		}

		if (ret == 0)
			break;
	}

	if (acl_vstream_writen(cstream, res, len) == ACL_VSTREAM_EOF) {
		printf("write error\r\n");
		return -1;
	}

	return 0;
}
Example #4
0
static int gid_store_sync(GID_STORE *store)
{
	char  buf[1024];

	/* 需要先将文件内容清空 */
#if 0
	if (acl_file_ftruncate(store->fh.fp, 0) < 0) {
		acl_msg_error("%s(%d), %s: ftruncate %s error(%s)",
			__FILE__, __LINE__, __FUNCTION__,
			ACL_VSTREAM_PATH(store->fh.fp), acl_last_serror());
		return (-1);
	}
#endif
	if (acl_vstream_fseek(store->fh.fp, SEEK_SET, 0) < 0) {
		acl_msg_error("%s(%d), %s: fseek %s error(%s)",
			__FILE__, __LINE__, __FUNCTION__,
			ACL_VSTREAM_PATH(store->fh.fp), acl_last_serror());
	}

	snprintf(buf, sizeof(buf), "%s:%s %d %lld %lld %lld\r\n",
		store->tag, store->sid, store->step, store->cur_gid,
		store->min_gid, store->max_gid);

	/* 初始化文件内容: tag:sid step cur_gid min_gid max_gid\r\n */
	if (acl_vstream_writen(store->fh.fp,
		buf, strlen(buf)) == ACL_VSTREAM_EOF)
	{
		acl_msg_error("%s(%d), %s: write to %s error(%s)",
			__FILE__, __LINE__, __FUNCTION__,
			ACL_VSTREAM_PATH(store->fh.fp), acl_last_serror());
		return (-1);
	}

	return (0);
}
Example #5
0
static int request(ACL_VSTREAM *client, int cmd_num, int out)
{
	const char  cmd1[] = "CMD^new_gid|TAG^default:hello\r\n";
	const char  cmd2[] = "CMD^test_gid\r\n";
	const char *cmd;
	char  buf[256];
	int   ret;

	if (cmd_num == 1)
		cmd = cmd1;
	else
		cmd = cmd2;
	ret = acl_vstream_writen(client, cmd, strlen(cmd));
	if (ret == ACL_VSTREAM_EOF) {
		printf("write cmd to server error\n");
		return (-1);
	}
	ret = acl_vstream_gets(client, buf, sizeof(buf));
	if (ret == ACL_VSTREAM_EOF) {
		printf("gets error\n");
		return (-1);
	}

	if (out)
		printf(">>gets: %s\n", buf);
	return (0);
}
Example #6
0
int smtp_send(SMTP_CLIENT *client, const char* src, size_t len)
{
	if (acl_vstream_writen(client->conn, src, len) == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): write data error(%s)",
			__FUNCTION__, __LINE__, acl_last_serror());
		return -1;
	}

	return 0;
}
Example #7
0
File: main.c Project: iYefeng/acl
static void echo_client(ACL_FIBER *fiber, void *ctx)
{
	ACL_VSTREAM *cstream = (ACL_VSTREAM *) ctx;
	char  buf[8192];
	int   ret, count = 0;
	int   ntimeout = 0;
	FIBER_TIMER *ft = (FIBER_TIMER *) acl_mymalloc(sizeof(FIBER_TIMER));

	ft->fiber = fiber;
	ft->timer = acl_fiber_create_timer(__rw_timeout * 1000,
			320000, io_timer, ft);
	ft->conn  = cstream;

#define	SOCK ACL_VSTREAM_SOCK

	while (1) {
		printf("begin read\n");
		ret = acl_vstream_gets(cstream, buf, sizeof(buf) - 1);

		if (ret == ACL_VSTREAM_EOF) {
			printf("fiber-%d, gets error: %s, %d, %d, fd: %d, "
				"count: %d\r\n", acl_fiber_id(fiber),
				acl_last_serror(), errno, acl_fiber_errno(fiber),
				SOCK(cstream), count);

			if (errno != ETIMEDOUT)
				break;

			if (++ntimeout > 2)
			{
				printf("too many timeout: %d\r\n", ntimeout);
				break;
			}

			printf("ntimeout: %d\r\n", ntimeout);
			ft->timer = acl_fiber_create_timer(__rw_timeout * 1000,
					320000, io_timer, ft);
		}

		acl_fiber_reset_timer(ft->timer, __rw_timeout * 1000);
		buf[ret] = 0;
		//printf("gets line: %s", buf);

		if (acl_vstream_writen(cstream, buf, ret) == ACL_VSTREAM_EOF) {
			printf("write error, fd: %d\r\n", SOCK(cstream));
			break;
		}

		count++;
	}

	acl_myfree(ft);
	acl_vstream_close(cstream);
}
Example #8
0
int http_util_put_req_data(HTTP_UTIL *http_util, const char *data, size_t dlen)
{
	const char *myname = "http_util_put_req_data";

	if (data == NULL || dlen == 0) {
		acl_msg_info("%s(%d): data %s, dlen %d invalid",
			myname, __LINE__, data ? "not null" : "null", (int) dlen);
		return (-1);
	}
	if (acl_vstream_writen(http_util->stream, data, dlen) == ACL_VSTREAM_EOF)
		return (-1);
	return ((int) dlen);
}
Example #9
0
void event_dog_notify(EVENT_DOG *evdog)
{
	const char *myname = "event_dog_notify";
	char  buf[2];

	buf[0] = '0';
	buf[1] = 0;
	
	if (acl_vstream_writen(evdog->server, buf, 1) == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): notify error, reset", myname, __LINE__);
		event_dog_reopen(evdog);
	}
}
Example #10
0
int CGb2Utf8::TransformFile(const char *pFrom, const char *pTo)
{
	char *sBuf = NULL;
	size_t iLen;

#undef RETURN
#define RETURN(_x_) do \
{ \
	if (sBuf) \
		acl_myfree(sBuf); \
	return(_x_); \
} while(0);

	sBuf = acl_vstream_loadfile(pFrom);
	if (sBuf == NULL)
		RETURN (-1);
	if (*sBuf == 0)
		RETURN (-1);

	iLen = strlen(sBuf);

	acl::charset_conv conv;
	acl::string buf;
	if (conv.convert(m_fromCharset.GetString(), m_toCharset.GetString(),
		sBuf, iLen, &buf) == false)
	{
		logger_error("conver from %s to %s error: %s, file: %s",
			m_fromCharset.GetString(), m_toCharset.GetString(),
			conv.serror(), pFrom);
		RETURN (-1);
	}

	ACL_VSTREAM *fp;
	fp = acl_vstream_fopen(pFrom, O_RDWR | O_TRUNC | O_BINARY /* | O_APPEND */,
		0600, 1024);
	if (fp == NULL)
	{
		logger_error("open %s error %s", pFrom, acl::last_serror());
		RETURN (-1);
	}
 	int ret = acl_vstream_writen(fp, buf.c_str(), buf.length());
	acl_vstream_close(fp);
	if (ret == ACL_VSTREAM_EOF)
		logger_error("write to %s error %s", pFrom, acl::last_serror());
	else
		logger("transer from %s to %s ok, file: %s",
			m_fromCharset.GetString(), m_toCharset.GetString(), pFrom);
	RETURN (ret == ACL_VSTREAM_EOF ? -1 : 0);
}
Example #11
0
static void reply_client2(ACL_VSTREAM *client)
{
	char  buf[1024];
	int   ret;

	while (1) {
		ret = acl_vstream_gets(client, buf, sizeof(buf) - 1);
		if (ret == ACL_VSTREAM_EOF)
			break;
		buf[ret] = 0;
		acl_vstream_printf("get one line:%s", buf);
		if (acl_vstream_writen(client, buf, ret) == ACL_VSTREAM_EOF)
			break;
	}
}
Example #12
0
bool CDelBOM::DeleteBOM(CString& filePath)
{
	char* sBuf;

	sBuf = acl_vstream_loadfile(filePath);
	if (sBuf == NULL)
		return false;
	size_t len = strlen(sBuf);
	if (len < 3)
	{
		acl_myfree(sBuf);
		return false;
	}

	// 先判断文件内容前缀是否是BOM格式
	if (sBuf[0] != (char) 0xEF || sBuf[1] != (char) 0xBB || sBuf[2] != (char) 0xBF)
	{
		acl_myfree(sBuf);
		return false;
	}

	// 将内容指针偏移 3 个字节,即去掉BOM格式
	len -= 3;
	char* ptr = sBuf + 3;

	ACL_VSTREAM* fp = acl_vstream_fopen(filePath,
		O_WRONLY | O_APPEND | O_TRUNC, 0600, 4096);
	if (fp == NULL)
	{
		acl_myfree(sBuf);
		return false;
	}
	else if (len == 0)
	{
		acl_vstream_fclose(fp);
		acl_myfree(sBuf);
		return true;
	}
	else if (acl_vstream_writen(fp, ptr, len) == ACL_VSTREAM_EOF)
	{
		acl_msg_error("write to file: %s error: %s",
			filePath.GetString(), acl_last_serror());
	}

	acl_vstream_fclose(fp);
	acl_myfree(sBuf);
	return true;
}
Example #13
0
int protocol(SPOOL *spool, ACL_VSTREAM *cstream)
{
	char  buf[4096];
	int   n, ret;

	spool = spool;

	n = acl_vstream_gets(cstream, buf, sizeof(buf));
	if (n == ACL_VSTREAM_EOF)
		return (-1);

	ret = acl_vstream_writen(cstream, buf, n);
	if (ret != n)
		return (-1);

	return (0);
}
Example #14
0
File: zdb_io.c Project: 2202877/acl
static void io_blk_free(ZDB_IO_BLK *blk)
{
	const char *myname = "io_blk_free";
	int   ret;

	if ((blk->flag & BLK_F_DIRTY)) {
		ZDB_IO *io = blk->io;

		avl_remove(&io->blk_tree, blk);

#ifdef	PWRITE
		ret = PWRITE(IO_HANDLE(io), blk->dat, blk->dlen, blk->off);
		if (ret != (int) blk->dlen) {
			acl_msg_error("%s(%d): pwrite to %s error(%s),"
				" ret(%d) != len(%d), off: " ACL_FMT_I64D,
				myname, __LINE__, PATH(IO_STREAM(io)),
				acl_last_serror(), ret,
				(int) blk->dlen, blk->off);
		}
#else
		if (acl_vstream_fseek(IO_STREAM(io), blk->off, SEEK_SET) < 0) {
			acl_msg_error("%s(%d): fseek %s error(%s), off: " ACL_FMT_I64D,
				myname, __LINE__, IO_PATH(io),
				acl_last_serror(), blk->off);
		} else if ((ret = acl_vstream_writen(IO_STREAM(io), blk->dat, blk->dlen))
				== ACL_VSTREAM_EOF)
		{
			acl_msg_error("%s(%d): readn from %s, ret(%d) != size(%d),"
				" off(" ACL_FMT_I64D "), error(%s)", myname, __LINE__,
				IO_PATH(io), ret, (int) blk->dlen,
				blk->off, acl_last_serror());
		}
#endif
	}

	if (blk->io->dat_slice)
		acl_slice_free2(blk->io->dat_slice, blk->dat);
	else
		acl_myfree(blk->dat);
	if (blk->io->blk_slice)
		acl_slice_free2(blk->io->blk_slice, blk);
	else
		acl_myfree(blk);
	__n--;
}
Example #15
0
int http_util_get_res_body(HTTP_UTIL *http_util, char *buf, size_t size)
{
	const char *myname = "http_util_get_res_body";
	int   ret;

	if (buf == NULL || size == 0) {
		acl_msg_error("%s(%d): buf(%s), size(%d) invalid",
			myname, __LINE__, buf ? "not null" : "null", (int) size);
		return (-1);
	}

	if ((http_util->flag & (HTTP_UTIL_FLAG_HAS_RES_BODY
		| HTTP_UTIL_FLAG_NO_RES_BODY)) == 0)
	{
		if (!http_util_has_res_body(http_util))
			return (http_util->res_body_dlen);
	}

	ret = (int) http_res_body_get_sync(http_util->http_res,
			http_util->stream, buf, (int) size);
	if (ret <= 0)
		return (ret);
	http_util->res_body_dlen += ret;
	if (http_util->dump_stream == NULL)
		return (ret);

	if (acl_vstream_writen(http_util->dump_stream, buf, ret) == ACL_VSTREAM_EOF)
	{
		/* 如果有一次不能转储数据至文件或流则关闭该功能不再进行转储 */

		acl_msg_error("%s(%d): dump to stream(%s) error(%s)",
			myname, __LINE__, ACL_VSTREAM_PATH(http_util->dump_stream),
			acl_last_serror());
		if ((http_util->flag & HTTP_UTIL_FLAG_SET_DUMP_FILE)) {
			if (http_util->dump_stream)
				acl_vstream_close(http_util->dump_stream);
			http_util->flag &= ~HTTP_UTIL_FLAG_SET_DUMP_FILE;
		} else
			http_util->flag &= ~HTTP_UTIL_FLAG_SET_DUMP_STREAM;

		http_util->dump_stream = NULL;
	}

	return (ret);
}
Example #16
0
File: main.c Project: jhomble/redis
static void run_client(const char *addr, const char *filename)
{
    char *request = acl_vstream_loadfile(filename);
    ACL_VSTREAM *client;
    int   ret;
    char  buf[1024];

    if (request == NULL) {
        printf("load file(%s) error(%s)\n", filename, acl_last_serror());
        return;
    }

    client = acl_vstream_connect(addr, ACL_BLOCKING,
                                 0, 0, 4096);
    if (client == NULL) {
        printf("connect addr(%s) error(%s)\n", addr, acl_last_serror());
        acl_myfree(request);
        return;
    }

    acl_tcp_set_sndbuf(ACL_VSTREAM_SOCK(client), 10);
    if (acl_vstream_writen(client, request, strlen(request)) == ACL_VSTREAM_EOF) {
        printf("write to addr(%s) error(%s)\n", addr, acl_last_serror());
        acl_vstream_close(client);
        acl_myfree(request);
        return;
    }

    memset(buf, 0, sizeof(buf));

    while (1) {
        ret = acl_vstream_read(client, buf, sizeof(buf) - 1);
        if (ret == ACL_VSTREAM_EOF)
            break;
        buf[ret] = 0;
        usleep(100000);
        printf(">>>%s\n", buf);
    }

    printf(">>>last data(%s)\n", buf);
    acl_vstream_close(client);
    acl_myfree(request);
}
Example #17
0
int acl_mbox_send(ACL_MBOX *mbox, void *msg)
{
	int ret;

	acl_pthread_mutex_lock(mbox->lock);
	acl_ypipe_write(mbox->ypipe, msg);
	ret = acl_ypipe_flush(mbox->ypipe);
	acl_pthread_mutex_unlock(mbox->lock);
	if (ret == 0)
		return 0;

	mbox->nsend++;

	if (acl_vstream_writen(mbox->out, __key, sizeof(__key) - 1)
		== ACL_VSTREAM_EOF)
	{
		return -1;
	} else
		return 0;

}
Example #18
0
static void event_dog_open(EVENT_DOG *evdog)
{
	const char *myname = "event_dog_open";
	const char *addr = "127.0.0.1:0";
	char  ebuf[256];

	evdog->sstream = acl_vstream_listen(addr, 32);
	if (evdog->sstream == NULL)
		acl_msg_fatal("%s(%d): listen on addr(%s) error(%s)",
			myname, __LINE__, addr,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	evdog->server = acl_vstream_connect(evdog->sstream->local_addr,
			ACL_BLOCKING, 0, 0, 1024);
	if (evdog->server == NULL)
		acl_msg_fatal("%s(%d): connect to addr(%s) error(%s)",
			myname, __LINE__, addr,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_vstream_writen(evdog->server, ebuf, 1) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): pre write error(%s)",
			myname, __LINE__,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	evdog->client = acl_vstream_accept(evdog->sstream, ebuf, sizeof(ebuf));
	if (evdog->client == NULL)
		acl_msg_fatal("%s(%d): accept error(%s)",
			myname, __LINE__,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_vstream_readn(evdog->client, ebuf, 1) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): pre read error(%s)",
			myname, __LINE__,
			acl_last_strerror(ebuf, sizeof(ebuf)));

	acl_vstream_close(evdog->sstream);
	evdog->sstream = NULL;

	acl_event_enable_read(evdog->eventp, evdog->client, 0, read_fn, evdog);
}
Example #19
0
File: main.c Project: fatzero/acl
static void echo_client(ACL_VSTREAM *cstream)
{
	char  buf[8192];
	int   ret;

	while (1) {
		ret = acl_vstream_gets(cstream, buf, sizeof(buf) - 1);
		if (ret == ACL_VSTREAM_EOF) {
			printf("gets error\r\n");
			break;
		}
		buf[ret] = 0;
		printf("gets line: %s", buf);

		if (acl_vstream_writen(cstream, buf, ret) == ACL_VSTREAM_EOF) {
			printf("write error\r\n");
			break;
		}
	}

	acl_vstream_close(cstream);
}
Example #20
0
static void notify_thread(void *arg)
{
	const char *myname = "notify_thread";
	WARN_INFO *info = (WARN_INFO*) arg;
	ACL_VSTREAM *client;
	ACL_VSTRING *buf;
	int   ret;

	buf = acl_vstring_alloc(256);
	acl_vstring_sprintf(buf, "PROC=%s|PID=%d|RCPT=%s|info=%s\r\n",
		info->path, info->pid, info->notify_recipients, info->desc);

	client = acl_vstream_connect(info->notify_addr,
		ACL_BLOCKING, 60, 60, 1024);
	if (client == NULL) {
		acl_msg_error("%s(%d): connect %s error, info(%s)", myname,
			__LINE__, info->notify_addr, acl_vstring_str(buf));
		acl_vstring_free(buf);
		warn_info_free(info);
		return;
	}

	/* 禁止将该句柄传递给子进程 */
	acl_close_on_exec(ACL_VSTREAM_SOCK(client), ACL_CLOSE_ON_EXEC);

	ret = acl_vstream_writen(client, acl_vstring_str(buf),
		ACL_VSTRING_LEN(buf));
	if (ret == ACL_VSTREAM_EOF)
		acl_msg_error("%s(%d): write to notify server error, info(%s)",
			myname, __LINE__, acl_vstring_str(buf));
	else
		acl_msg_info("%s(%d): notify ok!", myname, __LINE__);

	acl_vstream_close(client);
	acl_vstring_free(buf);
	warn_info_free(info);
}
Example #21
0
int http_util_req_open(HTTP_UTIL *http_util)
{
	const char *myname = "http_util_req_open";
	int   ret;

	/* 连接远程 http 服务器 */

	http_util->stream = acl_vstream_connect(http_util->server_addr,
			ACL_BLOCKING /* 采用阻塞方式 */,
			http_util->conn_timeout /* 连接超时时间 */,
			http_util->rw_timeout /* 网络 IO 操作超时时间 */,
			4096 /* stream 流缓冲区大小为 4096 字节 */);
	if (http_util->stream == NULL) {
		acl_msg_error("%s(%d): connect %s error(%s)",
			myname, __LINE__, http_util->server_addr,
			acl_last_serror());
		return (-1);
	}

	/* 构建 HTTP 请求头数据 */

	http_hdr_build_request(http_util->hdr_req, http_util->req_buf);

	/* 向 HTTP 服务器发送请求 */

	ret = acl_vstream_writen(http_util->stream,
			acl_vstring_str(http_util->req_buf),
			ACL_VSTRING_LEN(http_util->req_buf));
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): send request to server(%s) error(%s)",
			myname, __LINE__, http_util->server_addr,
			acl_last_serror());
		return (-1);
	}

	return (0);
}
Example #22
0
static int sms_send(ACL_VSTREAM *client, const char *phone, const char *proc,
	int pid, const char *info, const char *ip)
{
	ACL_VSTRING *buf = acl_vstring_alloc(256);
	ACL_XML *xml = acl_xml_alloc();
	char  res[1024];
	int   ret;

	acl_vstring_sprintf(buf, "<send_sms phone=\"%s\" message=\"proc:%s, pid:%d, ip:%s, info:%s\" />",
			phone, proc, pid, ip, info);
	if (acl_vstream_writen(client, acl_vstring_str(buf), ACL_VSTRING_LEN(buf))
		== ACL_VSTREAM_EOF)
	{
		acl_msg_error("write to sms server error, msg: %s",
			acl_vstring_str(buf));
		acl_vstring_free(buf);
		return (-1);
	}

	acl_msg_info(">>send: %s", acl_vstring_str(buf));
	acl_vstring_free(buf);

	while (1) {
		ret = acl_vstream_read(client, res, sizeof(res) - 1);
		if (ret == ACL_VSTREAM_EOF)
			return (-1);
		res[ret] = 0;
		acl_xml_parse(xml, res);
		if (acl_xml_is_complete(xml, "send_sms")) {
			acl_msg_info("send ok!(%s)", res);
			break;
		}
	}

	return (0);
}
Example #23
0
static void __service(ACL_VSTREAM *stream, char *service, char **argv)
{
	char  myname[] = "__service";
	int   n, ret;
	VSTREAM_PROXY_OBJ *vpobj, *peer;

	/*
	 * Sanity check. This service takes no command-line arguments.
	 */
	if (argv[0])
		acl_msg_fatal("%s(%d)->%s: unexpected command-line argument: %s",
				__FILE__, __LINE__, myname, argv[0]);

	if (stream == NULL)
		acl_msg_fatal("%s(%d)->%s: stream null",
				__FILE__, __LINE__, myname);
	vpobj = (VSTREAM_PROXY_OBJ *) stream->context;
	if (vpobj == NULL)
		acl_msg_fatal("%s(%d)->%s: stream's context null",
				__FILE__, __LINE__, myname);

	if (acl_msg_verbose > 3)
		acl_msg_info("%s(%d)->%s: service name = %s, rw_timeout = %d",
			__FILE__, __LINE__, myname, service, stream->rw_timeout);

	acl_watchdog_pat();

	peer = vpobj->peer;
	if (peer == NULL || peer->stream == NULL || peer->peer != vpobj) {
		acl_multi_server_disconnect(stream);
		return;
	}

	acl_multi_server_cancel_rw_timer(peer->stream);

	n = acl_vstream_read(stream, __data_buf, var_proxy_bufsize);
	if (n == ACL_VSTREAM_EOF) {
		acl_multi_server_disconnect(stream);
		if (acl_msg_verbose > 3)
			acl_msg_info("%s(%d)->%s: read over",
				__FILE__, __LINE__, myname);
		return;
	}

	if (vpobj->flag == VSTREAM_PROXY_FRONT_FLAG && __request_stream) {
		ret = acl_vstream_writen(__request_stream, __data_buf, n);
		if (ret != n) {
			acl_msg_error("%s(%d)->%s: writen to %s, serr = %s",
					__FILE__, __LINE__, myname,
					var_proxy_request_file, strerror(errno));
			acl_vstream_close(__request_stream);
			__request_stream = NULL;
		}
	} else if (vpobj->flag == VSTREAM_PROXY_BACKEND_FLAG && __respond_stream) {
		ret = acl_vstream_writen(__respond_stream, __data_buf, n);
		if (ret != n) {
			acl_msg_error("%s(%d)->%s: writen to %s, serr = %s",
					__FILE__, __LINE__, myname,
					var_proxy_request_file, strerror(errno));
			acl_vstream_close(__respond_stream);
			__respond_stream = NULL;
		}
	}

	ret = acl_vstream_writen(peer->stream, __data_buf, n);
	if (ret != n) {
		acl_multi_server_disconnect(peer->stream);
		if (acl_msg_verbose > 3)
			acl_msg_info("%s(%d)->%s: write error = %s",
				__FILE__, __LINE__, myname,
				strerror(errno));
		return;
	}

	acl_multi_server_request_rw_timer(stream);
	acl_multi_server_request_rw_timer(peer->stream);
}
Example #24
0
File: main.c Project: jhomble/redis
static void thread_run(void *arg)
{
    CONN *conn = (CONN*) arg;
    ACL_VSTREAM *client = conn->stream;
    const char *reply_200 = "HTTP/1.1 200 OK\r\n"
                            "Server: nginx/0.6.32\r\n"
                            "Date: Tue, 29 Dec 2009 02:18:25 GMT\r\n"
                            "Content-Type: text/html\r\n"
                            "Content-Length: 43\r\n"
                            "Last-Modified: Mon, 16 Nov 2009 02:18:14 GMT\r\n"
                            "Connection: keep-alive\r\n"
                            "Accept-Ranges: bytes\r\n\r\n"
                            "<html>\n"
                            "<body>\n"
                            "hello world!\n"
                            "</body>\n"
                            "</html>\n";
    int   ret, keep_alive;
    char  buf[4096];

    while (0) {
        ret = read(ACL_VSTREAM_SOCK(client), buf, sizeof(buf));
        if (ret == ACL_VSTREAM_EOF)
            break;
        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF)
            break;
    }

    while (0) {
        ret = acl_vstream_read(client, buf, sizeof(buf));
        if (ret == ACL_VSTREAM_EOF)
            break;
        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF)
            break;
    }

    while (0) {
        /*
        HTTP_REQ *req;
        */
        HTTP_HDR_REQ *hdr_req = http_hdr_req_new();

        ret = http_hdr_req_get_sync(hdr_req, client, 300);
        if (ret < 0) {
            http_hdr_req_free(hdr_req);
            break;
        }
        if (http_hdr_req_parse(hdr_req) < 0) {
            http_hdr_req_free(hdr_req);
            printf("parse error\n");
            break;
        }

        /*
        keep_alive = hdr_req->hdr.keep_alive;

        if (hdr_req->hdr.content_length > 0) {
        	req = http_req_new(hdr_req);
        	ret = (int) http_req_body_get_sync(req, client, buf, sizeof(buf));
        	if (ret < 0) {
        		http_req_free(req);
        		break;
        	}
        	http_req_free(req);
        } else {
        	http_hdr_req_free(hdr_req);
        }
        */

        http_hdr_req_free(hdr_req);
        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF) {
            break;
        }
        /*
        if (!keep_alive)
        	break;
        	*/
    }

    while (1) {
        HTTP_REQ *req;
        HTTP_HDR_REQ *hdr_req = http_hdr_req_new();

        ret = http_hdr_req_get_sync(hdr_req, client, 0);
        if (ret < 0) {
            http_hdr_req_free(hdr_req);
            break;
        }
        if (http_hdr_req_parse(hdr_req) < 0) {
            http_hdr_req_free(hdr_req);
            printf("parse error\n");
            break;
        }

        keep_alive = hdr_req->hdr.keep_alive;

        if (hdr_req->hdr.content_length > 0) {
            req = http_req_new(hdr_req);
            ret = (int) http_req_body_get_sync(req, client, buf, sizeof(buf));
            if (ret < 0) {
                http_req_free(req);
                break;
            }
            http_req_free(req);
        } else {
            http_hdr_req_free(hdr_req);
        }

        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF) {
            break;
        }
        if (!keep_alive)
            break;
    }

    acl_vstream_close(client);
    acl_myfree(conn);
    printf("thread(%ld) exit\n", (long) acl_pthread_self());
}
Example #25
0
static void get_url(const char *method, const char *url,
	const char *proxy, const char *dump)
{
	/* 创建 HTTP 请求头 */
	HTTP_HDR_REQ *hdr_req = http_hdr_req_create(url, method, "HTTP/1.1");
	ACL_VSTREAM *stream;
	ACL_VSTRING *buf = acl_vstring_alloc(256);
	HTTP_HDR_RES *hdr_res;
	HTTP_RES *res;
	ACL_FILE *fp = NULL;
	const char *ptr;
	int   ret;

	/* 输出 HTTP 请求头内容 */

	http_hdr_print(&hdr_req->hdr, "---request hdr---");

	/* 如果设定代理服务器,则连接代理服务器地址,
	 * 否则使用 HTTP 请求头里指定的地址
	 */

	if (*proxy)
		acl_vstring_strcpy(buf, proxy);
	else
		acl_vstring_strcpy(buf, http_hdr_req_host(hdr_req));

	/* 获得远程 HTTP 服务器的连接地址 */

	ptr = acl_vstring_memchr(buf, ':');
	if (ptr == NULL)
		acl_vstring_strcat(buf, ":80");
	else {
		int   port;
		ptr++;
		port = atoi(ptr);
		if (port <= 0 || port >= 65535) {
			printf("http server's addr(%s) invalid\n", acl_vstring_str(buf));
			acl_vstring_free(buf);
			http_hdr_req_free(hdr_req);
			return;
		}
	}

	/* 连接远程 http 服务器 */

	stream = acl_vstream_connect(acl_vstring_str(buf) /* 服务器地址 */,
			ACL_BLOCKING /* 采用阻塞方式 */,
			10 /* 连接超时时间为 10 秒 */,
			10 /* 网络 IO 操作超时时间为 10 秒 */,
			4096 /* stream 流缓冲区大小为 4096 字节 */);
	if (stream == NULL) {
		printf("connect addr(%s) error(%s)\n",
			acl_vstring_str(buf), acl_last_serror());
		acl_vstring_free(buf);
		http_hdr_req_free(hdr_req);
		return;
	}

	/* 构建 HTTP 请求头数据 */

	http_hdr_build_request(hdr_req, buf);

	/* 向 HTTP 服务器发送请求 */

	ret = acl_vstream_writen(stream, acl_vstring_str(buf), ACL_VSTRING_LEN(buf));
	if (ret == ACL_VSTREAM_EOF) {
		printf("write to server error(%s)\n", acl_last_serror());
		acl_vstream_close(stream);
		acl_vstring_free(buf);
		http_hdr_req_free(hdr_req);
		return;
	}

	/* 创建一个 HTTP 响应头对象 */

	hdr_res = http_hdr_res_new();

	/* 读取 HTTP 服务器响应头*/

	ret = http_hdr_res_get_sync(hdr_res, stream, 10 /* IO 超时时间为 10 秒 */);
	if (ret < 0) {
		printf("get http reply header error(%s)\n", acl_last_serror());
		http_hdr_res_free(hdr_res);
		acl_vstream_close(stream);
		acl_vstring_free(buf);
		http_hdr_req_free(hdr_req);
		return;
	}

	if (http_hdr_res_parse(hdr_res) < 0) {
		printf("parse http reply header error\n");
		http_hdr_print(&hdr_res->hdr, "--- reply http header ---");
		http_hdr_res_free(hdr_res);
		acl_vstream_close(stream);
		acl_vstring_free(buf);
		http_hdr_req_free(hdr_req);
		return;
	}

	/* 如果需要转储至磁盘则需要先打开文件 */

	if (dump != NULL) {
		fp = acl_fopen(dump, "w+");
		if (fp == NULL)
			printf("open file(%s) error(%s)\n",
				dump, acl_last_serror());
	}

	/* 如果 HTTP 响应没有数据体则仅输出 HTTP 响应头即可 */

	if (hdr_res->hdr.content_length == 0
		|| (hdr_res->hdr.content_length == -1
			&& !hdr_res->hdr.chunked
			&& hdr_res->reply_status > 300
			&& hdr_res->reply_status < 400))
	{
		if (fp)
			http_hdr_fprint(ACL_FSTREAM(fp), &hdr_res->hdr,
				"--- reply http header ---");
		else
			http_hdr_fprint(ACL_VSTREAM_OUT, &hdr_res->hdr,
				"--- reply http header ---");
		http_hdr_res_free(hdr_res);
		acl_vstream_close(stream);
		acl_vstring_free(buf);
		http_hdr_req_free(hdr_req);
		return;
	}

	/* 输出 HTTP 响应头 */

	http_hdr_print(&hdr_res->hdr, "--- reply http header ---");

	/* 创建 HTTP 响应体对象 */

	res = http_res_new(hdr_res);

	/* 如果有数据体则开始读取 HTTP 响应数据体部分 */
	while (1) {
		http_off_t  n;
		char  buf2[4096];
		
		n = http_res_body_get_sync(res, stream, buf2, sizeof(buf2) - 1);
		if (n <= 0)
			break;

		if (fp) {
			if (acl_fwrite(buf2, (size_t) n, 1, fp) == (size_t) EOF) {
				printf("write to dump file(%s) error(%s)\n",
					dump, acl_last_serror());
				break;
			}
		} else {
			buf2[n] = 0;
			printf("%s", buf2);
		}
	}

	if (fp)
		acl_fclose(fp);
	http_res_free(res);  /* 释放 HTTP 响应对象, hdr_res 会在此函数内部自动被释放 */
	acl_vstream_close(stream);  /* 关闭网络流 */
	acl_vstring_free(buf);  /* 释放内存区 */
	http_hdr_req_free(hdr_req);  /* 释放 HTTP 请求头对象 */
}
Example #26
0
acl_int64 gid_cmdline_next(ACL_VSTREAM *client, const char *tag, int *errnum)
{
	char  buf[1204];
	ACL_ARGV *tokens;
	ACL_ITER iter;
	const char *status = NULL, *gid = NULL, *tag_ptr = NULL, *msg = NULL, *err = NULL;

	if (tag && *tag)
		snprintf(buf, sizeof(buf), "CMD^%s|TAG^%s\r\n", GID_CMD_NEXT, tag);
	else
		snprintf(buf, sizeof(buf), "CMD^%s\r\n", GID_CMD_NEXT);

	if (acl_vstream_writen(client, buf, strlen(buf)) == ACL_VSTREAM_EOF) {
		if (errnum)
			*errnum = GID_ERR_IO;
		return (-1);
	} else if (acl_vstream_gets_nonl(client, buf, sizeof(buf)) == ACL_VSTREAM_EOF)
	{
		if (errnum)
			*errnum = GID_ERR_IO;
		return (-1);
	}

	tokens = acl_argv_split(buf, "|");
	acl_foreach(iter, tokens) {
		const char *ptr = (const char*) iter.data;

		if (strncasecmp(ptr, "STATUS^", sizeof("STATUS^") - 1) == 0) {
			status = ptr + sizeof("STATUS^") - 1;
		} else if (strncasecmp(ptr, "GID^", sizeof("GID^") - 1) == 0) {
			gid = ptr + sizeof("GID^") - 1;
		} else if (strncasecmp(ptr, "TAG^", sizeof("TAG^") - 1) == 0) {
			tag_ptr = ptr + sizeof("TAG^") - 1;
		} else if (strncasecmp(ptr, "MSG^", sizeof("MSG^") - 1) == 0) {
			msg = ptr + sizeof("MSG^") - 1;
		} else if (strncasecmp(ptr, "ERR^", sizeof("ERR^") - 1) == 0) {
			err = ptr + sizeof("ERR^");
		}
	}

	if (status == NULL) {
		if (errnum)
			*errnum = GID_ERR_PROTO;
		acl_argv_free(tokens);
		return (-1);
	} else if (strcasecmp(status, "OK") != 0) {
		if (errnum) {
			if (err)
				*errnum = atoi(err);
			else
				*errnum = GID_ERR_SERVER;
		}
		acl_argv_free(tokens);
		return (-1);
	} else if (gid == NULL) {
		if (errnum)
			*errnum = GID_ERR_PROTO;
		acl_argv_free(tokens);
		return (-1);
	} else {
		acl_int64 ngid = atoll(gid);
		acl_argv_free(tokens);
		return (ngid);
	}
}
Example #27
0
File: main.c Project: 10jschen/acl
static void run_one(TLS_APPL_STATE *client_tls_ctx)
{
	ACL_VSTREAM *client;
	TLS_SESS_STATE *client_sess_ctx;
	TLS_CLIENT_START_PROPS tls_props;
	char  buf[4096];
	int   i, ret;
	time_t last, now;

	int   tls_level = 2;
	char *tls_nexthop = "test.com.cn";
	char *host = "test.com.cn";
	char *namaddrport = "test.com.cn";
	char *serverid = "service:addr:port:helo";
	char *tls_protocols = SSL_TXT_TLSV1;
	char *tls_grade = "low";  /* high, medium, low, export, null */
	char *tls_exclusions = "";
	ACL_ARGV *tls_matchargv = 0;

	client = acl_vstream_connect(serv_addr, ACL_BLOCKING, 20, 30, 8192);
	if (client == NULL) {
		printf("connect %s error(%s)\n", serv_addr, acl_last_serror());
		return;
	}

	acl_tcp_set_nodelay(ACL_VSTREAM_SOCK(client));

#if 0
	client_sess_ctx = TLS_CLIENT_START(&tls_props,
			ctx = client_tls_ctx,
			stream = client,
			log_level = var_client_tls_loglevel,
			timeout = var_client_starttls_tmout,
			tls_level = tls_level,
			nexthop = tls_nexthop,
			host = host,
			namaddr = namaddrport,
			serverid = serverid,
			protocols = tls_protocols,
			cipher_grade = tls_grade,
			cipher_exclusions = tls_exclusions,
			matchargv = tls_matchargv,
			fpt_dgst = var_client_tls_fpt_dgst);
#else
	tls_props.ctx = client_tls_ctx;
	tls_props.stream = client;
	tls_props.log_level = var_client_tls_loglevel;
	tls_props.timeout = var_client_starttls_tmout;
	tls_props.tls_level = tls_level;
	tls_props.nexthop = tls_nexthop;
	tls_props.host = host;
	tls_props.namaddr = namaddrport;
	tls_props.serverid = serverid;
	tls_props.protocols = tls_protocols;
	tls_props.cipher_grade = tls_grade;
	tls_props.cipher_exclusions = tls_exclusions;
	tls_props.matchargv = tls_matchargv;
	tls_props.fpt_dgst = var_client_tls_fpt_dgst;

	client_sess_ctx = tls_client_start(&tls_props);
#endif

	if (client_sess_ctx == NULL) {
		printf("TLS_CLIENT_START error\n");
		acl_vstream_close(client);
		return;
	}

	if (tls_level >= TLS_LEV_VERIFY) {
		if (!TLS_CERT_IS_TRUSTED(client_sess_ctx)) {
			printf("Server certificate not trusted\n");
		}
	}

	if (tls_level > TLS_LEV_ENCRYPT) {
		if (!TLS_CERT_IS_MATCHED(client_sess_ctx)) {
			printf("Server certificate not verified\n");
		}
	}

	time(&last);
	i = 0;
	while (1) {
		ret = acl_vstream_fprintf(client, "hello world\n");
		if (ret == ACL_VSTREAM_EOF)
			goto END;
		ret = acl_vstream_gets(client, buf, sizeof(buf));
		if (ret == ACL_VSTREAM_EOF)
			goto END;
		break;
		i++;
		if (i % 50000 == 0) {
			time(&now);
			printf("client: i=%d, time=%ld\n", i, now - last);
			last = now;
		}
	}

if (0)
{
	if (acl_vstream_writen(client, request, strlen(request)) == ACL_VSTREAM_EOF)
		printf("write request error\n");
	else {
		while (1) {
			if (acl_vstream_gets_nonl(client, buf, sizeof(buf)) == ACL_VSTREAM_EOF)
				break;
			/*
			printf("%s\n", buf);
			*/
		}
		/*
		printf("gets respond over now\n");
		*/
	}
}

END:
	tls_client_stop(client_tls_ctx, client, var_client_starttls_tmout, 0, client_sess_ctx);
	acl_vstream_close(client);
}
Example #28
0
void *CHttpClient::DoRequestThread(void* arg)
{
	CHttpClient *phClient = (CHttpClient*) arg;
	HTTP_HDR_REQ *hdr_req = NULL;
	HTTP_HDR_RES *hdr_res = NULL;
	HTTP_RES *http_res = NULL;
	ACL_VSTRING *buf = acl_vstring_alloc(256);
	ACL_VSTREAM *server = NULL;
	const char *pHost = NULL;
	ACL_VSTREAM *fp = NULL;
	int   ret;
	UINT  nForward = 0;
	time_t begin = 0;
	struct timeval begin_tv, end_tv;
	double time_cost = 0;

#undef RETURN
#define RETURN(_x_) do  \
{  \
	if (hdr_req)  \
		http_hdr_req_free(hdr_req);  \
	if (buf)  \
		acl_vstring_free(buf);  \
	if (hdr_res)  \
		http_hdr_res_free(hdr_res);  \
	if (http_res) {  \
		http_res->hdr_res = NULL;  \
		http_res_free(http_res);  \
	}  \
	if (server)  \
		acl_vstream_close(server);  \
	if (fp)  \
		acl_vstream_close(fp);  \
	gettimeofday(&end_tv, NULL); \
	time_cost = stamp_sub(&end_tv, &begin_tv); \
	if (time_cost >= 0) \
		phClient->ReportTime(time_cost); \
	phClient->ReportComplete(); \
	return (_x_);  \
} while(0)

	const char *pUrl = phClient->m_sReqUrl.GetString();
	hdr_req = http_hdr_req_create(pUrl, phClient->m_bPostMethod ? "POST" : "GET",
		phClient->m_bHttp11 ? "HTTP/1.1" : "HTTP/1.0");
	ASSERT(hdr_req);

	if (phClient->m_bZip)
		http_hdr_put_str(&hdr_req->hdr, "Accept-Encoding", "gzip, deflate");
	if (phClient->m_bKeepAlive)
		http_hdr_entry_replace(&hdr_req->hdr, "Connection", "Keep-Alive", 1);
	if (phClient->m_bPostMethod)
		http_hdr_put_int(&hdr_req->hdr, "Content-Length",
			(int) phClient->m_sHttpBody.GetLength());
	if (!phClient->m_sAccept.IsEmpty())
		http_hdr_put_str(&hdr_req->hdr, "Accept", phClient->m_sAccept.GetString());
	if (!phClient->m_sCtype.IsEmpty())
		http_hdr_put_str(&hdr_req->hdr, "Content-Type", phClient->m_sCtype.GetString());

	if (phClient->m_sHttpHdrAppend.GetLength() > 0) {
		ACL_ARGV *argv;
		HTTP_HDR_ENTRY *entry;
		int   i;
		
		argv = acl_argv_split(phClient->m_sHttpHdrAppend.GetString(), "\r\n");
		for (i = 0; i < argv->argc; i++) {
			entry = http_hdr_entry_new(argv->argv[i]);
			if (entry == NULL)
				continue;
			http_hdr_append_entry(&hdr_req->hdr, entry);
		}
		acl_argv_free(argv);
	}

FORWARD:

	http_hdr_build_request(hdr_req, buf);
	pHost = http_hdr_req_host(hdr_req);
	ASSERT(pHost);

	phClient->ReportReqHdr(acl_vstring_str(buf), (int) ACL_VSTRING_LEN(buf));

	CString serverAddr;

	if (phClient->m_bUseAddr)
		serverAddr.Format("%s", phClient->m_sServerAddr);
	if (serverAddr.GetLength() == 0)
		serverAddr.Format("%s", pHost);
	if (strchr(serverAddr.GetString(), ':') == NULL)
		serverAddr.AppendFormat(":80");

	time(&begin);
	gettimeofday(&begin_tv, NULL);
	server = acl_vstream_connect(serverAddr.GetString(),
				ACL_BLOCKING, 10, 10, 4096);
	if (server == NULL) {
		CString msg;

		msg.Format("Connect server(%s) error", serverAddr.GetString());
		phClient->ReportErrConnect(msg.GetString());
		RETURN (NULL);
	}

	if (phClient->m_bLocalSave && fp == NULL) {
		fp = acl_vstream_fopen(phClient->m_sLocalFile.GetString(),
			O_WRONLY | O_CREAT | O_TRUNC, 0600, 4096);
		if (fp == NULL) {
			acl_msg_error("%s(%d): can't create file(%s)",
				__FILE__, __LINE__, phClient->m_sLocalFile.GetString());
			RETURN (NULL);
		}
	}

	ret = acl_vstream_writen(server, acl_vstring_str(buf), ACL_VSTRING_LEN(buf));
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("%s(%d): write error", __FILE__, __LINE__);
		RETURN (NULL);
	}

	if (phClient->m_bPostMethod && !phClient->m_sHttpBody.IsEmpty())
	{
		if (acl_vstream_writen(server, phClient->m_sHttpBody.GetString(),
			phClient->m_sHttpBody.GetLength()) == ACL_VSTREAM_EOF)
		{
			acl_msg_error("%s(%d): write body error", __FILE__, __LINE__);
			RETURN (NULL);
		}
	}

	hdr_res = http_hdr_res_new();
	if (http_hdr_res_get_sync(hdr_res, server, 10) < 0) {
		acl_msg_error("%s(%d): get res hdr error", __FILE__, __FILE__, __LINE__);
		RETURN (NULL);
	}

	if (http_hdr_res_parse(hdr_res) < 0) {
		acl_msg_error("%s(%d): parse hdr_res error", __FILE__, __LINE__);
		RETURN (NULL);
	}

	http_hdr_build(&hdr_res->hdr, buf);
	phClient->ReportResHdr(acl_vstring_str(buf), (int) ACL_VSTRING_LEN(buf));
	phClient->ReportResContentLength((int) hdr_res->hdr.content_length);

	if (hdr_res->reply_status > 300	&& hdr_res->reply_status < 400) {
		const char* pLocation;
		HTTP_HDR_REQ *hdr_req_tmp;

		if (!phClient->m_bForwardAuto)
			RETURN (NULL);

		if (nForward++ >= phClient->m_nMaxTry) {
			acl_msg_error("%s(%d): too many redirect, nForward(%d)",
				__FILE__, __LINE__, nForward);
			RETURN (NULL);
		}

		pLocation = http_hdr_entry_value(&hdr_res->hdr, "Location");
		if (pLocation == NULL || *pLocation == 0) {
			acl_msg_error("%s(%d): 302 reply with no Location", __FILE__, __LINE__);
			RETURN (NULL);
		}

		hdr_req_tmp = http_hdr_req_rewrite(hdr_req, pLocation);
		if (hdr_req_tmp == NULL)
			RETURN (NULL);
		http_hdr_req_free(hdr_req);
		http_hdr_res_free(hdr_res);
		hdr_req = hdr_req_tmp;
		goto FORWARD;
	}

	http_res = http_res_new(hdr_res);
	while (1) {
		char  tmp_buf[4096];

		ret = (int) http_res_body_get_sync2(http_res, server,
				tmp_buf, sizeof(tmp_buf) - 1);
		if (ret <= 0)
			break;

		phClient->ReportResBody(tmp_buf, ret);
		phClient->ReportResDownLength((int)(time(NULL) - begin), ret);

		if (fp != NULL && acl_vstream_writen(fp, tmp_buf, ret) == ACL_VSTREAM_EOF) {
			acl_msg_error("%s(%d): write to file error", __FILE__, __LINE__);
			break;
		}
	}
	RETURN (NULL);
}
Example #29
0
static int parse_xml_file(const char *filepath)
{
	int   n;
	acl_int64 len;
	char  buf[10240];
	ACL_VSTREAM *in = acl_vstream_fopen(filepath, O_RDONLY, 0600, 8192);
	const char* outfile = "./out.xml";
	ACL_VSTREAM *out = acl_vstream_fopen(outfile, O_RDWR | O_CREAT | O_TRUNC, 0600, 8192);
	ACL_XML2 *xml;
	const char *mmap_file = "./local.map";
	const char* ptr;

	if (in == NULL) {
		printf("open %s error %s\r\n", filepath, acl_last_serror());
		return -1;
	}

	if (out == NULL)
	{
		printf("open %s error %s\r\n", outfile, acl_last_serror());
		acl_vstream_close(in);
		return -1;
	}

	len = acl_vstream_fsize(in);
	if (len <= 0) {
		printf("fsize %s error %s\r\n", filepath, acl_last_serror());
		acl_vstream_close(in);
		acl_vstream_close(out);
		return -1;
	}

	acl_vstream_printf(">>>file(%s)'s size: %lld\r\n", filepath, len);

	len *= 4;

	xml = acl_xml2_mmap_file(mmap_file, len, 10, 1, NULL);

	len = 0;
	while (1) {
		n = acl_vstream_read(in, buf, sizeof(buf) - 1);
		if (n == ACL_VSTREAM_EOF)
			break;
		buf[n] = 0;
		acl_xml2_update(xml, buf);
		len += n;
	}

	acl_vstream_close(in);

	acl_vstream_printf(">>read size: %lld\r\n", len);

	ptr = acl_xml2_build(xml);
	if (ptr == NULL)
		printf("acl_xml2_build error\r\n");

	len = xml->ptr - ptr;
	acl_vstream_printf(">>>build xml's size:%lld\r\n", len);
	acl_vstream_printf(">>> ptr: {%s}\r\n", ptr);

	if (acl_vstream_writen(out, ptr, len) == ACL_VSTREAM_EOF) {
		printf("write error %s, len: %ld\r\n",
			acl_last_serror(), (long) len);
		return -1;
	}

	acl_vstream_close(out);
	acl_xml2_free(xml);

	return 0;
}
Example #30
0
int   main(int argc, char *argv[])
{
	ACL_VSTREAM *client;
	char  addr[64], *buf = NULL, line[128];
	int   n, i, len = 4096, count = 10, inter = 1000;

	snprintf(addr, sizeof(addr), "127.0.0.1:8888");

	while ((n = getopt(argc, argv, "hs:l:n:i:")) > 0) {
		switch (n) {
		case 'h':
			usage(argv[0]);
			return 0;
		case 's':
			snprintf(addr, sizeof(addr), "%s", optarg);
			break;
		case 'l':
			len = atoi(optarg);
			break;
		case 'n':
			count = atoi(optarg);
			break;
		case 'i':
			inter = atoi(optarg);
			break;
		default:
			break;
		}
	}

	if (len <= 0) {
		printf("invalid len: %d <= 0\r\n", len);
		return 1;
	}

	/* 连接服务器 */
	client = acl_vstream_connect(addr, ACL_BLOCKING, 10, 10, 4096);
	if (client == NULL) {
		printf("connect %s error %s\r\n", addr, acl_last_serror());
		return 1;
	}
	printf("connect %s ok ...\r\n", addr);

	buf = (char*) acl_mymalloc(len);
	snprintf(buf, len, "%d\r\n", len);

	/* 发送一行数据通知服务端每次数据体的长度 */
	if (acl_vstream_writen(client, buf, strlen(buf)) == ACL_VSTREAM_EOF)
	if (n == ACL_VSTREAM_EOF)
		goto END;

	memset(buf, 'X', len);

	for (i = 0; i < count; i++) {
		n = acl_vstream_writen(client, buf, len);
		if (n == ACL_VSTREAM_EOF) {
			printf("write error %s\r\n", acl_last_serror());
			break;
		}
		if (i % inter == 0) {
			snprintf(line, sizeof(line), "curr: %d, total: %d", i, count);
			ACL_METER_TIME(line);
		}
	}

END:

	if (buf)
		acl_myfree(buf);
	acl_vstream_close(client);

	return 0;
}