static void test_file(void) { const char *filename = "test2.txt"; ACL_FILE *fp = acl_fopen(filename, "a+"); char buf[1024]; int i; if (fp == NULL) { printf("fopen %s error(%s)\n", filename, acl_last_serror()); return; } for (i = 0 ; i < 100; i++) { if (acl_fputs("hello world!", fp) == EOF) { printf("fputs to %s error(%s)\n", filename, acl_last_serror()); break; } } printf("write to %s ok\n", filename); acl_fclose(fp); fp = acl_fopen(filename, "r"); if (fp == NULL) { printf("fopen %s error(%s)\n", filename, acl_last_serror()); return; } i = 0; while (!acl_feof(fp)) { if (acl_fgets(buf, sizeof(buf), fp) == NULL) { printf("fgets return null, i=%d\n", i); } else { i++; printf(">>gets line(%d): %s", i, buf); } } acl_fclose(fp); }
/* example: { "mysql_dbaddr", "127.0.0.1:3306", &var_cfg_mysql_dbaddr }, */ { "backend_addr", "127.0.0.1:3306", &var_cfg_backend_addr }, { "request_file", "./request.txt", &var_cfg_request_file }, { "respond_file", "./respond.txt", &var_cfg_respond_file }, { 0, 0, 0 }, }; static ACL_FILE *request_fp, *respond_fp; void service_init(void *init_ctx acl_unused) { const char *myname = "service_init"; request_fp = acl_fopen(var_cfg_request_file, "a+"); if (request_fp == NULL) acl_msg_error("%s(%d): open %s error(%s)", myname, __LINE__, var_cfg_request_file, acl_last_serror()); respond_fp = acl_fopen(var_cfg_respond_file, "a+"); if (respond_fp == NULL) acl_msg_error("%s(%d): open %s error(%s)", myname, __LINE__, var_cfg_respond_file, acl_last_serror()); } void service_exit(void *exist_ctx acl_unused) { if (request_fp) acl_fclose(request_fp);
static ZDB_DISK *zdb_disks_load(const char *dbname, const char *dbpath) { const char *myname = "zdb_disks_load"; ACL_VSTRING *buf = acl_vstring_alloc(256); ACL_FILE *fp = NULL; char disk_info[INFO_LEN + 1]; ZDB_DISK *disk, *disks; ACL_ARRAY *a = NULL; ACL_ITER iter; int n, i; #undef RETURN #define RETURN(x) do { \ if (fp) \ acl_fclose(fp); \ acl_vstring_free(buf); \ if (a) \ acl_array_destroy(a, free_disk); \ return (x); \ } while (0) acl_vstring_sprintf(buf, "%s/.%s.disk", dbpath, dbname); fp = acl_fopen(STR(buf), "r"); if (fp == NULL) { acl_msg_error("%s(%d): fopen(%s) error(%s)", myname, __LINE__, STR(buf), acl_last_serror()); RETURN (NULL); } a = acl_array_create(10); while (1) { ACL_ARGV *argv; if (acl_fgets_nonl(disk_info, sizeof(disk_info), fp) == NULL) break; argv = acl_argv_split(disk_info, "|"); if (argv->argc != ITEM_CNT) { acl_msg_error("%s(%d): invalid line(%s)", myname, __LINE__, disk_info); acl_argv_free(argv); continue; } disk = (ZDB_DISK*) acl_mycalloc(1, sizeof(ZDB_DISK)); disk->path = acl_mystrdup(argv->argv[0]); disk->idisk = atoi(argv->argv[1]); disk->priority = atoi(argv->argv[2]); disk->limit = acl_atoui64(argv->argv[3]); disk->count = acl_atoui64(argv->argv[4]); if (acl_array_append(a, disk) < 0) acl_msg_fatal("%s(%d): add disk error(%s)", myname, __LINE__, acl_last_serror()); acl_argv_free(argv); } n = acl_array_size(a); if (n <= 0) { acl_msg_error("%s(%d): empty array of ZDB_DISK", myname, __LINE__); RETURN (NULL); } disks = (ZDB_DISK*) acl_mycalloc(n + 1, sizeof(ZDB_DISK)); i = 0; acl_foreach(iter, a) { disk = (ZDB_DISK*) iter.data; disks[i].limit = disk->limit; disks[i].count = disk->count; disks[i].path = acl_mystrdup(disk->path); disks[i].idisk = disk->idisk; disks[i].priority = disk->priority; disks[i].dat_ifiles = NULL; disks[i].dat_ifiles_size = 0; if (disks[i].idisk != i) { acl_msg_error("%s(%d): idisk(%d) != %d invalid for %s", myname, __LINE__, disks[i].idisk, i, disks[i].path); acl_myfree(disks); RETURN (NULL); } i++; }
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 请求头对象 */ }