Commit 99243a4b authored by Wandenberg's avatar Wandenberg

ensure that buffers content will not be changed and the allocated memory is clean

parent 8147b89e
...@@ -139,6 +139,7 @@ ngx_http_push_stream_send_response_channels_info(ngx_http_request_t *r, ngx_queu ...@@ -139,6 +139,7 @@ ngx_http_push_stream_send_response_channels_info(ngx_http_request_t *r, ngx_queu
chain->buf->last_buf = 0; chain->buf->last_buf = 0;
chain->buf->memory = 1; chain->buf->memory = 1;
chain->buf->temporary = 0;
chain->buf->pos = text->data; chain->buf->pos = text->data;
chain->buf->last = text->data + text->len; chain->buf->last = text->data + text->len;
chain->buf->start = chain->buf->pos; chain->buf->start = chain->buf->pos;
......
...@@ -158,6 +158,8 @@ ngx_http_push_stream_read_request_body_to_buffer(ngx_http_request_t *r) ...@@ -158,6 +158,8 @@ ngx_http_push_stream_read_request_body_to_buffer(ngx_http_request_t *r)
buf = ngx_create_temp_buf(r->pool, r->headers_in.content_length_n + 1); buf = ngx_create_temp_buf(r->pool, r->headers_in.content_length_n + 1);
if (buf != NULL) { if (buf != NULL) {
buf->memory = 1;
buf->temporary = 0;
ngx_memset(buf->start, '\0', r->headers_in.content_length_n + 1); ngx_memset(buf->start, '\0', r->headers_in.content_length_n + 1);
chain = r->request_body->bufs; chain = r->request_body->bufs;
......
...@@ -362,7 +362,7 @@ ngx_http_push_stream_postconfig(ngx_conf_t *cf) ...@@ -362,7 +362,7 @@ ngx_http_push_stream_postconfig(ngx_conf_t *cf)
{ {
if ((ngx_http_push_stream_padding_max_len > 0) && (ngx_http_push_stream_module_paddings_chunks == NULL)) { if ((ngx_http_push_stream_padding_max_len > 0) && (ngx_http_push_stream_module_paddings_chunks == NULL)) {
ngx_uint_t steps = ngx_http_push_stream_padding_max_len / 100; ngx_uint_t steps = ngx_http_push_stream_padding_max_len / 100;
if ((ngx_http_push_stream_module_paddings_chunks = ngx_palloc(cf->pool, sizeof(ngx_str_t) * (steps + 1))) == NULL) { if ((ngx_http_push_stream_module_paddings_chunks = ngx_pcalloc(cf->pool, sizeof(ngx_str_t) * (steps + 1))) == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to create padding messages"); ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to create padding messages");
return NGX_ERROR; return NGX_ERROR;
} }
...@@ -394,7 +394,7 @@ ngx_http_push_stream_postconfig(ngx_conf_t *cf) ...@@ -394,7 +394,7 @@ ngx_http_push_stream_postconfig(ngx_conf_t *cf)
if ((ngx_http_push_stream_padding_max_len > 0) && (ngx_http_push_stream_module_paddings_chunks_for_eventsource == NULL)) { if ((ngx_http_push_stream_padding_max_len > 0) && (ngx_http_push_stream_module_paddings_chunks_for_eventsource == NULL)) {
ngx_uint_t steps = ngx_http_push_stream_padding_max_len / 100; ngx_uint_t steps = ngx_http_push_stream_padding_max_len / 100;
if ((ngx_http_push_stream_module_paddings_chunks_for_eventsource = ngx_palloc(cf->pool, sizeof(ngx_str_t) * (steps + 1))) == NULL) { if ((ngx_http_push_stream_module_paddings_chunks_for_eventsource = ngx_pcalloc(cf->pool, sizeof(ngx_str_t) * (steps + 1))) == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to create padding messages for eventsource"); ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to create padding messages for eventsource");
return NGX_ERROR; return NGX_ERROR;
} }
......
...@@ -602,7 +602,7 @@ ngx_http_push_stream_create_channel_subscription(ngx_http_request_t *r, ngx_http ...@@ -602,7 +602,7 @@ ngx_http_push_stream_create_channel_subscription(ngx_http_request_t *r, ngx_http
{ {
ngx_http_push_stream_subscription_t *subscription; ngx_http_push_stream_subscription_t *subscription;
if ((subscription = ngx_palloc(r->pool, sizeof(ngx_http_push_stream_subscription_t))) == NULL) { if ((subscription = ngx_pcalloc(r->pool, sizeof(ngx_http_push_stream_subscription_t))) == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate subscribed channel reference"); ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate subscribed channel reference");
return NULL; return NULL;
} }
......
...@@ -748,6 +748,7 @@ ngx_http_push_stream_send_response_text(ngx_http_request_t *r, const u_char *tex ...@@ -748,6 +748,7 @@ ngx_http_push_stream_send_response_text(ngx_http_request_t *r, const u_char *tex
b->last_in_chain = 1; b->last_in_chain = 1;
b->flush = 1; b->flush = 1;
b->memory = 1; b->memory = 1;
b->temporary = 0;
b->pos = (u_char *) text; b->pos = (u_char *) text;
b->start = b->pos; b->start = b->pos;
b->end = b->pos + len; b->end = b->pos + len;
...@@ -1177,7 +1178,7 @@ ngx_http_push_stream_timer_set(ngx_msec_t timer_interval, ngx_event_t *event, ng ...@@ -1177,7 +1178,7 @@ ngx_http_push_stream_timer_set(ngx_msec_t timer_interval, ngx_event_t *event, ng
static void static void
ngx_http_push_stream_timer_reset(ngx_msec_t timer_interval, ngx_event_t *timer_event) ngx_http_push_stream_timer_reset(ngx_msec_t timer_interval, ngx_event_t *timer_event)
{ {
if (!ngx_exiting && (timer_interval != NGX_CONF_UNSET_MSEC)) { if (!ngx_exiting && (timer_interval != NGX_CONF_UNSET_MSEC) && (timer_event != NULL)) {
if (timer_event->timedout) { if (timer_event->timedout) {
ngx_time_update(); ngx_time_update();
} }
...@@ -1192,12 +1193,17 @@ ngx_http_push_stream_ping_timer_wake_handler(ngx_event_t *ev) ...@@ -1192,12 +1193,17 @@ ngx_http_push_stream_ping_timer_wake_handler(ngx_event_t *ev)
ngx_http_request_t *r = (ngx_http_request_t *) ev->data; ngx_http_request_t *r = (ngx_http_request_t *) ev->data;
ngx_http_push_stream_main_conf_t *mcf = ngx_http_get_module_main_conf(r, ngx_http_push_stream_module); ngx_http_push_stream_main_conf_t *mcf = ngx_http_get_module_main_conf(r, ngx_http_push_stream_module);
ngx_http_push_stream_loc_conf_t *pslcf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module); ngx_http_push_stream_loc_conf_t *pslcf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module);
ngx_http_push_stream_module_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_push_stream_module);
ngx_int_t rc = NGX_OK; ngx_int_t rc = NGX_OK;
if ((ctx == NULL) || (ctx->ping_timer == NULL)) {
return;
}
if (pslcf->location_type == NGX_HTTP_PUSH_STREAM_SUBSCRIBER_MODE_EVENTSOURCE) { if (pslcf->location_type == NGX_HTTP_PUSH_STREAM_SUBSCRIBER_MODE_EVENTSOURCE) {
rc = ngx_http_push_stream_send_response_text(r, NGX_HTTP_PUSH_STREAM_EVENTSOURCE_PING_MESSAGE_CHUNK.data, NGX_HTTP_PUSH_STREAM_EVENTSOURCE_PING_MESSAGE_CHUNK.len, 0); rc = ngx_http_push_stream_send_response_text(r, NGX_HTTP_PUSH_STREAM_EVENTSOURCE_PING_MESSAGE_CHUNK.data, NGX_HTTP_PUSH_STREAM_EVENTSOURCE_PING_MESSAGE_CHUNK.len, 0);
} else if (pslcf->location_type == NGX_HTTP_PUSH_STREAM_SUBSCRIBER_MODE_WEBSOCKET) { } else if (pslcf->location_type == NGX_HTTP_PUSH_STREAM_SUBSCRIBER_MODE_WEBSOCKET) {
rc = ngx_http_push_stream_send_response_text(r, NGX_HTTP_PUSH_STREAM_WEBSOCKET_PING_LAST_FRAME_BYTE, sizeof(NGX_HTTP_PUSH_STREAM_WEBSOCKET_PING_LAST_FRAME_BYTE), 1); rc = ngx_http_push_stream_send_response_text(r, NGX_HTTP_PUSH_STREAM_WEBSOCKET_PING_LAST_FRAME_BYTE, sizeof(NGX_HTTP_PUSH_STREAM_WEBSOCKET_PING_LAST_FRAME_BYTE), 0);
} else { } else {
if (mcf->ping_msg == NULL) { if (mcf->ping_msg == NULL) {
// create ping message // create ping message
...@@ -1214,7 +1220,6 @@ ngx_http_push_stream_ping_timer_wake_handler(ngx_event_t *ev) ...@@ -1214,7 +1220,6 @@ ngx_http_push_stream_ping_timer_wake_handler(ngx_event_t *ev)
if (rc != NGX_OK) { if (rc != NGX_OK) {
ngx_http_push_stream_send_response_finalize(r); ngx_http_push_stream_send_response_finalize(r);
} else { } else {
ngx_http_push_stream_module_ctx_t *ctx = ngx_http_get_module_ctx(r, ngx_http_push_stream_module);
ngx_http_push_stream_timer_reset(pslcf->ping_message_interval, ctx->ping_timer); ngx_http_push_stream_timer_reset(pslcf->ping_message_interval, ctx->ping_timer);
} }
} }
...@@ -1424,16 +1429,19 @@ ngx_http_push_stream_cleanup_request_context(ngx_http_request_t *r) ...@@ -1424,16 +1429,19 @@ ngx_http_push_stream_cleanup_request_context(ngx_http_request_t *r)
ngx_del_timer(ctx->ping_timer); ngx_del_timer(ctx->ping_timer);
} }
if (ctx->subscriber != NULL) {
ngx_http_push_stream_worker_subscriber_cleanup(ctx->subscriber);
}
if (ctx->temp_pool != NULL) { if (ctx->temp_pool != NULL) {
ngx_destroy_pool(ctx->temp_pool); ngx_destroy_pool(ctx->temp_pool);
ctx->temp_pool = NULL;
} }
if (ctx->subscriber != NULL) { ctx->temp_pool = NULL;
ngx_http_push_stream_worker_subscriber_cleanup(ctx->subscriber); ctx->disconnect_timer = NULL;
ctx->ping_timer = NULL;
ctx->subscriber = NULL; ctx->subscriber = NULL;
} }
}
} }
...@@ -1900,6 +1908,8 @@ ngx_http_push_stream_send_only_added_headers(ngx_http_request_t *r) ...@@ -1900,6 +1908,8 @@ ngx_http_push_stream_send_only_added_headers(ngx_http_request_t *r)
out.buf = b; out.buf = b;
out.next = NULL; out.next = NULL;
b->flush = 1; b->flush = 1;
b->memory = 1;
b->temporary = 0;
return ngx_http_write_filter(r, &out); return ngx_http_write_filter(r, &out);
} }
...@@ -1917,7 +1927,7 @@ ngx_http_push_stream_parse_paddings(ngx_conf_t *cf, ngx_str_t *paddings_by_user ...@@ -1917,7 +1927,7 @@ ngx_http_push_stream_parse_paddings(ngx_conf_t *cf, ngx_str_t *paddings_by_user
ngx_str_t aux, *agent; ngx_str_t aux, *agent;
if ((paddings = ngx_palloc(cf->pool, sizeof(ngx_queue_t))) == NULL) { if ((paddings = ngx_pcalloc(cf->pool, sizeof(ngx_queue_t))) == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to save padding info"); ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to save padding info");
return NULL; return NULL;
} }
...@@ -1976,7 +1986,7 @@ ngx_http_push_stream_parse_paddings(ngx_conf_t *cf, ngx_str_t *paddings_by_user ...@@ -1976,7 +1986,7 @@ ngx_http_push_stream_parse_paddings(ngx_conf_t *cf, ngx_str_t *paddings_by_user
return NULL; return NULL;
} }
if ((padding = ngx_palloc(cf->pool, sizeof(ngx_http_push_stream_padding_t))) == NULL) { if ((padding = ngx_pcalloc(cf->pool, sizeof(ngx_http_push_stream_padding_t))) == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to save padding info"); ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "push stream module: unable to allocate memory to save padding info");
return NULL; return NULL;
} }
......
...@@ -377,5 +377,6 @@ ngx_http_push_stream_set_buffer(ngx_buf_t *buf, u_char *start, u_char *last, ssi ...@@ -377,5 +377,6 @@ ngx_http_push_stream_set_buffer(ngx_buf_t *buf, u_char *start, u_char *last, ssi
buf->pos = buf->start; buf->pos = buf->start;
buf->last = (last != NULL) ? last : start; buf->last = (last != NULL) ? last : start;
buf->end = buf->start + len; buf->end = buf->start + len;
buf->temporary = 1; buf->temporary = 0;
buf->memory = 1;
} }
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