Decrease buff size for http module from 8k to 4k; format codes style.

This commit is contained in:
shuxin   zheng 2023-08-23 14:13:49 +08:00
parent 1705f9b56e
commit c79eaac19c
6 changed files with 231 additions and 208 deletions

View File

@ -16,8 +16,9 @@
#include "acl_cpp/http/http_client.hpp"
#endif
namespace acl
{
#define HTTP_BUF_SIZE 4096
namespace acl {
http_client::http_client(void)
: stream_(NULL)
@ -1009,7 +1010,7 @@ int http_client::read_response_body(string& out, bool clean, int* real_size)
}
int saved_count = (int) out.length();
char buf[8192];
char buf[HTTP_BUF_SIZE];
READ_AGAIN: // 对于有 GZIP 头数据,可能需要重复读
@ -1116,7 +1117,7 @@ int http_client::read_request_body(string& out, bool clean, int* real_size)
out.clear();
}
char buf[8192];
char buf[HTTP_BUF_SIZE];
int ret = (int) http_req_body_get_sync(req_, vstream, buf, sizeof(buf));

View File

@ -19,8 +19,7 @@
#include "acl_cpp/http/http_request.hpp"
#endif
namespace acl
{
namespace acl {
#define RESET_RANGE() do \
{ \

View File

@ -3,7 +3,7 @@
#include "http/lib_http_struct.h"
#define HTTP_BSIZE 8192
#define HTTP_BUF_SIZE 4096
extern http_off_t var_http_buf_size;
extern int var_http_tls_cache;

View File

@ -32,7 +32,6 @@ typedef struct HTTP_CHAT_CTX {
/*----------------------------------------------------------------------------*/
static HTTP_CHAT_CTX *new_ctx(void)
{
const char *myname = "new_ctx";
HTTP_CHAT_CTX *ctx;
ctx = (HTTP_CHAT_CTX*) acl_mycalloc(1, sizeof(HTTP_CHAT_CTX));
@ -40,7 +39,7 @@ static HTTP_CHAT_CTX *new_ctx(void)
char ebuf[256];
acl_msg_fatal("%s, %s(%d): calloc error(%s)",
__FILE__, myname, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
__FILE__, __FUNCTION__, __LINE__, acl_last_strerror(ebuf, sizeof(ebuf)));
}
return ctx;
@ -89,11 +88,7 @@ static int hdr_ready(HTTP_HDR *hdr, const char *line, int dlen)
static int hdr_get(HTTP_HDR *hdr, ACL_VSTREAM *stream, int timeout)
{
/* 当使用协程共享栈时如果将此BUFF设的过大会增大共享栈的空间大小造成内存浪费 */
#ifdef HTTP_READ_BUF8192
char buf[8192];
#else
char buf[4912];
#endif
char buf[HTTP_BUF_SIZE];
int ret;
stream->rw_timeout = timeout;
@ -168,13 +163,12 @@ static http_off_t chunked_data_get(HTTP_CHAT_CTX *ctx, void *buf, int size)
static int chunked_hdr_get(HTTP_CHAT_CTX *ctx)
{
const char *myname = "chunked_hdr_get";
#if defined(ACL_WINDOWS) && _MSC_VER >= 1500
char ext[64];
#else
char *ext = NULL;
#endif
char buf[HTTP_BSIZE];
char buf[HTTP_BUF_SIZE];
int ret, n, chunk_len;
n = acl_vstream_gets(ctx->stream, buf, sizeof(buf));
@ -197,7 +191,7 @@ static int chunked_hdr_get(HTTP_CHAT_CTX *ctx)
if (ret < 0 || chunk_len < 0) {
acl_msg_error("%s(%d): chunked hdr(%s) invalid, dlen(%d), "
"'\\n': %d, %d", myname, __LINE__, buf, n, buf[0], '\n');
"'\\n': %d, %d", __FUNCTION__, __LINE__, buf, n, buf[0], '\n');
return -1;
}
@ -207,13 +201,12 @@ static int chunked_hdr_get(HTTP_CHAT_CTX *ctx)
static int chunked_sep_gets(HTTP_CHAT_CTX *ctx)
{
const char *myname = "chunked_sep_gets2";
char buf[HTTP_BSIZE];
char buf[HTTP_BUF_SIZE];
int n;
n = acl_vstream_gets(ctx->stream, buf, sizeof(buf));
if (n == ACL_VSTREAM_EOF) {
acl_msg_error("%s(%d): gets sep line error", myname, __LINE__);
acl_msg_error("%s(%d): gets sep line error", __FUNCTION__, __LINE__);
return -1;
}
@ -223,14 +216,13 @@ static int chunked_sep_gets(HTTP_CHAT_CTX *ctx)
static int chunked_trailer_get(HTTP_CHAT_CTX *ctx)
{
const char *myname = "chunked_tailer_get2";
char buf[HTTP_BSIZE];
char buf[HTTP_BUF_SIZE];
int n;
while (1) {
n = acl_vstream_gets(ctx->stream, buf, sizeof(buf));
if (n == ACL_VSTREAM_EOF) {
acl_msg_error("%s(%d): get line error", myname, __LINE__);
acl_msg_error("%s(%d): get line error", __FUNCTION__, __LINE__);
return -1;
}
ctx->body_len += n;
@ -244,7 +236,6 @@ static int chunked_trailer_get(HTTP_CHAT_CTX *ctx)
static http_off_t body_get(HTTP_CHAT_CTX *ctx, void *buf, int size)
{
const char *myname = "body_get";
http_off_t ret;
/* Transfer-Encoding: chunked 的优先级要高于 Conteng-Length */
@ -284,7 +275,7 @@ static http_off_t body_get(HTTP_CHAT_CTX *ctx, void *buf, int size)
return ret;
} else {
acl_msg_error("%s(%d): unknown oper status(%d)",
myname, __LINE__, ctx->chunk.chunk_oper);
__FUNCTION__, __LINE__, ctx->chunk.chunk_oper);
return -1;
}
}
@ -362,7 +353,6 @@ http_off_t http_res_body_get_sync(HTTP_RES *respond, ACL_VSTREAM *stream,
void http_chat_sync_reqctl(HTTP_REQ *request, int name, ...)
{
const char *myname = "http_chat_sync_reqctl";
va_list ap;
int n;
@ -379,7 +369,7 @@ void http_chat_sync_reqctl(HTTP_REQ *request, int name, ...)
break;
default:
acl_msg_panic("%s, %s(%d): bad name %d",
myname, __FILE__, __LINE__, name);
__FUNCTION__, __FILE__, __LINE__, name);
break;
}
}
@ -388,7 +378,6 @@ void http_chat_sync_reqctl(HTTP_REQ *request, int name, ...)
void http_chat_sync_resctl(HTTP_RES *respond, int name, ...)
{
const char *myname = "http_chat_sync_resctl";
va_list ap;
int n;
@ -405,7 +394,7 @@ void http_chat_sync_resctl(HTTP_RES *respond, int name, ...)
break;
default:
acl_msg_panic("%s, %s(%d): bad name %d",
myname, __FILE__, __LINE__, name);
__FUNCTION__, __FILE__, __LINE__, name);
break;
}
}

View File

@ -24,12 +24,11 @@ static void __hdr_init(HTTP_HDR *hh)
HTTP_HDR *http_hdr_new(size_t size)
{
const char *myname = "http_hdr_new";
HTTP_HDR *hh;
if (size != sizeof(HTTP_HDR_REQ) && size != sizeof(HTTP_HDR_RES))
acl_msg_fatal("%s, %s(%d): size(%d) invalid",
__FILE__, myname, __LINE__, (int) size);
__FILE__, __FUNCTION__, __LINE__, (int) size);
hh = (HTTP_HDR*) acl_mycalloc(1, (int) size);
hh->entry_lnk = acl_array_create(__http_hdr_def_entry);
@ -153,7 +152,6 @@ HTTP_HDR_ENTRY *http_hdr_entry_new(const char *data)
HTTP_HDR_ENTRY *http_hdr_entry_head(char *data)
{
/* data format: GET / HTTP/1.1 or 200 OK */
const char *myname = "http_hdr_entry_head";
char *ptr, *pname, *psep = NULL;
HTTP_HDR_ENTRY *entry;
@ -168,7 +166,7 @@ HTTP_HDR_ENTRY *http_hdr_entry_head(char *data)
}
if (*ptr == 0) {
acl_msg_error("%s, %s(%d): invalid data=%s",
__FILE__, myname, __LINE__, data);
__FILE__, __FUNCTION__, __LINE__, data);
return (NULL);
}
while (*ptr == ' ' || *ptr == '\t')
@ -177,7 +175,7 @@ HTTP_HDR_ENTRY *http_hdr_entry_head(char *data)
if (psep)
*psep = ' ';
acl_msg_error("%s, %s(%d): invalid data=%s",
__FILE__, myname, __LINE__, data);
__FILE__, __FUNCTION__, __LINE__, data);
return (NULL);
}
@ -188,7 +186,6 @@ HTTP_HDR_ENTRY *http_hdr_entry_head(char *data)
HTTP_HDR_ENTRY *http_hdr_entry_new2(char *data)
{
/* data format: Content-Length: 245 */
const char *myname = "http_hdr_entry_new2";
char *ptr, *pname, *psep = NULL;
HTTP_HDR_ENTRY *entry;
@ -203,7 +200,7 @@ HTTP_HDR_ENTRY *http_hdr_entry_new2(char *data)
}
if (*ptr == 0) {
acl_msg_error("%s, %s(%d): invalid data=%s",
__FILE__, myname, __LINE__, data);
__FILE__, __FUNCTION__, __LINE__, data);
return (NULL);
}
while (*ptr == ':' || *ptr == ' ' || *ptr == '\t')
@ -212,7 +209,7 @@ HTTP_HDR_ENTRY *http_hdr_entry_new2(char *data)
if (psep)
*psep = ':';
acl_msg_error("%s, %s(%d): invalid data=%s",
__FILE__, myname, __LINE__, data);
__FILE__, __FUNCTION__, __LINE__, data);
return (NULL);
}
@ -224,20 +221,17 @@ HTTP_HDR_ENTRY *http_hdr_entry_new2(char *data)
void http_hdr_append_entry(HTTP_HDR *hh, HTTP_HDR_ENTRY *entry)
{
const char *myname = "http_hdr_append_entry";
if (acl_array_append(hh->entry_lnk, entry) < 0)
if (acl_array_append(hh->entry_lnk, entry) < 0) {
acl_msg_fatal("%s, %s(%d): acl_array_append error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
}
int http_hdr_parse_version(HTTP_HDR *hh, const char *data)
{
const char *myname = "http_hdr_parse_version";
if (hh == NULL)
acl_msg_fatal("%s, %s(%d): hh null",
__FILE__, myname, __LINE__);
if (hh == NULL) {
acl_msg_fatal("%s, %s(%d): hh null", __FILE__, __FUNCTION__, __LINE__);
}
if (data == NULL || *data == 0)
return -1;
@ -261,13 +255,13 @@ int http_hdr_parse_version(HTTP_HDR *hh, const char *data)
static HTTP_HDR_ENTRY *__get_hdr_entry(const HTTP_HDR *hh, const char *name)
{
const char *myname = "__get_hdr_entry";
HTTP_HDR_ENTRY *entry;
ACL_ITER iter;
if (hh->entry_lnk == NULL)
if (hh->entry_lnk == NULL) {
acl_msg_fatal("%s, %s(%d): entry_lnk null",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
}
acl_foreach(iter, hh->entry_lnk) {
entry = (HTTP_HDR_ENTRY *) iter.data;
@ -296,12 +290,11 @@ char *http_hdr_entry_value(const HTTP_HDR *hh, const char *name)
int http_hdr_entry_replace(HTTP_HDR *hh, const char *name,
const char *value, int force)
{
const char *myname = "http_hdr_entry_replace";
HTTP_HDR_ENTRY *entry;
if (hh == NULL || name == NULL || value == NULL) {
acl_msg_error("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return -1;
}
@ -378,12 +371,11 @@ int http_hdr_entry_replace2(HTTP_HDR *hh, const char *name,
void http_hdr_entry_off(HTTP_HDR *hh, const char *name)
{
const char *myname = "http_hdr_entry_off";
HTTP_HDR_ENTRY *entry;
if (hh == NULL || name == NULL)
acl_msg_fatal("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
entry = __get_hdr_entry(hh, name);
if (entry == NULL)
@ -396,9 +388,6 @@ void http_hdr_entry_off(HTTP_HDR *hh, const char *name)
int http_hdr_parse(HTTP_HDR *hh)
{
/*
const char *myname = "http_hdr_parse";
*/
int keep_alive = -1;
HTTP_HDR_ENTRY *entry;
ACL_ITER iter;
@ -460,7 +449,7 @@ int http_hdr_parse(HTTP_HDR *hh)
#endif
if (hh->content_length < 0) {
acl_msg_error("%s: content_length(%s) invalid",
myname, entry->value);
__FUNCTION__, entry->value);
return (-1);
}
care_cnt++;
@ -514,7 +503,7 @@ int http_hdr_parse(HTTP_HDR *hh)
if (hh->content_length < 0) {
/*
acl_msg_error("%s: content_length(%s) invalid",
myname, entry->value);
__FUNCTION__, entry->value);
return (-1);
*/
hh->content_length = -1;
@ -560,22 +549,21 @@ int http_hdr_parse(HTTP_HDR *hh)
void http_hdr_print(const HTTP_HDR *hh, const char *msg)
{
const char *myname = "http_hdr_print";
HTTP_HDR_ENTRY *entry;
int n, i;
if (hh == NULL)
acl_msg_fatal("%s, %s(%d): invalid input",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
n = acl_array_size(hh->entry_lnk);
if (n <= 0) {
acl_msg_info("%s, %s(%d): array empty",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return;
}
printf("------------- in %s - msg=(%s)----------------\r\n",
myname, msg ? msg : "");
__FUNCTION__, msg ? msg : "");
entry = (HTTP_HDR_ENTRY *) acl_array_index(hh->entry_lnk, 0);
if (entry)
printf("%s %s\r\n", entry->name, entry->value);
@ -591,25 +579,24 @@ void http_hdr_print(const HTTP_HDR *hh, const char *msg)
void http_hdr_fprint(ACL_VSTREAM *fp, const HTTP_HDR *hh, const char *msg)
{
const char *myname = "http_hdr_fprint";
HTTP_HDR_ENTRY *entry;
int n, i;
if (fp == NULL || hh == NULL) {
acl_msg_error("%s(%d): input invalid", myname, __LINE__);
acl_msg_error("%s(%d): input invalid", __FUNCTION__, __LINE__);
return;
}
n = acl_array_size(hh->entry_lnk);
if (n <= 0) {
acl_msg_info("%s, %s(%d): array empty",
myname, __FILE__, __LINE__);
__FUNCTION__, __FILE__, __LINE__);
return;
}
if (msg && *msg)
acl_vstream_fprintf(fp, "---------- in %s - (%s) -------\r\n",
myname, msg);
__FUNCTION__, msg);
entry = (HTTP_HDR_ENTRY *) acl_array_index(hh->entry_lnk, 0);
if (entry)
acl_vstream_fprintf(fp, "%s %s\r\n", entry->name, entry->value);
@ -628,25 +615,24 @@ void http_hdr_fprint(ACL_VSTREAM *fp, const HTTP_HDR *hh, const char *msg)
void http_hdr_sprint(ACL_VSTRING *bf, const HTTP_HDR *hh, const char *msg)
{
const char *myname = "http_hdr_fprint";
HTTP_HDR_ENTRY *entry;
int n, i;
if (bf == NULL || hh == NULL) {
acl_msg_error("%s(%d): input invalid", myname, __LINE__);
acl_msg_error("%s(%d): input invalid", __FUNCTION__, __LINE__);
return;
}
n = acl_array_size(hh->entry_lnk);
if (n <= 0) {
acl_msg_info("%s, %s(%d): array empty",
myname, __FILE__, __LINE__);
__FUNCTION__, __FILE__, __LINE__);
return;
}
if (msg && *msg)
acl_vstring_sprintf(bf, "----------- in %s - (%s)-------\r\n",
myname, msg);
__FUNCTION__, msg);
entry = (HTTP_HDR_ENTRY *) acl_array_index(hh->entry_lnk, 0);
if (entry)

View File

@ -14,26 +14,28 @@ static void __get_host_from_url(char *buf, size_t size, const char *url);
static void __hdr_init(HTTP_HDR_REQ *hh)
{
const char *myname = "__hdr_init";
hh->url_part = acl_vstring_alloc(128);
if (hh->url_part == NULL)
if (hh->url_part == NULL) {
acl_msg_fatal("%s, %s(%d): alloc error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
hh->url_path = acl_vstring_alloc(64);
if (hh->url_path == NULL)
if (hh->url_path == NULL) {
acl_msg_fatal("%s, %s(%d): alloc error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
hh->url_params = acl_vstring_alloc(64);
if (hh->url_params == NULL)
if (hh->url_params == NULL) {
acl_msg_fatal("%s, %s(%d): alloc error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
hh->file_path = acl_vstring_alloc(256);
if (hh->file_path == NULL)
if (hh->file_path == NULL) {
acl_msg_fatal("%s, %s(%d): alloc error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
}
static void __request_args_free_fn(void *arg)
@ -48,14 +50,18 @@ static void __cookies_args_free_fn(void *arg)
static void __hdr_free_member(HTTP_HDR_REQ *hh)
{
if (hh->url_part)
if (hh->url_part) {
acl_vstring_free(hh->url_part);
if (hh->url_path)
}
if (hh->url_path) {
acl_vstring_free(hh->url_path);
if (hh->url_params)
}
if (hh->url_params) {
acl_vstring_free(hh->url_params);
if (hh->file_path)
}
if (hh->file_path) {
acl_vstring_free(hh->file_path);
}
if (hh->params_table) {
acl_htable_free(hh->params_table, __request_args_free_fn);
hh->params_table = NULL;
@ -93,17 +99,20 @@ static void __hdr_reset(HTTP_HDR_REQ *hh, int clear_cookies)
ACL_VSTRING_TERMINATE(hh->file_path);
}
if (hh->params_table)
if (hh->params_table) {
acl_htable_reset(hh->params_table, __request_args_free_fn);
}
if (clear_cookies && hh->cookies_table)
if (clear_cookies && hh->cookies_table) {
acl_htable_reset(hh->cookies_table, __cookies_args_free_fn);
}
}
static void thread_cache_free(ACL_ARRAY *pool)
{
if ((unsigned long) acl_pthread_self() != acl_main_thread_self())
if ((unsigned long) acl_pthread_self() != acl_main_thread_self()) {
acl_array_free(pool, (void (*)(void*)) http_hdr_req_free);
}
}
static acl_pthread_key_t cache_key = (acl_pthread_key_t) -1;
@ -185,7 +194,6 @@ HTTP_HDR_REQ *http_hdr_req_new(void)
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;
@ -195,17 +203,17 @@ HTTP_HDR_REQ *http_hdr_req_create(const char *url,
"; zh-CN; rv:1.9.0.3) Gecko/2008092417 ACL/3.5.1";
if (url == NULL || *url == 0) {
acl_msg_error("%s(%d): url invalid", myname, __LINE__);
acl_msg_error("%s(%d): url invalid", __FUNCTION__, __LINE__);
acl_vstring_free(req_line);
return NULL;
}
if (method == NULL || *method == 0) {
acl_msg_error("%s(%d): method invalid", myname, __LINE__);
acl_msg_error("%s(%d): method invalid", __FUNCTION__, __LINE__);
acl_vstring_free(req_line);
return NULL;
}
if (version == NULL || *version == 0) {
acl_msg_error("%s(%d): version invalid", myname, __LINE__);
acl_msg_error("%s(%d): version invalid", __FUNCTION__, __LINE__);
acl_vstring_free(req_line);
return NULL;
}
@ -224,9 +232,9 @@ HTTP_HDR_REQ *http_hdr_req_create(const char *url,
}
ptr = strchr(url, '/');
if (ptr)
if (ptr) {
acl_vstring_strcat(req_line, ptr);
else {
} else {
ACL_VSTRING_ADDCH(req_line, '/');
ACL_VSTRING_TERMINATE(req_line);
}
@ -239,7 +247,7 @@ HTTP_HDR_REQ *http_hdr_req_create(const char *url,
if (entry == NULL) {
acl_msg_error("%s(%d): http_hdr_entry_new return null for (%s)",
myname, __LINE__, acl_vstring_str(req_line));
__FUNCTION__, __LINE__, acl_vstring_str(req_line));
return NULL;
}
@ -255,8 +263,9 @@ HTTP_HDR_REQ *http_hdr_req_create(const char *url,
hdr_req->host[0] = 0;
__get_host_from_url(hdr_req->host, sizeof(hdr_req->host), url);
if (hdr_req->host[0] != 0)
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);
@ -265,15 +274,15 @@ HTTP_HDR_REQ *http_hdr_req_create(const char *url,
static void clone_table_entry(ACL_HTABLE_INFO *info, void *arg)
{
const char *myname = "clone_table_entry";
ACL_HTABLE *table = (ACL_HTABLE*) arg;
char *value;
value = acl_mystrdup(info->value);
if (acl_htable_enter(table, info->key.key, value) == NULL)
if (acl_htable_enter(table, info->key.key, value) == NULL) {
acl_msg_fatal("%s, %s(%d): acl_htable_enter error=%s",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
}
HTTP_HDR_REQ *http_hdr_req_clone(const HTTP_HDR_REQ* hdr_req)
@ -311,8 +320,9 @@ void http_hdr_req_free(HTTP_HDR_REQ *hh)
{
ACL_ARRAY *pool;
if (hh == NULL)
if (hh == NULL) {
return;
}
if (var_http_tls_cache <= 0 || cache_pool == NULL) {
__hdr_free_member(hh);
@ -339,8 +349,9 @@ void http_hdr_req_free(HTTP_HDR_REQ *hh)
void http_hdr_req_reset(HTTP_HDR_REQ *hh)
{
if (hh == NULL)
if (hh == NULL) {
return;
}
http_hdr_reset((HTTP_HDR *) hh);
__hdr_free_member(hh);
@ -354,7 +365,6 @@ void http_hdr_req_reset(HTTP_HDR_REQ *hh)
static void __add_cookie_item(ACL_HTABLE *table, const char *data)
{
/* data format: name=value */
const char *myname = "__add_cookie_item";
ACL_ARGV *argv = NULL;
ACL_VSTRING *str = NULL;
const char *name;
@ -364,18 +374,21 @@ static void __add_cookie_item(ACL_HTABLE *table, const char *data)
#undef RETURN
#define RETURN do { \
if (argv) \
acl_argv_free(argv); \
if (argv) { \
acl_argv_free(argv); \
} \
return; \
} while(0);
#undef TRUNC_BLANK
#define TRUNC_BLANK(_x_) do { \
char *_ptr_; \
while(*_x_ == ' ' || *_x_ == '\t') \
while(*_x_ == ' ' || *_x_ == '\t') { \
_x_++; \
if (*_x_ == 0) \
} \
if (*_x_ == 0) { \
RETURN; \
} \
_ptr_ = _x_; \
while (*_ptr_) { \
if (*_ptr_ == ' ' || *_ptr_ == '\t') { \
@ -389,8 +402,9 @@ static void __add_cookie_item(ACL_HTABLE *table, const char *data)
#undef TRUNC_BLANK_NORETURN
#define TRUNC_BLANK_NORETURN(_x_) do { \
char *_ptr_; \
while(*_x_ == ' ' || *_x_ == '\t') \
while(*_x_ == ' ' || *_x_ == '\t') { \
_x_++; \
} \
_ptr_ = _x_; \
while (*_ptr_) { \
if (*_ptr_ == ' ' || *_ptr_ == '\t') { \
@ -402,8 +416,9 @@ static void __add_cookie_item(ACL_HTABLE *table, const char *data)
} while (0);
argv = acl_argv_split(data, "=");
if (argv->argc < 2) /* data: "name" or "name="*/
if (argv->argc < 2) { /* data: "name" or "name="*/
RETURN;
}
ptr = acl_argv_index(argv, 0);
TRUNC_BLANK(ptr);
@ -422,15 +437,18 @@ static void __add_cookie_item(ACL_HTABLE *table, const char *data)
for (i = 1; i < argv->argc; i++) {
ptr = acl_argv_index(argv, i);
if (ptr == NULL)
if (ptr == NULL) {
break;
}
TRUNC_BLANK_NORETURN(ptr);
if (*ptr == 0)
if (*ptr == 0) {
continue;
if (i == 1)
}
if (i == 1) {
acl_vstring_sprintf_append(str, "%s", ptr);
else
} else {
acl_vstring_sprintf_append(str, "=%s", ptr);
}
}
/* 将真实的存储数据的区域内存引出, 同时将外包结构内存释放,
@ -438,9 +456,10 @@ static void __add_cookie_item(ACL_HTABLE *table, const char *data)
*/
value = acl_vstring_export(str);
if (acl_htable_enter(table, name, value) == NULL)
if (acl_htable_enter(table, name, value) == NULL) {
acl_msg_fatal("%s, %s(%d): acl_htable_enter error=%s",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
RETURN;
}
@ -448,21 +467,24 @@ static void __add_cookie_item(ACL_HTABLE *table, const char *data)
int http_hdr_req_cookies_parse(HTTP_HDR_REQ *hh)
{
/* data format: "name1=value1; name2=value2; name3=value3" */
const char *myname = "http_hdr_req_cookies_parse";
const HTTP_HDR_ENTRY *entry;
ACL_ARGV *argv;
const char *ptr;
ACL_ITER iter;
if (hh == NULL)
if (hh == NULL) {
acl_msg_fatal("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
if ((hh->flag & HTTP_HDR_REQ_FLAG_PARSE_COOKIE) == 0)
__FILE__, __FUNCTION__, __LINE__);
}
if ((hh->flag & HTTP_HDR_REQ_FLAG_PARSE_COOKIE) == 0) {
return 0;
}
entry = http_hdr_entry((HTTP_HDR *) hh, "Cookie");
if (entry == NULL)
if (entry == NULL) {
return 0;
}
/* bugfix: 在创建哈希表时此处不应设置 ACL_HTABLE_FLAG_KEY_REUSE 标志位,
* __add_cookie_item acl_htable_enter
@ -470,24 +492,27 @@ int http_hdr_req_cookies_parse(HTTP_HDR_REQ *hh)
* acl_argv_free(argv) name
* ---zsx, 2014.5.13
*/
if (hh->cookies_table == NULL)
if (hh->cookies_table == NULL) {
#if 0
hh->cookies_table = acl_htable_create(__http_hdr_max_cookies,
ACL_HTABLE_FLAG_KEY_REUSE);
#else
hh->cookies_table = acl_htable_create(__http_hdr_max_cookies, 0);
#endif
}
if (hh->cookies_table == NULL)
if (hh->cookies_table == NULL) {
acl_msg_fatal("%s, %s(%d): htable create error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
/* 分隔数据段 */
argv = acl_argv_split(entry->value, ";");
acl_foreach(iter, argv) {
ptr = (const char*) iter.data;
if (ptr && *ptr)
if (ptr && *ptr) {
__add_cookie_item(hh->cookies_table, ptr);
}
}
acl_argv_free(argv);
return 0;
@ -495,17 +520,15 @@ int http_hdr_req_cookies_parse(HTTP_HDR_REQ *hh)
const char *http_hdr_req_cookie_get(HTTP_HDR_REQ *hh, const char *name)
{
const char *myname = "http_hdr_req_cookie_get";
if (hh == NULL || name == NULL) {
acl_msg_error("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return NULL;
}
if (hh->cookies_table == NULL) {
acl_msg_warn("%s, %s(%d): cookies_table null",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return NULL;
}
@ -518,7 +541,6 @@ const char *http_hdr_req_cookie_get(HTTP_HDR_REQ *hh, const char *name)
static void __add_request_item(ACL_HTABLE *table, const char *data)
{
/* data format: name=value */
const char *myname = "__add_request_item";
ACL_ARGV *argv;
const char *name;
char *value;
@ -540,13 +562,15 @@ static void __add_request_item(ACL_HTABLE *table, const char *data)
}
value = acl_mystrdup(acl_argv_index(argv, 1));
if (value == NULL)
acl_msg_fatal("%s, %s(%d): strdup error=%s", __FILE__, myname,
__LINE__, acl_last_serror());
if (value == NULL) {
acl_msg_fatal("%s, %s(%d): strdup error=%s",
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
if (acl_htable_enter(table, name, value) == NULL)
acl_msg_error("%s, %s(%d): acl_htable_enter error=%s", __FILE__,
myname, __LINE__, acl_last_serror());
if (acl_htable_enter(table, name, value) == NULL) {
acl_msg_error("%s, %s(%d): acl_htable_enter error=%s",
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
acl_argv_free(argv);
}
@ -561,26 +585,30 @@ static void __get_host_from_url(char *buf, size_t size, const char *url)
size_t n;
buf[0] = 0;
if (strncasecmp(url, "http://", sizeof("http://") - 1) == 0)
if (strncasecmp(url, "http://", sizeof("http://") - 1) == 0) {
ptr1 = url + sizeof("http://") - 1;
else if (strncasecmp(url, "https://", sizeof("https://") - 1) == 0)
} else if (strncasecmp(url, "https://", sizeof("https://") - 1) == 0) {
ptr1 = url + sizeof("https://") - 1;
else
} else {
ptr1 = url;
}
if (ptr1 == NULL || *ptr1 == 0 || *ptr1 == '/')
if (ptr1 == NULL || *ptr1 == 0 || *ptr1 == '/') {
return;
}
ptr2 = strchr(ptr1, '/');
if (ptr2)
if (ptr2) {
n = ptr2 - ptr1;
else
} else {
n = strlen(ptr1);
}
n++;
if (n > size)
if (n > size) {
n = size;
}
ACL_SAFE_STRNCPY(buf, ptr1, (int) n);
}
@ -605,8 +633,9 @@ static void __strip_url_path(ACL_VSTRING *buf, const char *url)
acl_foreach(iter, argv) {
ptr = (const char*) iter.data;
if (strcmp(ptr, ".") == 0 || strcmp(ptr, "..") == 0)
if (strcmp(ptr, ".") == 0 || strcmp(ptr, "..") == 0) {
continue;
}
ACL_VSTRING_ADDCH(buf, '/');
acl_vstring_strcat(buf, ptr);
}
@ -622,7 +651,6 @@ static void __strip_url_path(ACL_VSTRING *buf, const char *url)
static void __parse_url_and_port(HTTP_HDR_REQ *hh, const char *url)
{
const char *myname = "__parse_url_and_port";
int i;
ACL_ARGV *url_argv;
const char *ptr, *url_params = NULL;
@ -634,9 +662,9 @@ static void __parse_url_and_port(HTTP_HDR_REQ *hh, const char *url)
hh->port = atoi(ptr);
}
if (strncasecmp(url, "http://", sizeof("http://") - 1) == 0)
if (strncasecmp(url, "http://", sizeof("http://") - 1) == 0) {
url += sizeof("http://") - 1;
else if (strncasecmp(url, "https://", sizeof("https://") - 1) == 0) {
} else if (strncasecmp(url, "https://", sizeof("https://") - 1) == 0) {
url += sizeof("https://") - 1;
hh->port = 443; /* set the default https server port */
}
@ -650,12 +678,13 @@ static void __parse_url_and_port(HTTP_HDR_REQ *hh, const char *url)
}
/* sanity check */
if (hh->port <= 0)
if (hh->port <= 0) {
hh->port = 80;
}
if (*url == '/')
if (*url == '/') {
acl_vstring_strcpy(hh->url_part, url);
else if ((url = strchr(url, '/')) == NULL) {
} else if ((url = strchr(url, '/')) == NULL) {
ACL_VSTRING_ADDCH(hh->url_part, '/');
ACL_VSTRING_TERMINATE(hh->url_part);
ACL_VSTRING_ADDCH(hh->url_path, '/');
@ -687,29 +716,35 @@ static void __parse_url_and_port(HTTP_HDR_REQ *hh, const char *url)
ptr++;
}
if (url_params == NULL)
if (url_params == NULL) {
__strip_url_path(hh->url_path, url);
else {
} else {
acl_vstring_strncpy(hh->url_path, url, ptr - url);
__strip_url_path(hh->url_path, acl_vstring_str(hh->url_path));
if (*url_params)
if (*url_params) {
acl_vstring_strcpy(hh->url_params, url_params);
}
}
if ((hh->flag & HTTP_HDR_REQ_FLAG_PARSE_PARAMS) == 0)
if ((hh->flag & HTTP_HDR_REQ_FLAG_PARSE_PARAMS) == 0) {
return;
if (ACL_VSTRING_LEN(hh->url_params) == 0)
}
if (ACL_VSTRING_LEN(hh->url_params) == 0) {
return;
if (hh->params_table == NULL)
}
if (hh->params_table == NULL) {
hh->params_table = acl_htable_create(__http_hdr_max_request, 0);
if (hh->params_table == NULL)
}
if (hh->params_table == NULL) {
acl_msg_fatal("%s, %s(%d): htable create error(%s)",
__FILE__, myname, __LINE__, acl_last_serror());
__FILE__, __FUNCTION__, __LINE__, acl_last_serror());
}
url_argv = acl_argv_split(acl_vstring_str(hh->url_params), "&");
for (i = 0; i < url_argv->argc; i++) {
ptr = acl_argv_index(url_argv, i);
if (ptr == NULL)
if (ptr == NULL) {
break;
}
__add_request_item(hh->params_table, ptr);
}
acl_argv_free(url_argv);
@ -722,7 +757,6 @@ void http_uri_correct(int onoff)
int http_hdr_req_line_parse(HTTP_HDR_REQ *hh)
{
const char *myname = "http_hdr_req_line_parse";
ACL_ARGV *request_argv;
HTTP_HDR_ENTRY *entry;
char *purl;
@ -731,29 +765,29 @@ int http_hdr_req_line_parse(HTTP_HDR_REQ *hh)
if (hh == NULL) {
acl_msg_error("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return -1;
}
if (hh->hdr.entry_lnk == NULL)
acl_msg_fatal("%s, %s(%d): entry_lnk null",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
if (acl_array_size(hh->hdr.entry_lnk) <= 0) {
acl_msg_error("%s, %s(%d): no method",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return -1;
}
entry = (HTTP_HDR_ENTRY *) acl_array_index(hh->hdr.entry_lnk, 0);
if (entry == NULL) {
acl_msg_error("%s, %s(%d): null array",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return -1;
}
if (entry->value == NULL || *(entry->value) == 0) {
acl_msg_error("%s, %s(%d): entry->value invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return -1;
}
@ -765,8 +799,7 @@ int http_hdr_req_line_parse(HTTP_HDR_REQ *hh)
&& strcasecmp(entry->name, "HEAD") != 0)
{
acl_msg_error("%s, %s(%d): invalid http method=%s",
__FILE__, myname, __LINE__,
entry->name);
__FILE__, __FUNCTION__, __LINE__, entry->name);
return -1;
}
@ -780,7 +813,7 @@ int http_hdr_req_line_parse(HTTP_HDR_REQ *hh)
request_argv = acl_argv_split(entry->value, "\t ");
if (request_argv->argc != 2) {
acl_msg_error("%s, %s(%d): invalid request line=%s, argc=%d",
__FILE__, myname, __LINE__,
__FILE__, __FUNCTION__, __LINE__,
entry->value, request_argv->argc);
acl_argv_free(request_argv);
return -1;
@ -795,10 +828,11 @@ int http_hdr_req_line_parse(HTTP_HDR_REQ *hh)
/* get HOST item */
ptr = http_hdr_entry_value(&hh->hdr, "Host");
if (ptr)
if (ptr) {
ACL_SAFE_STRNCPY(hh->host, ptr, sizeof(hh->host));
else
} else {
__get_host_from_url(hh->host, sizeof(hh->host), purl);
}
/* parse the first line's url and get server side's port */
__parse_url_and_port(hh, purl);
@ -808,7 +842,6 @@ int http_hdr_req_line_parse(HTTP_HDR_REQ *hh)
ret = http_hdr_parse_version(&hh->hdr, ptr);
acl_argv_free(request_argv);
return ret;
}
@ -819,33 +852,36 @@ int http_hdr_req_parse(HTTP_HDR_REQ *hh)
int http_hdr_req_parse3(HTTP_HDR_REQ *hh, int parse_params, int parse_cookie)
{
if (parse_params)
if (parse_params) {
hh->flag |= HTTP_HDR_REQ_FLAG_PARSE_PARAMS;
if (http_hdr_req_line_parse(hh) < 0)
}
if (http_hdr_req_line_parse(hh) < 0) {
return -1;
}
if (parse_cookie) {
hh->flag |= HTTP_HDR_REQ_FLAG_PARSE_COOKIE;
if (http_hdr_req_cookies_parse(hh) < 0)
if (http_hdr_req_cookies_parse(hh) < 0) {
return -1;
}
}
return http_hdr_parse(&hh->hdr);
}
int http_hdr_req_rewrite2(HTTP_HDR_REQ *hh, const char *url)
{
const char *myname = "http_hdr_req_rewrite2";
HTTP_HDR_ENTRY *first_entry, *entry;
ACL_VSTRING *buf;
char host[256], *ptr;
const char *phost, *purl;
int i, n;
if (hh->hdr.entry_lnk == NULL)
acl_msg_fatal("%s(%d): entry_lnk null", myname, __LINE__);
if (hh->hdr.entry_lnk == NULL) {
acl_msg_fatal("%s(%d): entry_lnk null", __FUNCTION__, __LINE__);
}
n = acl_array_size(hh->hdr.entry_lnk);
if (n <= 0) {
acl_msg_error("%s(%d): first entry null", myname, __LINE__);
acl_msg_error("%s(%d): first entry null", __FUNCTION__, __LINE__);
return -1;
}
@ -853,13 +889,15 @@ int http_hdr_req_rewrite2(HTTP_HDR_REQ *hh, const char *url)
if (strncasecmp(url, "http://", strlen("http://")) == 0) {
phost = url + strlen("http://");
purl = strchr(phost, '/');
if (purl == NULL)
if (purl == NULL) {
purl = "/";
}
} else if (strncasecmp(url, "https://", strlen("https://")) == 0) {
phost = url + strlen("https://");
purl = strchr(phost, '/');
if (purl == NULL)
if (purl == NULL) {
purl = "/";
}
} else {
phost = hh->host; /* 如果URL中没有 http[s]:// 则默认采用原 Host 字段 */
purl = url;
@ -867,27 +905,30 @@ int http_hdr_req_rewrite2(HTTP_HDR_REQ *hh, const char *url)
host[0] = 0;
ACL_SAFE_STRNCPY(host, phost, sizeof(host));
ptr = strchr(host, '/');
if (ptr)
if (ptr) {
*ptr = 0;
}
/* 将新的主机信息覆盖旧信息 */
ACL_SAFE_STRNCPY(hh->host, host, sizeof(hh->host));
buf = acl_vstring_alloc(256);
if (phost == hh->host) {
if (*url == '/')
if (*url == '/') {
acl_vstring_sprintf(buf, "%s HTTP/%d.%d",
purl, hh->hdr.version.major, hh->hdr.version.minor);
else
} else {
acl_vstring_sprintf(buf, "/%s HTTP/%d.%d",
purl, hh->hdr.version.major, hh->hdr.version.minor);
}
} else {
acl_vstring_sprintf(buf, "%s HTTP/%d.%d",
purl, hh->hdr.version.major, hh->hdr.version.minor);
}
first_entry = (HTTP_HDR_ENTRY *) acl_array_index(hh->hdr.entry_lnk, 0);
if (first_entry == NULL || first_entry->value == NULL)
acl_msg_fatal("%s(%d): first_entry invalid", myname, __LINE__);
if (first_entry == NULL || first_entry->value == NULL) {
acl_msg_fatal("%s(%d): first_entry invalid", __FUNCTION__, __LINE__);
}
acl_myfree(first_entry->value);
first_entry->value = acl_vstring_export(buf);
__hdr_reset(hh, 0);
@ -904,8 +945,9 @@ int http_hdr_req_rewrite2(HTTP_HDR_REQ *hh, const char *url)
}
hh->flag |= (HTTP_HDR_REQ_FLAG_PARSE_PARAMS | HTTP_HDR_REQ_FLAG_PARSE_COOKIE);
if (http_hdr_req_line_parse(hh) < 0)
return (-1);
if (http_hdr_req_line_parse(hh) < 0) {
return -1;
}
return 0;
}
@ -931,49 +973,49 @@ const char *http_hdr_req_method(const HTTP_HDR_REQ *hh)
const char *http_hdr_req_param(const HTTP_HDR_REQ *hh, const char *name)
{
const char *myname = "http_hdr_req_get";
if (hh == NULL || name == NULL) {
acl_msg_error("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return NULL;
}
if (hh->params_table == NULL)
if (hh->params_table == NULL) {
return NULL;
}
return acl_htable_find(hh->params_table, name);
}
const char *http_hdr_req_url_part(const HTTP_HDR_REQ *hh)
{
const char *myname = "http_hdr_req_url_part";
if (hh == NULL) {
acl_msg_error("%s, %s(%d): input invalid",
__FILE__, myname, __LINE__);
__FILE__, __FUNCTION__, __LINE__);
return NULL;
}
if (ACL_VSTRING_LEN(hh->url_part) == 0)
if (ACL_VSTRING_LEN(hh->url_part) == 0) {
return NULL;
}
return acl_vstring_str(hh->url_part);
}
const char *http_hdr_req_url_path(const HTTP_HDR_REQ *hh)
{
if (ACL_VSTRING_LEN(hh->url_path) == 0)
if (ACL_VSTRING_LEN(hh->url_path) == 0) {
return NULL;
}
return acl_vstring_str(hh->url_path);
}
const char *http_hdr_req_host(const HTTP_HDR_REQ *hh)
{
if (hh->host[0] != 0)
if (hh->host[0] != 0) {
return hh->host;
else
} else {
return NULL;
}
}
static void free_vstring(ACL_VSTRING *buf)
@ -1000,24 +1042,27 @@ const char *http_hdr_req_url(const HTTP_HDR_REQ *hh)
int http_hdr_req_range(const HTTP_HDR_REQ *hdr_req, http_off_t *range_from,
http_off_t *range_to)
{
const char *myname = "http_hdr_req_range";
char buf[256], *ptr1;
const char *ptr;
if (range_from == NULL)
acl_msg_fatal("%s(%d): range_from null", myname, __LINE__);
if (range_to == NULL)
acl_msg_fatal("%s(%d): range_to null", myname, __LINE__);
if (range_from == NULL) {
acl_msg_fatal("%s(%d): range_from null", __FUNCTION__, __LINE__);
}
if (range_to == NULL) {
acl_msg_fatal("%s(%d): range_to null", __FUNCTION__, __LINE__);
}
/* 数据格式: Range: bytes={range_from}-{range_to}
* : Range: bytes={range_from}-
*/
ptr = http_hdr_entry_value(&hdr_req->hdr, "Range");
if (ptr == NULL)
if (ptr == NULL) {
return -1;
}
ptr = strstr(ptr, "bytes=");
if (ptr == NULL)
if (ptr == NULL) {
return -1;
}
ptr += strlen("bytes=");
ACL_SAFE_STRNCPY(buf, ptr, sizeof(buf));
ptr1 = buf;
@ -1025,18 +1070,21 @@ int http_hdr_req_range(const HTTP_HDR_REQ *hdr_req, http_off_t *range_from,
if (*ptr1 == '-' || *ptr1 == ' ') {
*ptr1 = 0;
*range_from = acl_atoi64(buf);
if (*range_from < 0)
return (-1);
if (*(++ptr1) == 0)
if (*range_from < 0) {
return -1;
}
if (*(++ptr1) == 0) {
*range_to = -1;
else
} else {
*range_to = acl_atoi64(ptr1);
if (*range_to <= 0)
}
if (*range_to <= 0) {
*range_to = -1;
}
return 0;
}
ptr1++;
}
return -1;
}
}