summaryrefslogtreecommitdiffhomepage
path: root/plop.c
blob: d676c4ff14c19fa8390a1bddc010860fc23a2c86 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <errno.h>
#include <fcntl.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <netdb.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include "http.h"

/// Tries to create, bind and start listening on INET server socket.
/// \param node Hostname
/// \param service Port
/// \return File descriptor of the socket or -1 in case of an error
/// \see getaddrinfo(3)
// TODO: Handle UNIX sockets
int make_server(const char * node, const char * service)
{
	struct addrinfo hints = {
		.ai_family = AF_UNSPEC,
		.ai_socktype = SOCK_STREAM,
		.ai_flags = AI_PASSIVE,
	};

	struct addrinfo * result;
	struct addrinfo * it;

	if (0 != getaddrinfo(node, service, &hints, &result))
	{
		return -1; // TODO: Handle errors properly
	}

	int server;

	for (it = result; it != NULL; it = it->ai_next)
	{
		server = socket(it->ai_family, it->ai_socktype, it->ai_protocol);

		if (-1 == server)
			continue;

		if (-1 == fcntl(server, F_SETFL, (fcntl(server, F_GETFL, 0) | O_NONBLOCK)))
		{
			close(server);
			continue;
		}

		if (0 == bind(server, it->ai_addr,it->ai_addrlen))
			break;

		close(server);
	}

	if (it == NULL)
	{
		server = -1; // TODO: Handle errors properly
	}

	freeaddrinfo(result);

	if (-1 == server || -1 == listen(server, 5))
	{
		return -1; // TODO: Handle errors properly
	}

	return server;
}

/// Progresses the connection for a client and shifts it back in the array.
/// `pfd` must be at least `shift_by + 1`-th element of the array.
/// \param L Server's Lua state
/// \param pfd Element in array processed by poll
/// \param data Pointer to data associated with the client
/// \param shift_by Element will be moved by in array this many indices
/// \return Shift value for next handler
/// \see poll(2)
int handle_client(lua_State * L, struct pollfd * pfd, char ** data, const int shift_by)
{
	if (0 == pfd->revents)
	{
		return shift_by;
	}

	if (~(POLLIN | POLLOUT) & pfd->revents)
	{
		return -1; // TODO: Handle errors properly
	}

	if (-1 == collect_request(pfd->fd, data))
	{
		return -1; // TODO: Handle errors properly
	}

	lua_getglobal(L, "Handler");
	lua_pushstring(L, *data);
	lua_call(L, 1, 1);

	size_t length;
	const char * body = lua_tolstring(L, -1, &length);

	if (NULL != body)
	{
		respond_with_body(pfd->fd, STATUS_OK, body, (int) length);
	}

	close(pfd->fd);
	(pfd - shift_by)->fd = -1;
	(pfd - shift_by)->events = pfd->events;

	free(*data);
	*data = NULL;

	return shift_by + 1;
}

/// Accepts awaiting connections if any.
/// \param fdv Array of descriptors for poll
/// \param fdc Number of valid descriptors in the array including the server
/// \param size Size limit for the array
/// \return Number of valid descriptors in the array or -1 if an error occurred.
/// \see poll(2)
int handle_server(struct pollfd * fdv, int fdc, const int size)
{
	if (0 == fdv[0].revents)
	{
		return fdc;
	}

	while (size > fdc + 1 && -1 != (fdv[fdc].fd = accept(fdv[0].fd, NULL, NULL)))
	{
		fdv[fdc].events = POLLIN | POLLOUT;
		fdc++;
	}

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

	return fdc;
}

/// Standard entry point for the program.
/// \param argc Argument count
/// \param argv Argument array
/// \return Error code
int main(int argc, char ** argv)
{
	lua_State * L = luaL_newstate();
	luaL_openlibs(L);

	if (LUA_OK != luaL_dofile(L, "default.lua"))
	{
		return 2; // TODO: Document error codes/print usage information
	}

	if (2 != argc)
	{
		return 1; // TODO: Document error codes/print usage information
	}

	static const int max_clients = 200;

	int fdc = 0;
	char * data[max_clients];
	struct pollfd fdv[max_clients];

	memset(data, 0, sizeof(data));
	memset(fdv, 0, sizeof(fdv));

	fdv[0].fd = make_server(NULL, argv[1]);
	fdv[0].events = POLLIN;
	fdc++;

	while (0 < fdc && -1 != poll(fdv, fdc, -1))
	{
		int shift_by = 0;

		for (int i = 1; i < fdc; ++i)
		{
			shift_by = handle_client(L, &fdv[i], &data[i], shift_by);
		}

		fdc = handle_server(fdv, fdc - shift_by, max_clients);
	}
}