static ngx_int_t rawstream_respond_message(subscriber_t *sub, nchan_msg_t *msg) { full_subscriber_t *fsub = (full_subscriber_t *)sub; ngx_buf_t *buf, *msg_buf = msg->buf; ngx_int_t rc; nchan_loc_conf_t *cf = ngx_http_get_module_loc_conf(fsub->sub.request, ngx_nchan_module); nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(fsub->sub.request, ngx_nchan_module); nchan_buf_and_chain_t *bc; ngx_chain_t *chain; ngx_file_t *file_copy; size_t separator_len = cf->subscriber_http_raw_stream_separator.len; size_t msg_len = ngx_buf_size((msg->buf)); if(fsub->data.timeout_ev.timer_set) { ngx_del_timer(&fsub->data.timeout_ev); ngx_add_timer(&fsub->data.timeout_ev, sub->cf->subscriber_timeout * 1000); } if(msg_len + separator_len == 0) { //nothing to output return NGX_OK; } if((bc = nchan_bufchain_pool_reserve(ctx->bcp, (1 + (msg_len > 0 ? 1: 0)))) == NULL) { ERR("cant allocate buf-and-chains for http-raw-stream client output"); return NGX_ERROR; } chain = &bc->chain; //message if(msg_len > 0) { buf = chain->buf; *buf = *msg_buf; if(buf->file) { file_copy = nchan_bufchain_pool_reserve_file(ctx->bcp); nchan_msg_buf_open_fd_if_needed(buf, file_copy, NULL); } buf->last_buf = 0; buf->last_in_chain = 0; buf->flush = 0; chain = chain->next; } //separator buf = chain->buf; ngx_memzero(buf, sizeof(ngx_buf_t)); buf->start = cf->subscriber_http_raw_stream_separator.data; buf->pos = buf->start; buf->end = buf->start + separator_len; buf->last = buf->end; buf->memory = 1; buf->last_buf = 0; buf->last_in_chain = 1; buf->flush = 1; rawstream_ensure_headers_sent(fsub); DBG("%p output msg to subscriber", sub); rc = nchan_output_msg_filter(fsub->sub.request, msg, &bc->chain); return rc; }
static ngx_int_t multipart_respond_message(subscriber_t *sub, nchan_msg_t *msg) { full_subscriber_t *fsub = (full_subscriber_t *)sub; ngx_buf_t *buf, *msg_buf = msg->buf, *msgid_buf; ngx_int_t rc; nchan_loc_conf_t *cf = ngx_http_get_module_loc_conf(fsub->sub.request, nchan_module); nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(fsub->sub.request, nchan_module); ngx_int_t n; nchan_buf_and_chain_t *bc; ngx_chain_t *chain; ngx_file_t *file_copy; multipart_privdata_t *mpd = (multipart_privdata_t *)fsub->privdata; headerbuf_t *headerbuf = nchan_reuse_queue_push(ctx->output_str_queue); u_char *cur = headerbuf->charbuf; if(fsub->data.timeout_ev.timer_set) { ngx_del_timer(&fsub->data.timeout_ev); ngx_add_timer(&fsub->data.timeout_ev, sub->cf->subscriber_timeout * 1000); } //generate the headers if(!cf->msg_in_etag_only) { //msgtime cur = ngx_cpymem(cur, "\r\nLast-Modified: ", sizeof("\r\nLast-Modified: ") - 1); cur = ngx_http_time(cur, msg->id.time); *cur++ = CR; *cur++ = LF; //msgtag cur = ngx_cpymem(cur, "Etag: ", sizeof("Etag: ") - 1); cur += msgtag_to_strptr(&msg->id, (char *)cur); *cur++ = CR; *cur++ = LF; } else { ngx_str_t *tmp_etag = msgid_to_str(&msg->id); cur = ngx_snprintf(cur, 58 + 10*NCHAN_FIXED_MULTITAG_MAX, "\r\nEtag: %V\r\n", tmp_etag); } n=4; if(msg->content_type.len == 0) { //don't need content_type buf'n'chain n--; } if(ngx_buf_size(msg_buf) == 0) { //don't need msgbuf n --; } if((bc = nchan_bufchain_pool_reserve(ctx->bcp, n)) == NULL) { ERR("cant allocate buf-and-chains for multipart/mixed client output"); return NGX_ERROR; } chain = &bc->chain; msgid_buf = chain->buf; //message id ngx_memzero(chain->buf, sizeof(ngx_buf_t)); chain->buf->memory = 1; chain->buf->start = headerbuf->charbuf; chain->buf->pos = headerbuf->charbuf; //content_type maybe if(msg->content_type.len > 0) { chain = chain->next; buf = chain->buf; msgid_buf->last = cur; msgid_buf->end = cur; ngx_memzero(buf, sizeof(ngx_buf_t)); buf->memory = 1; buf->start = cur; buf->pos = cur; buf->last = ngx_snprintf(cur, 255, "Content-Type: %V\r\n\r\n", &msg->content_type); buf->end = buf->last; } else { *cur++ = CR; *cur++ = LF; msgid_buf->last = cur; msgid_buf->end = cur; } chain = chain->next; buf = chain->buf; //msgbuf if(ngx_buf_size(msg_buf) > 0) { ngx_memcpy(buf, msg_buf, sizeof(*msg_buf)); if(msg_buf->file) { file_copy = nchan_bufchain_pool_reserve_file(ctx->bcp); nchan_msg_buf_open_fd_if_needed(buf, file_copy, NULL); } buf->last_buf = 0; buf->last_in_chain = 0; buf->flush = 0; } chain = chain->next; buf = chain->buf; ngx_memzero(buf, sizeof(ngx_buf_t)); buf->start = &mpd->boundary[0]; buf->pos = buf->start; buf->end = mpd->boundary_end; buf->last = buf->end; buf->memory = 1; buf->last_buf = 0; buf->last_in_chain = 1; buf->flush = 1; ctx->prev_msg_id = fsub->sub.last_msgid; update_subscriber_last_msg_id(sub, msg); ctx->msg_id = fsub->sub.last_msgid; multipart_ensure_headers_sent(fsub); DBG("%p output msg to subscriber", sub); rc = nchan_output_msg_filter(fsub->sub.request, msg, &bc->chain); return rc; }
static ngx_int_t chunked_respond_message(subscriber_t *sub, nchan_msg_t *msg) { full_subscriber_t *fsub = (full_subscriber_t *)sub; nchan_request_ctx_t *ctx = ngx_http_get_module_ctx(fsub->sub.request, ngx_nchan_module); chunksizebuf_t *chunksizebuf = nchan_reuse_queue_push(ctx->output_str_queue); u_char *chunk_start = &chunksizebuf->chr[0]; static u_char *chunk_end=(u_char *)"\r\n"; ngx_file_t *file_copy; nchan_buf_and_chain_t *bc = nchan_bufchain_pool_reserve(ctx->bcp, 3); ngx_chain_t *chain; ngx_buf_t *buf, *msg_buf = &msg->buf; ngx_int_t rc; if(fsub->data.timeout_ev.timer_set) { ngx_del_timer(&fsub->data.timeout_ev); ngx_add_timer(&fsub->data.timeout_ev, sub->cf->subscriber_timeout * 1000); } ctx->prev_msg_id = fsub->sub.last_msgid; update_subscriber_last_msg_id(sub, msg); ctx->msg_id = fsub->sub.last_msgid; if (ngx_buf_size(msg_buf) == 0) { //empty messages are skipped, because a zero-length chunk finalizes the request return NGX_OK; } //chunk size chain = &bc->chain; buf = chain->buf; ngx_memzero(buf, sizeof(*buf)); buf->memory = 1; buf->start = chunk_start; buf->pos = chunk_start; buf->end = ngx_snprintf(chunk_start, 15, "%xi\r\n", ngx_buf_size(msg_buf)); buf->last = buf->end; //message chain = chain->next; buf = chain->buf; *buf = *msg_buf; if(buf->file) { file_copy = nchan_bufchain_pool_reserve_file(ctx->bcp); nchan_msg_buf_open_fd_if_needed(buf, file_copy, NULL); } buf->last_buf = 0; buf->last_in_chain = 0; buf->flush = 0; //trailing newlines chain = chain->next; buf = chain->buf; ngx_memzero(buf, sizeof(*buf)); buf->start = chunk_end; buf->pos = chunk_end; buf->end = chunk_end + 2; buf->last = buf->end; buf->memory = 1; buf->last_buf = 0; buf->last_in_chain = 1; buf->flush = 1; chunked_ensure_headers_sent(fsub); DBG("%p output msg to subscriber", sub); rc = nchan_output_msg_filter(fsub->sub.request, msg, &bc->chain); return rc; }