get_body.c 1013 Bytes
#include <stdlib.h>

#include "http/header.h"
#include "http/message.h"
#include "http/request/parser.h"
#include "cbuf.h"

#define MAX(a,b)	(((a) > (b))? (a) : (b))


#define MAX(x,y)    ((x) > (y) ? (x) : (y))


/**
 * @TODO: not final...input buffer handling not final
 */
void
httpRequestParserGetBody(HttpRequestParser this)
{
	HttpMessage message = (HttpMessage)(this->cur_request);
	size_t len;

	if (0 == message->nbody) {
		char * nbody = httpHeaderGet(
				&(message->header),
				"Content-Length");

		if (NULL == nbody) {
			this->state = HTTP_REQUEST_DONE;
			return;
		}
		else {
			message->type  = HTTP_MESSAGE_BUFFERED;
	 		message->nbody = atoi(nbody);
			message->body  = calloc(1, message->nbody);
			message->dbody = 0;
		}
	}
	this->buffer->bused  -= len;

	if (message->dbody < message->nbody) {
		len = MAX(
				message->nbody - message->dbody,
				this->buffer->bused);

		memcpy(message->body, cbufGetData(this->buffer, len), len);

		message->dbody += len;
	}
}

// vim: set ts=4 sw=4: