get_body.c 1.11 KB
#include <stdlib.h>

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

#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);
	char *      str_nbody;
	int         nbody;
	int         len;

	str_nbody = httpHeaderGet(
			&(message->header),
			"Content-Length");

	if (NULL == str_nbody) {
		this->state = HTTP_REQUEST_DONE;
		return -1;
	}

	nbody = atoi(str_nbody);

	if (0 == message->nbody) {
		message->type  = HTTP_MESSAGE_BUFFERED;
		if (0 < nbody)
			message->body  = malloc(nbody);
	}

	len = MAX(nbody - message->nbody, this->buffer->bused);
	memcpy(message->body + message->nbody,
			this->buffer->buffer + this->buffer->bstart,
			len);

	message->nbody       += len;
	this->buffer->bstart += len;
	if (this->buffer->bstart >= this->buffer->bsize) {
		this->buffer->bstart -= this->buffer->bsize;
	}
	this->buffer->bused  -= len;

	if (message->nbody == nbody) {
		this->state = HTTP_REQUEST_DONE;
	}
}

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