static ngx_http_push_channel_t * ngx_http_push_store_find_channel(ngx_str_t *id, time_t channel_timeout, ngx_log_t *log) {
  //get the channel and check channel authorization while we're at it.
  ngx_http_push_channel_t        *channel;
  ngx_shmtx_lock(&ngx_http_push_shpool->mutex);
  channel = ngx_http_push_find_channel(id, channel_timeout, log);
  ngx_shmtx_unlock(&ngx_http_push_shpool->mutex);
  return channel;
}
static void ngx_http_push_publisher_body_handler(ngx_http_request_t * r) { 
	ngx_str_t                      *id;
	ngx_http_push_loc_conf_t       *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_module);
	ngx_slab_pool_t                *shpool = (ngx_slab_pool_t *) ngx_http_push_shm_zone->shm.addr;
	ngx_buf_t                      *buf = NULL, *buf_copy;
	ngx_http_push_channel_t        *channel;
	ngx_uint_t                      method = r->method;
	
	time_t                          last_seen = 0;
	ngx_uint_t                      subscribers = 0;
	ngx_uint_t                      messages = 0;
	
	if((id = ngx_http_push_get_channel_id(r, cf))==NULL) {
		ngx_http_finalize_request(r, r->headers_out.status ? NGX_OK : NGX_HTTP_INTERNAL_SERVER_ERROR);
		return;
	}
	
	ngx_shmtx_lock(&shpool->mutex);
	//POST requests will need a channel created if it doesn't yet exist.
	if(method==NGX_HTTP_POST || method==NGX_HTTP_PUT) {
		channel = ngx_http_push_get_channel(id, &((ngx_http_push_shm_data_t *) ngx_http_push_shm_zone->data)->tree, shpool, r->connection->log);
		NGX_HTTP_PUSH_PUBLISHER_CHECK_LOCKED(channel, NULL, r, "push module: unable to allocate memory for new channel", shpool);
	}
	//no other request method needs that.
	else{
		//just find the channel. if it's not there, NULL.
		channel = ngx_http_push_find_channel(id, &((ngx_http_push_shm_data_t *) ngx_http_push_shm_zone->data)->tree, shpool, r->connection->log);
	}
	
	if(channel!=NULL) {
		subscribers = channel->subscribers;
		last_seen = channel->last_seen;
		messages  = channel->messages;
	}
	else {
		//404!
		ngx_shmtx_unlock(&shpool->mutex);
		r->headers_out.status=NGX_HTTP_NOT_FOUND;
		
		//just the headers, please. we don't care to describe the situation or
		//respond with an html page
		r->headers_out.content_length_n=0;
		r->header_only = 1;
		
		ngx_http_finalize_request(r, ngx_http_send_header(r));
		return;
	}
	ngx_shmtx_unlock(&shpool->mutex);
	
	switch(method) {
		ngx_http_push_msg_t        *msg, *previous_msg;
		size_t                      content_type_len;
		ngx_uint_t                  received;
		ngx_http_push_msg_t        *sentinel;
		
		case NGX_HTTP_POST:
			//first off, we'll want to extract the body buffer
		
			//note: this works mostly because of r->request_body_in_single_buf = 1; 
			//which, i suppose, makes this module a little slower than it could be.
			//this block is a little hacky. might be a thorn for forward-compatibility.
			if(r->headers_in.content_length_n == -1 || r->headers_in.content_length_n == 0) {
				buf = ngx_create_temp_buf(r->pool, 0);
				//this buffer will get copied to shared memory in a few lines, 
				//so it does't matter what pool we make it in.
			}
			else if(r->request_body->temp_file==NULL) { //everything in the first buffer, please
				//no file
				buf=r->request_body->bufs->buf;
			}
			else { //(r->request_body->bufs->next!=NULL)
				//there's probably a file
				buf=r->request_body->bufs->next->buf;
			}
			
			NGX_HTTP_PUSH_PUBLISHER_CHECK(buf, NULL, r, "push module: can't find or allocate publisher request body buffer");
					
			content_type_len = (r->headers_in.content_type!=NULL ? r->headers_in.content_type->value.len : 0);
			
			ngx_shmtx_lock(&shpool->mutex);
			
			//create a buffer copy in shared mem
			msg = ngx_slab_alloc_locked(shpool, sizeof(*msg) + content_type_len);
			NGX_HTTP_PUSH_PUBLISHER_CHECK_LOCKED(msg, NULL, r, "push module: unable to allocate message in shared memory", shpool);
			previous_msg=ngx_http_push_get_latest_message_locked(channel); //need this for entity-tags generation
			NGX_HTTP_PUSH_CREATE_BUF_COPY(buf, buf_copy, shpool, ngx_slab_alloc_locked);
			NGX_HTTP_PUSH_PUBLISHER_CHECK_LOCKED(buf_copy, NULL, r, "push module: unable to allocate buffer in shared memory", shpool);
			msg->buf=buf_copy;
			
			if(cf->store_messages) {
				ngx_queue_insert_tail(&channel->message_queue->queue, &msg->queue);
				channel->messages++;
			}
			
			//Stamp the new message with entity tags
			msg->message_time=ngx_time(); //ESSENTIAL TODO: make sure this ends up producing GMT time
			msg->message_tag=(previous_msg!=NULL && msg->message_time == previous_msg->message_time) ? (previous_msg->message_tag + 1) : 0;		
			
			//store the content-type
			if(content_type_len>0) {
				msg->content_type.len=r->headers_in.content_type->value.len;
				msg->content_type.data=(u_char *) (msg+1); //we had reserved a contiguous chunk, myes?
				ngx_memcpy(msg->content_type.data, r->headers_in.content_type->value.data, msg->content_type.len);
			}
			else {
				msg->content_type.len=0;
				msg->content_type.data=NULL;
			}
			
			//set message expiration time
			time_t                  message_timeout = cf->buffer_timeout;
			msg->expires = (message_timeout==0 ? 0 : (ngx_time() + message_timeout));
			
			//FMI (For My Information): shm is still locked.
			switch(ngx_http_push_broadcast_message_locked(channel, msg, r->connection->log, shpool)) {
				
				case NGX_HTTP_PUSH_MESSAGE_QUEUED:
					//message was queued successfully, but there were no 
					//subscribers to receive it.
					r->headers_out.status = NGX_HTTP_ACCEPTED;
					r->headers_out.status_line.len =sizeof("202 Accepted")- 1;
					r->headers_out.status_line.data=(u_char *) "202 Accepted";
					break;
					
				case NGX_HTTP_PUSH_MESSAGE_RECEIVED:
					//message was queued successfully, and it was already sent
					//to at least one subscriber
					r->headers_out.status = NGX_HTTP_CREATED;
					r->headers_out.status_line.len =sizeof("201 Created")- 1;
					r->headers_out.status_line.data=(u_char *) "201 Created";
					
					//update the number of times the message was received.
					//in the interest of premature optimization, I assume all
					//current subscribers have received the message successfully.
					received = msg->received;					
					//nasty overflow check
					msg->received = (received + subscribers < received) ? NGX_MAX_UINT32_VALUE : (received + subscribers);
					break;
					
				case NGX_ERROR:
					//WTF?
					ngx_shmtx_unlock(&shpool->mutex);
					ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: error broadcasting message to workers");
					ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
					return;
					
				default: 
					//for debugging, mostly. I don't expect this branch to be
					//hit during regular operation
					ngx_shmtx_unlock(&shpool->mutex);
					ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: TOTALLY UNEXPECTED error broadcasting message to workers");
					ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
					return;
			}
			//shm is still locked I hope.
			
			if(buf->file!=NULL) {
				//future subscribers won't be able to use this file descriptor --
				//it will be closed once the publisher request is finalized. 
				//(That's about to happen a handful of lines below.)
				msg->buf->file->fd=NGX_INVALID_FILE;
			}
			
			//now see if the queue is too big
			if(channel->messages > (ngx_uint_t) cf->max_messages) {
				//exceeeds max queue size. force-delete oldest message
				ngx_http_push_force_delete_message_locked(channel, ngx_http_push_get_oldest_message_locked(channel), shpool);
			}
			if(channel->messages > (ngx_uint_t) cf->min_messages) {
				//exceeeds min queue size. maybe delete the oldest message
				ngx_http_push_msg_t    *oldest_msg = ngx_http_push_get_oldest_message_locked(channel);
				NGX_HTTP_PUSH_PUBLISHER_CHECK_LOCKED(oldest_msg, NULL, r, "push module: oldest message not found", shpool);
				if(oldest_msg->received >= (ngx_uint_t) cf->min_message_recipients) {
					//received more than min_message_recipients times
					ngx_http_push_delete_message_locked(channel, oldest_msg, shpool);
				}
			}
			messages = channel->messages;
			
			ngx_shmtx_unlock(&shpool->mutex);
			ngx_http_finalize_request(r, ngx_http_push_channel_info(r, messages, subscribers, last_seen));
			return;
			
		case NGX_HTTP_PUT:
		case NGX_HTTP_GET: 
			r->headers_out.status = NGX_HTTP_OK;
			ngx_http_finalize_request(r, ngx_http_push_channel_info(r, messages, subscribers, last_seen));
			return;
			
		case NGX_HTTP_DELETE:
			ngx_shmtx_lock(&shpool->mutex);
			sentinel = channel->message_queue; 
			msg = sentinel;
						
			while((msg=(ngx_http_push_msg_t *)ngx_queue_next(&msg->queue))!=sentinel) {
				//force-delete all the messages
				ngx_http_push_force_delete_message_locked(NULL, msg, shpool);
			}
			channel->messages=0;
			
			//410 gone
			NGX_HTTP_PUSH_PUBLISHER_CHECK_LOCKED(ngx_http_push_broadcast_status_locked(channel, NGX_HTTP_GONE, &NGX_HTTP_PUSH_HTTP_STATUS_410, r->connection->log, shpool), NGX_ERROR, r, "push module: unable to send current subscribers a 410 Gone response", shpool);
			ngx_http_push_delete_node_locked(&((ngx_http_push_shm_data_t *) ngx_http_push_shm_zone->data)->tree, (ngx_rbtree_node_t *) channel, shpool);
			ngx_shmtx_unlock(&shpool->mutex);
			//done.
			r->headers_out.status=NGX_HTTP_OK;
			ngx_http_finalize_request(r, ngx_http_push_channel_info(r, messages, subscribers, last_seen));
			return;
			
		default: 
			//some other weird request method
			ngx_http_push_add_response_header(r, &NGX_HTTP_PUSH_HEADER_ALLOW, &NGX_HTTP_PUSH_ALLOW_GET_POST_PUT_DELETE);
			ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
			return;
	}
}
static ngx_int_t ngx_http_push_subscriber_handler(ngx_http_request_t *r) {
	ngx_http_push_loc_conf_t       *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_module);
	ngx_slab_pool_t                *shpool = (ngx_slab_pool_t *)ngx_http_push_shm_zone->shm.addr;
	ngx_str_t                      *id;
	ngx_http_push_channel_t        *channel;
	ngx_http_push_msg_t            *msg;
	ngx_int_t                       msg_search_outcome;
	
	ngx_str_t                      *content_type=NULL;
	ngx_str_t                      *etag;
	
    if (r->method == NGX_HTTP_OPTIONS) {
        ngx_buf_t *buf = ngx_create_temp_buf(r->pool, sizeof(NGX_HTTP_PUSH_OPTIONS_OK_MESSAGE));
		ngx_chain_t *chain;
		buf->pos=(u_char *)NGX_HTTP_PUSH_OPTIONS_OK_MESSAGE;
		buf->last=buf->pos + sizeof(NGX_HTTP_PUSH_OPTIONS_OK_MESSAGE)-1;
		chain = ngx_http_push_create_output_chain(buf, r->pool, r->connection->log);
		buf->last_buf=1;
        r->headers_out.content_length_n=ngx_buf_size(buf);
		r->headers_out.status=NGX_HTTP_OK;
		ngx_http_send_header(r);
		ngx_http_output_filter(r, chain);
        return NGX_OK;
    }
    
	if (r->method != NGX_HTTP_GET) {
		ngx_http_push_add_response_header(r, &NGX_HTTP_PUSH_HEADER_ALLOW, &NGX_HTTP_PUSH_ALLOW_GET); //valid HTTP for the win
		return NGX_HTTP_NOT_ALLOWED;
	}    
	
	if((id=ngx_http_push_get_channel_id(r, cf)) == NULL) {
		return r->headers_out.status ? NGX_OK : NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	//get the channel and check channel authorization while we're at it.
	ngx_shmtx_lock(&shpool->mutex);
	if (cf->authorize_channel==1) {
		channel = ngx_http_push_find_channel(id, r->connection->log);
	}else{
		channel = ngx_http_push_get_channel(id, r->connection->log, cf->channel_timeout);
	}

	if (channel==NULL) {
		//unable to allocate channel OR channel not found
		ngx_shmtx_unlock(&shpool->mutex);
		if(cf->authorize_channel) {
			return NGX_HTTP_FORBIDDEN;
		}
		else {
			ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: unable to allocate shared memory for channel");
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
		}
	}

    msg = ngx_http_push_find_message_locked(channel, r, &msg_search_outcome); 
    channel->last_seen = ngx_time();
    channel->expires = ngx_time() + cf->channel_timeout;
    ngx_shmtx_unlock(&shpool->mutex);
    
    if (cf->ignore_queue_on_no_cache && !ngx_http_push_allow_caching(r)) {
        msg_search_outcome = NGX_HTTP_PUSH_MESSAGE_EXPECTED; 
        msg = NULL;
    }
	
	switch(ngx_http_push_handle_subscriber_concurrency(r, channel, cf)) {
		case NGX_DECLINED: //this request was declined for some reason.
			//status codes and whatnot should have already been written. just get out of here quickly.
			return NGX_OK;
		case NGX_ERROR:
			ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: error handling subscriber concurrency setting");
			return NGX_ERROR;
	}

	switch(msg_search_outcome) {
		//for message-found:
		ngx_chain_t                *chain;
		time_t                      last_modified;
		size_t                      content_type_len;

		case NGX_HTTP_PUSH_MESSAGE_EXPECTED:
			// ♫ It's gonna be the future soon ♫
			switch(cf->subscriber_poll_mechanism) {
				//for NGX_HTTP_PUSH_MECHANISM_LONGPOLL
				ngx_http_push_pid_queue_t  *sentinel, *cur, *found;
				ngx_http_push_subscriber_t *subscriber;
				ngx_http_push_subscriber_t *subscriber_sentinel;
				
				case NGX_HTTP_PUSH_MECHANISM_LONGPOLL:
					//long-polling subscriber. wait for a message.
					
					//subscribers are queued up in a local pool. Queue sentinels are separate and also local, but not in the pool.
					ngx_shmtx_lock(&shpool->mutex);
					sentinel = &channel->workers_with_subscribers;
					cur = (ngx_http_push_pid_queue_t *)ngx_queue_head(&sentinel->queue);
					found = NULL;
					
					ngx_http_push_subscriber_cleanup_t *clndata;
					ngx_pool_cleanup_t             *cln;
					while(cur!=sentinel) {
						if(cur->pid==ngx_pid) {
							found = cur;
							break;
						}
						cur = (ngx_http_push_pid_queue_t *)ngx_queue_next(&cur->queue);
					}
					if(found == NULL) { //found nothing
						if((found=ngx_http_push_slab_alloc_locked(sizeof(*found)))==NULL) {
							ngx_shmtx_unlock(&shpool->mutex);
							ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: unable to allocate worker subscriber queue marker in shared memory");
							return NGX_HTTP_INTERNAL_SERVER_ERROR;
						}
						//initialize
						ngx_queue_insert_tail(&sentinel->queue, &found->queue);
						found->pid=ngx_pid;
						found->slot=ngx_process_slot;
						found->subscriber_sentinel=NULL;
					}
					ngx_shmtx_unlock(&shpool->mutex);
					
					if((subscriber = ngx_palloc(ngx_http_push_pool, sizeof(*subscriber)))==NULL) { //unable to allocate request queue element
						return NGX_ERROR;
					}
					
					 //attach a cleaner to remove the request from the channel.
					if ((cln=ngx_pool_cleanup_add(r->pool, sizeof(*clndata))) == NULL) { //make sure we can.
						return NGX_ERROR;
					}
					cln->handler = (ngx_pool_cleanup_pt) ngx_http_push_subscriber_cleanup;
					clndata = (ngx_http_push_subscriber_cleanup_t *) cln->data;
					clndata->channel=channel;
					clndata->subscriber=subscriber;
					
					subscriber->request = r;
					subscriber->clndata=clndata;
					
					ngx_shmtx_lock(&shpool->mutex);
					channel->subscribers++; // do this only when we know everything went okay.
					
					//figure out the subscriber sentinel
					subscriber_sentinel = ((ngx_http_push_pid_queue_t *)found)->subscriber_sentinel;
					if(subscriber_sentinel==NULL) {
						//it's perfectly nornal for the sentinel to be NULL.
						if((subscriber_sentinel=ngx_palloc(ngx_http_push_pool, sizeof(*subscriber_sentinel)))==NULL) {
							ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: unable to allocate channel subscriber sentinel");
							return NGX_HTTP_INTERNAL_SERVER_ERROR;
						}
						ngx_queue_init(&subscriber_sentinel->queue);
						((ngx_http_push_pid_queue_t *)found)->subscriber_sentinel=subscriber_sentinel;
					}
					ngx_shmtx_unlock(&shpool->mutex);
					
					ngx_queue_insert_tail(&subscriber_sentinel->queue, &subscriber->queue);
					
					ngx_memzero(&subscriber->event, sizeof(subscriber->event));
					if (cf->subscriber_timeout > 0) {		
						subscriber->event.handler = ngx_http_push_clean_timeouted_subscriber;	
						subscriber->event.data = subscriber;
						subscriber->event.log = r->connection->log;
						ngx_add_timer(&subscriber->event, cf->subscriber_timeout * 1000);
					}

					//r->read_event_handler = ngx_http_test_reading;
					//r->write_event_handler = ngx_http_request_empty_handler;
					r->discard_body = 1;
					//r->keepalive = 1; //stayin' alive!!
					return NGX_DONE;
					
				case NGX_HTTP_PUSH_MECHANISM_INTERVALPOLL:
				
					//interval-polling subscriber requests get a 304 with their entity tags preserved.
					if (r->headers_in.if_modified_since != NULL) {
						r->headers_out.last_modified_time=ngx_http_parse_time(r->headers_in.if_modified_since->value.data, r->headers_in.if_modified_since->value.len);
					}
					if ((etag=ngx_http_push_subscriber_get_etag(r)) != NULL) {
						r->headers_out.etag=ngx_http_push_add_response_header(r, &NGX_HTTP_PUSH_HEADER_ETAG, etag);
					}
					return NGX_HTTP_NOT_MODIFIED;
					
				default:
					//if this ever happens, there's a bug somewhere else. probably config stuff.
					return NGX_HTTP_INTERNAL_SERVER_ERROR;
			}
		
		case NGX_HTTP_PUSH_MESSAGE_EXPIRED:
			//subscriber wants an expired message
			//TODO: maybe respond with entity-identifiers for oldest available message?
			return NGX_HTTP_NO_CONTENT; 
		
		case NGX_HTTP_PUSH_MESSAGE_FOUND:
			//found the message
			ngx_shmtx_lock(&shpool->mutex);
			ngx_http_push_reserve_message_locked(channel, msg);
			NGX_HTTP_PUSH_MAKE_ETAG(msg->message_tag, etag, ngx_palloc, r->pool);
			if(etag==NULL) {
				//oh, nevermind...
				ngx_shmtx_unlock(&shpool->mutex);
				ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: unable to allocate memory for Etag header");
				return NGX_ERROR;
			}
			
			content_type_len = msg->content_type.len;
			if(content_type_len>0) {
				NGX_HTTP_PUSH_MAKE_CONTENT_TYPE(content_type, content_type_len, msg, r->pool);
				if(content_type==NULL) {
					ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: unable to allocate memory for content-type header while responding to subscriber request");
					ngx_shmtx_unlock(&shpool->mutex);
					return NGX_ERROR;
				}
			}
			
			//preallocate output chain. yes, same one for every waiting subscriber
			if((chain = ngx_http_push_create_output_chain_locked(msg->buf, r->pool, r->connection->log, shpool))==NULL) {
				ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push module: unable to allocate buffer chain while responding to subscriber request");
				ngx_shmtx_unlock(&shpool->mutex);
				return NGX_ERROR;
			}
			
			last_modified = msg->message_time;
			
			//is the message still needed?
			ngx_http_push_release_message_locked(channel, msg);
			ngx_shmtx_unlock(&shpool->mutex);
			
			if(chain->buf->file!=NULL) {
				//close file when we're done with it
				ngx_pool_cleanup_t *cln;
				ngx_pool_cleanup_file_t *clnf;
			 
				if((cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_pool_cleanup_file_t)))==NULL) {
					return NGX_HTTP_INTERNAL_SERVER_ERROR;
				}
				cln->handler = ngx_pool_cleanup_file;
				clnf = cln->data;
				clnf->fd = chain->buf->file->fd;
				clnf->name = chain->buf->file->name.data;
				clnf->log = r->pool->log;
			}
			
			return ngx_http_push_prepare_response_to_subscriber_request(r, chain, content_type, etag, last_modified);
			
		default: //we shouldn't be here.
			return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}
}