aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrumeet <yuuta@yuuta.moe>2021-11-18 17:18:44 -0800
committerTrumeet <yuuta@yuuta.moe>2021-11-18 17:18:44 -0800
commitf2518fd044b89abd3747c5d6b4d5fcf6ac88e484 (patch)
treecab6c18fdd516830c22729a75fa7516d6d945765
parent809d222a302874b9b9cf6620f0b960090ddbbe9e (diff)
downloadmdrd-master.tar
mdrd-master.tar.gz
mdrd-master.tar.bz2
mdrd-master.zip
refactor: move the handler out of main()HEADmaster
-rw-r--r--main.c119
1 files changed, 60 insertions, 59 deletions
diff --git a/main.c b/main.c
index 1e77027..5184b14 100644
--- a/main.c
+++ b/main.c
@@ -31,69 +31,70 @@ static void print_error(const char *error, ...) {
fmt);
}
-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\n"
- "Content-Type: text/plain\r\n"
- "Content-Length: 0\r\n"
- "\r\n");
- free(uri);
- goto end;
- }
- print_error("Cannot open file %s: %s\n", uri, strerror(r));
+static inline void run() {
+ 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");
+ return;
+ }
+ 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");
+ return;
+ }
+ 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));
+ return;
+ }
+ 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\n"
+ "Content-Type: text/plain\r\n"
+ "Content-Length: 0\r\n"
+ "\r\n");
free(uri);
- goto end;
+ return;
}
+ print_error("Cannot open file %s: %s\n", uri, strerror(r));
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;
- }
+ return;
+ }
+ 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_node_free(document);
- printf("Content-Type: text/html\r\n"
- "Content-Length: %d\r\n"
- "\r\n"
- "<html><head><meta charset=\"utf-8\"></head><body>%s</body></html>",
- (strlen("<html><head><meta charset=\"utf-8\"></head><body></body></html>") + strlen(html))
- * sizeof(char),
- html);
- free(html);
- fclose(file);
- goto end;
- end:
- continue;
+ }
+ cmark_node *document = cmark_parser_finish(parser);
+ cmark_parser_free(parser);
+ char *html = cmark_render_html(document, CMARK_OPT_DEFAULT);
+ cmark_node_free(document);
+ printf("Content-Type: text/html\r\n"
+ "Content-Length: %d\r\n"
+ "\r\n"
+ "<html><head><meta charset=\"utf-8\"></head><body>%s</body></html>",
+ (strlen("<html><head><meta charset=\"utf-8\"></head><body></body></html>") + strlen(html))
+ * sizeof(char),
+ html);
+ free(html);
+ fclose(file);
+}
+
+int main() {
+ while (FCGI_Accept() >= 0) {
+ run();
}
return 0;
}