aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 37903326d896b29a69c6f1f925cdea8e1c76dd64 (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
79
80
81
82
#include <fcgi_stdio.h>
#include <cmark.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

static void print_error(const char *error, ...) {
    printf("Status: 500 Internal Server Error\r\n"
           "Content-Type: text/html\r\n"
           "\r\n"
           "<html lang=\"en\">"
           "<head>"
           "<meta charset=\"utf-8\">"
           "<title>Error</title>"
           "</head>"
           "<body>"
           "<p>The server encountered the following error(s) rendering your document:<br />");
    va_list args;
    va_start(args, error);
    vprintf(error, args);
    va_end(args);
    printf("</p>"
           "</body>");
}

int main() {
    while (FCGI_Accept() >= 0) {
        const char *doc_root = getenv("DOCUMENT_ROOT");
        if (doc_root == NULL) {
            print_error("%s is not set. Check if you included fastcgi_params in your web server configuration.",
                        "DOCUMENT_ROOT");
            goto end;
        }
        const char *doc_uri = getenv("DOCUMENT_URI");
        if (doc_uri == NULL) {
            print_error("%s is not set. Check if you included fastcgi_params in your web server configuration.",
                        "DOCUMENT_URI");
            goto end;
        }
        char *uri = calloc(strlen(doc_root) + 1 /* separator */ + strlen(doc_uri) + 1 /* \0 */,
                           sizeof(char));
        if (uri == NULL) {
            int r = errno;
            print_error("Cannot allocate memory: %s\n", strerror(r));
            goto end;
        }
        sprintf(uri, "%s/%s", doc_root, doc_uri);
        FILE *file = fopen(uri, "r");
        if (file == NULL) {
            int r = errno;
            if (r == ENOENT) {
                printf("Status: 404 Not Found\r\nContent-Type: text/plain\r\n\r\n");
                free(uri);
                goto end;
            }
            print_error("Cannot open file %s: %s\n", uri, strerror(r));
            free(uri);
            goto end;
        }
        free(uri);
        cmark_parser *parser = cmark_parser_new(CMARK_OPT_DEFAULT);
        ssize_t bytes;
        char buffer[1024];
        while ((bytes = fread(buffer, 1, sizeof(buffer), file)) > 0) {
            cmark_parser_feed(parser, buffer, bytes);
            if (bytes < sizeof(buffer)) {
                break;
            }
        }
        cmark_node *document = cmark_parser_finish(parser);
        cmark_parser_free(parser);
        char *html = cmark_render_html(document, CMARK_OPT_DEFAULT); //cmark_markdown_to_html(buffer, strlen(buffer), 0);
        cmark_node_free(document);
        printf("Content-Type: text/html\r\n\r\n%s", html);
        free(html);
        fclose(file);
        goto end;
        end:
        continue;
    }
    return 0;
}