123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /*
- * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <err.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "buffer.h"
- #include "filehelper.h"
- #include "handler.h"
- #include "template.h"
- #ifndef CMS_CONTENT_DIR
- #error "Need CMS_CONTENT_DIR defined to compile"
- #endif
- #ifndef CMS_HOSTNAME
- #error "Need CMS_HOSTNAME defined to compile"
- #endif
- #ifndef CMS_SESSION_DIR
- #error "Need CMS_SESSION_Dir defined to compile"
- #endif
- char *cms_content_dir = CMS_CONTENT_DIR;
- char *cms_template_dir = CMS_TEMPLATE_DIR;
- char *cms_session_db = CMS_SESSION_DIR "/session.db";
- char *cms_session_htpasswd = CMS_SESSION_DIR "/htpasswd";
- static __dead void usage(void);
- __dead void
- usage(void)
- {
- extern char *__progname;
- dprintf(STDERR_FILENO, "usage: %s URI\n", __progname);
- exit(1);
- }
- int
- main(int argc, char **argv)
- {
- char *result = "";
- struct buffer_list *hb, *out;
- struct request *r;
- if (argc > 2)
- usage();
- if (argc >= 2) {
- cms_content_dir = CMS_CHROOT CMS_CONTENT_DIR;
- cms_template_dir = CMS_CHROOT CMS_TEMPLATE_DIR;
- cms_session_db = CMS_CHROOT CMS_SESSION_DIR "/session.db";
- cms_session_htpasswd = CMS_CHROOT CMS_SESSION_DIR "/htpasswd";
- if (argv[1][0] != '/')
- errx(1, "Require absolute path as argument");
- // XXX Substitude PATH_INFO env variable
- setenv("PATH_INFO", argv[1], 1);
- }
- char *path_info = getenv("PATH_INFO");
- if (path_info == NULL || strlen(path_info) == 0
- || strcmp(path_info, "index.html") == 0
- || strcmp(path_info, "/index.html") == 0)
- path_info = "/home.html";
- r = request_new(path_info);
- if (r == NULL)
- _error("404 Not Found", NULL);
- struct page_info *page = request_fetch_page(r);
- if (page == NULL)
- _error("404 Not Found", NULL);
- request_init_tmpl_data(r);
- request_handle_login(r);
- out = request_render_page(r, CMS_DEFAULT_TEMPLATE);
- result = buffer_list_concat_string(out);
- request_add_header(r, "Content-type", "application/xhtml+xml");
- request_add_header(r, "Status", "200 Ok");
- hb = request_output_headers(r);
- char *header = buffer_list_concat_string(hb);
- dprintf(STDOUT_FILENO, "%s\r\n%s", header, result);
- request_free(r);
- buffer_list_free(hb);
- buffer_list_free(out);
- return 0;
- }
|