summaryrefslogtreecommitdiff
path: root/blog.c
blob: d3c8e49421c54d7190782902def302c3ccd8e9ba (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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

extern char **environ;

int handle_post(void)
{
	printf("Status: 301 Found\r\n");
	printf("Location: http%s://%s%s\r\n\r\n", getenv("HTTPS") ? "s" : "", getenv("HTTP_HOST"), getenv("DOCUMENT_URI"));
	return 0;
}

int show_entry(const char *path)
{
	printf("Status: 200 OK\r\n");
	printf("Content-Type: text/plain\r\n\r\n");
	puts(path);
	return 0;
}

int main(void)
{
	char *method = getenv("REQUEST_METHOD");
	if (!strcmp(method, "POST")) {
		return handle_post();
	}

	char *path_info = getenv("PATH_INFO");
	if (path_info && *path_info && strcmp(path_info, "/")) {
		return show_entry(path_info);
	}

	printf("Status: 200 OK\r\n");
	printf("Content-Type: text/html\r\n\r\n");

	puts("<!DOCTYPE html>");
	puts("<html lang=\"en\">");
	puts("<meta charset=\"utf-8\">");
	puts("<title>Jakob Kaivo/blog</title>");
	puts("<link type=\"shortcut icon\" href=\"/icon.png\">");
	puts("<link rel=\"stylesheet\" type=\"text/css\" href=\"/style.css\">");
	puts("</head>");
	puts("<body>");
	printf("<form method=\"POST\" action=\"%s\">\n", getenv("DOCUMENT_URI"));
	puts("Username: <input type=\"text\" name=\"username\"><br>");
	puts("Password: <input type=\"password\" name=\"password\"><br>");
	puts("Title: <input type=\"text\" name=\"title\"><br>");
	puts("<textarea name=\"body\"></textarea><br>");
	puts("<input type=\"submit\">");
	puts("</form>");

	puts("<pre>");
	for (char **e = environ; e && *e; e++) {
		puts(*e);
	}
	puts("</pre>");

	puts("</body>");
	puts("</html>");

	return 0;
}