summaryrefslogtreecommitdiff
path: root/blog.c
blob: b0c6cc245cc3f2a47c4264125b18e391256d352a (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
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "blog.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"));

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

	read_post_data();
	if (!verify_creds(find_post_data("username"), find_post_data("password"))) {
		// handle invalid login
	}

	for (char **e = environ; e && *e; e++) {
		puts(*e);
	}
	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("<!--");
	for (char **e = environ; e && *e; e++) {
		puts(*e);
	}
	puts("-->");

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

	return 0;
}