Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-server

Source of release 0.5.1

0.5.1 / src / internal / routes.lucb

## Registration is single-threaded before start. The frozen table is read by all
## network threads; application route identifiers contain no callback pointers.

from luce_std import files
import internal.storage
import internal.url

pub enum Kind as u8:
    application = 0
    static_files = 1
    websocket = 2

pub struct Route:
    pub var method: str
    pub var pattern: str
    pub var kind: Kind
    pub var root: files.Directory

    pub func destroy():
        storage.release(self.method)
        storage.release(self.pattern)
        self.root.destroy()

pub struct Table:
    pub var entries: Route[128]
    pub var count: usize

    pub func add(method: str, pattern: str, kind: Kind = Kind.application) -> usize!:
        try url.validate_pattern(pattern)
        if method.length == 0:
            error(storage.invalid, "a route needs a method")
        for byte in method.bytes:
            if byte < 'A' or byte > 'Z':
                error(storage.invalid, "route methods must be uppercase ASCII")
        for entry in self.entries[..<self.count]:
            if entry.method == method and entry.pattern == pattern and ((entry.kind == Kind.static_files) == (kind == Kind.static_files)):
                error(storage.conflict, "this route is already registered")
        if self.count == self.entries.length:
            error(storage.limit, "the route table is full")
        let copied_method = try storage.copy(method)
        errdefer storage.release(copied_method)
        let copied_pattern = try storage.copy(pattern)
        errdefer storage.release(copied_pattern)
        self.entries[self.count] = Route(method = copied_method, pattern = copied_pattern, kind = kind)
        let index = self.count
        self.count += 1
        return index

    ## Exact method routes take precedence over HEAD's GET fallback. A literal
    ## route outranks a parameter route; otherwise registration order is stable.
    pub func find(method: str, path: str) -> usize?:
        var fallback: usize? = none
        for pass in 0..<2:
            for index in 0..<self.count:
                let entry = self.entries[index]
                if entry.kind == Kind.static_files:
                    continue
                let wanted = "GET" if pass == 1 else method
                if entry.method != wanted or not url.matches(entry.pattern, path):
                    continue
                if entry.pattern == path:
                    return index
                if fallback == none:
                    fallback = index
            if fallback != none:
                return fallback
            if method != "HEAD":
                break
        return none

    ## An application owns its path for every method. A broad static mount must
    ## not hide its automatic OPTIONS or method-not-allowed response.
    pub func contains_path(path: str) -> bool:
        for entry in self.entries[..<self.count]:
            if entry.kind != Kind.static_files and url.matches(entry.pattern, path):
                return true
        return false

    pub func mount(path: str) -> usize?:
        var result: usize? = none
        var longest: usize = 0
        for index in 0..<self.count:
            let entry = self.entries[index]
            if entry.kind != Kind.static_files:
                continue
            let prefix = entry.pattern
            if prefix == "/" or (path.length >= prefix.length and (str)path.bytes[..<prefix.length] == prefix and (path.length == prefix.length or prefix.bytes[prefix.length - 1] == '/' or path.bytes[prefix.length] == '/')):
                if prefix.length > longest:
                    result = index
                    longest = prefix.length
        return result

    pub func destroy():
        for index in 0..<self.count:
            self.entries[index].destroy()
        self.count = 0