Subversion Repositories Keine

Rev

Rev 4 | Rev 6 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: cgi.c 5 2024-09-11 10:27:37Z nishi $ */
2
 
3
#include "../config.h"
4
 
5
#include "kn_cgi.h"
6
 
7
#include <stdlib.h>
8
#include <string.h>
9
#include <stdbool.h>
10
#include <stdio.h>
11
 
12
#include "kn_version.h"
13
#include "kn_util.h"
4 nishi 14
#include "kn_man.h"
2 nishi 15
 
16
struct q_entry {
17
	char* key;
18
	char* value;
19
};
20
 
21
bool no = false;
22
bool showmain = false;
23
struct q_entry** entries = NULL;
4 nishi 24
char* path;
2 nishi 25
 
4 nishi 26
char* kn_get_query(const char* key) {
2 nishi 27
	if(entries == NULL) return NULL;
28
	int i;
4 nishi 29
	for(i = 0; entries[i] != NULL; i++) {
30
		if(strcmp(entries[i]->key, key) == 0) return entries[i]->value;
2 nishi 31
	}
32
	return NULL;
33
}
34
 
4 nishi 35
char* kn_null(const char* a) { return a == NULL ? "" : (char*)a; }
2 nishi 36
 
4 nishi 37
void kn_parse_query(void) {
2 nishi 38
	char* query = getenv("QUERY_STRING");
4 nishi 39
	if(query != NULL) {
2 nishi 40
		entries = malloc(sizeof(*entries));
41
		entries[0] = NULL;
42
		int i;
43
		int incr = 0;
4 nishi 44
		for(i = 0;; i++) {
45
			if(query[i] == 0 || query[i] == '&') {
2 nishi 46
				char* a = malloc(i - incr + 1);
4 nishi 47
				memcpy(a, query + incr, i - incr);
2 nishi 48
				a[i - incr] = 0;
49
 
50
				char* key = a;
51
				char* value = "";
52
 
53
				int j;
4 nishi 54
				for(j = 0; key[j] != 0; j++) {
55
					if(key[j] == '=') {
2 nishi 56
						key[j] = 0;
57
						value = key + j + 1;
58
						break;
59
					}
60
				}
61
 
62
				struct q_entry* e = malloc(sizeof(*e));
63
				e->key = kn_strdup(key);
64
				e->value = kn_strdup(value);
65
 
66
				struct q_entry** old = entries;
4 nishi 67
				for(j = 0; old[j] != NULL; j++)
68
					;
2 nishi 69
				entries = malloc(sizeof(*entries) * (j + 2));
70
				for(j = 0; old[j] != NULL; j++) entries[j] = old[j];
71
				entries[j] = e;
72
				entries[j + 1] = NULL;
73
				free(old);
74
 
75
				free(a);
76
 
77
				incr = i + 1;
78
				if(query[i] == 0) break;
79
			}
80
		}
81
	}
4 nishi 82
	if(kn_get_query("page") == NULL) {
2 nishi 83
		printf("Status: 200 OK\n\n");
84
		showmain = true;
4 nishi 85
	} else {
86
		if((path = kn_find(MANPAGE_DIR, kn_get_query("page"))) != NULL) {
87
			printf("Status: 200 OK\n\n");
88
		} else {
89
			printf("Status: 404 Not Found\n\n");
90
			no = true;
91
		}
2 nishi 92
	}
93
}
94
 
4 nishi 95
void kn_cgi(void) {
2 nishi 96
	printf("<html>\n");
97
	printf("	<head>\n");
98
	printf("		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
99
	printf("		<title>Keine - ");
4 nishi 100
	if(no) {
2 nishi 101
		printf("Not found");
4 nishi 102
	} else if(showmain) {
2 nishi 103
		printf("Main");
4 nishi 104
	} else {
105
		printf("%s", kn_get_query("page"));
2 nishi 106
	}
107
	printf("</title>\n");
108
	printf("		<style>\n");
109
	printf("html {\n");
110
	printf("	background-color: #222222;\n");
111
	printf("	color: #ffffff;\n");
112
	printf("}\n");
113
	printf("body {\n");
114
	printf("	width: 900px;\n");
115
	printf("	margin: 0 auto;\n");
116
	printf("}\n");
117
	printf("a:link {\n");
118
	printf("	color: #88f;\n");
119
	printf("}\n");
120
	printf("a:visited {\n");
121
	printf("	color: #44b;\n");
122
	printf("}\n");
123
	printf("		</style>\n");
124
	printf("	</head>\n");
125
	printf("	<body>\n");
126
	printf("		<div style=\"text-align: center;\">\n");
127
	printf("			<form action=\"%s%s\">\n", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO")));
128
	printf("				<a href=\"%s%s\">Main</a> | ", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO")));
129
	printf("				Name: <input name=\"page\">\n");
130
	printf("				<input type=\"submit\">\n");
131
	printf("			</form>\n");
132
	printf("		</div>\n");
133
	printf("		<hr>\n");
4 nishi 134
	if(no) {
2 nishi 135
		printf("		Not found.\n");
4 nishi 136
	} else if(showmain) {
137
		printf("%s", MAIN_HTML);
138
	} else {
139
		printf("<pre>");
140
		char* c = kn_manpage_process(path);
141
		if(c != NULL) {
142
			printf("%s", c);
143
			free(c);
144
		}
145
		printf("</pre>\n");
2 nishi 146
	}
147
	printf("		<hr>\n");
5 nishi 148
#ifdef FOOTER_HTML
149
	printf("%s\n", FOOTER_HTML);
150
	printf("		<hr>\n");
151
#endif
4 nishi 152
	printf("		<i>Generated by <a href=\"http://nishi.boats/keine\">Keine</a> %s</i>\n", kn_get_version());
2 nishi 153
	printf("	</body>\n");
154
	printf("</html>\n");
4 nishi 155
	if(path != NULL) free(path);
2 nishi 156
}