Skip to content

Commit

Permalink
Showing 7 changed files with 61 additions and 62 deletions.
2 changes: 1 addition & 1 deletion include/http2.h
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ static sw_inline void swHttp2_init_settings(swHttp2_settings *settings)
settings->window_size = SW_HTTP2_DEFAULT_WINDOW_SIZE;
settings->max_concurrent_streams = SW_HTTP2_MAX_MAX_CONCURRENT_STREAMS;
settings->max_frame_size = SW_HTTP2_MAX_MAX_FRAME_SIZE;
settings->max_header_list_size = SW_HTTP2_MAX_MAX_HEADER_LIST_SIZE;
settings->max_header_list_size = SW_HTTP2_DEFAULT_MAX_HEADER_LIST_SIZE;
}

/**
4 changes: 4 additions & 0 deletions src/core/base.c
Original file line number Diff line number Diff line change
@@ -68,7 +68,11 @@ void swoole_init(void)

SwooleG.pid = getpid();

#ifdef __MACH__
SwooleG.socket_buffer_size = 256 * 1024;
#else
SwooleG.socket_buffer_size = SW_SOCKET_BUFFER_SIZE;
#endif

#ifdef SW_DEBUG
SwooleG.log_level = 0;
4 changes: 0 additions & 4 deletions swoole.c
Original file line number Diff line number Diff line change
@@ -1152,10 +1152,6 @@ PHP_MINIT_FUNCTION(swoole)
SwooleG.socket_buffer_size = SWOOLE_G(socket_buffer_size);
}

#ifdef __MACH__
SwooleG.socket_buffer_size = 256 * 1024;
#endif

//default 60s
SwooleG.dns_cache_refresh_time = 60;

15 changes: 8 additions & 7 deletions swoole_config.h
Original file line number Diff line number Diff line change
@@ -234,13 +234,14 @@
/**
* HTTP2 Protocol
*/
#define SW_HTTP2_DATA_BUFFER_SIZE 8192
#define SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE (1 << 12)
#define SW_HTTP2_MAX_MAX_CONCURRENT_STREAMS 128
#define SW_HTTP2_MAX_MAX_FRAME_SIZE ((1u << 14))
#define SW_HTTP2_MAX_WINDOW_SIZE ((1u << 31) - 1)
#define SW_HTTP2_DEFAULT_WINDOW_SIZE 65535
#define SW_HTTP2_MAX_MAX_HEADER_LIST_SIZE UINT32_MAX
#define SW_HTTP2_DATA_BUFFER_SIZE 8192
#define SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE (1 << 12)
#define SW_HTTP2_MAX_MAX_CONCURRENT_STREAMS 128
#define SW_HTTP2_MAX_MAX_FRAME_SIZE ((1u << 14))
#define SW_HTTP2_MAX_WINDOW_SIZE ((1u << 31) - 1)
#define SW_HTTP2_DEFAULT_WINDOW_SIZE 65535
#define SW_HTTP2_DEFAULT_MAX_HEADER_LIST_SIZE (SW_SOCKET_BUFFER_SIZE / 2)
#define SW_HTTP2_MAX_MAX_HEADER_LIST_SIZE UINT32_MAX

#define SW_HTTP_CLIENT_USERAGENT "swoole-http-client"
#define SW_HTTP_CLIENT_BOUNDARY_PREKEY "----SwooleBoundary"
21 changes: 10 additions & 11 deletions swoole_http_v2_client_coro.cc
Original file line number Diff line number Diff line change
@@ -383,23 +383,22 @@ static ssize_t http2_client_build_header(zval *zobject, zval *req, char *buffer)
int i;
int index = 0;
int find_host = 0;
zval *zmethod = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("method"), 0);
zval *zpath = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("path"), 0);
zval *zheaders = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("headers"), 0);
zval *zcookies = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("cookies"), 0);
nghttp2_nv nv[8 + (ZVAL_IS_ARRAY(zheaders) ? php_swoole_array_length(zheaders) : 0)];

