Skip to content

Commit

Permalink
web-server: Proper URL resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 18, 2023
1 parent 136262e commit a6a1c45
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/apps/hideo-about/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Ui::Child app() {
"LICENSE");

auto closeBtn = Ui::button(
Ui::bindBubble<Events::ExitEvent>(Ok()),
Ui::bindBubble<Events::ExitEvent>(),
Ui::ButtonStyle::primary(),
"CLOSE");

Expand Down
36 changes: 0 additions & 36 deletions src/clis/server/main.cpp

This file was deleted.

5 changes: 3 additions & 2 deletions src/compile_flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
-I./
-I./apps
-I./clis
-I./srvs
-I./embed
-I./impls
-I./kernel
-I./libs
-I./specs
-I./impls
-I./srvs
-I./web
-I../.cutekit/extern/cute-engineering

-D__ck_sys_linux__
Expand Down
41 changes: 40 additions & 1 deletion src/impls/impl-efi/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ struct ConOut : public Sys::Fd {
Res<Strong<Fd>> dup() override {
notImplemented();
}

Res<Cons<Strong<Fd>, SocketAddr>> accept() override {
notImplemented();
}

Res<Stat> stat() override {
return Ok<Stat>();
}
};

struct FileProto : public Sys::Fd {
Expand Down Expand Up @@ -95,7 +103,7 @@ struct FileProto : public Sys::Fd {
u64 current = 0;
try$(_proto->getPosition(_proto, &current));

usize bufSize;
usize bufSize = 0;
// NOTE: This is expectected to fail
(void)_proto->getInfo(_proto, &Efi::FileInfo::GUID, &bufSize, nullptr);

Expand Down Expand Up @@ -124,6 +132,25 @@ struct FileProto : public Sys::Fd {
Res<Strong<Fd>> dup() override {
notImplemented();
}

Res<Cons<Strong<Fd>, SocketAddr>> accept() override {
notImplemented();
}

Res<Stat> stat() override {
Efi::FileInfo *info = nullptr;
usize bufSize = 0;
// NOTE: This is expectected to fail
(void)_proto->getInfo(_proto, &Efi::FileInfo::GUID, &bufSize, nullptr);

Buf<u8> buf;
buf.resize(bufSize, 0);

info = (Efi::FileInfo *)buf.buf();
try$(_proto->getInfo(_proto, &Efi::FileInfo::GUID, &bufSize, info));

return Ok<Stat>(info->fileSize);
}
};

Res<Strong<Sys::Fd>> createIn() {
Expand All @@ -138,6 +165,18 @@ Res<Strong<Sys::Fd>> createErr() {
return Ok(makeStrong<ConOut>(Efi::st()->stdErr));
}

/* --- Sockets -------------------------------------------------------------- */

Res<Strong<Sys::Fd>> connectTcp(SocketAddr) {
notImplemented();
}

Res<Strong<Sys::Fd>> listenTcp(SocketAddr) {
notImplemented();
}

/* --- Files ---------------------------------------------------------------- */

static Opt<Json::Value> _index = NONE;

static Res<Url::Path> resolve(Url::Url url) {
Expand Down
2 changes: 1 addition & 1 deletion src/impls/impl-sdl/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ struct SdlHost :
}

case SDL_QUIT: {
bubble<Events::ExitEvent>(*this, Ok());
bubble<Events::ExitEvent>(*this);
break;
}

Expand Down
18 changes: 18 additions & 0 deletions src/impls/impl-skift/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ struct VmoFd : public Sys::Fd {
Res<Strong<Fd>> dup() override {
notImplemented();
}

Res<Cons<Strong<Fd>, SocketAddr>> accept() override {
notImplemented();
}

Res<Stat> stat() override {
return Ok<Stat>();
}
};

Res<Strong<Sys::Fd>> openFile(Url::Url url) {
Expand Down Expand Up @@ -74,6 +82,16 @@ Res<Vec<Sys::DirEntry>> readDir(Url::Url) {
notImplemented();
}

/* --- Sockets -------------------------------------------------------------- */

Res<Strong<Sys::Fd>> connectTcp(SocketAddr) {
notImplemented();
}

Res<Strong<Sys::Fd>> listenTcp(SocketAddr) {
notImplemented();
}

/* --- Time ----------------------------------------------------------------- */

TimeStamp now() {
Expand Down
5 changes: 0 additions & 5 deletions src/libs/karm-base/res.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ template <typename T = None>
struct Ok {
T inner;

template <typename... Args>
static constexpr Ok emplace(Args &&...args) {
return Ok{std::forward<Args>(args)...};
}

template <typename... Args>
always_inline constexpr Ok(Args &&...args)
: inner(std::forward<Args>(args)...) {}
Expand Down
8 changes: 8 additions & 0 deletions src/libs/karm-base/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ always_inline constexpr Opt<usize> indexOf(T const &slice, U const &needle) {
return NONE;
}

template <Sliceable T, typename U = T::Inner>
always_inline constexpr Opt<usize> lastIndexOf(T const &slice, U const &needle) {
for (usize i = slice.len(); i > 0; i--)
if (slice[i - 1] == needle)
return i - 1;
return NONE;
}

always_inline Opt<usize> search(Sliceable auto const &slice, auto cmp) {
if (slice.len() == 0) {
return NONE;
Expand Down
2 changes: 1 addition & 1 deletion src/libs/karm-ui/scafold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Child controls(TitlebarStyle style) {
cond(style == TitlebarStyle::DEFAULT),
button(bindBubble<Events::RequestMaximizeEvent>(), ButtonStyle::subtle(), Mdi::CROP_SQUARE) |
cond(style == TitlebarStyle::DEFAULT),
button(bindBubble<Events::ExitEvent>(Ok()), ButtonStyle::subtle(), Mdi::CLOSE));
button(bindBubble<Events::ExitEvent>(), ButtonStyle::subtle(), Mdi::CLOSE));
}

Child titlebar(Mdi::Icon icon, String title, TitlebarStyle style) {
Expand Down
30 changes: 20 additions & 10 deletions src/specs/url/url.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#include <karm-base/ctype.h>

#include "url.h"

namespace Url {

/* --- Path ----------------------------------------------------------------- */

Path Path::parse(Io::SScan &s, bool inUrl) {
Path Path::parse(Io::SScan &s, bool inUrl, bool stopAtWhitespace) {
Path path;

if (s.skip(SEP)) {
path.rooted = true;
}

s.begin();
while (not s.ended() and (inUrl or (s.curr() != '?' and s.curr() != '#'))) {
while (not s.ended() and (inUrl or (s.curr() != '?' and s.curr() != '#')) and
(not stopAtWhitespace or not isAsciiSpace(s.curr()))) {
if (s.curr() == SEP) {
path._parts.pushBack(s.end());
s.next();
Expand All @@ -23,34 +26,31 @@ Path Path::parse(Io::SScan &s, bool inUrl) {
}

auto last = s.end();
if (last.len() > 0) {
if (last.len() > 0)
path._parts.pushBack(last);
}

return path;
}

Path Path::parse(Str str, bool inUrl) {
Path Path::parse(Str str, bool inUrl, bool stopAtWhitespace) {
Io::SScan s{str};
return parse(s, inUrl);
return parse(s, inUrl, stopAtWhitespace);
}

void Path::normalize() {
Vec<String> parts;
for (auto part : iter()) {
if (part == ".") {
if (part == ".")
continue;
}

if (part == "..") {
if (parts.len() > 0) {
parts.popBack();
} else if (not rooted) {
parts.pushBack(part);
}
} else {
} else
parts.pushBack(part);
}
}

_parts = parts;
Expand Down Expand Up @@ -161,6 +161,16 @@ bool Url::isUrl(Str str) {
s.skip(':');
}

Url Url::join(Path const &other) const {
Url url = *this;
url.path = url.path.join(other);
return url;
}

Url Url::join(Str other) const {
return join(Path::parse(other));
}

Str Url::basename() const {
return path.basename();
}
Expand Down
37 changes: 33 additions & 4 deletions src/specs/url/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ struct Path {
bool rooted = false;
Vec<String> _parts;

static Path parse(Io::SScan &s, bool inUrl = false);
static Path parse(Io::SScan &s, bool inUrl = false, bool stopAtWhitespace = false);

static Path parse(Str str, bool inUrl = false);
static Path parse(Str str, bool inUrl = false, bool stopAtWhitespace = false);

void normalize();

Expand Down Expand Up @@ -53,6 +53,15 @@ struct Path {
}

auto operator<=>(Path const &) const = default;

Str sufix() const {
if (not _parts.len())
return "";
auto dotIndex = lastIndexOf(last(_parts), '.');
if (not dotIndex.has())
return "";
return next(last(_parts), *dotIndex + 1);
}
};

/* --- Url ------------------------------------------------------------------ */
Expand All @@ -76,6 +85,10 @@ struct Url {
return path.rooted;
}

Url join(Path const &other) const;

Url join(Str other) const;

Str basename() const;

void append(Str part) {
Expand Down Expand Up @@ -111,16 +124,32 @@ inline auto operator""_url(char const *str, usize len) {
return Url::Url::parse({str, len});
}

inline auto operator/(Url::Url const &url, Url::Path const &path) {
return url.join(path);
}

inline auto operator/(Url::Url const &url, Str path) {
return url.join(path);
}

inline auto operator/(Url::Path const &path, Url::Path const &other) {
return path.join(other);
}

inline auto operator/(Url::Path const &path, Str other) {
return path.join(other);
}

template <>
struct Karm::Fmt::Formatter<Url::Path> {
Res<usize> format(Io::TextWriter &writer, Url::Path path) {
Res<usize> format(Io::TextWriter &writer, Url::Path const &path) {
return path.write(writer);
}
};

template <>
struct Karm::Fmt::Formatter<Url::Url> {
Res<usize> format(Io::TextWriter &writer, Url::Url url) {
Res<usize> format(Io::TextWriter &writer, Url::Url const &url) {
return url.write(writer);
}
};
Loading

0 comments on commit a6a1c45

Please sign in to comment.