diff --git a/include/http2.h b/include/http2.h index 3b56e85386f..c507a8e0b18 100644 --- a/include/http2.h +++ b/include/http2.h @@ -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; } /** diff --git a/src/core/base.c b/src/core/base.c index 0b0577bf9d2..bc82a7427c8 100644 --- a/src/core/base.c +++ b/src/core/base.c @@ -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; diff --git a/swoole.c b/swoole.c index 0a2c5248223..3414ba6b344 100644 --- a/swoole.c +++ b/swoole.c @@ -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; diff --git a/swoole_config.h b/swoole_config.h index f7af5b1a640..8918d0d3586 100644 --- a/swoole_config.h +++ b/swoole_config.h @@ -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" diff --git a/swoole_http_v2_client_coro.cc b/swoole_http_v2_client_coro.cc index f91df831c6f..24e2c5b2879 100644 --- a/swoole_http_v2_client_coro.cc +++ b/swoole_http_v2_client_coro.cc @@ -383,15 +383,14 @@ 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")); } @@ -399,7 +398,7 @@ static ssize_t http2_client_build_header(zval *zobject, zval *req, char *buffer) { 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); diff --git a/swoole_http_v2_server.cc b/swoole_http_v2_server.cc index ea7374ad2f9..e4affcb1e0d 100644 --- a/swoole_http_v2_server.cc +++ b/swoole_http_v2_server.cc @@ -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(); diff --git a/tests/include/skipif.inc b/tests/include/skipif.inc index 1f916c9f43c..343b46c478d 100644 --- a/tests/include/skipif.inc +++ b/tests/include/skipif.inc @@ -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'); } }