Merge branch 'gitee-master' into gitlab-upstream

This commit is contained in:
zhengshuxin 2023-08-26 08:40:59 +08:00
commit 1717b70300
15 changed files with 311 additions and 247 deletions

View File

@ -20,6 +20,9 @@ typedef struct ACL_MDB ACL_MDB;
#define ACL_MDT_FLAG_UNI (1 << 0) /**< 表示唯一 */
#define ACL_MDT_FLAG_KMR (1 << 1) /**< 表示重用键内存 */
#define ACL_MDT_FLAG_DMR (1 << 2) /**< 表示重用值内存 */
/**< 当存在重复键,且同时指定了 ACL_MDT_FLAG_UNI | ACL_MDT_FLAG_OOV 时则使用新值覆盖旧值 */
#define ACL_MDT_FLAG_OOV (1 << 3)
#define ACL_MDT_FLAG_SLICE1 (1 << 10) /**< 启用 ACL_SLICE_FLAG_GC1 */
#define ACL_MDT_FLAG_SLICE2 (1 << 11) /**< 启用 ACL_SLICE_FLAG_GC2 */
#define ACL_MDT_FLAG_SLICE3 (1 << 12) /**< 启用 ACL_SLICE_FLAG_GC3 */

View File

@ -135,7 +135,7 @@ ACL_API void acl_htable_set_errno(ACL_HTABLE *table, int error);
* )
* @return , == NULL: ,
* 使
* acl_htable_last_errno() (ACL_HTABLE_STAT_DUPLEX_KEY)
* acl_htable_errno() (ACL_HTABLE_STAT_DUPLEX_KEY)
*/
ACL_API ACL_HTABLE_INFO *acl_htable_enter(ACL_HTABLE *table,
const char *key, void *value);
@ -146,10 +146,15 @@ ACL_API ACL_HTABLE_INFO *acl_htable_enter(ACL_HTABLE *table,
* @param key , key
* @param value (,
* )
* @param old_holder {void**} key
* old_holder 便
* @return , == NULL: ,
* 使
* acl_htable_last_errno() (ACL_HTABLE_STAT_DUPLEX_KEY)
* acl_htable_errno() (ACL_HTABLE_STAT_DUPLEX_KEY)
*/
ACL_API ACL_HTABLE_INFO *acl_htable_enter_r2(ACL_HTABLE *table,
const char *key, void *value, void **old_holder);
ACL_API ACL_HTABLE_INFO *acl_htable_enter_r(ACL_HTABLE *table,
const char *key, void *value);

View File

@ -241,8 +241,8 @@ static ACL_MDT_NOD *mdt_add(ACL_MDT *mdt, void *data, unsigned int dlen,
/* 在表索引中增加新的索引数据项 */
mdt->idx_add(idx, keys[i], rec);
} else if (idx->flag & ACL_MDT_FLAG_UNI) {
acl_msg_warn("%s(%d): duplex key, key_label(%s), key(%s)",
myname, __LINE__, key_labels[i], keys[i]);
//acl_msg_warn("%s(%d): duplex key, key_label(%s), key(%s)",
// myname, __LINE__, key_labels[i], keys[i]);
mdt_node_free(mdt, node, NULL);
return (NULL);
}

View File

@ -513,8 +513,6 @@ ACL_HTABLE_INFO *acl_htable_enter(ACL_HTABLE *table, const char *key_in, void *v
for (ht = table->data[n]; ht; ht = ht->next) {
if (STREQ(key, ht->key.c_key)) {
table->status = ACL_HTABLE_STAT_DUPLEX_KEY;
acl_msg_info("%s(%d): duplex key(%s) exist",
myname, __LINE__, key);
RETURN (ht);
}
}
@ -560,7 +558,12 @@ ACL_HTABLE_INFO *acl_htable_enter(ACL_HTABLE *table, const char *key_in, void *v
ACL_HTABLE_INFO *acl_htable_enter_r(ACL_HTABLE *table,
const char *key_in, void *value)
{
const char *myname = "acl_htable_enter_r";
return acl_htable_enter_r2(table, key_in, value, NULL);
}
ACL_HTABLE_INFO *acl_htable_enter_r2(ACL_HTABLE *table,
const char *key_in, void *value, void **old_holder)
{
ACL_HTABLE_INFO *ht;
int ret;
unsigned hash, n;
@ -609,9 +612,11 @@ ACL_HTABLE_INFO *acl_htable_enter_r(ACL_HTABLE *table,
for (ht = table->data[n]; ht; ht = ht->next) {
if (STREQ(key, ht->key.c_key)) {
acl_msg_info("%s(%d): duplex key(%s) exist",
myname, __LINE__, key);
table->status = ACL_HTABLE_STAT_DUPLEX_KEY;
if (old_holder) {
*old_holder = ht->value;
ht->value = value;
}
UNLOCK_TABLE(table);
RETURN (ht);
}
@ -638,6 +643,10 @@ ACL_HTABLE_INFO *acl_htable_enter_r(ACL_HTABLE *table,
htable_link(table, ht, n);
UNLOCK_TABLE(table);
if (old_holder) {
*old_holder = NULL;
}
RETURN (ht);
}
/* acl_htable_find - lookup value */

View File

@ -39,11 +39,9 @@ static EPOLL_CTX *main_epoll_ctx = NULL;
static void main_epoll_end(void)
{
const char *myname = "main_epoll_end";
if (main_epoll_ctx != NULL) {
acl_msg_info("%s(%d), %s: close epoll_fd: %d, tid: %lu, %lu",
__FILE__, __LINE__, myname, main_epoll_ctx->epfd,
__FILE__, __LINE__, __FUNCTION__, main_epoll_ctx->epfd,
(unsigned long) main_epoll_ctx->tid,
(unsigned long) acl_pthread_self());
@ -59,11 +57,10 @@ static acl_pthread_once_t epoll_once = ACL_PTHREAD_ONCE_INIT;
static void thread_epoll_end(void *ctx)
{
const char *myname = "thread_epoll_end";
EPOLL_CTX *epoll_ctx = (EPOLL_CTX*) ctx;
acl_msg_info("%s(%d), %s: close epoll_fd: %d, tid: %lu, %lu",
__FILE__, __LINE__, myname, epoll_ctx->epfd,
__FILE__, __LINE__, __FUNCTION__, epoll_ctx->epfd,
(unsigned long) epoll_ctx->tid,
(unsigned long) acl_pthread_self());
@ -80,7 +77,6 @@ static void thread_epoll_once(void)
static EPOLL_CTX *thread_epoll_init(void)
{
const char *myname = "thread_epoll_init";
EPOLL_CTX *epoll_ctx = (EPOLL_CTX*) acl_mymalloc(sizeof(EPOLL_CTX));
if (acl_pthread_setspecific(epoll_key, epoll_ctx) != 0)
@ -90,7 +86,7 @@ static EPOLL_CTX *thread_epoll_init(void)
epoll_ctx->epfd = epoll_create(1);
if (epoll_ctx == NULL) {
acl_msg_error("%s(%d): epoll_create error: %s",
myname, __LINE__, acl_last_serror());
__FUNCTION__, __LINE__, acl_last_serror());
return NULL;
}
@ -100,12 +96,12 @@ static EPOLL_CTX *thread_epoll_init(void)
atexit(main_epoll_end);
#endif
acl_msg_info("%s(%d): %s, create epoll_fd: %d, tid: %lu, %lu",
__FILE__, __LINE__, myname, epoll_ctx->epfd,
__FILE__, __LINE__, __FUNCTION__, epoll_ctx->epfd,
(unsigned long) epoll_ctx->tid,
(unsigned long) acl_pthread_self());
} else {
acl_msg_info("%s(%d): %s, create epoll_fd: %d, tid: %lu, %lu",
__FILE__, __LINE__, myname, epoll_ctx->epfd,
__FILE__, __LINE__, __FUNCTION__, epoll_ctx->epfd,
(unsigned long) epoll_ctx->tid,
(unsigned long) acl_pthread_self());
}
@ -115,13 +111,11 @@ static EPOLL_CTX *thread_epoll_init(void)
static int thread_epoll_reopen(EPOLL_CTX *epoll_ctx)
{
const char *myname = "thread_epoll_reopen";
close(epoll_ctx->epfd);
epoll_ctx->epfd = epoll_create(1);
if (epoll_ctx->epfd == -1) {
acl_msg_error("%s(%d): epoll_create error: %s",
myname, __LINE__, acl_last_serror());
__FUNCTION__, __LINE__, acl_last_serror());
return -1;
}
return 0;
@ -129,7 +123,6 @@ static int thread_epoll_reopen(EPOLL_CTX *epoll_ctx)
int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
{
const char *myname = "acl_read_epoll_wait";
int ret, retried = 0;
EPOLL_CTX *epoll_ctx;
struct epoll_event ee, events[1];
@ -143,7 +136,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
epoll_ctx = thread_epoll_init();
if (epoll_ctx == NULL) {
acl_msg_error("%s(%d): thread_epoll_init error",
myname, __LINE__);
__FUNCTION__, __LINE__);
return -1;
}
}
@ -175,7 +168,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
}
acl_msg_error("%s(%d): epoll_ctl error: %s, fd: %d, "
"epfd: %d, tid: %lu, %lu", myname, __LINE__,
"epfd: %d, tid: %lu, %lu", __FUNCTION__, __LINE__,
acl_last_serror(), fd, epoll_ctx->epfd,
(unsigned long) epoll_ctx->tid,
(unsigned long) acl_pthread_self());
@ -197,7 +190,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
continue;
} else if (ret == EBADF || ret == EINTR) {
acl_msg_error("%s(%d): fd: %d, epfd: %d,"
" error: %s", myname, __LINE__, fd,
" error: %s", __FUNCTION__, __LINE__, fd,
epoll_ctx->epfd, acl_last_serror());
if (retried) {
@ -214,7 +207,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
}
acl_msg_error("%s(%d): epoll_wait error: %s, fd: %d,"
" epfd: %d, tid: %lu, %lu", myname, __LINE__,
" epfd: %d, tid: %lu, %lu", __FUNCTION__, __LINE__,
acl_last_serror(), fd, epoll_ctx->epfd,
(unsigned long) epoll_ctx->tid,
(unsigned long) acl_pthread_self());
@ -224,7 +217,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
/*
acl_msg_warn("%s(%d), %s: poll timeout: %s, fd: %d, "
"delay: %d, spent: %ld", __FILE__, __LINE__,
myname, acl_last_serror(), fd, delay,
__FUNCTION__, acl_last_serror(), fd, delay,
(long) (time(NULL) - begin));
*/
if (delay == 0) {
@ -241,7 +234,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
} else {
acl_msg_warn("%s(%d), %s: poll error: %s, fd: %d, "
"delay: %d, spent: %ld", __FILE__, __LINE__,
myname, acl_last_serror(), fd, delay,
__FUNCTION__, acl_last_serror(), fd, delay,
(long) (time(NULL) - begin));
ret = -1;
}
@ -253,7 +246,7 @@ int acl_read_epoll_wait(ACL_SOCKET fd, int delay)
ee.data.fd = fd;
if (epoll_ctl(epoll_ctx->epfd, EPOLL_CTL_DEL, fd, &ee) == -1) {
acl_msg_error("%s(%d): epoll_ctl error: %s, fd: %d, epfd: %d,"
" tid: %lu, %lu", myname, __LINE__, acl_last_serror(),
" tid: %lu, %lu", __FUNCTION__, __LINE__, acl_last_serror(),
fd, epoll_ctx->epfd, (unsigned long) epoll_ctx->tid,
(unsigned long) acl_pthread_self());
return -1;
@ -295,7 +288,6 @@ void acl_set_poll(acl_poll_fn fn)
int acl_read_poll_wait(ACL_SOCKET fd, int delay)
{
const char *myname = "acl_read_poll_wait";
struct pollfd fds;
time_t begin;
int left = delay;
@ -341,7 +333,7 @@ int acl_read_poll_wait(ACL_SOCKET fd, int delay)
}
/*
acl_msg_error("%s(%d), %s: poll error(%s), fd: %d",
__FILE__, __LINE__, myname,
__FILE__, __LINE__, __FUNCTION__,
acl_last_serror(), (int) fd);
*/
return -1;
@ -349,7 +341,7 @@ int acl_read_poll_wait(ACL_SOCKET fd, int delay)
/*
acl_msg_warn("%s(%d), %s: poll timeout: %s, fd: %d, "
"delay: %d, spent: %ld", __FILE__, __LINE__,
myname, acl_last_serror(), fd, delay,
__FUNCTION__, acl_last_serror(), fd, delay,
(long) (time(NULL) - begin));
*/
if (left > 0) {
@ -369,7 +361,7 @@ int acl_read_poll_wait(ACL_SOCKET fd, int delay)
if (fds.revents & (POLLHUP | POLLERR | POLLNVAL)) {
acl_msg_warn("%s(%d), %s: poll error: %s, "
"fd: %d, delay: %d, spent: %ld",
__FILE__, __LINE__, myname,
__FILE__, __LINE__, __FUNCTION__ ,
acl_last_serror(), fd, delay,
(long) (time(NULL) - begin));
return 0;
@ -377,7 +369,7 @@ int acl_read_poll_wait(ACL_SOCKET fd, int delay)
acl_msg_warn("%s(%d), %s: poll error: %s, fd: %d, "
"delay: %d, spent: %ld", __FILE__, __LINE__,
myname, acl_last_serror(), fd, delay,
__FUNCTION__, acl_last_serror(), fd, delay,
(long) (time(NULL) - begin));
return -1;
}
@ -392,7 +384,6 @@ static HANDLE __handle = NULL;
int acl_read_iocp_wait(ACL_SOCKET fd, int timeout)
{
const char *myname = "acl_read_iocp_wait";
OVERLAPPED *overlapped, *lpOverlapped;
DWORD recvBytes;
BOOL isSuccess;
@ -427,7 +418,7 @@ int acl_read_iocp_wait(ACL_SOCKET fd, int timeout)
if (ReadFile((HANDLE) fd, NULL, 0, &recvBytes, overlapped) == FALSE
&& acl_last_error() != ERROR_IO_PENDING) {
acl_msg_warn("%s(%d): ReadFile error(%s)",
myname, __LINE__, acl_last_serror());
__FUNCTION__, __LINE__, acl_last_serror());
return -1;
}
@ -467,7 +458,6 @@ void acl_set_select(acl_select_fn fn)
int acl_read_select_wait(ACL_SOCKET fd, int delay)
{
const char *myname = "acl_read_select_wait";
fd_set rfds, xfds;
struct timeval tv;
struct timeval *tp;
@ -480,7 +470,7 @@ int acl_read_select_wait(ACL_SOCKET fd, int delay)
#ifndef ACL_WINDOWS
if (FD_SETSIZE <= (unsigned) fd)
acl_msg_fatal("%s(%d), %s: descriptor %d does not fit "
"FD_SETSIZE %d", __FILE__, __LINE__, myname,
"FD_SETSIZE %d", __FILE__, __LINE__, __FUNCTION__,
(int) fd, FD_SETSIZE);
#endif
@ -528,14 +518,14 @@ int acl_read_select_wait(ACL_SOCKET fd, int delay)
}
#endif
acl_msg_error("%s(%d), %s: select error(%s), fd: %d",
__FILE__, __LINE__, myname,
__FILE__, __LINE__, __FUNCTION__,
acl_last_serror(), (int) fd);
return -1;
case 0:
/*
acl_msg_warn("%s(%d), %s: poll timeout: %s, fd: %d, "
"timeout: %d, spent: %ld", __FILE__, __LINE__,
myname, acl_last_serror(), fd, timeout,
__FUNCTION__, acl_last_serror(), fd, timeout,
(long) (time(NULL) - begin));
*/
if (delay == 0) {

View File

@ -1,6 +1,9 @@
#include "stdafx.h"
#include "util.h"
static acl::atomic_long __count;
static int __meter = 10000;
static acl::string __keypre("test_key_cluster");
static bool test_del(acl::redis& cmd, int i)
@ -202,6 +205,13 @@ protected:
}
*/
long long n = ++__count;
if (n % __meter == 0) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "%lld", n);
acl::meter_time(__FILE__, __LINE__, tmp);
}
cmd_string.clear();
cmd_key.clear();
}
@ -229,6 +239,7 @@ static void usage(const char* procname)
"-r retry_for_cluster_resnum[default: 10]\r\n"
"-p password [set the password of redis cluster]\r\n"
"-m [if use mbox in pipeline mode, default: false]\r\n"
"-b meter_base[default: 10000]\r\n"
"-a cmd[set|get|expire|ttl|exists|type|del]\r\n",
procname);
}
@ -240,7 +251,7 @@ int main(int argc, char* argv[])
acl::box_type_t btype = acl::BOX_TYPE_TBOX;
acl::string addr("127.0.0.1:6379"), cmd("del"), passwd;
while ((ch = getopt(argc, argv, "hs:n:t:a:p:m")) > 0) {
while ((ch = getopt(argc, argv, "hs:n:b:t:a:p:m")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
@ -251,6 +262,9 @@ int main(int argc, char* argv[])
case 'n':
n = atoi(optarg);
break;
case 'b':
__meter = atoi(optarg);
break;
case 't':
max_threads = atoi(optarg);
break;

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 \
{ \

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,7 @@
#include "stdafx.h"
static int __meter = 10000;
static acl::atomic_long __count;
static acl::string __keypre("test_key_cluster");
static bool test_del(acl::redis_key& cmd, size_t tid, size_t fid, int i)
@ -239,6 +241,13 @@ protected:
}
*/
long long n = ++__count;
if (n % __meter == 0) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "%lld", n);
acl::meter_time(__FILE__, __LINE__, tmp);
}
redis.clear();
}
}
@ -311,6 +320,7 @@ static void usage(const char* procname)
"-s one_redis_addr[127.0.0.1:6379]\r\n"
"-F [if using fiber_redis_pipeline, default: false]\r\n"
"-n count[default: 10]\r\n"
"-m meter_base[default: 10000]\r\n"
"-C connect_timeout[default: 10]\r\n"
"-I rw_timeout[default: 10]\r\n"
"-t max_threads[default: 10]\r\n"
@ -332,7 +342,7 @@ int main(int argc, char* argv[])
bool share_stack = false, use_fiber_tbox = false;
acl::string addr("127.0.0.1:6379"), cmd, passwd;
while ((ch = getopt(argc, argv, "hs:n:C:I:t:c:a:p:Sz:F")) > 0) {
while ((ch = getopt(argc, argv, "hs:n:m:C:I:t:c:a:p:Sz:F")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
@ -343,6 +353,9 @@ int main(int argc, char* argv[])
case 'n':
n = atoi(optarg);
break;
case 'm':
__meter = atoi(optarg);
break;
case 'C':
conn_timeout = atoi(optarg);
break;

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;
@ -88,7 +87,8 @@ static int hdr_ready(HTTP_HDR *hdr, const char *line, int dlen)
static int hdr_get(HTTP_HDR *hdr, ACL_VSTREAM *stream, int timeout)
{
char buf[8192];
/* 当使用协程共享栈时如果将此BUFF设的过大会增大共享栈的空间大小造成内存浪费 */
char buf[HTTP_BUF_SIZE];
int ret;
stream->rw_timeout = timeout;
@ -163,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));
@ -192,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;
}
@ -202,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;
}
@ -218,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;
@ -239,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 */
@ -279,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;
}
}
@ -357,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;
@ -374,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;
}
}
@ -383,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;
@ -400,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;
}
}

View File

@ -6,7 +6,7 @@
#include "http/lib_http.h"
#include "http.h"
http_off_t var_http_buf_size = HTTP_BSIZE;
http_off_t var_http_buf_size = HTTP_BUF_SIZE;
int var_http_tls_cache = 50;
void http_buf_size_set(http_off_t size)