Commit 216cc09c authored by Wandenberg Peixoto's avatar Wandenberg Peixoto Committed by Wandenberg

fix to support gzip usage

parent 211edf51
h1(#changelog). Changelog
* Fix to support gzip usage
* Added the feature to send a custom 'channel delete message' on the body of the DELETE request
* Changed push_stream_channel_id variable to directive, and make possible set it inside an if block
* Changed push_stream_channels_path variable to directive, and make possible set it inside an if block
......
......@@ -5,6 +5,7 @@ describe "Channel Statistics" do
{}
end
shared_examples_for "statistics location" do
it "should return 404 for a nonexistent channel" do
channel = 'ch_test_get_channel_statistics_whithout_created_channel'
......@@ -22,16 +23,26 @@ describe "Channel Statistics" do
it "should return channels statistics for an existent channel" do
channel = 'ch_test_get_channel_statistics_to_existing_channel'
body = 'body'
actual_response = ''
nginx_run_server(config) do |conf|
#create channel
publish_message(channel, headers, body)
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :head => headers
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :head => headers, :decoding => false
pub_2.stream do |chunk|
actual_response << chunk
end
pub_2.callback do
pub_2.should be_http_status(200).with_body
response = JSON.parse(pub_2.response)
pub_2.should be_http_status(200)
if (conf.gzip == "on")
pub_2.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
end
response = JSON.parse(actual_response)
response["channel"].to_s.should eql(channel)
response["published_messages"].to_i.should eql(1)
response["stored_messages"].to_i.should eql(1)
......@@ -50,7 +61,7 @@ describe "Channel Statistics" do
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :head => headers
pub_1.callback do
pub_1.should be_http_status(200).with_body
pub_1.should be_http_status(200)
response = JSON.parse(pub_1.response)
response["channel"].to_s.should eql(channel)
response["published_messages"].to_i.should eql(0)
......@@ -67,7 +78,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(0)
EventMachine.stop
......@@ -79,16 +90,26 @@ describe "Channel Statistics" do
it "should return detailed channels statistics for an existent channel" do
channel = 'ch_test_get_detailed_channels_statistics_to_existing_channel'
body = 'body'
actual_response = ''
nginx_run_server(config) do |conf|
#create channel
publish_message(channel, headers, body)
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers, :decoding => false
pub_2.stream do |chunk|
actual_response << chunk
end
pub_2.callback do
pub_2.should be_http_status(200).with_body
response = JSON.parse(pub_2.response)
pub_2.should be_http_status(200)
if (conf.gzip == "on")
pub_2.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
end
response = JSON.parse(actual_response)
response["infos"].length.should eql(1)
response["infos"][0]["channel"].to_s.should eql(channel)
response["infos"][0]["published_messages"].to_i.should eql(1)
......@@ -111,7 +132,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(1)
response["channels"].to_i.should eql(0)
......@@ -134,7 +155,7 @@ describe "Channel Statistics" do
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_1.callback do
pub_1.should be_http_status(200).with_body
pub_1.should be_http_status(200)
response = JSON.parse(pub_1.response)
response["infos"].length.should eql(1)
response["infos"][0]["channel"].to_s.should eql(channel)
......@@ -152,7 +173,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_1.callback do
pub_1.should be_http_status(200).with_body
pub_1.should be_http_status(200)
response = JSON.parse(pub_1.response)
response.has_key?("channels").should be_true
response["channels"].to_i.should eql(0)
......@@ -165,16 +186,26 @@ describe "Channel Statistics" do
it "should return summarized channels statistics for an existent channel" do
channel = 'ch_test_get_summarized_channels_statistics_to_existing_channel'
body = 'body'
actual_response = ''
nginx_run_server(config) do |conf|
#create channel
publish_message(channel, headers, body)
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers, :decoding => false
pub_2.stream do |chunk|
actual_response << chunk
end
pub_2.callback do
pub_2.should be_http_status(200).with_body
response = JSON.parse(pub_2.response)
pub_2.should be_http_status(200)
if (conf.gzip == "on")
pub_2.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
end
response = JSON.parse(actual_response)
response.has_key?("channels").should be_true
response["channels"].to_i.should eql(1)
response["published_messages"].to_i.should eql(1)
......@@ -196,7 +227,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response.has_key?("channels").should be_true
response["channels"].to_i.should eql(0)
......@@ -217,7 +248,7 @@ describe "Channel Statistics" do
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_1.callback do
pub_1.should be_http_status(200).with_body
pub_1.should be_http_status(200)
response = JSON.parse(pub_1.response)
response.has_key?("channels").should be_true
response["channels"].to_i.should eql(1)
......@@ -325,7 +356,7 @@ describe "Channel Statistics" do
body = 'body'
number_of_channels = 20000
nginx_run_server(config.merge(:shared_memory_size => '200m', :keepalive => "on"), :timeout => 10) do |conf|
nginx_run_server(config.merge(:shared_memory_size => '200m', :keepalive => "on"), :timeout => 15) do |conf|
#create channels
0.step(number_of_channels - 1, 1000) do |i|
socket = open_socket(nginx_host, nginx_port)
......@@ -339,7 +370,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(number_of_channels)
EventMachine.stop
......@@ -353,7 +384,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=prefix_*').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(0)
EventMachine.stop
......@@ -375,7 +406,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ch_test_*').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(1)
response["infos"][0]["channel"].to_s.should eql(channel)
......@@ -401,7 +432,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=*').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(2)
response["infos"][0]["channel"].to_s.should eql(channel)
......@@ -429,7 +460,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=bd_test_*').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(1)
response["channels"].to_i.should eql(0)
......@@ -452,7 +483,7 @@ describe "Channel Statistics" do
create_channel_by_subscribe(channel, headers) do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ch_test_*').get :head => headers
pub_1.callback do
pub_1.should be_http_status(200).with_body
pub_1.should be_http_status(200)
response = JSON.parse(pub_1.response)
response["infos"].length.should eql(1)
response["infos"][0]["channel"].to_s.should eql(channel)
......@@ -470,7 +501,7 @@ describe "Channel Statistics" do
body = 'body'
number_of_channels = 20000
nginx_run_server(config.merge(:shared_memory_size => '200m', :keepalive => "on"), :timeout => 10) do |conf|
nginx_run_server(config.merge(:shared_memory_size => '200m', :keepalive => "on"), :timeout => 15) do |conf|
#create channels
0.step(number_of_channels - 1, 1000) do |i|
socket = open_socket(nginx_host, nginx_port)
......@@ -484,7 +515,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ch_test_get_detailed_channels_statistics_to_many_channels_using_prefix_10*').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["infos"].length.should eql(1111)
EventMachine.stop
......@@ -504,7 +535,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["hostname"].to_s.should_not be_empty
response["time"].to_s.should_not be_empty
......@@ -516,7 +547,7 @@ describe "Channel Statistics" do
sleep(2)
pub_3 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=ALL').get :head => headers
pub_3.callback do
pub_3.should be_http_status(200).with_body
pub_3.should be_http_status(200)
response = JSON.parse(pub_3.response)
response["uptime"].to_i.should be_in_the_interval(2, 3)
EventMachine.stop
......@@ -537,7 +568,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["hostname"].to_s.should_not be_empty
response["time"].to_s.should_not be_empty
......@@ -553,7 +584,7 @@ describe "Channel Statistics" do
sleep(2)
pub_3 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_3.callback do
pub_3.should be_http_status(200).with_body
pub_3.should be_http_status(200)
response = JSON.parse(pub_3.response)
response["uptime"].to_i.should be_in_the_interval(2, 3)
response["by_worker"][0]["uptime"].to_i.should be_in_the_interval(2, 3)
......@@ -575,7 +606,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["stored_messages"].to_i.should eql(1)
response["messages_in_trash"].to_i.should eql(0)
......@@ -583,7 +614,7 @@ describe "Channel Statistics" do
sleep(5)
pub_3 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_3.callback do
pub_3.should be_http_status(200).with_body
pub_3.should be_http_status(200)
response = JSON.parse(pub_3.response)
response["stored_messages"].to_i.should eql(0)
response["messages_in_trash"].to_i.should eql(1)
......@@ -607,7 +638,7 @@ describe "Channel Statistics" do
EventMachine.run do
pub_2 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_2.callback do
pub_2.should be_http_status(200).with_body
pub_2.should be_http_status(200)
response = JSON.parse(pub_2.response)
response["channels"].to_i.should eql(2)
response["broadcast_channels"].to_i.should eql(1)
......@@ -621,7 +652,7 @@ describe "Channel Statistics" do
pub_3 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats').get :head => headers
pub_3.callback do
pub_3.should be_http_status(200).with_body
pub_3.should be_http_status(200)
response = JSON.parse(pub_3.response)
response["channels"].to_i.should eql(1)
response["broadcast_channels"].to_i.should eql(1)
......@@ -649,3 +680,30 @@ describe "Channel Statistics" do
end
end
end
context "when getting statistics" do
context "without gzip" do
let(:config) do
{:gzip => "off"}
end
let(:headers) do
{'accept' => 'application/json'}
end
it_should_behave_like "statistics location"
end
context "with gzip" do
let(:config) do
{:gzip => "on"}
end
let(:headers) do
{'accept' => 'application/json', 'accept-encoding' => 'gzip, compressed'}
end
it_should_behave_like "statistics location"
end
end
end
......@@ -5,7 +5,9 @@ module NginxConfiguration
:master_process => 'on',
:daemon => 'on',
:content_type => 'text/html; charset=utf-8',
:gzip => 'off',
:content_type => 'text/html',
:keepalive => 'off',
:ping_message_interval => '10s',
......@@ -83,6 +85,14 @@ http {
access_log <%= access_log %>;
gzip <%= gzip %>;
gzip_buffers 16 4k;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;
gzip_comp_level 9;
gzip_http_version 1.0;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 100;
......
......@@ -454,6 +454,59 @@ describe "Publisher Properties" do
end
end
end
it "should accept respond get requests with gzip" do
channel = 'test_receive_get_response_with_gzip'
body = 'body'
actual_response = ''
nginx_run_server(config.merge(:gzip => "on"), :timeout => 5) do |conf|
EventMachine.run do
#create channel
publish_message_inline(channel, {}, body)
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel).get :head => headers.merge({'accept' => 'application/json', 'accept-encoding' => 'gzip, compressed'}), :decoding => false
pub.stream do |chunk|
actual_response << chunk
end
pub.callback do
pub.response_header.status.should eql(200)
pub.response_header.content_length.should_not eql(0)
pub.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
response = JSON.parse(actual_response)
response["channel"].to_s.should eql(channel)
EventMachine.stop
end
end
end
end
it "should accept respond post requests with gzip" do
channel = 'test_receive_post_response_with_gzip'
body = 'body'
actual_response = ''
nginx_run_server(config.merge(:gzip => "on"), :timeout => 5) do |conf|
EventMachine.run do
pub = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel).post :body => body, :head => headers.merge({'accept' => 'application/json', 'accept-encoding' => 'gzip, compressed'}), :decoding => false
pub.stream do |chunk|
actual_response << chunk
end
pub.callback do
pub.response_header.status.should eql(200)
pub.response_header.content_length.should_not eql(0)
pub.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
response = JSON.parse(actual_response)
response["channel"].to_s.should eql(channel)
EventMachine.stop
end
end
end
end
end
context "when is on normal mode" do
......
......@@ -41,7 +41,6 @@ end
def publish_message(channel, headers, body)
EventMachine.run do
pub = publish_message_inline(channel, headers, body) do
pub.should be_http_status(200).with_body
response = JSON.parse(pub.response)
response["channel"].to_s.should eql(channel)
EventMachine.stop
......@@ -51,7 +50,7 @@ end
def create_channel_by_subscribe(channel, headers, timeout=60, &block)
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers, :timeout => timeout
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s, :connect_timeout => timeout, :inactivity_timeout => timeout).get :head => headers.merge({"accept-encoding" => ""})
sub_1.stream do |chunk|
block.call
end
......
......@@ -422,6 +422,32 @@ describe "Subscriber Properties" do
end
end
end
it "should accpet return content gzipped" do
channel = 'ch_test_get_content_gzipped'
body = 'body'
actual_response = ''
nginx_run_server(config.merge({:gzip => "on"})) do |conf|
EventMachine.run do
sent_headers = headers.merge({'accept-encoding' => 'gzip, compressed'})
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => sent_headers, :decoding => false
sub_1.stream do |chunk|
actual_response << chunk
end
sub_1.callback do
sub_1.should be_http_status(200)
sub_1.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
actual_response.should eql("#{body}\r\n")
EventMachine.stop
end
publish_message_inline(channel, {}, body)
end
end
end
end
context "when using subscriber push mode config" do
......
......@@ -339,6 +339,32 @@ describe "Subscriber Properties" do
end
end
it "should accpet return content gzipped" do
channel = 'ch_test_get_content_gzipped'
body = 'body'
actual_response = ''
nginx_run_server(config.merge({:gzip => "on"})) do |conf|
EventMachine.run do
publish_message_inline(channel, {}, body)
sent_headers = headers.merge({'accept-encoding' => 'gzip, compressed'})
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => sent_headers, :decoding => false
sub_1.stream do |chunk|
actual_response << chunk
end
sub_1.callback do
sub_1.should be_http_status(200)
sub_1.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
actual_response.should eql("#{body}\r\n")
EventMachine.stop
end
end
end
end
end
it "should not cache the response" do
......
......@@ -973,4 +973,30 @@ describe "Subscriber Properties" do
end
end
end
it "should accpet return content gzipped" do
channel = 'ch_test_get_content_gzipped'
body = 'body'
actual_response = ''
nginx_run_server(config.merge({:gzip => "on", :subscriber_connection_ttl => '1s', :content_type => "text/html"})) do |conf|
EventMachine.run do
sent_headers = headers.merge({'accept-encoding' => 'gzip, compressed'})
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => sent_headers, :decoding => false
sub_1.stream do |chunk|
actual_response << chunk
end
sub_1.callback do
sub_1.should be_http_status(200)
sub_1.response_header["CONTENT_ENCODING"].should eql("gzip")
actual_response = Zlib::GzipReader.new(StringIO.new(actual_response)).read
actual_response.should eql("HEADER\r\nTEMPLATE\r\n1234\r\n\r\n<script>p(1,'ch_test_get_content_gzipped','body');</script>\r\n</body></html>\r\n")
EventMachine.stop
end
publish_message_inline(channel, {}, body)
end
end
end
end
......@@ -265,6 +265,7 @@ ngx_http_push_stream_send_response_all_channels_info_detailed(ngx_http_request_t
r->headers_out.content_type.len = subtype->content_type->len;
r->headers_out.content_type.data = subtype->content_type->data;
r->headers_out.content_type_len = subtype->content_type->len;
r->headers_out.content_length_n = content_len;
r->headers_out.status = NGX_HTTP_OK;
......
......@@ -489,6 +489,7 @@ ngx_http_push_stream_subscriber_prepare_request_to_keep_connected(ngx_http_reque
r->write_event_handler = ngx_http_request_empty_handler;
r->headers_out.content_type = cf->content_type;
r->headers_out.content_type_len = cf->content_type.len;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = -1;
......
......@@ -535,6 +535,7 @@ ngx_http_push_stream_send_response(ngx_http_request_t *r, ngx_str_t *text, const
r->headers_out.content_type.len = content_type->len;
r->headers_out.content_type.data = content_type->data;
r->headers_out.content_type_len = content_type->len;
r->headers_out.content_length_n = text->len;
r->headers_out.status = status_code;
......@@ -1436,6 +1437,7 @@ ngx_http_push_stream_add_polling_headers(ngx_http_request_t *r, time_t last_modi
ngx_str_t content_type = (ctx->callback != NULL) ? NGX_HTTP_PUSH_STREAM_CALLBACK_CONTENT_TYPE : cf->content_type;
r->headers_out.content_type = content_type;
r->headers_out.content_type_len = content_type.len;
if (last_modified_time > 0) {
r->headers_out.last_modified_time = last_modified_time;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment