summary refs log tree commit diff stats
path: root/parse_cgi.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse_cgi.c')
-rw-r--r--parse_cgi.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/parse_cgi.c b/parse_cgi.c
new file mode 100644
index 0000000..fb345a0
--- /dev/null
+++ b/parse_cgi.c
@@ -0,0 +1,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;
+}