summaryrefslogtreecommitdiffhomepage
path: root/http.c
blob: dc0e9c8f15c7e15481877dda93f7d58544323638 (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
#include "http.h"

#include <stdio.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]);
}