static void ngx_http_push_stream_publisher_body_handler(ngx_http_request_t *r) { ngx_str_t *event_id, *event_type; ngx_http_push_stream_module_ctx_t *ctx = ngx_http_get_module_ctx(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); ngx_buf_t *buf = NULL; ngx_http_push_stream_channel_t *channel; ngx_http_push_stream_requested_channel_t *requested_channel; ngx_queue_t *cur = &ctx->requested_channels->queue; // check if body message wasn't empty if (r->headers_in.content_length_n <= 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: Post request was sent with no message"); ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_BAD_REQUEST, &NGX_HTTP_PUSH_STREAM_EMPTY_POST_REQUEST_MESSAGE); return; } // get and check if has access to request body NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(r->request_body->bufs, NULL, r, "push stream module: unexpected publisher message request body buffer location. please report this to the push stream module developers."); // copy request body to a memory buffer,将request body拷贝到一个buffer中 buf = ngx_http_push_stream_read_request_body_to_buffer(r); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(buf, NULL, r, "push stream module: cannot allocate memory for read the message"); event_id = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_EVENT_ID); event_type = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_EVENT_TYPE); while ((cur = ngx_queue_next(cur)) != &ctx->requested_channels->queue) { requested_channel = ngx_queue_data(cur, ngx_http_push_stream_requested_channel_t, queue); channel = ngx_http_push_stream_add_msg_to_channel(r, requested_channel->id, buf->pos, ngx_buf_size(buf), event_id, event_type, r->pool); if (channel == NULL) { ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; } } if (cf->channel_info_on_publish) { ngx_http_push_stream_send_response_channels_info_detailed(r, ctx->requested_channels); } else { ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_OK, NULL); } ngx_http_finalize_request(r, NGX_OK); return; }
static void ngx_http_push_stream_publisher_delete_handler(ngx_http_request_t *r) { ngx_http_push_stream_main_conf_t *mcf = ngx_http_get_module_main_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_buf_t *buf = NULL; u_char *text = mcf->channel_deleted_message_text.data; size_t len = mcf->channel_deleted_message_text.len; ngx_uint_t qtd_channels = 0; ngx_int_t rc; ngx_http_push_stream_requested_channel_t *requested_channel; ngx_queue_t *q; if (r->headers_in.content_length_n > 0) { // get and check if has access to request body NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(r->request_body->bufs, NULL, r, "push stream module: unexpected publisher message request body buffer location. please report this to the push stream module developers."); buf = ngx_http_push_stream_read_request_body_to_buffer(r); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(buf, NULL, r, "push stream module: cannot allocate memory for read the message"); text = buf->pos; len = ngx_buf_size(buf); } for (q = ngx_queue_head(&ctx->requested_channels->queue); q != ngx_queue_sentinel(&ctx->requested_channels->queue); q = ngx_queue_next(q)) { requested_channel = ngx_queue_data(q, ngx_http_push_stream_requested_channel_t, queue); rc = ngx_http_push_stream_delete_channel(mcf, requested_channel->channel, text, len, r->pool); if (rc == -1) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: error while deleting channel '%V'", &requested_channel->channel->id); ngx_http_push_stream_send_only_header_response_and_finalize(r, NGX_HTTP_INTERNAL_SERVER_ERROR, NULL); return; } if (rc > 0) { qtd_channels++; } } if (qtd_channels == 0) { ngx_http_push_stream_send_only_header_response_and_finalize(r, NGX_HTTP_NOT_FOUND, NULL); } else { ngx_http_push_stream_send_only_header_response_and_finalize(r, NGX_HTTP_OK, &NGX_HTTP_PUSH_STREAM_CHANNEL_DELETED); } }
static void ngx_http_push_stream_publisher_delete_handler(ngx_http_request_t *r) { ngx_http_push_stream_main_conf_t *mcf = ngx_http_get_module_main_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_buf_t *buf = NULL; u_char *text = mcf->channel_deleted_message_text.data; size_t len = mcf->channel_deleted_message_text.len; ngx_uint_t qtd_channels = 0; ngx_http_push_stream_requested_channel_t *requested_channel; ngx_queue_t *cur = &ctx->requested_channels->queue; if (r->headers_in.content_length_n > 0) { // get and check if has access to request body NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(r->request_body->bufs, NULL, r, "push stream module: unexpected publisher message request body buffer location. please report this to the push stream module developers."); buf = ngx_http_push_stream_read_request_body_to_buffer(r); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(buf, NULL, r, "push stream module: cannot allocate memory for read the message"); text = buf->pos; len = ngx_buf_size(buf); } while ((cur = ngx_queue_next(cur)) != &ctx->requested_channels->queue) { requested_channel = ngx_queue_data(cur, ngx_http_push_stream_requested_channel_t, queue); if (ngx_http_push_stream_delete_channel(mcf, requested_channel->id, text, len, r->pool)) { qtd_channels++; } } if (qtd_channels == 0) { ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_NOT_FOUND, NULL); } else { ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_OK, &NGX_HTTP_PUSH_STREAM_CHANNEL_DELETED); } }
static void ngx_http_push_stream_publisher_body_handler(ngx_http_request_t *r) { ngx_str_t *id; ngx_str_t *event_id, *event_type; ngx_http_push_stream_loc_conf_t *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module); ngx_buf_t *buf = NULL; ngx_chain_t *chain; ngx_http_push_stream_channel_t *channel; ssize_t n; off_t len; u_char *push_text_body=NULL; // check if body message wasn't empty if (r->headers_in.content_length_n <= 0) { ngx_log_error(NGX_LOG_ERR, (r)->connection->log, 0, "push stream module: Post request was sent with no message"); ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_BAD_REQUEST, &NGX_HTTP_PUSH_STREAM_EMPTY_POST_REQUEST_MESSAGE); return; } // get and check if has access to request body NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(r->request_body->bufs, NULL, r, "push stream module: unexpected publisher message request body buffer location. please report this to the push stream module developers."); // get and check channel id value id = ngx_http_push_stream_get_channel_id(r, cf); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(id, NULL, r, "push stream module: something goes very wrong, arrived on ngx_http_push_stream_publisher_body_handler without channel id"); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(id, NGX_HTTP_PUSH_STREAM_UNSET_CHANNEL_ID, r, "push stream module: something goes very wrong, arrived on ngx_http_push_stream_publisher_body_handler without channel id"); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(id, NGX_HTTP_PUSH_STREAM_TOO_LARGE_CHANNEL_ID, r, "push stream module: something goes very wrong, arrived on ngx_http_push_stream_publisher_body_handler with channel id too large"); // copy request body to a memory buffer buf = ngx_create_temp_buf(r->pool, r->headers_in.content_length_n + 1); NGX_HTTP_PUSH_STREAM_CHECK_AND_FINALIZE_REQUEST_ON_ERROR(buf, NULL, r, "push stream module: cannot allocate memory for read the message"); ngx_memset(buf->start, '\0', r->headers_in.content_length_n + 1); chain = r->request_body->bufs; while ((chain != NULL) && (chain->buf != NULL)) { len = ngx_buf_size(chain->buf); // if buffer is equal to content length all the content is in this buffer if (len >= r->headers_in.content_length_n) { buf->start = buf->pos; buf->last = buf->pos; len = r->headers_in.content_length_n; } if (chain->buf->in_file) { n = ngx_read_file(chain->buf->file, buf->start, len, 0); if (n == NGX_FILE_ERROR) { ngx_log_error(NGX_LOG_ERR, (r)->connection->log, 0, "push stream module: cannot read file with request body"); ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; } buf->last = buf->last + len; ngx_delete_file(chain->buf->file->name.data); chain->buf->file->fd = NGX_INVALID_FILE; } else { buf->last = ngx_copy(buf->start, chain->buf->pos, len); } chain = chain->next; buf->start = buf->last; } event_id = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_EVENT_ID); event_type = ngx_http_push_stream_get_header(r, &NGX_HTTP_PUSH_STREAM_HEADER_EVENT_TYPE); push_text_body = ngx_http_push_stream_str_replace(buf->pos, (u_char *)"\r\n", (u_char *)"", 0, r->pool); push_text_body = ngx_http_push_stream_str_replace(push_text_body, (u_char *)"\n", (u_char *)"", 0, r->pool); push_text_body = ngx_http_push_stream_str_replace(push_text_body, (u_char *)"\t", (u_char *)"", 0, r->pool); channel = ngx_http_push_stream_add_msg_to_channel(r, id, push_text_body, ngx_strlen(push_text_body), event_id, event_type, r->pool); //channel = ngx_http_push_stream_add_msg_to_channel(r, id, buf->pos, ngx_buf_size(buf), event_id, event_type, r->pool); if (channel == NULL) { ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); return; } if (cf->channel_info_on_publish) { ngx_http_push_stream_send_response_channel_info(r, channel); } else { ngx_http_push_stream_send_only_header_response(r, NGX_HTTP_OK, NULL); } ngx_http_finalize_request(r, NGX_OK); return; }