zval *zmethod = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("method"), 1);
zval *zpath = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("path"), 1);
zval *zheaders = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("headers"), 1);
zval *zcookies = sw_zend_read_property(swoole_http2_request_class_entry_ptr, req, ZEND_STRL("cookies"), 1);

nghttp2_nv nv[1024];
http2_client_property *hcc = (http2_client_property *) swoole_get_property(zobject, HTTP2_CLIENT_CORO_PROPERTY);
if (ZVAL_IS_NULL(zmethod) || Z_TYPE_P(zmethod) != IS_STRING || Z_STRLEN_P(zmethod) == 0)
if (Z_TYPE_P(zmethod) != IS_STRING || Z_STRLEN_P(zmethod) == 0)
{
http2_add_header(&nv[index++], ZEND_STRL(":method"), ZEND_STRL("GET"));
}
else
{
http2_add_header(&nv[index++], ZEND_STRL(":method"), Z_STRVAL_P(zmethod), Z_STRLEN_P(zmethod));
}
if (ZVAL_IS_NULL(zpath) || Z_TYPE_P(zpath) != IS_STRING || Z_STRLEN_P(zpath) == 0)
if (Z_TYPE_P(zpath) != IS_STRING || Z_STRLEN_P(zpath) == 0)
{
http2_add_header(&nv[index++], ZEND_STRL(":path"), "/", 1);
}
@@ -418,7 +417,7 @@ static ssize_t http2_client_build_header(zval *zobject, zval *req, char *buffer)
//Host
index++;

if (zheaders && Z_TYPE_P(zheaders) == IS_ARRAY)
if (Z_TYPE_P(zheaders) == IS_ARRAY)
{
HashTable *ht = Z_ARRVAL_P(zheaders);
zval *value = NULL;
@@ -455,7 +454,7 @@ static ssize_t http2_client_build_header(zval *zobject, zval *req, char *buffer)
}

//http cookies
if (zcookies && Z_TYPE_P(zcookies) == IS_ARRAY)
if (Z_TYPE_P(zcookies) == IS_ARRAY)
{
http2_client_add_cookie(nv, &index, zcookies);
}
@@ -466,7 +465,7 @@ static ssize_t http2_client_build_header(zval *zobject, zval *req, char *buffer)
buflen = nghttp2_hd_deflate_bound(hcc->deflater, nv, index);
if (buflen > hcc->remote_settings.max_header_list_size)
{
swoole_php_error(E_WARNING, "header cannot bigger than remote max_header_list_size %u.", hcc->remote_settings.header_table_size);
swoole_php_error(E_WARNING, "header cannot bigger than remote max_header_list_size %u.", hcc->remote_settings.max_header_list_size);
return SW_ERR;
}
rv = nghttp2_hd_deflate_hd(hcc->deflater, (uchar *) buffer, buflen, nv, index);
75 changes: 37 additions & 38 deletions swoole_http_v2_server.cc
Original file line number Diff line number Diff line change
@@ -113,12 +113,13 @@ static sw_inline void http2_add_header(nghttp2_nv *headers, const char *k, int k
static int http_build_trailer(http_context *ctx, uchar *buffer)
{
int ret;
nghttp2_nv nv[128];
size_t index = 0;
zval *ztrailer = sw_zend_read_property(swoole_http_response_class_entry_ptr, ctx->response.zobject, ZEND_STRL("trailer"), 0);
uint32_t nv_size = ZVAL_IS_ARRAY(ztrailer) ? php_swoole_array_length(ztrailer) : 0;

zval *ztrailer = sw_zend_read_property(swoole_http_response_class_entry_ptr, ctx->response.zobject, ZEND_STRL("trailer"), 1);
if (ZVAL_IS_ARRAY(ztrailer))
if (nv_size > 0)
{
nghttp2_nv nv[nv_size];
HashTable *ht = Z_ARRVAL_P(ztrailer);
zval *value = NULL;
char *key = NULL;
@@ -134,47 +135,42 @@ static int http_build_trailer(http_context *ctx, uchar *buffer)
(void) type;
}
SW_HASHTABLE_FOREACH_END();
}

ssize_t rv;
size_t buflen;
size_t i;
size_t sum = 0;
http2_session *client = http2_sessions[ctx->fd];
nghttp2_hd_deflater *deflater = client->deflater;
ssize_t rv;
size_t buflen;
http2_session *client = http2_sessions[ctx->fd];
nghttp2_hd_deflater *deflater = client->deflater;

if (!deflater)
{
ret = nghttp2_hd_deflate_new(&deflater, SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE);
if (ret != 0)
if (!deflater)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_init failed with error: %s\n", nghttp2_strerror(ret));
return SW_ERR;
ret = nghttp2_hd_deflate_new(&deflater, SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE);
if (ret != 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_init failed with error: %s\n", nghttp2_strerror(ret));
return SW_ERR;
}
client->deflater = deflater;
}
client->deflater = deflater;
}

