Commit ec18573c authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

refactor of publisher to put post handler in a different function to be reusable

parent 7777234d
...@@ -206,7 +206,7 @@ static const ngx_str_t NGX_HTTP_PUSH_STREAM_HEADER_ALLOW = ngx_string("Allow"); ...@@ -206,7 +206,7 @@ static const ngx_str_t NGX_HTTP_PUSH_STREAM_HEADER_ALLOW = ngx_string("Allow");
static const ngx_str_t NGX_HTTP_PUSH_STREAM_HEADER_EXPLAIN = ngx_string("X-Nginx-PushStream-Explain"); static const ngx_str_t NGX_HTTP_PUSH_STREAM_HEADER_EXPLAIN = ngx_string("X-Nginx-PushStream-Explain");
// other stuff // other stuff
static const ngx_str_t NGX_HTTP_PUSH_STREAM_ALLOWED_METHODS = ngx_string("GET, POST"); static const ngx_str_t NGX_HTTP_PUSH_STREAM_ALLOW_GET_POST_METHODS = ngx_string("GET, POST");
static const ngx_str_t NGX_HTTP_PUSH_STREAM_ALLOW_GET = ngx_string("GET"); static const ngx_str_t NGX_HTTP_PUSH_STREAM_ALLOW_GET = ngx_string("GET");
#define NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(val, fail, r, errormessage) \ #define NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(val, fail, r, errormessage) \
......
...@@ -25,10 +25,11 @@ ...@@ -25,10 +25,11 @@
#include <ngx_http_push_stream_module_publisher.h> #include <ngx_http_push_stream_module_publisher.h>
static ngx_int_t ngx_http_push_stream_publisher_handle_post(ngx_http_push_stream_loc_conf_t *cf, ngx_http_request_t *r, ngx_str_t *id);
static ngx_int_t static ngx_int_t
ngx_http_push_stream_publisher_handler(ngx_http_request_t *r) ngx_http_push_stream_publisher_handler(ngx_http_request_t *r)
{ {
ngx_int_t rc;
ngx_str_t *id = NULL; ngx_str_t *id = NULL;
ngx_http_push_stream_channel_t *channel = NULL; ngx_http_push_stream_channel_t *channel = NULL;
ngx_http_push_stream_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module); ngx_http_push_stream_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module);
...@@ -38,7 +39,7 @@ ngx_http_push_stream_publisher_handler(ngx_http_request_t *r) ...@@ -38,7 +39,7 @@ ngx_http_push_stream_publisher_handler(ngx_http_request_t *r)
// only accept GET and POST methods // only accept GET and POST methods
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_POST))) { if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_POST))) {
ngx_http_push_stream_add_response_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_ALLOW, &NGX_HTTP_PUSH_STREAM_ALLOWED_METHODS); ngx_http_push_stream_add_response_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_ALLOW, &NGX_HTTP_PUSH_STREAM_ALLOW_GET_POST_METHODS);
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_NOT_ALLOWED, NULL); return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_NOT_ALLOWED, NULL);
} }
...@@ -59,47 +60,56 @@ ngx_http_push_stream_publisher_handler(ngx_http_request_t *r) ...@@ -59,47 +60,56 @@ ngx_http_push_stream_publisher_handler(ngx_http_request_t *r)
channel = ngx_http_push_stream_find_channel(id, r->connection->log); channel = ngx_http_push_stream_find_channel(id, r->connection->log);
if (r->method == NGX_HTTP_POST) { if (r->method == NGX_HTTP_POST) {
// check if channel id isn't equals to ALL return ngx_http_push_stream_publisher_handle_post(cf, r, id);
if (ngx_memn2cmp(id->data, NGX_HTTP_PUSH_STREAM_ALL_CHANNELS_INFO_ID.data, id->len, NGX_HTTP_PUSH_STREAM_ALL_CHANNELS_INFO_ID.len) == 0) { }
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_FORBIDDEN, &NGX_HTTP_PUSH_STREAM_NO_CHANNEL_ID_NOT_AUTHORIZED_MESSAGE);
}
// create the channel if doesn't exist // GET only make sense with a previous existing channel
channel = ngx_http_push_stream_get_channel(id, r->connection->log, cf); if (channel == NULL) {
if (channel == NULL) { return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_NOT_FOUND, NULL);
ngx_log_error(NGX_LOG_ERR, (r)->connection->log, 0, "push stream module: unable to allocate memory for new channel"); }
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_INTERNAL_SERVER_ERROR, NULL);
}
if (channel == NGX_HTTP_PUSH_STREAM_NUMBER_OF_CHANNELS_EXCEEDED) { return ngx_http_push_stream_send_response_channel_info(r, channel);
ngx_log_error(NGX_LOG_ERR, (r)->connection->log, 0, "push stream module: number of channels were exceeded"); }
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_FORBIDDEN, &NGX_HTTP_PUSH_STREAM_NUMBER_OF_CHANNELS_EXCEEDED_MESSAGE);
}
/* static ngx_int_t
* Instruct ngx_http_read_subscriber_request_body to store the request ngx_http_push_stream_publisher_handle_post(ngx_http_push_stream_loc_conf_t *cf, ngx_http_request_t *r, ngx_str_t *id)
* body entirely in a memory buffer or in a file. {
*/ ngx_int_t rc;
r->request_body_in_single_buf = 0; ngx_http_push_stream_channel_t *channel = NULL;
r->request_body_in_persistent_file = 1;
r->request_body_in_clean_file = 0;
r->request_body_file_log_level = 0;
// parse the body message and return
rc = ngx_http_read_client_request_body(r, ngx_http_push_stream_publisher_body_handler);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE; // check if channel id isn't equals to ALL
if (ngx_memn2cmp(id->data, NGX_HTTP_PUSH_STREAM_ALL_CHANNELS_INFO_ID.data, id->len, NGX_HTTP_PUSH_STREAM_ALL_CHANNELS_INFO_ID.len) == 0) {
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_FORBIDDEN, &NGX_HTTP_PUSH_STREAM_NO_CHANNEL_ID_NOT_AUTHORIZED_MESSAGE);
} }
// GET only make sense with a previous existing channel // create the channel if doesn't exist
channel = ngx_http_push_stream_get_channel(id, r->connection->log, cf);
if (channel == NULL) { if (channel == NULL) {
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_NOT_FOUND, NULL); ngx_log_error(NGX_LOG_ERR, (r)->connection->log, 0, "push stream module: unable to allocate memory for new channel");
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_INTERNAL_SERVER_ERROR, NULL);
} }
return ngx_http_push_stream_send_response_channel_info(r, channel); if (channel == NGX_HTTP_PUSH_STREAM_NUMBER_OF_CHANNELS_EXCEEDED) {
ngx_log_error(NGX_LOG_ERR, (r)->connection->log, 0, "push stream module: number of channels were exceeded");
return ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_FORBIDDEN, &NGX_HTTP_PUSH_STREAM_NUMBER_OF_CHANNELS_EXCEEDED_MESSAGE);
}
/*
* Instruct ngx_http_read_subscriber_request_body to store the request
* body entirely in a memory buffer or in a file.
*/
r->request_body_in_single_buf = 0;
r->request_body_in_persistent_file = 1;
r->request_body_in_clean_file = 0;
r->request_body_file_log_level = 0;
// parse the body message and return
rc = ngx_http_read_client_request_body(r, ngx_http_push_stream_publisher_body_handler);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE;
} }
static void static void
......
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