summary refs log tree commit diff
path: root/handlers/handler.go
blob: 798976648899ddec22c4fe804e27c95f4a5a4985 (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
package handlers

import (
	"fmt"
	"github.com/valyala/fasthttp"
	"net/http"
	"portgate"
	"strings"
)

// RequestHandler keeps data relevant to the request handlers.
type RequestHandler struct {
	// Pointer to the global Portgate config, the values of which can change at runtime.
	config *portgate.Config
	// HTTP Client for requesting resources from the destination host.
	client fasthttp.Client
	// Handler for static Portgate assets.
	staticHandler fasthttp.RequestHandler
	// Templates for Portgate pages.
	templates portgate.Templates
}

// NewRequestHandler creates a new RequestHandler instance.
func NewRequestHandler(config *portgate.Config, templates portgate.Templates) RequestHandler {
	// Serves static Portgate files when called.
	fs := fasthttp.FS{
		Root: "./assets/static/",
		PathRewrite: func(ctx *fasthttp.RequestCtx) []byte {
			return []byte(strings.TrimPrefix(string(ctx.Path()), "/_portgate/static"))
		},
		PathNotFound: nil,
	}
	staticHandler := fs.NewRequestHandler()

	return RequestHandler{
		config:        config,
		client:        fasthttp.Client{},
		staticHandler: staticHandler,
		templates:     templates,
	}
}

// HandleRequest handles all types of requests and delegates to more specific handlers.
func (h *RequestHandler) HandleRequest(ctx *fasthttp.RequestCtx) {
	destination := portgate.DestinationFromURL(string(ctx.Path()))

	if destination.IsPortgatePath {
		h.handlePortgateRequest(ctx, destination)
		return
	}

	if destination.Port == 0 {
		// Try to get the port from the Referer.
		destination = destination.AddReferer(string(ctx.Request.Header.Referer()))

		// Still no port?
		if destination.Port == 0 {
			h.handleUnknownRequest(ctx)
			return
		}

		portgateUrl := fmt.Sprintf("/%d%s", destination.Port, destination.Path)
		ctx.Redirect(portgateUrl, http.StatusTemporaryRedirect)
		return
	}

	h.handlePassthroughRequest(ctx, destination)
}

// handleUnknownRequest handles any request which could not be processed due to missing
// information.
func (h *RequestHandler) handleUnknownRequest(ctx *fasthttp.RequestCtx) {
	// TODO: Show error page
	ctx.Error("Unknown request.", http.StatusNotFound)
}

// handleUnknownRequest handles errors which occurred during a request with a generic message.
func (h *RequestHandler) handleError(ctx *fasthttp.RequestCtx) {
	// TODO: Show error page
	ctx.Error("An error occurred", http.StatusInternalServerError)
}
"> output abstract syntax tree for file in s-expr format\n" " -?, --help\t\t\t print this help message\n" " -v, --version\t\t print current version\n"); } void version(void) { fprintf( stderr, "catboot (catskill), version " VERSION "\n" "\n" "This program's source code is subject to the terms of the Mozilla Public\n" "License, v. 2.0. If a copy of the MPL was not distributed with this\n" "file, You can obtain one at https://mozilla.org/MPL/2.0/.\n" "\n" "Copyright (c) 2025, Mel G. <mel@rnrd.eu>\n"); } integer debug_lex_pass(struct String source) { struct Lexer lexer; lexer_new(&lexer, source); struct Token token; do { token = lexer_next(&lexer); token_print(&token); printf(" "); } while (token.kind != TOKEN_END_OF_FILE); return 0; } integer debug_parse_pass(struct Source_File source_file) { struct Lexer lexer; lexer_new(&lexer, source_file.source); struct Parser parser; parser_new(&parser, &lexer, source_file); struct Tree tree; int parser_result = parser_do_your_thing(&parser, &tree); if (parser_result != 0) { fprintf(stderr, "parser finished with errors\n"); } tree_printer(&tree); return parser_result; } enum Command_Result { COMMAND_OK, COMMAND_FAIL, }; struct Command_Arguments { const ascii* input; }; enum Command_Result version_command(struct Command_Arguments* arguments) { version(); return COMMAND_OK; } enum Command_Result help_command(struct Command_Arguments* arguments) { usage(); return COMMAND_OK; } enum Command_Result test_lex_command(struct Command_Arguments* arguments) { struct String source = read_file(arguments->input); // TODO: lexer errors debug_lex_pass(source); return COMMAND_OK; } enum Command_Result test_parse_command(struct Command_Arguments* arguments) { struct String source = read_file(arguments->input); struct Source_File source_file = { .source = source, .path = string_from_static_c_string(arguments->input), }; if (debug_parse_pass(source_file)) return COMMAND_FAIL; return COMMAND_OK; } enum Command_Result default_command(struct Command_Arguments* arguments) { struct String source = read_file(arguments->input); struct Source_File source_file = { .source = source, .path = string_from_static_c_string(arguments->input), }; debug_lex_pass(source); printf("\n"); if (debug_parse_pass(source_file)) return COMMAND_FAIL; printf("\n"); return COMMAND_OK; } typedef enum Command_Result (*Command_Function)(struct Command_Arguments*); struct Command_Definition { bool is_default; const ascii* name; const ascii short_name; Command_Function function; }; struct Command_Definition command_definitions[] = { { .name = "test-lex", .function = test_lex_command }, { .name = "test-parse", .function = test_parse_command }, { .name = "version", .short_name = 'v', .function = version_command }, { .name = "help", .short_name = '?', .function = help_command }, { .is_default = true, .function = default_command }, }; int32 main(const int32 argc, ascii* argv[]) { if (argc < 2) { usage(); return EXIT_FAILURE; } bool have_command = false; ascii *command_name = nil, short_command_name = '\0', *input = nil; for (uint a = 1; a < argc; ++a) { ascii* arg = argv[a]; if (arg[0] == '-') { check(!have_command, "multiple commands given"); if (arg[1] == '-') { check(!command_name, "multiple full-name commands given"); command_name = &arg[2]; } else { check(!short_command_name, "multiple short-name commands given"); short_command_name = arg[1]; } have_command = true; } else { check(!input, "multiple inputs given"); input = arg; } } struct Command_Arguments arguments = { .input = input }; for (uint d = 0; d < ARRAY_SIZE(command_definitions); ++d) { struct Command_Definition* definition = &command_definitions[d]; if (definition->is_default || (command_name && strcmp(definition->name, command_name) == 0) || (short_command_name && definition->short_name == short_command_name)) { switch (definition->function(&arguments)) { case COMMAND_OK: return EXIT_SUCCESS; case COMMAND_FAIL: return EXIT_FAILURE; } } } }