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
|
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cJSON.h"
#ifndef __OpenBSD__
#define pledge(a, b) (0)
#endif
int main(int argc, char **argv) {
if (pledge("stdio rpath", NULL) == -1) {
perror("pledge");
return 1;
}
printf("Status: 200 OK\r\n");
printf("Content-Type: text/html\r\n");
printf("\r\n");
printf("<!DOCTYPE html>\n<html>\n<meta charset=\"UTF-8\">\n<title>CGI</title>\n<style>table,"
"td,th{border:1px solid black;border-collapse:collapse}</style>\n");
size_t bufn = 0;
char *buf = NULL;
ssize_t n;
while (1) {
buf = realloc(buf, bufn + 4096 + 1);
if (buf == NULL)
err(1, "realloc");
n = read(0, buf+bufn, 4096);
if (n == -1)
err(1, "read");
if (n == 0)
break;
bufn += n;
}
n += bufn;
buf[n] = '\0';
if (n > 0) {
cJSON *json = cJSON_ParseWithLength(buf, n), *commits, *c, *id, *msg, *a, *name;
if (json == NULL)
errx(1, "parsing JSON failed");
commits = cJSON_GetObjectItemCaseSensitive(json, "commits");
c = cJSON_GetArrayItem(commits, 0);
id = cJSON_GetObjectItemCaseSensitive(c, "id");
msg = cJSON_GetObjectItemCaseSensitive(c, "message");
a = cJSON_GetObjectItemCaseSensitive(c, "author");
name = cJSON_GetObjectItemCaseSensitive(a, "name");
printf("<h2>stdin</h2>\n<pre>");
if (cJSON_IsString(name))
printf("%s commited %.10s: %s\n", name->valuestring, id->valuestring, msg->valuestring);
else
printf("Failed to find commit info.\n");
cJSON_Delete(json);
printf("</pre>\n");
}
free(buf);
printf("</html>\n");
return 0;
}
|