public
Description: my blog as a CGI written in C
Homepage: http://alltom.com/
Clone URL: git://github.com/alltom/calltom.git
calltom / alltom.c
100644 140 lines (107 sloc) 3.846 kb
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "alltom.h"
 
static void process_root(Dict *params, char *method, char **path);
static void process_pages(Dict *params, char *method, char **path);
static void process_page(Dict *params, char *method, char **path, char *page);
 
static char **parse_uri(char *uri);
 
int
main(void) {
Dict *get;
char **path;
 
get = get_query_dict(getenv("QUERY_STRING"));
path = parse_uri(getenv("PATH_INFO"));
 
process_root(get, getenv("REQUEST_METHOD"), path);
 
free_dict(get);
free(path);
 
return 0;
}
 
static char **
parse_uri(char *uri) {
/* count slashes */
int num_slashes = 1, i;
char **ary, *pos = uri; /* always starts with '/' */
while((pos = strchr(pos + 1, '/')) != NULL)
num_slashes++;
 
/* stuff into array */
ary = (char **) malloc(num_slashes * sizeof(char *));
if(ary == NULL) return NULL;
pos = strtok(uri + 1, "/");
for(i = 0; pos != NULL; pos = strtok(NULL, "/"), i++)
ary[i] = pos;
ary[i] = NULL;
 
return ary;
}
 
static void
show_404(void) {
printf("%s: %s\r\n", "Content-type", "text/html;charset=utf-8");
printf("%s: %s\r\n", "Status", "404 Not Found");
printf("\r\n");
 
printf("<title>AllTom.com - Not Found</title>\n");
printf("<h1>That URL was not found</h1>\n");
 
printf("<p>suxxor 4j00</p>\n");
 
printf("<hr />\n");
printf("<p>My site does NOT normally suck. Right now I'm experimenting with coding it as a single CGI in C, so this level of suckage should be temporary. All of the old content will return.</p>\n");
}
 
static void
process_root(Dict *get, char *method, char **path) {
if(path[0] != NULL) {
if(strcmp(path[0], "pages") == 0) {
process_pages(get, method, path+1);
} else {
show_404();
}
return;
}
 
printf("%s: %s\r\n", "Content-type", "text/html;charset=utf-8");
printf("\r\n");
 
printf("<title>AllTom.com</title>\n");
printf("<h1>AllTom.com</h1>\n");
 
printf("<p>I have many <a href=\"/pages\">pages</a>.</p>\n");
 
printf("<hr />\n");
printf("<p>My site does NOT normally suck. Right now I'm experimenting with coding it as a single CGI in C, so this level of suckage should be temporary. All of the old content will return.</p>\n");
}
 
static void
process_pages(Dict *get, char *method, char **path) {
if(path[0] != NULL) {
process_page(get, method, path+1, path[0]);
return;
}
 
printf("%s: %s\r\n", "Content-type", "text/html;charset=utf-8");
printf("\r\n");
 
printf("<title>AllTom.com Pages</title>\n");
printf("<h1>My Pages</h1>\n");
 
printf("<p>See?</p>\n");
 
printf("<ul>\n");
printf("<li><a href=\"/pages/a\">Page A</a></li>\n");
printf("<li><a href=\"/pages/b\">Page B</a></li>\n");
printf("</ul>\n");
 
printf("<hr />\n");
printf("<p>My site does NOT normally suck. Right now I'm experimenting with coding it as a single CGI in C, so this level of suckage should be temporary. All of the old content will return.</p>\n");
}
 
static void
process_page(Dict *get, char *method, char **path, char *page) {
if(path[0] != NULL) {
show_404();
return;
}
 
if(strcmp(page, "a") != 0 &&
strcmp(page, "A") != 0 &&
strcmp(page, "b") != 0 &&
strcmp(page, "B") != 0) {
show_404();
return;
}
page[0] = toupper(page[0]);
 
printf("%s: %s\r\n", "Content-type", "text/html;charset=utf-8");
printf("\r\n");
 
printf("<title>AllTom.com Page %s</title>\n", page);
printf("<h1>Page %s</h1>\n", page);
 
printf("<p>Page %s is nothing like those other pages.</p>\n", page);
 
printf("<hr />\n");
printf("<p>My site does NOT normally suck. Right now I'm experimenting with coding it as a single CGI in C, so this level of suckage should be temporary. All of the old content will return.</p>\n");
printf("<p><a href=\"http://github.com/AllTom/calltom/tree/master\">You can help</a> if you're not a lazy bum.</p>\n");
}
 
/* vim:set ts=4 sts=4 sw=4 noet: */