for (i = 0; i < index; ++i)
{
sum += nv[i].namelen + nv[i].valuelen;
}
buflen = nghttp2_hd_deflate_bound(deflater, nv, index);
rv = nghttp2_hd_deflate_hd(deflater, (uchar *) buffer, buflen, nv, index);
if (rv < 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_hd() failed with error: %s\n", nghttp2_strerror((int ) rv));
return SW_ERR;
}

buflen = nghttp2_hd_deflate_bound(deflater, nv, index);
rv = nghttp2_hd_deflate_hd(deflater, (uchar *) buffer, buflen, nv, index);
if (rv < 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_hd() failed with error: %s\n", nghttp2_strerror((int ) rv));
return SW_ERR;
}
ret = nghttp2_hd_deflate_change_table_size(deflater, SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE);
if (ret != 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_change_table_size failed with error: %s\n", nghttp2_strerror(ret));
return SW_ERR;
}

ret = nghttp2_hd_deflate_change_table_size(deflater, SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE);
if (ret != 0)
{
swoole_php_error(E_WARNING, "nghttp2_hd_deflate_change_table_size failed with error: %s\n", nghttp2_strerror(ret));
return SW_ERR;
return rv;
}

return rv;
return SW_OK;
}

static sw_inline void http2_onRequest(http_context *ctx, int from_fd)
@@ -233,7 +229,8 @@ static int http2_build_header(http_context *ctx, uchar *buffer, size_t body_leng
char intbuf[2][16];
int ret;
size_t index = 0;
nghttp2_nv nv[128];
zval *zheader = sw_zend_read_property(swoole_http_response_class_entry_ptr, ctx->response.zobject, ZEND_STRL("header"), 1);
nghttp2_nv nv[8 + (ZVAL_IS_ARRAY(zheader) ? php_swoole_array_length(zheader) : 0)];

assert(ctx->send_header == 0);

@@ -246,11 +243,9 @@ static int http2_build_header(http_context *ctx, uchar *buffer, size_t body_leng
http2_add_header(&nv[index++], ZEND_STRL(":status"), intbuf[0], ret);

// headers
zval *zheader = sw_zend_read_property(swoole_http_response_class_entry_ptr, ctx->response.zobject, ZEND_STRL("header"), 1);
if (ZVAL_IS_ARRAY(zheader))
{
uint32_t header_flag = 0x0;

HashTable *ht = Z_ARRVAL_P(zheader);
zval *value = NULL;
char *key = NULL;
@@ -279,6 +274,10 @@ static int http2_build_header(http_context *ctx, uchar *buffer, size_t body_leng
{
header_flag |= HTTP_HEADER_CONTENT_TYPE;
}
if (ZVAL_IS_NULL(value))
{
continue;
}
http2_add_header(&nv[index++], key, keylen, Z_STRVAL_P(value), Z_STRLEN_P(value));
}
SW_HASHTABLE_FOREACH_END();
2 changes: 1 addition & 1 deletion tests/include/skipif.inc
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ function skip_if_no_async_redis()

function skip_if_no_http2()
{
if (!class_exists("swoole_http2_reqeust", false)) {
if (!class_exists("swoole_http2_request", false)) {
skip('no http2');
}
}

0 comments on commit 5a1442a

Please sign in to comment.