summaryrefslogtreecommitdiff
path: root/http.c
blob: 40a2b827e54e8d5b12a3f9e4cd3e2513e15c459e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "http.h"

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct http_request http_parse_request(const char *reqstr)
{
	struct http_request req;
	char uribuf[CFWS_MAXURI];
	size_t urilen;

	sscanf(reqstr, "GET %s HTTP/1.1", uribuf);

	/* TODO: Support other method types, notably POST */
	req.method = HTTP_METHOD_GET;

	urilen = strlen(uribuf);
	req.uri = malloc(urilen + 1);
	memcpy(req.uri, uribuf, urilen + 1);
	
	/* TODO: Parse request headers */

	return req;
}

void http_free_request(struct http_request *req)
{
	free(req->uri);
}

int http_build_response(char *res, enum http_res_code code, const char *msg, size_t msglen)
{
	return snprintf(res, CFWS_MAX_RESPONSE, "HTTP/1.1 200 OK\r\n%.*s\r\n", msglen, msg);
}