to_string.c 953 Bytes
#include <search.h>
#include <string.h>
#include <stdio.h>

#include "http/response.h"
#include "http/header.h"

static char * string;

void
addHeaderString(const void * node, const VISIT which, const int depth)
{
	if (endorder == which || leaf == which) {
		string += httpHeaderToString(*(HttpHeader *)node, string);
		*string++ = '\r';
		*string++ = '\n';
	}
}

char *
httpResponseToString(HttpResponse response, char * _string)
{
	char   status[4];

	string = _string;

	snprintf(status, 4, "%d", response->status);

	strcpy(string, response->version);
	string += strlen(string);

	*string++ = ' ';

	strcpy(string, status);
	string += strlen(string);

	*string++ = ' ';

	strcpy(string, response->reason);
	string += strlen(string);

	*string++ = '\r';
	*string++ = '\n';

	twalk(response->header, addHeaderString);

	*string++ = '\r';
	*string++ = '\n';

	memcpy(string, response->body, response->nbody);

	return string;
}

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