Beispiel #1
0
onion_connection_status handler(void *request_list_v, onion_request * req,
                                onion_response * res) {
  // Some hello message
  onion_response_printf(res, "Starting poll\n");
  onion_response_flush(res);

  // Add it to the request_list lists.
  request_list_t *request_list = request_list_v;
  reqres_t *reqres = onion_low_malloc(sizeof(reqres_t));
  reqres->req = req;
  reqres->res = res;
  pthread_mutex_lock(&request_list->lock);
  request_list->reqres = onion_ptr_list_add(request_list->reqres, reqres);
  pthread_mutex_unlock(&request_list->lock);

  // Yield thread to the poller, ensures request and response are not freed.
  return OCS_YIELD;
}
Beispiel #2
0
/**
 * @short Prepares the POST
 */
static onion_connection_status prepare_POST(onion_request *req){
	// ok post
	onion_token *token=req->parser_data;
	const char *content_type=onion_dict_get(req->headers, "Content-Type");
	const char *content_size=onion_dict_get(req->headers, "Content-Length");

	if (!content_size){
		ONION_ERROR("I need the content size header to support POST data");
		return OCS_INTERNAL_ERROR;
	}
	size_t cl=atol(content_size);
	if (cl==0)
		return OCS_REQUEST_READY;

	//ONION_DEBUG("Content type %s",content_type);
	if (!content_type || (strstr(content_type, "application/x-www-form-urlencoded"))){
		if (cl>req->connection.listen_point->server->max_post_size){
			ONION_ERROR("Asked to send much POST data. Limit %d. Failing.",req->connection.listen_point->server->max_post_size);
			return OCS_INTERNAL_ERROR;
		}
		assert(token->extra==NULL);
		token->extra=onion_low_scalar_malloc(cl+1); // Cl + \0
		token->extra_size=cl;
		req->free_list=onion_ptr_list_add(req->free_list, token->extra); // Free when the request is freed.

		req->parser=parse_POST_urlencode;
		return OCS_NEED_MORE_DATA;
	}

	// multipart.

	const char *mp_token=strstr(content_type, "boundary=");
	if (!mp_token){
		ONION_ERROR("No boundary set at content-type");
		return OCS_INTERNAL_ERROR;
	}
	mp_token+=9;
	if (cl>req->connection.listen_point->server->max_post_size) // I hope the missing part is files, else error later.
		cl=req->connection.listen_point->server->max_post_size;

	int mp_token_size=strlen(mp_token);
	token->extra_size=cl; // Max size of the multipart->data
	onion_multipart_buffer *multipart=onion_low_malloc(token->extra_size+sizeof(onion_multipart_buffer)+mp_token_size+2);
	assert(token->extra==NULL);
	token->extra=(char*)multipart;

	multipart->boundary=(char*)multipart+sizeof(onion_multipart_buffer)+1;
	multipart->size=mp_token_size+4;
	multipart->pos=2; // First boundary already have [\r]\n readen
	multipart->post_total_size=cl;
	multipart->file_total_size=0;
	multipart->boundary[0]='\r';
	multipart->boundary[1]='\n';
	multipart->boundary[2]='-';
	multipart->boundary[3]='-';
	strcpy(&multipart->boundary[4],mp_token);
	multipart->data=(char*)multipart+sizeof(onion_multipart_buffer)+multipart->size+1;

	//ONION_DEBUG("Multipart POST boundary '%s'",multipart->boundary);

	req->parser=parse_POST_multipart_start;

	return OCS_NEED_MORE_DATA;
}