Ejemplo n.º 1
0
int smtp_send_file(SMTP_CLIENT *client, const char* filepath)
{
	int   ret;
	ACL_VSTREAM *in = acl_vstream_fopen(filepath, O_RDONLY, 0600, 4096);

	if (in == NULL) {
		acl_msg_error("%s(%d): open %s error(%s)", __FUNCTION__,
			__LINE__, filepath, acl_last_serror());
		return -1;
	}
	ret = smtp_send_stream(client, in);
	acl_vstream_close(in);
	return ret;
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
CMsnContactManager::CMsnContactManager()
{
	STRING_SET_MEPTY(cache_key_);
	STRING_SET_MEPTY(preferred_hostname_);
	STRING_SET_MEPTY(session_id_);

	groups_ = NULL;
	memberShips_ = NULL;
	contacts_ = NULL;
	addressAb_ = NULL;

	debug_fpout_ = acl_vstream_fopen("contact.txt",
		O_CREAT | O_TRUNC | O_WRONLY, 0700, 4096);
	if (debug_fpout_ == NULL)
		logger_error("fopen contact.txt error(%s)", acl_last_serror());
}
Ejemplo n.º 5
0
ACL_VSTREAM *local_listen()
{
	const char *myname = "local_listen";
	char  lock_file[MAX_PATH], ebuf[256];
	ACL_VSTREAM *sstream, *fp;
	ACL_FILE_HANDLE handle;

	get_lock_file(lock_file, sizeof(lock_file));

	fp = acl_vstream_fopen(lock_file, O_RDWR | O_CREAT, 0600, 1024);
	if (fp == NULL)
		acl_msg_fatal("%s(%d): open file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	handle = ACL_VSTREAM_FILE(fp);
	if (acl_myflock(handle, 0, ACL_MYFLOCK_OP_EXCLUSIVE | ACL_MYFLOCK_OP_NOWAIT) == -1) {
		acl_msg_error("%s(%d): lock file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
		return (NULL);
	}

	sstream = acl_vstream_listen_ex("127.0.0.1:0", 128, ACL_BLOCKING, 1024, 0);
	if (sstream == NULL)
		acl_msg_fatal("%s(%d): listen error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_file_ftruncate(fp, 0) < 0)
		acl_msg_fatal("%s(%d): truncate file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
	if (acl_vstream_fseek(fp, 0, SEEK_SET) < 0)
		acl_msg_fatal("%s(%d): fseek file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	if (acl_vstream_fprintf(fp, "%s\r\n", sstream->local_addr) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s(%d): fprintf to file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));

	/* XXX: 只能采用先解排它锁,再加共享锁,微软比较弱!!! */

	if (acl_myflock(handle, 0, ACL_MYFLOCK_OP_NONE) == -1)
		acl_msg_fatal("%s(%d): unlock file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
	if (acl_myflock(handle, 0, ACL_MYFLOCK_OP_SHARED | ACL_MYFLOCK_OP_NOWAIT) == -1)
		acl_msg_fatal("%s(%d): lock file(%s) error(%s)",
			myname, __LINE__, lock_file, acl_last_strerror(ebuf, sizeof(ebuf)));
	return (sstream);
}
Ejemplo n.º 6
0
static void test_thread_pool(void)
{
	acl_pthread_pool_t *thr_pool;
	ACL_VSTREAM *fp = acl_vstream_fopen("test.log", O_WRONLY | O_CREAT, 0600, 4096);
	int   i;

	acl_pthread_mutex_init(&__mutex, NULL);
	thr_pool = acl_thread_pool_create(10, 10);

	for (i = 0; i < 1000000; i++) {
		RUN_CTX *ctx = (RUN_CTX*) acl_mymalloc(sizeof(RUN_CTX));
		ctx->fp = fp;
		ctx->i = i;
		acl_pthread_pool_add(thr_pool, run_thread, ctx);
	}

	acl_pthread_pool_destroy(thr_pool);
	acl_pthread_mutex_destroy(&__mutex);
	acl_vstream_close(fp);
}
Ejemplo n.º 7
0
static int test_file3(void)
{
	char  buf[1024];
	int   ret;

	ACL_VSTREAM *fp = acl_vstream_fopen("test3.txt", O_RDONLY, 0700, 8192);
	if (fp == NULL) {
		printf("fopen error %s\n", acl_last_serror());
		return (-1);
	}

	while (1) {
		ret = acl_vstream_gets_nonl(fp, buf, sizeof(buf));
		if (ret == ACL_VSTREAM_EOF)
			break;
		printf(">>>gets(%s), ret: %d\n", buf, ret);
	}

	acl_vstream_fclose(fp);
	return (0);
}
Ejemplo n.º 8
0
bool aio_fstream::open(const char* path, unsigned int oflags, unsigned int mode)
{
	ACL_VSTREAM* fp = acl_vstream_fopen(path, oflags, mode, 8192);
	if (fp == NULL)
		return false;
	stream_ = acl_aio_open(handle_->get_handle(), fp);

	// 调用基类的 hook_error 以向 handle 中增加异步流计数,
	// 同时 hook 关闭及超时回调过程
	hook_error();

	// 只有当流连接成功后才可 hook IO 读写状态
	// hook 读回调过程
	if ((oflags & (O_RDONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC)))
		hook_read();

	// hook 写回调过程
	if ((oflags & (O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC)))
		hook_write();

	return true;
}
Ejemplo n.º 9
0
int get_addr_from_file(const char *filepath, char *buf, size_t size)
{
	const char *myname = "get_addr_from_file";
	ACL_VSTREAM *fp;
	char  ebuf[256];
	int   n;

	fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 1024);
	if (fp == NULL) {
		acl_msg_error("%s(%d): fopen file(%s) error(%s)",
			myname, __LINE__, filepath, acl_last_strerror(ebuf, sizeof(ebuf)));
		return (-1);
	}

	n = acl_vstream_gets_nonl(fp, buf, size);
	acl_vstream_close(fp);

	if (n == ACL_VSTREAM_EOF)
		acl_msg_error("%s(%d): gets from file(%s) error(%s)",
			myname, __LINE__, filepath, acl_last_strerror(ebuf, sizeof(ebuf)));

	return (0);
}
Ejemplo n.º 10
0
int http_util_set_dump_file(HTTP_UTIL *http_util, const char *filename)
{
	const char *myname = "http_util_set_dump_file";

	if (filename == NULL || *filename == 0) {
		acl_msg_error("%s(%d): filename invalid", myname, __LINE__);
		return (-1);
	}
	if ((http_util->flag & HTTP_UTIL_FLAG_SET_DUMP_STREAM)) {
		acl_msg_error("%s(%d): You've called http_util_set_dump_stream before!",
			myname, __LINE__);
		return (-1);
	}

	http_util->dump_stream = acl_vstream_fopen(filename,
			O_CREAT | O_TRUNC | O_WRONLY, 0600, 4096);
	if (http_util->dump_stream == NULL) {
		acl_msg_error("%s(%d): open dump file(%s) error(%s)",
			myname, __LINE__, filename, acl_last_serror());
		return (-1);
	}
	http_util->flag |= HTTP_UTIL_FLAG_SET_DUMP_FILE;
	return (0);
}
Ejemplo n.º 11
0
static int test_vstream(void)
{
	const char *filename = "test.txt";
	ACL_VSTREAM *fp = acl_vstream_fopen(filename, O_RDWR | O_CREAT, 0644, 1024);
	struct acl_stat sbuf;
	char  buf[256];
	struct tm *local_time;

	acl_lib_init();

	if (fp == NULL) {
		printf("open file(%s) error(%s)\r\n", filename, acl_last_serror());
		end();
		return (-1);
	}

	if (acl_vstream_fstat(fp, &sbuf) < 0) {
		acl_vstream_close(fp);
		printf("fstat file(%s) error(%s)\r\n", filename, acl_last_serror());
		end();
		return (-1);
	}

	printf("file(%s) stat:\r\n", filename);

	printf("size=%d\r\n", (int) sbuf.st_size);

	local_time = localtime(&sbuf.st_mtime);
	if (local_time) {
		strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time);
		printf("修改时间,mtime=%s\r\n", buf);
	} else {
		printf("mtime: error(%s)\r\n", acl_last_serror());
	}

	local_time = localtime(&sbuf.st_ctime);
	if (local_time) {
		strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time);
		printf("创建时间,ctime=%s\r\n", buf);
	} else {
		printf("ctime: error(%s)\r\n", acl_last_serror());
	}

	local_time = localtime(&sbuf.st_atime);
	if (local_time) {
		strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time);
		printf("访问时间,atime=%s\r\n", buf);
	} else {
		printf("atime: error(%s)\r\n", acl_last_serror());
	}

	int  ch, ch1;

	ch = ACL_VSTREAM_GETC(fp);
	ch = 'a';

	for (int i = 0; i < 100; i++) {
		ch1 = ACL_VSTREAM_PUTC(ch, fp);
		assert(ch1 == ch);
		printf("ok write char: %c\n", ch1);
	}
	ACL_VSTREAM_PUTC('\n', fp);
	acl_vstream_fclose(fp);
	end();	

	FILE *fp1 = fopen("test.txt", "w+");
	assert(fp1);
	int ret = fputs("hello", fp1);
	printf("ret: %d\n", ret);
	fclose(fp1);
	return (0);
}
Ejemplo n.º 12
0
Archivo: main.c Proyecto: 10jschen/acl
static void fmt_change(const char *filepath, const char *fmt)
{
	const char *myname = "fmt_change";
	ACL_VSTREAM *fp;
	char *buf, *ptr;
	const char *tmp;
	int   n, ret;

	buf = acl_vstream_loadfile(filepath);
	if (buf == NULL)
		acl_msg_fatal("%s: loadfile(%s, %s)",
			myname, filepath, strerror(errno));

	fp = acl_vstream_fopen(filepath, O_WRONLY | O_TRUNC, 0600, 4096);
	if (fp == NULL)
		acl_msg_fatal("%s: open file(%s, %s)",
			myname, filepath, strerror(errno));

	ptr = buf;

	while (*ptr) {
		n = 0;
		if (*ptr == '\r') {
			n++;
			ptr++;
			if (*ptr == '\n') {
				n++;
				ptr++;
				if (strstr(filepath, "samples") == NULL
				    && strstr(filepath, "unit_test") == NULL)
				{
					if (strrncasecmp(filepath, ".c", 2) == 0)
						__total_c_line++;
					else if (strrncasecmp(filepath, ".cpp", 4) == 0)
						__total_cpp_line++;
					else if (strstr(filepath, "mysql") == NULL
						&& strstr(filepath, "openssl") == NULL
						&& strstr(filepath, "dist") == NULL
						&& strstr(filepath, "bdb") == NULL
						&& strstr(filepath, "tc") == NULL
						&& strstr(filepath, "google") == NULL
						&& strstr(filepath, "iconv") == NULL
						&& strstr(filepath, "polarssl") == NULL
						&& strstr(filepath, "sqlite") == NULL
						&& strstr(filepath, "zlib") == NULL
						&& strstr(filepath, "cdb") == NULL)
					{
						if (strrncasecmp(filepath, ".h", 2) == 0)
							__total_h_line++;
						else if (strrncasecmp(filepath, ".hpp", 4) == 0)
							__total_hpp_line++;
					}
				}
			}
		} else if (*ptr == '\n') {
			n++;
			ptr++;
			if (strstr(filepath, "samples") == NULL
					&& strstr(filepath, "unit_test") == NULL)
			{
				if (strrncasecmp(filepath, ".c", 2) == 0)
					__total_c_line++;
				else if (strrncasecmp(filepath, ".cpp", 4) == 0)
					__total_cpp_line++;
				else if (strstr(filepath, "mysql") == NULL
					&& strstr(filepath, "openssl") == NULL
					&& strstr(filepath, "dist") == NULL
					&& strstr(filepath, "bdb") == NULL
					&& strstr(filepath, "tc") == NULL
					&& strstr(filepath, "google") == NULL
					&& strstr(filepath, "iconv") == NULL
					&& strstr(filepath, "polarssl") == NULL
					&& strstr(filepath, "sqlite") == NULL
					&& strstr(filepath, "zlib") == NULL
					&& strstr(filepath, "cdb") == NULL)
				{
					if (strrncasecmp(filepath, ".h", 2) == 0)
						__total_h_line++;
					else if (strrncasecmp(filepath, ".hpp", 4) == 0)
						__total_hpp_line++;
				}
			}
		}
		if (n) {
			tmp = fmt;
			n = strlen(tmp);
		} else {
			tmp = ptr++;
			n = 1;
		}
		ret = acl_vstream_buffed_writen(fp, tmp, n);
		if (ret == ACL_VSTREAM_EOF)
			acl_msg_fatal("%s: write to %s error(%s)",
				myname, filepath, strerror(errno));
	}

	if (acl_vstream_fflush(fp) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s: fflush to %s error(%s)",
			myname, filepath, strerror(errno));
	acl_vstream_close(fp);
	acl_myfree(buf);
}
Ejemplo n.º 13
0
Archivo: main.c Proyecto: 10jschen/acl
int main(int argc, char* argv[])
{
	char  buf[8192], filepath[256];
	int   ret, n;
	ACL_VSTREAM* fp;
	ACL_XML* xml;
	struct timeval begin, end;
	double spent;
	int   ch, use_slice = 0, cache_count = 1000;

	filepath[0] = 0;
	while ((ch = getopt(argc, argv, "hmc:f:")) > 0)
	{
		switch (ch)
		{
		case 'h':
			usage(argv[0]);
			return 0;
		case 'm':
			use_slice = 1;
			break;
		case 'c':
			cache_count = atoi(optarg);
			if (cache_count <= 0)
				cache_count = 1000;
			break;
		case 'f':
			snprintf(filepath, sizeof(filepath), "%s", optarg);
			break;
		default:
			break;
		}
	}

	if (use_slice)
		acl_mem_slice_init(8, 1024, 100000,
			ACL_SLICE_FLAG_GC2 |
			ACL_SLICE_FLAG_RTGC_OFF |
			ACL_SLICE_FLAG_LP64_ALIGN);

	if (filepath[0] == 0)
	{
		usage(argv[0]);
		return 1;
	}

	xml = acl_xml_alloc();
	if (cache_count > 0)
		acl_xml_cache(xml, cache_count);

	fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 8192);
	if (fp == NULL)
	{
		printf("open file %s error %s\r\n",
			filepath, acl_last_serror());
		acl_xml_free(xml);
		return 1;
	}

	gettimeofday(&begin, NULL);
	n = 0;
	ACL_METER_TIME("------begin------");
	while (1)
	{
		ret = acl_vstream_fgets(fp, buf, sizeof(buf) - 1);
		if (ret == ACL_VSTREAM_EOF)
			break;
		buf[ret] = 0;
		acl_xml_parse(xml, buf);
		if (++n % 10000 == 0)
		{
			printf("line: %d\r\n", n);
			ACL_METER_TIME("-------ok------");
		}
		if (n % cache_count == 0)
		{
			printf("reset xml, line: %d\r\n", n);
			acl_xml_reset(xml);
		}
	}

	gettimeofday(&end, NULL);
	spent = stamp_sub(&end, &begin);
	printf("\r\ntotal spent: %0.2f ms\r\n", spent);

	acl_xml_free(xml);
	acl_vstream_fclose(fp);
	return 0;
}
Ejemplo n.º 14
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);
}
Ejemplo n.º 15
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;
}