HTTP_HDR_REQ *http_hdr_req_clone(const HTTP_HDR_REQ* hdr_req) { HTTP_HDR_REQ *hh; hh = http_hdr_req_new(); http_hdr_clone(&hdr_req->hdr, &hh->hdr); hh->port = hdr_req->port; ACL_SAFE_STRNCPY(hh->method, hdr_req->method, sizeof(hh->method)); ACL_SAFE_STRNCPY(hh->host, hdr_req->host, sizeof(hh->host)); acl_vstring_strcpy(hh->url_part, acl_vstring_str(hdr_req->url_part)); acl_vstring_strcpy(hh->url_path, acl_vstring_str(hdr_req->url_path)); acl_vstring_strcpy(hh->url_params, acl_vstring_str(hdr_req->url_params)); acl_vstring_strcpy(hh->file_path, acl_vstring_str(hdr_req->file_path)); if (hdr_req->params_table) { hh->params_table = acl_htable_create(__http_hdr_max_request, 0); acl_htable_walk(hdr_req->params_table, clone_table_entry, (void*) hh->params_table); } if (hdr_req->cookies_table) { hh->cookies_table = acl_htable_create(__http_hdr_max_cookies, 0); acl_htable_walk(hdr_req->cookies_table, clone_table_entry, (void*) hh->cookies_table); } return hh; }
bool json_node::set_text(const char* text) { if (text == NULL || *text == 0) return false; if (node_me_->type != ACL_JSON_T_LEAF || node_me_->text == NULL) return false; acl_vstring_strcpy(node_me_->text, text); return true; }
bool json_node::set_tag(const char* name) { if (name == NULL || *name == 0) return false; if (node_me_->ltag == NULL || ACL_VSTRING_LEN(node_me_->ltag) == 0) return false; acl_vstring_strcpy(node_me_->ltag, name); return true; }
static void test61(void) { ACL_VSTRING* buf; buf = acl_vstring_alloc(256); acl_vstring_strcpy(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_strcat(buf, "hello world!"); acl_vstring_free(buf); }
/* 通知主线程,启动一个服务 */ static int proctl_monitor_cmd_start(ACL_VSTREAM *client, const char *filepath, const char *args) { const char *myname = "proctl_monitor_cmd_start"; ACL_VSTRING *cmdline; PROCTL_SERVICE *service; PROCTL_MSG *msg; if (filepath[0] == 0) { acl_vstream_fprintf(client, "-ERR|filepath null\r\n"); acl_msg_error("%s(%d): no filepath", myname, __LINE__); return (-1); } if (proctl_service_exist(filepath)) { acl_msg_error("%s(%d): child(%s) maybe be running!", myname, __LINE__, filepath); acl_vstream_fprintf(client, "-ERR|child(%s) maybe be running!\r\n", filepath); return (-1); } cmdline = acl_vstring_alloc(256); acl_vstring_strcpy(cmdline, "\""); acl_vstring_strcat(cmdline, filepath); acl_vstring_strcat(cmdline, "\""); if (args && *args) { acl_vstring_strcat(cmdline, " "); acl_vstring_strcat(cmdline, args); } service = proctl_service_alloc(filepath, cmdline); msg = proctl_msg_new(PROCTL_MSG_START); msg->service = service; proctl_msg_push(msg); return (0); }
HTTP_HDR_REQ *http_hdr_req_create(const char *url, const char *method, const char *version) { const char *myname = "http_hdr_req_create"; HTTP_HDR_REQ *hdr_req; ACL_VSTRING *req_line = acl_vstring_alloc(256); HTTP_HDR_ENTRY *entry; const char *ptr; static char *__user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0" "; zh-CN; rv:1.9.0.3) Gecko/2008092417 ACL/3.0.6"; if (url == NULL || *url == 0) { acl_msg_error("%s(%d): url invalid", myname, __LINE__); return NULL; } if (method == NULL || *method == 0) { acl_msg_error("%s(%d): method invalid", myname, __LINE__); return NULL; } if (version == NULL || *version == 0) { acl_msg_error("%s(%d): version invalid", myname, __LINE__); return NULL; } acl_vstring_strcpy(req_line, method); acl_vstring_strcat(req_line, " "); if (strncasecmp(url, "http://", sizeof("http://") - 1) == 0) url += sizeof("http://") - 1; else if (strncasecmp(url, "https://", sizeof("https://") - 1) == 0) url += sizeof("https://") -1; ptr = strchr(url, '/'); if (ptr) acl_vstring_strcat(req_line, ptr); else { ACL_VSTRING_ADDCH(req_line, '/'); ACL_VSTRING_TERMINATE(req_line); } acl_vstring_strcat(req_line, " "); acl_vstring_strcat(req_line, version); entry = http_hdr_entry_new(acl_vstring_str(req_line)); acl_vstring_free(req_line); if (entry == NULL) { acl_msg_error("%s(%d): http_hdr_entry_new return null for (%s)", myname, __LINE__, acl_vstring_str(req_line)); return NULL; } hdr_req = http_hdr_req_new(); http_hdr_append_entry(&hdr_req->hdr, entry); hdr_req->flag |= (HTTP_HDR_REQ_FLAG_PARSE_PARAMS | HTTP_HDR_REQ_FLAG_PARSE_COOKIE); if (http_hdr_req_line_parse(hdr_req) < 0) { http_hdr_req_free(hdr_req); return NULL; } hdr_req->host[0] = 0; __get_host_from_url(hdr_req->host, sizeof(hdr_req->host), url); if (hdr_req->host[0] != 0) http_hdr_put_str(&hdr_req->hdr, "Host", hdr_req->host); http_hdr_put_str(&hdr_req->hdr, "Connection", "Close"); http_hdr_put_str(&hdr_req->hdr, "User-Agent", __user_agent); return hdr_req; }
char *acl_sane_basename(ACL_VSTRING *bp, const char *path) { const char *myname = "acl_sane_basename"; const char *first; const char *last; /* * Your buffer or mine? */ if (bp == 0) { acl_msg_error("%s(%d): bp null", myname, __LINE__); return (NULL); } /* * Special case: return "." for null or zero-length input. */ if (path == 0 || *path == 0) return (STR(acl_vstring_strcpy(bp, "."))); /* * Remove trailing '/' characters from input. Return "/" if input is all * '/' characters. */ last = path + strlen(path) - 1; #ifdef ACL_UNIX while (*last == '/') { #elif defined(ACL_MS_WINDOWS) while (*last == '/' || *last == '\\') { #endif if (last == path) return (STR(acl_vstring_strcpy(bp, "/"))); last--; } /* * The pathname does not end in '/'. Skip to last '/' character if any. */ first = last - 1; #ifdef ACL_UNIX while (first >= path && *first != '/') #elif defined(ACL_MS_WINDOWS) while (first >= path && *first != '/' && *first != '\\') #endif first--; return (STR(acl_vstring_strncpy(bp, first + 1, last - first))); } /* acl_sane_dirname - keep directory prefix */ char *acl_sane_dirname(ACL_VSTRING *bp, const char *path) { const char *myname = "acl_sane_dirname"; const char *last; /* * Your buffer or mine? */ if (bp == 0) { acl_msg_error("%s(%d): bp null", myname, __LINE__); return (NULL); } /* * Special case: return "." for null or zero-length input. */ if (path == 0 || *path == 0) return (STR(acl_vstring_strcpy(bp, "."))); /* * Remove trailing '/' characters from input. Return "/" if input is all * '/' characters. */ last = path + strlen(path) - 1; #ifdef ACL_UNIX while (*last == '/') { #elif defined(ACL_MS_WINDOWS) while (*last == '/' || *last == '\\') { #endif if (last == path) return (STR(acl_vstring_strcpy(bp, "/"))); last--; } /* * This pathname does not end in '/'. Skip to last '/' character if any. */ #ifdef ACL_UNIX while (last >= path && *last != '/') #elif defined(ACL_MS_WINDOWS) while (last >= path && *last != '/' && *last != '\\') #endif last--; if (last < path) /* no '/' */ return (STR(acl_vstring_strcpy(bp, "."))); /* * Strip trailing '/' characters from dirname (not strictly needed). */ #ifdef ACL_UNIX while (last > path && *last == '/') #elif defined(ACL_MS_WINDOWS) while (last > path && (*last == '/' || *last == '\\')) #endif last--; return (STR(acl_vstring_strncpy(bp, path, last - path + 1))); } #ifdef TEST #include "stdlib/acl_vstring_vstream.h" int main(int argc acl_unused, char **argv acl_unused) { ACL_VSTRING *buf = acl_vstring_alloc(10); char *dir; char *base; while (acl_vstring_gets_nonl(buf, ACL_VSTREAM_IN) > 0) { dir = acl_sane_dirname((ACL_VSTRING *) 0, STR(buf)); base = acl_sane_basename((ACL_VSTRING *) 0, STR(buf)); acl_vstream_printf("input=\"%s\" dir=\"%s\" base=\"%s\"\n", STR(buf), dir, base); } acl_vstream_fflush(ACL_VSTREAM_OUT); acl_vstring_free(buf); return (0); }
CChatDemo::CChatDemo(const char *s) { m_buf = acl_vstring_alloc(256); acl_vstring_strcpy(m_buf, s); }
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 请求头对象 */ }
static int mac_expand_callback(int type, ACL_VSTRING *buf, char *ptr) { MAC_EXP *mc = (MAC_EXP *) ptr; int lookup_mode; const char *text; char *cp, *pp = NULL; int ch; ssize_t len; size_t size; /* * Sanity check. */ if (mc->level++ > 100) { acl_msg_warn("unreasonable macro call nesting: \"%s\"", acl_vstring_str(buf)); mc->status |= MAC_PARSE_ERROR; } if (mc->status & MAC_PARSE_ERROR) return (mc->status); /* * $Name etc. reference. * * In order to support expansion of lookup results, we must save the lookup * result. We use the input buffer since it will not be needed anymore. */ if (type == MAC_PARSE_EXPR) { /* * Look for the ? or : delimiter. In case of a syntax error, return * without doing damage, and issue a warning instead. */ for (cp = acl_vstring_str(buf); /* void */ ; cp++) { if ((ch = *cp) == 0) { lookup_mode = MAC_EXP_MODE_USE; break; } if (ch == '?' || ch == ':') { *cp++ = 0; lookup_mode = MAC_EXP_MODE_TEST; break; } if (!ACL_ISALNUM(ch) && ch != '_') { acl_msg_warn("macro name syntax error: \"%s\"", acl_vstring_str(buf)); mc->status |= MAC_PARSE_ERROR; return (mc->status); } } /* * Look up the named parameter. */ text = mc->lookup(acl_vstring_str(buf), lookup_mode, mc->context, &pp, &size); /* * Perform the requested substitution. */ switch (ch) { case '?': if (text != 0 && *text != 0) mac_parse(cp, mac_expand_callback, (char *) mc); break; case ':': if (text == 0 || *text == 0) mac_parse(cp, mac_expand_callback, (char *) mc); break; default: if (text == 0) { mc->status |= MAC_PARSE_UNDEF; } else if (*text == 0) { /* void */ ; } else if (mc->flags & MAC_EXP_FLAG_RECURSE) { acl_vstring_strcpy(buf, text); mac_parse(acl_vstring_str(buf), mac_expand_callback, (char *) mc); } else { len = (int) ACL_VSTRING_LEN(mc->result); acl_vstring_strcat(mc->result, text); if (mc->filter) { cp = acl_vstring_str(mc->result) + len; while (*(cp += strspn(cp, mc->filter))) *cp++ = '_'; } } break; } } /* * Literal text. */ else { acl_vstring_strcat(mc->result, acl_vstring_str(buf)); } mc->level--; if (pp) free(pp); return (mc->status); }