view scid/page-static.c @ 43:6854efe15210

scid: push json recursively
author David Demelier <markand@malikania.fr>
date Thu, 11 Aug 2022 11:34:32 +0200
parents 1d0ddf9e6efd
children 71cd8447e3a4
line wrap: on
line source

/*
 * page-static.c -- page /static
 *
 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or 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/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>

#include "pageutil.h"
#include "scid.h"
#include "util.h"

static void
get(struct kreq *req)
{
	struct stat st;
	char path[PATH_MAX];

	snprintf(path, sizeof (path), "%s%s", scid.themedir, req->fullpath);

	if (stat(path, &st) < 0)
		pageutil_status(req, KHTTP_404);
	else {
		khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
		khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[req->mime]);
		khttp_head(req, kresps[KRESP_CONTENT_LENGTH],
		    "%llu", (unsigned long long)(st.st_size));
		khttp_body(req);
		khttp_template(req, NULL, path);
		khttp_free(req);
	}
}

void
page_static(struct kreq *r)
{
	assert(r);

	switch (r->method) {
	case KMETHOD_GET:
		get(r);
		break;
	default:
		pageutil_status(r, KHTTP_400);
		break;
	}
}