summaryrefslogtreecommitdiffhomepage
path: root/http.c
blob: aa0e8dc326bf940544dd2312b912db5491e4c644 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "http.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>

const char * method_str[] = {
	[METHOD_GET] = "GET",
	[METHOD_HEAD] = "HEAD",
	[METHOD_POST] = "POST",
	[METHOD_PUT] = "PUT",
	[METHOD_DELETE] = "DELETE",
	[METHOD_OPTIONS] = "OPTIONS",
	[METHOD_PATCH] = "PATCH",
};

const char * status_str[] = {
	[STATUS_OK] = "200 OK",
	[STATUS_BAD_REQUEST] = "400 Bad Request",
	[STATUS_METHOD_NOT_ALLOWED] = "405 Method Not Allowed",
	[STATUS_REQUEST_TIMEOUT] = "408 Request Timeout",
	[STATUS_INTERNAL_SERVER_ERROR] = "500 Internal Server Error",
	[STATUS_NOT_IMPLEMENTED] = "501 Not Implemented",
	[STATUS_VERSION_NOT_SUPPORTED] = "505 Version Not Supported",
};

/// Sends a simple response only with a status to the client.
/// \param fd File descriptor of the client socket
/// \param status HTTP response status code
/// \return Negative value if an error was encountered; numbers of bytes written otherwise
int respond_only_status(const int fd, const enum status status)
{
	static const char * pattern =
		"HTTP/1.1 %s\r\n"
		"Connection: close\r\n"
		"\r\n";

	return dprintf(fd, pattern, status_str[status]);
}

/// Sends a response with a status and a body to the client.
/// \param fd File descriptor of the client socket
/// \param status HTTP response status code
/// \param body Content that will be sent
/// \return Negative value if an error was encountered; numbers of bytes written otherwise
int respond_with_body(const int fd, const enum status status, const char * body)
{
	static const char * pattern =
		"HTTP/1.1 %s\r\n"
		"Connection: close\r\n"
		"Content-Type: text/plain\r\n"
		"\r\n";

	if (0 > dprintf(fd, pattern, status_str[status]))
	{
		return -1; // TODO: Handle errors properly
	}

	return send(fd, body, strlen(body), 0);
}

/// Compares the `buffer` to list of supported methods.
/// \param buffer Pointer to the first byte of request
/// \return One of supported methods or METHOD_INVALID
enum method parse_method(const char * buffer)
{
	for (int i = 0; i < NUMBER_OF_METHODS; ++i)
	{
		if (0 == strncmp(method_str[i], buffer, strlen(method_str[i])))
		{
			return i;
		}
	}

	return METHOD_INVALID;
}

/// Receives and parses request or part of it from a client.
/// \param fd Client socket
/// \param request Parser output and state
/// \return Number of bytes parsed or -1 if an error occurred
int parse_request(const int fd, struct request * const request)
{
	char buffer[10240] = {0};

	int length = recv(fd, buffer, 10240, 0);

	if (-1 == length && EWOULDBLOCK != errno && EAGAIN != errno)
	{
		return -1;
	}

	if (0 == length)
	{
		return -1; // TODO: Handle errors properly
	}

	request->method = parse_method(buffer);

	if (METHOD_INVALID == request->method)
	{
		// TODO: 501 Not Implemented
	}

	request->body = malloc(length + 1);
	memcpy(request->body, buffer, length);
	request->body[length] = 0;

	return length;
}