Пример #1
0
static http_off_t chunked_data_get(HTTP_CHAT_CTX *ctx, void *buf, int size)
{
	if (ctx->chunk_len > 0) {
		char *ptr = buf;
		http_off_t ntotal = 0, ret, n;

		n = ctx->chunk_len - ctx->read_cnt;
		n = n > size ? size : n;
		while (n > 0) {
			ret = acl_vstream_read(ctx->stream, ptr, (size_t) n);
			if (ret == ACL_VSTREAM_EOF) {
				if (ntotal == 0)
					ntotal = -1;
				break;
			}
			ntotal += ret;
			n -= ret;
			ptr += ret;
			ctx->body_len += ret;
			ctx->read_cnt += ret;
			if ((ctx->flag & HTTP_CHAT_FLAG_BUFFED) == 0)
				break;
		}
		return (ntotal);
	} else {
		http_off_t   ret;

		ret = acl_vstream_read(ctx->stream, buf, (size_t) size);
		if (ret == ACL_VSTREAM_EOF)
			return (-1);
		ctx->body_len += ret;
		ctx->read_cnt += ret;
		return (ret);
	}
}
Пример #2
0
static void test_connect(const char *addr)
{
	ACL_VSTREAM *stream;
#if 1
	const char *request = "GET / HTTP/1.1\nHOST: store.hexun.com\nConnection: keep-alive\n\nGET / HTTP/1.1\nHOST: store.hexun.com\nConnection: close\n\n";
#else
	const char *request = "GET / HTTP/1.1\nHOST: store.hexun.com\nConnection: keep-alive\n\n";
#endif

	stream = acl_vstream_connect(addr, ACL_BLOCKING, 10, 10, 4096);
	if (stream == NULL) {
		printf("connect %s error(%s)\n", addr, acl_last_serror());
		return;
	}
	acl_vstream_fprintf(stream, "%s", request);
	printf("request:(%s)\n", request);

	while (1) {
		char  buf[4096];
		int   ret = acl_vstream_read(stream, buf, sizeof(buf) - 1);
		if (ret == ACL_VSTREAM_EOF)
			break;
		buf[ret] = 0;
		printf("%s", buf);
	}
}
Пример #3
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;
}
Пример #4
0
static void udp_read_callback(int, ACL_EVENT*, ACL_VSTREAM* in,
	void *context)
{
	char buf[4096];
	int  ret;
	acl::socket_stream* out = (acl::socket_stream*) context;

	ret = acl_vstream_read(in, buf, sizeof(buf));
	if (ret == ACL_VSTREAM_EOF)
	{
		printf("read error: %s\r\n", acl::last_serror());
		return;
	}

	if (out->write(buf, ret) == -1)
		printf("write error: %s\r\n", acl::last_serror());
}
Пример #5
0
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);
}
Пример #6
0
static int vstream_client(void)
{
	const char *myname = "vstream_client";
	ACL_VSTREAM *client;
	char  ebuf[256], buf[4096];
	int   n, dlen = 0;

	printf("addr: %s, timeout: %d\n", addr, timeout);
	client = acl_vstream_connect(addr, ACL_NON_BLOCKING, timeout, timeout, 1024);
	if (client == NULL) {
		printf("%s(%d): connect addr %s error(%s)\n",
			myname, __LINE__, addr, acl_last_strerror(ebuf, sizeof(ebuf)));
		return (-1);
	}

	printf("%s: connect %s ok\r\n", myname, addr);
	acl_non_blocking(ACL_VSTREAM_SOCK(client), ACL_BLOCKING);

	n = acl_write_wait(ACL_VSTREAM_SOCK(client), 10);
	if (n < 0) {
		printf("connect timeout: %s\n", acl_last_serror());
		goto END;
	}
	acl_vstream_fprintf(client, "hello world\n");
	while (1) {
		n = acl_vstream_read(client, buf, sizeof(buf));
		if (n == ACL_VSTREAM_EOF) {
			printf("read over: %s\n", acl_last_serror());
			break;
		}
		dlen += n;
		buf[n] = 0;
		printf("read reply: %s\n", buf);
	}

END:
	acl_vstream_close(client);
	printf("%s: read %d\n", myname, dlen);
	return (0);
}
Пример #7
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);
}
Пример #8
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);
}
Пример #9
0
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());
}
Пример #10
0
static void run(const char *local_addr, const char *peer_addr,
	int count, int dlen, int inter, int need_read, int quit)
{
	double spent;
	int   ret, i;
	char  buf[4096], data[4096];
	struct timeval begin, end;
	ACL_VSTREAM *stream = acl_vstream_bind(local_addr, 2);  /* 绑定 UDP 套接口 */

	if (stream == NULL) {
		printf("acl_vstream_bind %s error %s\r\n",
			local_addr, acl_last_serror());
		return;
	}

	if (dlen > (int) sizeof(data) - 1)
		dlen = (int) sizeof(data) - 1;
	for (i = 0; i < dlen; i++)
		data[i] = 'X';
	data[dlen] = 0;

	gettimeofday(&begin, NULL);
	acl_vstream_set_peer(stream, peer_addr);
	ACL_VSTREAM_SET_RWTIMO(stream, 1);

	for (i = 0; i < count; i++) {
		/* 如果服务端的地址是变化的,则应该在写每次前都需要调用
		 * acl_vstream_set_peer
		 */
		ret = acl_vstream_write(stream, data, dlen);
		if (ret == ACL_VSTREAM_EOF) {
			printf("acl_vtream_write error %s\r\n",
				acl_last_serror());
			break;
		}

		if (need_read) {
			ret = acl_vstream_read(stream, buf, sizeof(buf) - 1);
			if (ret == ACL_VSTREAM_EOF) {
				if (errno == ETIMEDOUT) {
					printf("timeout read\r\n");
					continue;
				}
				printf("acl_vstream_read error %s\r\n",
						acl_last_serror());
				break;
			} else
				buf[ret] = 0;
			if (i % inter == 0)
				printf("result: %s\r\n", buf);
		}

		if (i % inter == 0) {
			snprintf(buf, sizeof(buf), "total: %d, curr: %d",
				count, i);
			ACL_METER_TIME(buf);
		}
	}

	gettimeofday(&end, NULL);
	spent = stamp_sub(&end, &begin);

	printf("thread: %lu, total: %d, curr: %d, spent: %.2f, speed: %.2f\r\n",
		(unsigned long) acl_pthread_self(), count, i, spent,
		(i * 1000) / (spent > 1 ? spent : 1));

	printf("thread: %lu, local addr: %s, peer addr: %s\r\n",
		(unsigned long) acl_pthread_self(), ACL_VSTREAM_LOCAL(stream),
		ACL_VSTREAM_PEER(stream));

	if (quit)
		acl_vstream_write(stream, "quit", 4);
	acl_vstream_close(stream);
}
Пример #11
0
}

static void service_exit(char *service acl_unused, char **argv acl_unused)
{
}

/* 协议处理函数入口 */
static void service_main(ACL_VSTREAM *client, char *service acl_unused,
	char **argv acl_unused)
{
	char  buf[256];
	int   ret;

	ACL_VSTREAM_SET_RWTIMO(client, 10);

	ret = acl_vstream_read(client, buf, sizeof(buf));
	if (ret == ACL_VSTREAM_EOF) {
		if (0)
			acl_msg_error("read error %s, local: %s, peer: %s",
				acl_last_serror(), ACL_VSTREAM_LOCAL(client),
				ACL_VSTREAM_PEER(client));
		return;
	}
	buf[ret] = 0;

	if (0)
		acl_msg_info("read: %s", buf);

	ret = acl_vstream_write(client, buf, strlen(buf));
	if (ret == ACL_VSTREAM_EOF) {
		acl_msg_error("read error %s, local: %s, peer: %s",
Пример #12
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;
}