Skip to content

Commit

Permalink
Adjust http2 default setting values (#5005)
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 10, 2023
1 parent 43bc755 commit f4dcd89
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 48 deletions.
57 changes: 31 additions & 26 deletions ext-src/swoole_http2_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,14 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
switch (type) {
case SW_HTTP2_TYPE_SETTINGS: {
if (flags & SW_HTTP2_FLAG_ACK) {
swoole_http2_frame_trace_log(recv, "ACK");
swoole_http2_frame_trace_log("ACK");
return SW_CONTINUE;
}

while (length > 0) {
id = ntohs(*(uint16_t *) (buf));
value = ntohl(*(uint32_t *) (buf + sizeof(uint16_t)));
swoole_http2_frame_trace_log(recv, "id=%d, value=%d", id, value);
swoole_http2_frame_trace_log("id=%d, value=%d", id, value);
switch (id) {
case SW_HTTP2_SETTING_HEADER_TABLE_SIZE:
if (value != remote_settings.header_table_size) {
Expand Down Expand Up @@ -578,7 +578,8 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
}
case SW_HTTP2_TYPE_WINDOW_UPDATE: {
value = ntohl(*(uint32_t *) buf);
swoole_http2_frame_trace_log(recv, "window_size_increment=%d", value);
swoole_trace_log(
SW_TRACE_HTTP2, "[" SW_ECHO_YELLOW "] stream_id=%d, size=%d", "WINDOW_UPDATE", stream_id, value);
if (stream_id == 0) {
remote_window_size += value;
} else {
Expand All @@ -590,7 +591,7 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
return SW_CONTINUE;
}
case SW_HTTP2_TYPE_PING: {
swoole_http2_frame_trace_log(recv, "ping");
swoole_http2_frame_trace_log("ping");
if (!(flags & SW_HTTP2_FLAG_ACK)) {
Http2::set_frame_header(
frame, SW_HTTP2_TYPE_PING, SW_HTTP2_FRAME_PING_PAYLOAD_SIZE, SW_HTTP2_FLAG_ACK, stream_id);
Expand All @@ -607,8 +608,7 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
buf += 4;
value = ntohl(*(uint32_t *) (buf));
buf += 4;
swoole_http2_frame_trace_log(recv,
"last_stream_id=%d, error_code=%d, opaque_data=[%.*s]",
swoole_http2_frame_trace_log("last_stream_id=%d, error_code=%d, opaque_data=[%.*s]",
server_last_stream_id,
value,
(int) (length - SW_HTTP2_GOAWAY_SIZE),
Expand All @@ -625,7 +625,7 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
}
case SW_HTTP2_TYPE_RST_STREAM: {
value = ntohl(*(uint32_t *) (buf));
swoole_http2_frame_trace_log(recv, "error_code=%d", value);
swoole_http2_frame_trace_log("error_code=%d", value);

// delete and free quietly
delete_stream(stream_id);
Expand All @@ -638,15 +638,15 @@ ReturnCode Client::parse_frame(zval *return_value, bool pipeline_read) {
case SW_HTTP2_TYPE_PUSH_PROMISE: {
#ifdef SW_DEBUG
uint32_t promise_stream_id = ntohl(*(uint32_t *) (buf)) & 0x7fffffff;
swoole_http2_frame_trace_log(recv, "promise_stream_id=%d", promise_stream_id);
swoole_http2_frame_trace_log("promise_stream_id=%d", promise_stream_id);
#endif
// auto promise_stream = create_stream(promise_stream_id, false);
// RETVAL_ZVAL(promise_stream->response_object, 0, 0);
// return SW_READY;
return SW_CONTINUE;
}
default: {
swoole_http2_frame_trace_log(recv, "");
swoole_http2_frame_trace_log("");
}
}

Expand Down Expand Up @@ -854,7 +854,7 @@ static PHP_METHOD(swoole_http2_client_coro, set) {
*/
bool Client::send_window_update(int stream_id, uint32_t size) {
char frame[SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_WINDOW_UPDATE_SIZE];
swoole_trace_log(SW_TRACE_HTTP2, "[" SW_ECHO_YELLOW "] stream_id=%d, size=%d", "WINDOW_UPDATE", stream_id, size);
swoole_http2_send_trace_log("[" SW_ECHO_YELLOW "] stream_id=%d, size=%d", "WINDOW_UPDATE", stream_id, size);
*(uint32_t *) ((char *) frame + SW_HTTP2_FRAME_HEADER_SIZE) = htonl(size);
Http2::set_frame_header(frame, SW_HTTP2_TYPE_WINDOW_UPDATE, SW_HTTP2_WINDOW_UPDATE_SIZE, 0, stream_id);
return send(frame, SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_WINDOW_UPDATE_SIZE);
Expand All @@ -866,7 +866,16 @@ bool Client::send_window_update(int stream_id, uint32_t size) {
bool Client::send_setting() {
char frame[SW_HTTP2_SETTING_FRAME_SIZE];
size_t n = Http2::pack_setting_frame(frame, local_settings, false);
swoole_trace_log(SW_TRACE_HTTP2, "[" SW_ECHO_GREEN "]\t[length=%lu]", Http2::get_type(SW_HTTP2_TYPE_SETTINGS), n);
swoole_http2_send_trace_log("[" SW_ECHO_MAGENTA
"] <header_table_size=%u, enable_push=%u, max_concurrent_streams=%u, "
"init_window_size=%u, max_frame_size=%u, max_header_list_size=%u>",
Http2::get_type(SW_HTTP2_TYPE_SETTINGS),
local_settings.header_table_size,
local_settings.enable_push,
local_settings.max_concurrent_streams,
local_settings.init_window_size,
local_settings.max_frame_size,
local_settings.max_header_list_size);
return send(frame, n);
}

Expand Down Expand Up @@ -1101,6 +1110,7 @@ Stream *Client::create_stream(uint32_t stream_id, uint8_t flags) {
bool Client::send_ping_frame() {
char frame[SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_FRAME_PING_PAYLOAD_SIZE];
Http2::set_frame_header(frame, SW_HTTP2_TYPE_PING, SW_HTTP2_FRAME_PING_PAYLOAD_SIZE, SW_HTTP2_FLAG_NONE, 0);
swoole_http2_send_trace_log("[" SW_ECHO_CYAN "]", "PING");
return send(frame, SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_FRAME_PING_PAYLOAD_SIZE);
}

Expand Down Expand Up @@ -1175,11 +1185,8 @@ uint32_t Client::send_request(zval *zrequest) {

Http2::set_frame_header(buffer, SW_HTTP2_TYPE_HEADERS, bytes, flags, stream->stream_id);

swoole_trace_log(SW_TRACE_HTTP2,
"[" SW_ECHO_GREEN ", STREAM#%d] length=%zd",
Http2::get_type(SW_HTTP2_TYPE_HEADERS),
stream->stream_id,
bytes);
swoole_http2_send_trace_log(
"[" SW_ECHO_GREEN ", STREAM#%d] length=%zd", Http2::get_type(SW_HTTP2_TYPE_HEADERS), stream->stream_id, bytes);
if (!send(buffer, SW_HTTP2_FRAME_HEADER_SIZE + bytes)) {
return 0;
}
Expand All @@ -1206,11 +1213,10 @@ uint32_t Client::send_request(zval *zrequest) {
len = str_zpost_data.len();
}

swoole_trace_log(SW_TRACE_HTTP2,
"[" SW_ECHO_GREEN ", END, STREAM#%d] length=%zu",
Http2::get_type(SW_HTTP2_TYPE_DATA),
stream->stream_id,
len);
swoole_http2_send_trace_log("[" SW_ECHO_GREEN ", END, STREAM#%d] length=%zu",
Http2::get_type(SW_HTTP2_TYPE_DATA),
stream->stream_id,
len);

if (!send_data(stream->stream_id, p, len, flag)) {
return 0;
Expand Down Expand Up @@ -1295,11 +1301,10 @@ bool Client::send_goaway_frame(zend_long error_code, const char *debug_data, siz
if (debug_data_len > 0) {
memcpy(frame + SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_GOAWAY_SIZE, debug_data, debug_data_len);
}
swoole_trace_log(SW_TRACE_HTTP2,
"[" SW_ECHO_GREEN "] Send: last-sid=%u, error-code=" ZEND_LONG_FMT,
Http2::get_type(SW_HTTP2_TYPE_GOAWAY),
last_stream_id,
error_code);
swoole_http2_send_trace_log("[" SW_ECHO_RED "] last-sid=%u, error-code=" ZEND_LONG_FMT,
Http2::get_type(SW_HTTP2_TYPE_GOAWAY),
last_stream_id,
error_code);
ret = send(frame, length);
efree(frame);
return ret;
Expand Down
19 changes: 9 additions & 10 deletions ext-src/swoole_http2_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1048,14 +1048,14 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
switch (type) {
case SW_HTTP2_TYPE_SETTINGS: {
if (flags & SW_HTTP2_FLAG_ACK) {
swoole_http2_frame_trace_log(recv, "ACK");
swoole_http2_frame_trace_log("ACK");
break;
}

while (length > 0) {
id = ntohs(*(uint16_t *) (buf));
value = ntohl(*(uint32_t *) (buf + sizeof(uint16_t)));
swoole_http2_frame_trace_log(recv, "id=%d, value=%d", id, value);
swoole_http2_frame_trace_log("id=%d, value=%d", id, value);
switch (id) {
case SW_HTTP2_SETTING_HEADER_TABLE_SIZE:
if (value != client->remote_settings.header_table_size) {
Expand Down Expand Up @@ -1100,7 +1100,7 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
}
case SW_HTTP2_TYPE_HEADERS: {
stream = client->streams[stream_id];
swoole_http2_frame_trace_log(recv, "%s", (stream ? "exist stream" : "new stream"));
swoole_http2_frame_trace_log("%s", (stream ? "exist stream" : "new stream"));
HttpContext *ctx;
if (!stream) {
stream = new Http2Stream(client, stream_id);
Expand Down Expand Up @@ -1128,7 +1128,7 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
break;
}
case SW_HTTP2_TYPE_DATA: {
swoole_http2_frame_trace_log(recv, "data");
swoole_http2_frame_trace_log("data");
auto stream_iterator = client->streams.find(stream_id);
if (stream_iterator == client->streams.end()) {
swoole_error_log(SW_LOG_WARNING, SW_ERROR_HTTP2_STREAM_NOT_FOUND, "http2 stream#%d not found", stream_id);
Expand Down Expand Up @@ -1186,7 +1186,7 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
break;
}
case SW_HTTP2_TYPE_PING: {
swoole_http2_frame_trace_log(recv, "ping");
swoole_http2_frame_trace_log("ping");
if (!(flags & SW_HTTP2_FLAG_ACK)) {
char ping_frame[SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_FRAME_PING_PAYLOAD_SIZE];
Http2::set_frame_header(
Expand All @@ -1213,12 +1213,12 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
}
}
}
swoole_http2_frame_trace_log(recv, "window_size_increment=%d", value);
swoole_http2_frame_trace_log("window_size_increment=%d", value);
break;
}
case SW_HTTP2_TYPE_RST_STREAM: {
value = ntohl(*(int *) (buf));
swoole_http2_frame_trace_log(recv, "error_code=%d", value);
swoole_http2_frame_trace_log("error_code=%d", value);
if (client->streams.find(stream_id) != client->streams.end()) {
// TODO: i onRequest and use request->recv
// stream exist
Expand All @@ -1233,8 +1233,7 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
buf += 4;
value = ntohl(*(uint32_t *) (buf));
buf += 4;
swoole_http2_frame_trace_log(recv,
"last_stream_id=%d, error_code=%d, opaque_data=[%.*s]",
swoole_http2_frame_trace_log("last_stream_id=%d, error_code=%d, opaque_data=[%.*s]",
server_last_stream_id,
value,
(int) (length - SW_HTTP2_GOAWAY_SIZE),
Expand All @@ -1245,7 +1244,7 @@ int swoole_http2_server_parse(Http2Session *client, const char *buf) {
break;
}
default: {
swoole_http2_frame_trace_log(recv, "");
swoole_http2_frame_trace_log("");
}
}

Expand Down
3 changes: 3 additions & 0 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ typedef unsigned long ulong_t;
#define SW_ECHO_LEN_CYAN "\e[36m%.*s\e[0m"
#define SW_ECHO_LEN_WHITE "\e[37m%.*s\e[0m"

#define SW_ECHO_RED_BG "\e[41m%s\e[0m"
#define SW_ECHO_GREEN_BG "\e[42m%s\e[0m"

#define SW_COLOR_RED 1
#define SW_COLOR_GREEN 2
#define SW_COLOR_YELLOW 3
Expand Down
19 changes: 11 additions & 8 deletions include/swoole_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
#define SW_SESSION_LIST_SIZE (1 * 1024 * 1024)

#define SW_MSGMAX 65536
#define SW_MESSAGE_BOX_SIZE 65536
#define SW_MESSAGE_BOX_SIZE 65536

#define SW_DGRAM_HEADER_SIZE 32

Expand Down Expand Up @@ -231,29 +231,32 @@
#define SW_HTTP_REQUEST_ENTITY_TOO_LARGE_PACKET "HTTP/1.1 413 Request Entity Too Large\r\n\r\n"
#define SW_HTTP_SERVICE_UNAVAILABLE_PACKET "HTTP/1.1 503 Service Unavailable\r\n\r\n"

#define SW_HTTP_PAGE_CSS "<style> \
#define SW_HTTP_PAGE_CSS \
"<style> \
body { padding: 0.5em; line-height: 2; } \
h1 { font-size: 1.5em; padding-bottom: 0.3em; border-bottom: 1px solid #ccc; } \
ul { list-style-type: disc; } \
footer { border-top: 1px solid #ccc; } \
a { color: #0969da; } \
</style>"

#define SW_HTTP_POWER_BY "<footer><i>Powered by Swoole</i></footer>"
#define SW_HTTP_POWER_BY "<footer><i>Powered by Swoole</i></footer>"

#define SW_HTTP_PAGE_400 "<html><body>" SW_HTTP_PAGE_CSS "<h1>HTTP 400 Bad Request</h1>" SW_HTTP_POWER_BY "</body></html>"
#define SW_HTTP_PAGE_400 \
"<html><body>" SW_HTTP_PAGE_CSS "<h1>HTTP 400 Bad Request</h1>" SW_HTTP_POWER_BY "</body></html>"
#define SW_HTTP_PAGE_404 "<html><body>" SW_HTTP_PAGE_CSS "<h1>HTTP 404 Not Found</h1>" SW_HTTP_POWER_BY "</body></html>"
#define SW_HTTP_PAGE_500 "<html><body>" SW_HTTP_PAGE_CSS "<h1>HTTP 500 Internal Server Error</h1>" SW_HTTP_POWER_BY "</body></html>"
#define SW_HTTP_PAGE_500 \
"<html><body>" SW_HTTP_PAGE_CSS "<h1>HTTP 500 Internal Server Error</h1>" SW_HTTP_POWER_BY "</body></html>"

/**
* HTTP2 Protocol
*/
#define SW_HTTP2_DATA_BUFFER_SIZE 8192
#define SW_HTTP2_DEFAULT_HEADER_TABLE_SIZE (1 << 12)
#define SW_HTTP2_DEFAULT_MAX_CONCURRENT_STREAMS 128
#define SW_HTTP2_DEFAULT_MAX_CONCURRENT_STREAMS UINT_MAX
#define SW_HTTP2_DEFAULT_ENABLE_PUSH 0
#define SW_HTTP2_DEFAULT_MAX_FRAME_SIZE ((1u << 14))
#define SW_HTTP2_DEFAULT_INIT_WINDOW_SIZE (1u << 24)
#define SW_HTTP2_DEFAULT_MAX_FRAME_SIZE (1u << 14)
#define SW_HTTP2_DEFAULT_INIT_WINDOW_SIZE ((1 << 16) - 1)
#define SW_HTTP2_DEFAULT_MAX_HEADER_LIST_SIZE UINT_MAX

#define SW_HTTP_CLIENT_USERAGENT "swoole-http-client"
Expand Down
14 changes: 10 additions & 4 deletions include/swoole_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,23 @@ enum swHttp2StreamFlag {
#define SW_HTTP2_STREAM_ID_SIZE 4
#define SW_HTTP2_SETTINGS_PARAM_SIZE 6

#define swoole_http2_frame_trace_log(_trace_fn, _trace_str, ...) \
#define swoole_http2_frame_trace_log(_trace_str, ...) \
swoole_trace_log(SW_TRACE_HTTP2, \
"%s [" SW_ECHO_GREEN "] frame" \
"<length=%jd, flags=(%s), stream_id=%d> " _trace_str, \
#_trace_fn, \
SW_ECHO_RED_BG " [" SW_ECHO_GREEN "] " \
"<length=%jd, flags=(%s), stream_id=%d> " _trace_str, \
" RECV ", \
swoole::http2::get_type(type), \
length, \
swoole::http2::get_flag_string(flags).c_str(), \
stream_id, \
##__VA_ARGS__)

#define swoole_http2_send_trace_log(_trace_str, ...) \
swoole_trace_log(SW_TRACE_HTTP2, SW_ECHO_GREEN_BG " " _trace_str, " SEND ", ##__VA_ARGS__)

#define swoole_http2_recv_trace_log(_trace_str, ...) \
swoole_trace_log(SW_TRACE_HTTP2, SW_ECHO_RED_BG " " _trace_str, " RECV ", ##__VA_ARGS__)

namespace swoole {
namespace http2 {

Expand Down

0 comments on commit f4dcd89

Please sign in to comment.