void http_hdr_build_request(const HTTP_HDR_REQ *hdr_req, ACL_VSTRING *strbuf) { ACL_ARRAY *entries; HTTP_HDR_ENTRY *entry; int i, n; entries = hdr_req->hdr.entry_lnk; n = acl_array_size(entries); entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, 0); #if 0 acl_vstring_sprintf(strbuf, "%s %s\r\n", entry->name, entry->value); #else acl_vstring_sprintf(strbuf, "%s %s HTTP/%d.%d\r\n", entry->name, acl_vstring_str(hdr_req->url_part), hdr_req->hdr.version.major, hdr_req->hdr.version.minor); #endif for (i = 1; i < n; i++) { entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, i); if (entry == NULL) break; if (entry->off) continue; acl_vstring_sprintf_append(strbuf, "%s: %s\r\n", entry->name, entry->value); } acl_vstring_strcat(strbuf, "\r\n"); }
void http_error_reply(HTTP_CLIENT *http_client, int status, const char *msg) { static __thread ACL_VSTRING *__buf = NULL; const ACL_VSTRING *str; const char *ptr; struct iovec iov[2]; if (__buf == NULL) { __buf = acl_vstring_alloc(256); acl_pthread_atexit_add(__buf, free_buf); } ptr = http_tmpl_title(status); str = http_tmpl_get(status); if (msg == NULL || *msg == 0) msg = acl_vstring_str(str); acl_vstring_sprintf(__buf, reply_error_fmt, status, ptr); iov[0].iov_base = acl_vstring_str(__buf); iov[0].iov_len = ACL_VSTRING_LEN(__buf); iov[1].iov_base = (char*) msg; iov[1].iov_len = strlen(msg); acl_aio_writev(http_client->stream, iov, 2); }
static void test71(void) { ACL_VSTRING* buf = acl_vstring_alloc(256); acl_vstring_sprintf(buf, "max unsigned int: %u; max unsigned long long int: %llu", (unsigned int) -1, (unsigned long long) -1); acl_vstring_free(buf); }
static void attr_print64_long_num(ACL_VSTREAM *fp, unsigned long long_num) { static __thread ACL_VSTRING *plain; if (plain == 0) { plain = acl_vstring_alloc(10); acl_pthread_atexit_add(plain, free_vstring); } acl_vstring_sprintf(plain, "%lu", long_num); attr_print64_str(fp, STR(plain), (int) LEN(plain)); }
static void worker_thread(void *arg) { THREAD_CTX *ctx = (THREAD_CTX*) arg; /* 获得用户自定义对象 */ unsigned int i = 0; static __thread ACL_VSTRING *buf1 = NULL; static __thread ACL_VSTRING *buf2 = NULL; /* 仅是验证参数传递过程 */ assert(ctx->thr_pool == __thr_pool); if (buf1 == NULL) buf1 = acl_vstring_alloc(256); if (buf2 == NULL) buf2 = acl_vstring_alloc(256); acl_vstring_sprintf(buf1, "buf1: tid=%u", (unsigned int) acl_pthread_self()); acl_vstring_sprintf(buf2, "buf2: tid=%u", (unsigned int) acl_pthread_self()); /* 注册函数,当该线程退出时自动释放 buf 内存空间 */ acl_pthread_atexit_add(buf1, free_buf_fn); acl_pthread_atexit_add(buf2, free_buf_fn); while (i < 5) { if (__local != i) acl_msg_fatal("__local=%d invalid", __local); printf("thread id=%u, i=%d, __local=%d\r\n", (unsigned int) acl_pthread_self(), ctx->i, __local); i++; /* 在本线程中将线程局部变量加1 */ __local++; sleep(1); } acl_myfree(ctx); /* 至此,该工作线程进入空闲状态,直到空闲超时退出 */ }
int main(void) { char *src = acl_mystrdup("hello \tworld! you're welcome to China!"); char *ptr, *src_saved; ACL_VSTRING* buf; const char* s = "hello"; unsigned int n1 = (unsigned int) -1; unsigned long n2 = (unsigned long) -1; unsigned long long n3 = (unsigned long long) -1; const char *str2 = "hello world, you're welcome!"; ACL_ARGV *tokens = acl_argv_split(str2, " \t,'!"); ACL_ITER iter; printf("----------------------------------------------\r\n"); acl_foreach(iter, tokens) printf("tokens[%d]: %s\r\n", iter.i, (const char*) iter.data); printf("total: %d\r\n", iter.size); acl_argv_free(tokens); printf("----------------------------------------------\r\n"); src_saved = src; printf("src: %s\r\n", src); while ((ptr = acl_mystrtok(&src, " \t!'")) != NULL) { printf("--------------------------------------\r\n"); printf("ptr: |%s|\r\n", ptr); printf("src: |%s|\r\n", src); printf("src_saved: |%s|\r\n", src_saved); } acl_myfree(src_saved); printf("----------------------------------------------\r\n"); buf = acl_vstring_alloc(1); acl_vstring_sprintf(buf, "%*lu, s: %s, n1: %20u, n2: %20lu, n3: %20llu\n", (int) sizeof(unsigned long) * 4, (unsigned long) getpid(), s, n1, n2, n3); printf("buf: %s\r\n", acl_vstring_str(buf)); acl_vstring_free(buf); printf("Enter any key to continue ...\r\n"); getchar(); test_quote_split(); return 0; }
void http_hdr_build(const HTTP_HDR *hdr, ACL_VSTRING *strbuf) { ACL_ARRAY *entries; HTTP_HDR_ENTRY *entry; int i, n; entries = hdr->entry_lnk; n = acl_array_size(entries); entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, 0); acl_vstring_sprintf(strbuf, "%s %s\r\n", entry->name, entry->value); for (i = 1; i < n; i++) { entry = (HTTP_HDR_ENTRY *) acl_array_index(entries, i); if (entry == NULL) break; if (entry->off) continue; acl_vstring_sprintf_append(strbuf, "%s: %s\r\n", entry->name, entry->value); } acl_vstring_strcat(strbuf, "\r\n"); }
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); }
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); }
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 int __log_open(const char *filename, void *ctx) { LOG_WRAP *h_log = (LOG_WRAP *) ctx; int logme = 0; char *facility_name = NULL; E_LOG_PRIORITY_T priority = E_LOG_INFO; E_LOG_ACTION_T action = E_LOG_PER_DAY; int flush = 1; size_t limit_size = 0; E_LOG_SYNC_ACTION_T sync_action = E_LOG_SEM_WITH_MT; char *sem_name = NULL; char *ptr, *pname; ACL_ARGV *env_argv; ACL_VSTRING *log_buf; int i; if (filename == NULL || *filename == 0) return (-1); acl_snprintf(h_log->filename, sizeof(h_log->filename), "%s", filename); /* env: facility:x, priority:x, action:x, flush:x, limit_size:x, sync_action:x, sem_name:x */ ptr = getenv("SERVICE_ENV"); if (ptr == NULL) return (-1); env_argv = acl_argv_split(ptr, ",\t "); if (env_argv == NULL) return (-1); if (env_argv->argc == 0) { acl_argv_free(env_argv); return (-1); } log_buf = acl_vstring_alloc(256); for (i = 0; i < env_argv->argc; i++) { pname = acl_argv_index(env_argv, i); ptr = strchr(pname, ':'); if (ptr == NULL) continue; *ptr++ = 0; if (*ptr == 0) continue; if (i == 0) acl_vstring_sprintf(log_buf, "%s:%s", pname, ptr); else acl_vstring_sprintf_append(log_buf, ", %s:%s", pname, ptr); if (strcasecmp(pname, "logme") == 0) { if (strcasecmp(ptr, "TRUE") == 0) logme = 1; } else if (strcasecmp(pname, "facility") == 0) { facility_name = ptr; } else if (strcasecmp(pname, "priority") == 0) { if (strcasecmp(ptr, "E_LOG_NOLOG") == 0) priority = E_LOG_NOLOG; else if (strcasecmp(ptr, "E_LOG_EMERG") == 0) priority = E_LOG_EMERG; else if (strcasecmp(ptr, "E_LOG_ALERT") == 0) priority = E_LOG_ALERT; else if (strcasecmp(ptr, "E_LOG_CRIT") == 0) priority = E_LOG_CRIT; else if (strcasecmp(ptr, "E_LOG_ERR") == 0) priority = E_LOG_ERR; else if (strcasecmp(ptr, "E_LOG_WARNING") == 0) priority = E_LOG_WARNING; else if (strcasecmp(ptr, "E_LOG_NOTICE") == 0) priority = E_LOG_NOTICE; else if (strcasecmp(ptr, "E_LOG_INFO") == 0) priority = E_LOG_INFO; else if (strcasecmp(ptr, "E_LOG_DEBUG") == 0) priority = E_LOG_DEBUG; } else if (strcasecmp(pname, "action") == 0) { if (strcasecmp(ptr, "E_LOG_PER_HOUR") == 0) action = E_LOG_PER_HOUR; else if (strcasecmp(ptr, "E_LOG_PER_DAY") == 0) action = E_LOG_PER_DAY; else if (strcasecmp(ptr, "E_LOG_PER_WEEK") == 0) action = E_LOG_PER_WEEK; else if (strcasecmp(ptr, "E_LOG_PER_MONTH") == 0) action = E_LOG_PER_MONTH; else if (strcasecmp(ptr, "E_LOG_PER_YEAR") == 0) action = E_LOG_PER_YEAR; else if (strcasecmp(ptr, "E_LOG_LIMIT_SIZE") == 0) action = E_LOG_LIMIT_SIZE; else if (strcasecmp(ptr, "E_LOG_SYSLOG") == 0) action = E_LOG_SYSLOG; } else if (strcasecmp(pname, "flush") == 0) { if (strcasecmp(ptr, "sync_flush") == 0) flush = 1; else if (strcasecmp(ptr, "async_flush") == 0) flush = 0; } else if (strcasecmp(pname, "limit_size") == 0) { limit_size = atoi(ptr); } else if (strcasecmp(pname, "sync_action") == 0) { if (strcasecmp(ptr, "E_LOG_NO_SYNC") == 0) sync_action = E_LOG_NO_SYNC; else if (strcasecmp(ptr, "E_LOG_THREAD_MUTEX") == 0) sync_action = E_LOG_THREAD_MUTEX; else if (strcasecmp(ptr, "E_LOG_FILE_LOCK") == 0) sync_action = E_LOG_FILE_LOCK; else if (strcasecmp(ptr, "E_LOG_SEM_WITH_MT") == 0) sync_action = E_LOG_SEM_WITH_MT; else if (strcasecmp(ptr, "E_LOG_FILE_APPEND_WITH_MT") == 0) sync_action = E_LOG_FILE_APPEND_WITH_MT; } else if (strcasecmp(pname, "sem_name") == 0) { sem_name = ptr; } } #if 0 LC_SysLogCreate(&h_log->h_log, h_log->filename); #endif if (action == E_LOG_LIMIT_SIZE) { if (limit_size == 0) limit_size = 512; /* set default size: 512 MB */ } else limit_size = 0; if (sync_action == E_LOG_SEM_WITH_MT) { if (sem_name == NULL || *sem_name == 0) { sem_name = acl_concatenate("/tmp/", acl_safe_basename(filename), ".sem", NULL); } else sem_name = acl_mystrdup(sem_name); } else if (sem_name) { sem_name = NULL; } h_log->h_log = e_log_new2(h_log->filename, priority, action, flush, limit_size, facility_name, sync_action, sem_name); if (sem_name) acl_myfree(sem_name); if (logme) { /* char cmd[1024], buf[512]; snprintf(buf, sizeof(buf), "filename=%s, priority=%d, action=%d, flush=%d, " "limit_size=%d, facility_name=%s, sync_action=%d, sem_name=%s", h_log->filename, priority, action, flush, limit_size, facility_name, sync_action, sem_name); snprintf(cmd, sizeof(cmd), "echo '%s, buf(%s)' >> /tmp/test1.log", acl_vstring_str(log_buf), buf); system(cmd); */ e_log2(h_log->h_log, "master_env: %s", acl_vstring_str(log_buf)); } e_log2(h_log->h_log, "filename=%s, priority=%d, action=%d, flush=%d, " "limit_size=%d, facility_name=%s, sync_action=%d, sem_name=%s", h_log->filename, priority, action, flush, limit_size, facility_name, sync_action, sem_name); if (log_buf) acl_vstring_free(log_buf); return (0); }
static void thread_write_fn(void *arg) { char *id = (char*) arg; char key[256], buf[256]; ACL_VSTRING *value = acl_vstring_alloc(__value_size); time_t begin, last, now; DICT_POOL_DB *db; int i, n, j; KEY *key_array; key_array = (KEY*) acl_mycalloc(__max, sizeof(KEY)); n = __value_size; acl_vstring_sprintf(value, "%u:", (unsigned) acl_pthread_self()); n -= LEN(value); for (i = 0; i < n; i++) ACL_VSTRING_ADDCH(value, 'v'); ACL_VSTRING_TERMINATE(value); time(&begin); last = begin; for (j = 0, i = __begin; i < __begin + __max; i++) { snprintf(buf, sizeof(buf), "%s:%s:%d", __key_pre, id, i); #if 0 MDString(buf, key, sizeof(key)); #else acl_uint64 k = acl_hash_crc64(buf, strlen(buf)); snprintf(key, sizeof(key), "%llu", k); #endif key_array[j++].key = acl_mystrdup(key); } if (__max <= 100) { printf("before sort\n"); for (i = 0; i < __max; i++) { printf("key[%d]: %s\n", i, key_array[i].key); } } if (__sort) qsort(key_array, __max, sizeof(KEY), cmp_fn); if (__max <= 100) { printf("after sort\n"); for (i = 0; i < __max; i++) { printf("key[%d]: %s\n", i, key_array[i].key); } } for (i = 0; i < __max; i++) { char *ptr; size_t size; db = dict_pool_db(__dict_pool, key_array[i].key, strlen(key_array[i].key)); dict_pool_db_lock(db); ptr = dict_pool_db_get(db, key_array[i].key, strlen(key_array[i].key), &size); if (ptr != NULL) { printf("key: %s exist now, size: %d\n", key_array[i].key, (int) size); acl_myfree(ptr); } dict_pool_db_set(db, key_array[i].key, strlen(key_array[i].key), STR(value), LEN(value)); ptr = dict_pool_db_get(db, key_array[i].key, strlen(key_array[i].key), &size); if (ptr == NULL) { printf("key: %s not add into db\n", key_array[i].key); } else acl_myfree(ptr); dict_pool_db_unlock(db); if (i > 0 && i % __report_base == 0) { time(&now); printf("thread %u add one, i=%d, time=%ld, key=%s\r\n", (unsigned) acl_pthread_self(), i, now - last, key_array[i].key); last = now; } LOCK; __nwrite++; UNLOCK; } for (i = 0; i < __max; i++) { acl_myfree(key_array[i].key); } acl_myfree(key_array); acl_vstring_free(value); acl_myfree(id); printf("thread %u add over, i=%d, time=%ld\r\n", (unsigned) acl_pthread_self(), i, time(NULL) - begin); }