From e871c8ca1fb6eaf843fb541991cb60c17a30b680 Mon Sep 17 00:00:00 2001 From: Wandenberg Date: Wed, 23 Oct 2013 16:25:10 -0200 Subject: [PATCH] disable keepalive on websocket connections --- misc/spec/subscriber/websocket_spec.rb | 32 +++++++++++++++++++-- src/ngx_http_push_stream_module_websocket.c | 3 ++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/misc/spec/subscriber/websocket_spec.rb b/misc/spec/subscriber/websocket_spec.rb index dc13b749..e101de50 100644 --- a/misc/spec/subscriber/websocket_spec.rb +++ b/misc/spec/subscriber/websocket_spec.rb @@ -422,7 +422,7 @@ end it "should accept messages with different bytes" do - nginx_run_server(config.merge(:client_max_body_size => '130k', :client_body_buffer_size => '130k', :subscriber_connection_ttl => "1s", :message_template => "~text~|")) do |conf| + nginx_run_server(config.merge(:client_max_body_size => '130k', :client_body_buffer_size => '130k', :message_template => "~text~|")) do |conf| ranges = [0..255] ranges.each do |range| bytes = [] @@ -469,7 +469,35 @@ end end - it "should not try to parse the rewuest line when doing a reload" do + it "should not try to parse the request line when receive a frame after send close frame" do + channel = 'ch_test_data_after_close_frame_parse_request_line' + pid = pid2 = 0 + + frame = "%c%c%c%c%c%c" % [0x8A, 0x80, 0xBD, 0xD0, 0xE5, 0x2A] #send 'pong' frame + + request = "GET /ws/#{channel}.b1 HTTP/1.1\r\nHost: localhost\r\nConnection: Upgrade\r\nSec-WebSocket-Key: /mQoZf6pRiv8+6o72GncLQ==\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n" + + nginx_run_server(config.merge(:subscriber_connection_ttl => '1s')) do |conf| + File.open(conf.error_log, "a").truncate(0) + + socket = open_socket(nginx_host, nginx_port) + socket.print("#{request}\r\n") + headers, body = read_response_on_socket(socket) + + # wait for close frame + body, dummy = read_response_on_socket(socket, "\210\000") + body.should eql("\210\000") + + socket.print("WRITE SOMETHING UNKNOWN\r\n") + + sleep 1 + + error_log = File.read(conf.error_log) + error_log.should_not include("client sent invalid") + end + end + + it "should not try to parse the request line when doing a reload" do channel = 'ch_test_reload_not_parse_request_line' pid = pid2 = 0 diff --git a/src/ngx_http_push_stream_module_websocket.c b/src/ngx_http_push_stream_module_websocket.c index 438199d9..c3e3fca8 100644 --- a/src/ngx_http_push_stream_module_websocket.c +++ b/src/ngx_http_push_stream_module_websocket.c @@ -48,6 +48,9 @@ ngx_http_push_stream_websocket_handler(ngx_http_request_t *r) ngx_str_t *upgrade_header, *connection_header, *sec_key_header, *sec_version_header, *sec_accept_header; ngx_int_t version; + // WebSocket connections must not use keepalive + r->keepalive = 0; + // only accept GET method if (!(r->method & NGX_HTTP_GET)) { ngx_http_push_stream_add_response_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_ALLOW, &NGX_HTTP_PUSH_STREAM_ALLOW_GET);