summaryrefslogtreecommitdiff
path: root/main.c
blob: 5fa11e8af23737c180536d5f512215c55b4fecc2 (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
/*          __                               _
*    ____  / /_  ____ ___  __  ___________ _(_)
*   / __ \/ __ \/ __ `__ \/ / / / ___/ __ `/ /
*  / /_/ / / / / / / / / / /_/ / /__/ /_/ / /
*  \____/_/ /_/_/ /_/ /_/\__, /\___/\__, /_/
*                       /____/     /____/
*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2021, Max Christian Pohle <max@coderonline.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* {{{ DISCLAIMER
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* }}}
*/

#include "main.h"

static int read_everything(FILE * f_r, FILE * output) {
	const int read_buffer_length = READ_BUFFER_LENGTH;
	char read_buffer[read_buffer_length];
	for(size_t size = -2 ; ; size = fread(read_buffer, 1, read_buffer_length, f_r)) {
		if(-2 == size || (-1 == size && EWOULDBLOCK == errno)) {
			usleep(EWOULDBLOCK_DELAY); // try again a little later
			continue;
		}

		fwrite(read_buffer, 1, size, output);

		if (read_buffer_length > POST_DATA_MAX_LENGTH)
			return EXIT_FAILURE;

		if (size < read_buffer_length) { // I expect one nmemb of data
			break;
		}
	}
	fflush(output);
	return EXIT_SUCCESS;
}

static void * answer_request(size_t new_socket) {
	FILE * f_r = fdopen((size_t) new_socket, "r");

	char * output_buffer = NULL;
	size_t output_buffer_length = 0;
	FILE * output = open_memstream(&output_buffer, &output_buffer_length);

	read_everything(f_r, output);  // TODO: catch return value and error handling
	fflush(output);
	// shutdown(new_socket, SHUT_RD); // shutdown the reading half of the connection

	DEBUG("> Output buffer has a length of %ld bytes\n", output_buffer_length);

	// TODO: make parsing function abstract (e.g. parse(...) function point with dlsym)
	parse_http(new_socket, output_buffer, output_buffer_length);

	fclose(f_r);

	fclose(output);
	free(output_buffer);

	return NULL;
}

int become_daemon(const char * name) {
#	ifndef DEBUG // debugging is simpler in foreground mode
	if(fork()) {
		exit(EXIT_SUCCESS);
	} else {
		setsid();
		if(fork()) {
			exit(EXIT_SUCCESS);
		} else {
			syslog(0, "daemon '%s' is running with pid %d", name, getpid());
		}
	}
# endif
	return EXIT_SUCCESS;
}

static int serve(int server_fd)
{
	struct sockaddr_in address;
	socklen_t address_len = sizeof(address);
	DEBUG("> Waiting for connections on server file descriptor %d\n", server_fd);

	size_t new_socket = -1;
	while(-1 != (new_socket = accept(server_fd, (struct sockaddr*) &address, &address_len)))
	{
		DEBUG("> Client %ld is connected via port %d\n", new_socket, address.sin_port);

		// set non blocking mode...
		fcntl(new_socket, F_SETFL,
			fcntl(new_socket, F_GETFL) | O_NONBLOCK
		);

		answer_request(new_socket);

#ifdef VALGRIND
		break; // only run once, so that valgrind can test allocations&frees
#endif
	}

	err(errno, "error serving");
	close(server_fd);
}

int main(const int argc, char const * argv[]) {
	int server_fd = -1;

	int port = atoi(argc > 1 ? argv[1] : "8080");
	0 == port ? port = 8080 : port;


	// char * pidfile = NULL;


	// char ** next_param = NULL;
	// for(int i=1; i<argc; i++) {
	// 	switch(strspn(argv[i], "-")) {
	// 		case 1: // one minus (short param)
	// 			if(argv[i][2] != '\0') // one minus, one param and a null char
	// 				break;
	// 			if(next_param)
	// 				err(EXIT_FAILURE, "wrong parameter: %s\n", argv[i]);
	// 			switch(argv[i][1]) {
	// 				case 't': puts("t was chosen"); break;
	// 				case 'e': puts("e was chosen"); break;
	// 				case 's': puts("s was chosen"); break;
	// 			}
	// 		break;
	// 		case 2: // two minus (long param)
	// 			if(next_param)
	// 				err(EXIT_FAILURE, "wrong parameter: %s\n", argv[i]);
	// 			if(0 == strcmp("pidfile", &argv[i][2])) {
	// 				next_param = &pidfile;
	// 			}
	// 		break;
	// 		default:
	// 			// pidfile = (char*) argv[i];
	// 			*next_param = (char*) argv[i];
	// 	}
	// }

	// printf("pidfile is '%s'\n", pidfile);

	// return EXIT_SUCCESS;

	struct sockaddr_in address = {
		.sin_family      = AF_INET,
		.sin_addr.s_addr = INADDR_ANY,
		.sin_port        = htons(port)
	};

	// I <3 C
	0 == (server_fd = socket(AF_INET, SOCK_STREAM, 0))
	? err(errno, NULL)
	: bind(server_fd, (struct sockaddr*) &address, sizeof(address))
	? err(errno, NULL)
	: listen(server_fd, SOMAXCONN)
	? err(errno, NULL)
	: become_daemon(argv[0])
	? err(errno, NULL)
	: serve(server_fd)
	? err(errno, NULL)
	: exit(EXIT_SUCCESS)
	; return EXIT_FAILURE;
}

// modeline for vim: shiftwidth=2 tabstop=2 number foldmethod=marker
..