Commit 7a676397 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

set expires headers to avoid cache

parent 92b9aaff
......@@ -349,4 +349,19 @@ static const ngx_str_t NGX_HTTP_PUSH_STREAM_ALLOWED_HEADERS = ngx_string("If-Mo
#define NGX_HTTP_PUSH_STREAM_TIME_FMT_LEN 30 //sizeof("Mon, 28 Sep 1970 06:00:00 GMT")
/**
* borrowed from Nginx core files
*/
typedef enum {
NGX_HTTP_PUSH_STREAM_EXPIRES_OFF,
NGX_HTTP_PUSH_STREAM_EXPIRES_EPOCH,
NGX_HTTP_PUSH_STREAM_EXPIRES_MAX,
NGX_HTTP_PUSH_STREAM_EXPIRES_ACCESS,
NGX_HTTP_PUSH_STREAM_EXPIRES_MODIFIED,
NGX_HTTP_PUSH_STREAM_EXPIRES_DAILY,
NGX_HTTP_PUSH_STREAM_EXPIRES_UNSET
} ngx_http_push_stream_expires_t;
#endif /* NGX_HTTP_PUSH_STREAM_MODULE_H_ */
......@@ -292,4 +292,7 @@ static ngx_str_t * ngx_http_push_stream_get_formatted_hostname(ngx_pool
uint64_t ngx_http_push_stream_htonll(uint64_t value);
uint64_t ngx_http_push_stream_ntohll(uint64_t value);
static ngx_int_t ngx_http_push_stream_set_expires(ngx_http_request_t *r, ngx_http_push_stream_expires_t expires, time_t expires_time);
#endif /* NGX_HTTP_PUSH_STREAM_MODULE_UTILS_H_ */
......@@ -633,4 +633,19 @@ describe "Channel Statistics" do
end
end
end
it "should not cache the response" do
channel = 'ch_test_not_cache_the_response'
nginx_run_server(config) do |conf|
EventMachine.run do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/channels-stats?id=' + channel.to_s).get :head => headers
pub_1.callback do
pub_1.response_header["EXPIRES"].should eql("Thu, 01 Jan 1970 00:00:01 GMT")
pub_1.response_header["CACHE_CONTROL"].should eql("no-cache, no-store, must-revalidate")
EventMachine.stop
end
end
end
end
end
......@@ -388,6 +388,21 @@ describe "Publisher Properties" do
end
end
end
it "should not cache the response" do
channel = 'ch_test_not_cache_the_response'
nginx_run_server(config) do |conf|
EventMachine.run do
pub_1 = EventMachine::HttpRequest.new(nginx_address + '/pub?id=' + channel.to_s).get :head => headers
pub_1.callback do
pub_1.response_header["EXPIRES"].should eql("Thu, 01 Jan 1970 00:00:01 GMT")
pub_1.response_header["CACHE_CONTROL"].should eql("no-cache, no-store, must-revalidate")
EventMachine.stop
end
end
end
end
end
context "when is on normal mode" do
......
......@@ -407,6 +407,21 @@ describe "Subscriber Properties" do
end
end
end
it "should not cache the response" do
channel = 'ch_test_not_cache_the_response'
nginx_run_server(config.merge(:longpolling_connection_ttl => '1s')) do |conf|
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub_1.callback do
sub_1.response_header["EXPIRES"].should eql("Thu, 01 Jan 1970 00:00:01 GMT")
sub_1.response_header["CACHE_CONTROL"].should eql("no-cache, no-store, must-revalidate")
EventMachine.stop
end
end
end
end
end
context "when using subscriber push mode config" do
......
......@@ -340,6 +340,21 @@ describe "Subscriber Properties" do
end
end
it "should not cache the response" do
channel = 'ch_test_not_cache_the_response'
nginx_run_server(config) do |conf|
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub_1.callback do
sub_1.response_header["EXPIRES"].should eql("Thu, 01 Jan 1970 00:00:01 GMT")
sub_1.response_header["CACHE_CONTROL"].should eql("no-cache, no-store, must-revalidate")
EventMachine.stop
end
end
end
end
end
context "when using subscriber push mode config" do
......
......@@ -907,4 +907,19 @@ describe "Subscriber Properties" do
end
end
end
it "should not cache the response" do
channel = 'ch_test_not_cache_the_response'
nginx_run_server(config.merge(:subscriber_connection_ttl => '1s')) do |conf|
EventMachine.run do
sub_1 = EventMachine::HttpRequest.new(nginx_address + '/sub/' + channel.to_s).get :head => headers
sub_1.callback do
sub_1.response_header["EXPIRES"].should eql("Thu, 01 Jan 1970 00:00:01 GMT")
sub_1.response_header["CACHE_CONTROL"].should eql("no-cache, no-store, must-revalidate")
EventMachine.stop
end
end
end
end
end
......@@ -394,4 +394,19 @@ describe "Subscriber WebSocket" do
end
end
end
it "should not cache the response" do
channel = 'ch_test_not_cache_the_response'
request = "GET /ws/#{channel}.b1 HTTP/1.0\r\nConnection: Upgrade\r\nSec-WebSocket-Key: /mQoZf6pRiv8+6o72GncLQ==\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n"
nginx_run_server(config) do |conf|
socket = open_socket(nginx_host, nginx_port)
socket.print("#{request}\r\n")
headers, body = read_response_on_socket(socket)
headers.should include("Expires: Thu, 01 Jan 1970 00:00:01 GMT\r\n")
headers.should include("Cache-Control: no-cache, no-store, must-revalidate\r\n")
end
end
end
......@@ -36,6 +36,7 @@ ngx_http_push_stream_publisher_handler(ngx_http_request_t *r)
ngx_http_push_stream_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module);
r->keepalive = cf->keepalive;
ngx_http_push_stream_set_expires(r, NGX_HTTP_PUSH_STREAM_EXPIRES_EPOCH, 0);
if (cf->allowed_origins.len > 0) {
ngx_http_push_stream_add_response_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, &cf->allowed_origins);
......@@ -216,6 +217,7 @@ ngx_http_push_stream_channels_statistics_handler(ngx_http_request_t *r)
ngx_http_push_stream_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module);
r->keepalive = cf->keepalive;
ngx_http_push_stream_set_expires(r, NGX_HTTP_PUSH_STREAM_EXPIRES_EPOCH, 0);
// only accept GET method
if (!(r->method & NGX_HTTP_GET)) {
......
......@@ -63,6 +63,8 @@ ngx_http_push_stream_subscriber_handler(ngx_http_request_t *r)
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_OK, NULL);
}
ngx_http_push_stream_set_expires(r, NGX_HTTP_PUSH_STREAM_EXPIRES_EPOCH, 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);
......
......@@ -1704,3 +1704,119 @@ ngx_http_push_stream_unescape_uri(ngx_str_t *value)
}
}
}
/**
* borrowed from Nginx core files
*/
static ngx_int_t
ngx_http_push_stream_set_expires(ngx_http_request_t *r, ngx_http_push_stream_expires_t expires, time_t expires_time)
{
size_t len;
time_t now, expires_header_time, max_age;
ngx_uint_t i;
ngx_table_elt_t *expires_header, *cc, **ccp;
expires_header = r->headers_out.expires;
if (expires_header == NULL) {
expires_header = ngx_list_push(&r->headers_out.headers);
if (expires_header == NULL) {
return NGX_ERROR;
}
r->headers_out.expires = expires_header;
expires_header->hash = 1;
ngx_str_set(&expires_header->key, "Expires");
}
len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT");
expires_header->value.len = len - 1;
ccp = r->headers_out.cache_control.elts;
if (ccp == NULL) {
if (ngx_array_init(&r->headers_out.cache_control, r->pool, 1, sizeof(ngx_table_elt_t *)) != NGX_OK) {
return NGX_ERROR;
}
ccp = ngx_array_push(&r->headers_out.cache_control);
if (ccp == NULL) {
return NGX_ERROR;
}
cc = ngx_list_push(&r->headers_out.headers);
if (cc == NULL) {
return NGX_ERROR;
}
cc->hash = 1;
ngx_str_set(&cc->key, "Cache-Control");
*ccp = cc;
} else {
for (i = 1; i < r->headers_out.cache_control.nelts; i++) {
ccp[i]->hash = 0;
}
cc = ccp[0];
}
if (expires == NGX_HTTP_PUSH_STREAM_EXPIRES_EPOCH) {
expires_header->value.data = (u_char *) "Thu, 01 Jan 1970 00:00:01 GMT";
ngx_str_set(&cc->value, "no-cache, no-store, must-revalidate");
return NGX_OK;
}
if (expires == NGX_HTTP_PUSH_STREAM_EXPIRES_MAX) {
expires_header->value.data = (u_char *) "Thu, 31 Dec 2037 23:55:55 GMT";
/* 10 years */
ngx_str_set(&cc->value, "max-age=315360000");
return NGX_OK;
}
expires_header->value.data = ngx_pnalloc(r->pool, len);
if (expires_header->value.data == NULL) {
return NGX_ERROR;
}
if (expires_time == 0 && expires != NGX_HTTP_PUSH_STREAM_EXPIRES_DAILY) {
ngx_memcpy(expires_header->value.data, ngx_cached_http_time.data, ngx_cached_http_time.len + 1);
ngx_str_set(&cc->value, "max-age=0");
return NGX_OK;
}
now = ngx_time();
if (expires == NGX_HTTP_PUSH_STREAM_EXPIRES_DAILY) {
expires_header_time = ngx_next_time(expires_time);
max_age = expires_header_time - now;
} else if (expires == NGX_HTTP_PUSH_STREAM_EXPIRES_ACCESS || r->headers_out.last_modified_time == -1) {
expires_header_time = now + expires_time;
max_age = expires_time;
} else {
expires_header_time = r->headers_out.last_modified_time + expires_time;
max_age = expires_header_time - now;
}
ngx_http_time(expires_header->value.data, expires_header_time);
if (expires_time < 0 || max_age < 0) {
ngx_str_set(&cc->value, "no-cache, no-store, must-revalidate");
return NGX_OK;
}
cc->value.data = ngx_pnalloc(r->pool, sizeof("max-age=") + NGX_TIME_T_LEN + 1);
if (cc->value.data == NULL) {
return NGX_ERROR;
}
cc->value.len = ngx_sprintf(cc->value.data, "max-age=%T", max_age) - cc->value.data;
return NGX_OK;
}
......@@ -52,6 +52,8 @@ ngx_http_push_stream_websocket_handler(ngx_http_request_t *r)
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_NOT_ALLOWED, NULL);
}
ngx_http_push_stream_set_expires(r, NGX_HTTP_PUSH_STREAM_EXPIRES_EPOCH, 0);
upgrade_header = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_UPGRADE);
connection_header = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_CONNECTION);
sec_key_header = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_SEC_WEBSOCKET_KEY);
......
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