Lindenii Project Forge
Login

server

Lindenii Forge’s main backend daemon
Commit info
ID
03122a39f821721cbf0cbb8d8d362b84bd6d4601
Author
Runxi Yu <me@runxiyu.org>
Author date
Sat, 15 Mar 2025 16:58:28 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Sat, 15 Mar 2025 16:58:28 +0800
Actions
Remove the "path segments" section in index.htmpl
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

use fmt;
use htmpl;
use io;
use net::http;
use net::uri;
use strconv;
use strings;

fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem) = {
	let segments = match(segments_from_path(request.target.raw_path)) {
	case let s: []str =>
		yield s;
	case uri::invalid =>
		start_response(conn, 400, "text/plain")?;
		fmt::fprintln(conn, "Invalid URI")?;
		return void;
	case nomem =>
		return nomem;
	case =>
		abort("unreachable");
	};
	defer strings::freeall(segments);

	let trailing_slash: bool = false;

	if (segments[len(segments) - 1] == "") {
		trailing_slash = true;
		free(segments[len(segments) - 1]);
		segments = segments[.. len(segments) - 1];
	};

	if (len(segments) == 0) {
		start_response(conn, 200, "text/html")?;
		return tp_index(conn, segments);
		return tp_index(conn);
	};
	
	if (segments[0] == ":") {
		if (len(segments) == 1) {
			start_response(conn, 404, "text/plain")?;
			fmt::fprintln(conn, "Error: Blank system endpoint")?;
			return;
		};

		switch (segments[1]) {
		case "static" =>
			if (len(segments) == 2) {
				start_response(conn, 404, "text/plain")?;
				fmt::fprintln(conn, "Error: Blank static endpoint")?;
				return;
			};
			let fs_segments = segments[2 ..];
			for (let fs_segment .. fs_segments) {
				if (strings::contains(fs_segment, "/")) {
					start_response(conn, 400, "text/plain")?;
					fmt::fprintln(conn, "Error: Slash found in filesystem path")?;
					return;
				};
			};
			start_response(conn, 501, "text/plain")?;
			fmt::fprintln(conn, "Not implemented yet")?;
		case =>
			start_response(conn, 404, "text/plain")?;
			fmt::fprintln(conn, "Error: Unknown system endpoint")?;
		};
	};
};

fn start_response(conn: io::handle, status: uint, content_type: str) (void | io::error | nomem) = { // TODO: add len and other headers
	fmt::fprint(conn, "HTTP/1.1 ")?;
	fmt::fprint(conn, strconv::utos(status))?;
	fmt::fprint(conn, " ")?;
	fmt::fprint(conn, http::status_reason(status))?;
	fmt::fprint(conn, "\r\n")?;
	fmt::fprint(conn, "Content-Type: ")?;
	fmt::fprint(conn, content_type)?;
	fmt::fprint(conn, "\r\n")?;
	fmt::fprint(conn, "\r\n")?;
};
{{ define tp_index(handle: io::handle, segments: []str) (void | io::error | nomem) }}
{{ define tp_index(handle: io::handle) (void | io::error | nomem) }}
<!DOCTYPE html>
<html lang="en">
<head>
{{ render _tp_head_common(handle) }}
<title>Index &ndash; {{ global.title }}</title>
</head>
<body>
{{ render _tp_header(handle, "test", "test") }}
<h2>Path segments</h2>
<ul>
	{{ for let s .. segments }}
	<li>{{ s }}</li>
	{{ end }}
</ul>
<div class="padding-wrapper">
<table class="wide rounded">
	<thead>
		<tr>
			<th colspan="2" class="title-row">Groups</th>
		</tr>
		<tr>
			<th scope="col">Name</th>
			<th scope="col">Description</th>
		</tr>
	</thead>
	<tbody>
	</tbody>
</table>
<div class="padding-wrapper">
	<table class="wide rounded">
		<thead>
			<tr>
				<th colspan="2" class="title-row">
					Info
				</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<th scope="row">SSH public key</th>
				<td><code>{{ global.ssh_pubkey }}</code></td>
			</tr>
			<tr>
				<th scope="row">SSH fingerprint</th>
				<td><code>{{ global.ssh_fp }}</code></td>
			</tr>
		</tbody>
	</table>
</div>
<footer>
	{{ render _tp_footer(handle) }}
</footer>
</body>
</html>
{{ end }}