Compare commits

11 Commits

Author SHA1 Message Date
adf11bd144 Fix plucky 2026-02-04 22:13:52 -06:00
f53bacb18b Add node 2026-02-04 21:28:59 -06:00
94eb56aad6 Fix gitea 2026-02-04 21:00:34 -06:00
4efd654941 Add gitea workflows 2026-02-04 20:39:57 -06:00
34b484f633 Add gitea container 2026-02-04 18:38:55 -06:00
96ba20d65c Remove expensive builds on commit 2026-02-04 18:29:57 -06:00
f825c2616a Change version 0.0.1 2026-01-30 19:21:16 -06:00
67874eba30 Make portable json support absolute paths as an option, for android crosslang 2026-01-15 13:02:49 -06:00
8eb7e050c0 Fix array 2026-01-14 15:24:55 -06:00
e96b359bb8 Add route server 2026-01-12 14:05:38 -06:00
848fca7f36 Add route server 2026-01-12 12:25:06 -06:00
23 changed files with 985 additions and 114 deletions

44
.gitea/workflows/tag.yaml Normal file
View File

@@ -0,0 +1,44 @@
name: Build and Deploy on Tag
on:
push:
tags:
- 'v*'
env:
GITEA_AUTH: ${{ secrets.MY_GITEA_AUTH }}
jobs:
build-arch:
runs-on: arch-builder
steps:
- run: pacman --noconfirm -Sy nodejs npm
- uses: actions/checkout@v4
- run: pacman --noconfirm -Sy mbedtls curl
- run: pacman --config /opt/cross/ppc/pacman.conf --noconfirm -Sy mbedtls
- run: cp Packaging/Linux/PKGBUILD /home/build/PKGBUILD
- run: cp Packaging/Linux/build-arch.sh /home/build/build-arch.sh
- run: chmod 755 /home/build/build-arch.sh
- run: chown build:build /home/build/PKGBUILD
- run: chown build:build /home/build/build-arch.sh
- run: su build -c /home/build/build-arch.sh
build-jammy:
runs-on: deb-builder-jammy
steps:
- uses: actions/checkout@v4
- name: Build for jammy, noble
run: |
bash build-ubuntu-jammy.sh
bash push-ubuntu-jammy.sh
working-directory: ./Packaging/Linux
build-plucky:
runs-on: deb-builder-plucky
steps:
- uses: actions/checkout@v4
- name: Build for plucky, resolute
run: |
bash build-ubuntu-plucky.sh
bash push-ubuntu-plucky.sh
working-directory: ./Packaging/Linux
- uses: akkuman/gitea-release-action@v1
env:
NODE_OPTIONS: '--experimental-fetch' # if nodejs < 18

View File

@@ -1,18 +1,21 @@
version: 39 version: 43
jobs: jobs:
- name: Build for x86_64 - name: Build for x86_64
steps: steps:
- !CheckoutStep - type: CheckoutStep
name: Checkout name: Checkout
cloneCredential: !DefaultCredential {} cloneCredential:
type: DefaultCredential
withLfs: true withLfs: true
withSubmodules: false withSubmodules: false
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL condition: SUCCESSFUL
- !CommandStep optional: false
- type: CommandStep
name: Execute build name: Execute build
runInContainer: true runInContainer: true
image: onedev.site.tesses.net/dependencies/dependencies:latest image: onedev.site.tesses.net/dependencies/dependencies:latest
interpreter: !DefaultInterpreter interpreter:
type: DefaultInterpreter
commands: | commands: |
mkdir build mkdir build
cd build cd build
@@ -20,72 +23,28 @@ jobs:
make -j12 make -j12
make install DESTDIR=out make install DESTDIR=out
useTTY: true useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL condition: SUCCESSFUL
- !BuildImageStep optional: false
- type: BuildImageStep
name: Build Docker Image name: Build Docker Image
dockerfile: Dockerfile.run dockerfile: Dockerfile.run
output: !RegistryOutput output:
tags: onedev.site.tesses.net/tesses-framework/tesses-framework:latest onedev.site.tesses.net/tesses-framework/tesses-framework:@commit_hash@ type: RegistryOutput
tags: onedev.site.tesses.net/tesses-framework/tesses-framework:latest onedev.site.tesses.net/tesses-framework/tesses-framework:@commit_hash@ git.tesseslanguage.com/tesses50/tesses-framework:latest git.tesseslanguage.com/tesses50/tesses-framework:@commit_hash@
registryLogins: registryLogins:
- registryUrl: '@server_url@' - registryUrl: '@server_url@'
userName: '@job_token@' userName: '@job_token@'
passwordSecret: dockersecret passwordSecret: dockersecret
- registryUrl: git.tesseslanguage.com
userName: tesses50
passwordSecret: GITEA_AUTH
platforms: linux/amd64 platforms: linux/amd64
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL condition: SUCCESSFUL
- !CommandStep optional: false
name: Build archlinux
runInContainer: true
image: git.tesseslanguage.com/tesses50/arch-builds:2025-11-11
interpreter: !DefaultInterpreter
commands: |
pacman --noconfirm -Sy mbedtls curl
pacman --config /opt/cross/ppc/pacman.conf --noconfirm -Sy mbedtls
cp Packaging/Linux/PKGBUILD /home/build/PKGBUILD
cp Packaging/Linux/build-arch.sh /home/build/build-arch.sh
chmod 755 /home/build/build-arch.sh
chown build:build /home/build/PKGBUILD
chown build:build /home/build/build-arch.sh
su build -c /home/build/build-arch.sh
envVars:
- name: GITEA_AUTH
value: '@secret:GITEA_AUTH@'
useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
- !CommandStep
name: Build and Publish Deb Package
runInContainer: true
image: onedev.site.tesses.net/dependencies/debbuilder/jammy:latest
interpreter: !DefaultInterpreter
commands: |
cd Packaging/Linux
bash build-ubuntu-jammy.sh
bash push-ubuntu-jammy.sh
envVars:
- name: GITEA_AUTH
value: '@secret:GITEA_AUTH@'
- name: BUILD_NO
value: '@build_number@'
useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
- !CommandStep
name: Build and Publish Deb Package (Plucky)
runInContainer: true
image: onedev.site.tesses.net/dependencies/debbuilder/plucky:latest
interpreter: !DefaultInterpreter
commands: |
cd Packaging/Linux
bash build-ubuntu-plucky.sh
bash push-ubuntu-plucky.sh
envVars:
- name: GITEA_AUTH
value: '@secret:GITEA_AUTH@'
- name: BUILD_NO
value: '@build_number@'
useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
triggers: triggers:
- !BranchUpdateTrigger - type: BranchUpdateTrigger
branches: master branches: master
userMatch: anyone
projects: tesses-framework projects: tesses-framework
retryCondition: never retryCondition: never
maxRetries: 3 maxRetries: 3

View File

@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(TessesFramework VERSION 1.0.0) set(TESSESFRAMEWORK_MAJOR_VERSION 0)
set(TESSESFRAMEWORK_MINOR_VERSION 0)
set(TESSESFRAMEWORK_PATCH_VERSION 1)
project(TessesFramework VERSION ${TESSESFRAMEWORK_MAJOR_VERSION}.${TESSESFRAMEWORK_MINOR_VERSION}.${TESSESFRAMEWORK_PATCH_VERSION})
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
@@ -9,6 +13,7 @@ src/Random.cpp
src/Date/Date.cpp src/Date/Date.cpp
src/Http/FileServer.cpp src/Http/FileServer.cpp
src/Http/MountableServer.cpp src/Http/MountableServer.cpp
src/Http/RouteServer.cpp
src/Http/CallbackServer.cpp src/Http/CallbackServer.cpp
src/Http/HttpServer.cpp src/Http/HttpServer.cpp
src/Http/HttpUtils.cpp src/Http/HttpUtils.cpp
@@ -408,6 +413,11 @@ add_executable(trng apps/trng.cpp)
target_link_libraries(trng PUBLIC tessesframework) target_link_libraries(trng PUBLIC tessesframework)
install(TARGETS trng DESTINATION "${CMAKE_INSTALL_BINDIR}") install(TARGETS trng DESTINATION "${CMAKE_INSTALL_BINDIR}")
add_executable(twatch apps/twatch.cpp)
target_link_libraries(twatch PUBLIC tessesframework)
install(TARGETS twatch DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif() endif()
include(InstallRequiredSystemLibraries) include(InstallRequiredSystemLibraries)

View File

@@ -1,6 +1,6 @@
# Maintainer: Mike Nolan <tesses@tesses.net> # Maintainer: Mike Nolan <tesses@tesses.net>
pkgname=tesses-framework # '-bzr', '-git', '-hg' or '-svn' pkgname=tesses-framework # '-bzr', '-git', '-hg' or '-svn'
pkgver=1.0.0 pkgver=0.0.1
pkgrel=1 pkgrel=1
pkgdesc="" pkgdesc=""
arch=('x86_64' 'powerpc') arch=('x86_64' 'powerpc')
@@ -18,21 +18,7 @@ options=(!strip)
else else
options=(!buildflags !strip) options=(!buildflags !strip)
fi fi
# Please refer to the 'USING VCS SOURCES' section of the PKGBUILD man page for
# a description of each element in the source array.
pkgver() {
cd "$srcdir/${pkgname}"
# The examples below are not absolute and need to be adapted to each repo. The
# primary goal is to generate version numbers that will increase according to
# pacman's version comparisons with later commits to the repo. The format
# VERSION='VER_NUM.rREV_NUM.HASH', or a relevant subset in case VER_NUM or HASH
# are not available, is recommended.
# Git, no tags available
printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}
prepare() { prepare() {
cd "$srcdir/${pkgname}" cd "$srcdir/${pkgname}"

View File

@@ -1,2 +1 @@
export BUILD=$(($BUILD_NO-163)) export DEB_VERSION=0.0.1
export DEB_VERSION=1.0.0-$BUILD

View File

@@ -1,6 +1,11 @@
#pragma once #pragma once
#define TESSES_FRAMEWORK_FLAG_OFF 0 #define TESSES_FRAMEWORK_FLAG_OFF 0
#define TESSES_FRAMEWORK_FLAG_ON 1 #define TESSES_FRAMEWORK_FLAG_ON 1
#define TESSES_FRAMEWORK_MAJOR @TESSESFRAMEWORK_MAJOR_VERSION@
#define TESSES_FRAMEWORK_MINOR @TESSESFRAMEWORK_MINOR_VERSION@
#define TESSES_FRAMEWORK_PATCH @TESSESFRAMEWORK_PATCH_VERSION@
#if defined(TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_SQLITE@) && !defined(TESSESFRAMEWORK_ENABLE_SQLITE) #if defined(TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_SQLITE@) && !defined(TESSESFRAMEWORK_ENABLE_SQLITE)
#define TESSESFRAMEWORK_ENABLE_SQLITE #define TESSESFRAMEWORK_ENABLE_SQLITE
#endif #endif

24
apps/twatch.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <TessesFramework/TessesFramework.hpp>
using namespace Tesses::Framework;
using namespace Tesses::Framework::Filesystem;
using namespace Tesses::Framework::Filesystem::Literals;
int main(int argc, char** argv)
{
TF_Init();
if(argc<2)
{
std::cout << "USAGE " << argv[0] << " <file|dir>" << std::endl;
return 1;
}
auto watcher=FSWatcher::Create(LocalFS,VFSPath{argv[1]});
watcher->events = FSWatcherEventType::All;
watcher->event = [](FSWatcherEvent& evt)->void{
std::cout << evt.ToString() << std::endl;
};
watcher->SetEnabled(true);
TF_RunEventLoop();
return 0;
}

View File

@@ -139,11 +139,35 @@ class MyOtherWebServer : public IHttpServer
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
TF_InitWithConsole(); TF_InitWithConsole();
std::shared_ptr<RouteServer> routeSvr = std::make_shared<RouteServer>();
routeSvr->Get("/name/{name}/greeting",[](ServerContext& ctx)->bool{
std::string name;
if(ctx.pathArguments.TryGetFirst("name",name))
{
ctx.WithMimeType("text/plain").SendText("Hello " + name);
}
else {
ctx.WithMimeType("text/plain").SendText("Please provide a name");
}
return true;
});
routeSvr->Get("/name/{name}/length",[](ServerContext& ctx)->bool{
std::string name;
if(ctx.pathArguments.TryGetFirst("name",name))
{
ctx.WithMimeType("text/plain").SendText("The length of the name is " + std::to_string(name.size()));
}
else {
ctx.WithMimeType("text/plain").SendText("Please provide a name");
}
return true;
});
std::shared_ptr<MyOtherWebServer> myo = std::make_shared<MyOtherWebServer>(); std::shared_ptr<MyOtherWebServer> myo = std::make_shared<MyOtherWebServer>();
std::shared_ptr<MyWebServer> mws = std::make_shared<MyWebServer>(); std::shared_ptr<MyWebServer> mws = std::make_shared<MyWebServer>();
std::shared_ptr<MountableServer> mountable = std::make_shared<MountableServer>(myo); std::shared_ptr<MountableServer> mountable = std::make_shared<MountableServer>(myo);
mountable->Mount("/mymount/",mws); mountable->Mount("/mymount/",mws);
mountable->Mount("/routeSvr/", routeSvr);
HttpServer server(10001,mountable); HttpServer server(10001,mountable);
server.StartAccepting(); server.StartAccepting();
TF_RunEventLoop(); TF_RunEventLoop();

View File

@@ -10,6 +10,7 @@
#include <functional> #include <functional>
#include "Threading/Mutex.hpp" #include "Threading/Mutex.hpp"
#include <optional> #include <optional>
#include <atomic>
namespace Tesses::Framework namespace Tesses::Framework
{ {

View File

@@ -42,6 +42,9 @@ namespace Tesses::Framework::Filesystem
void Lock(VFSPath path); void Lock(VFSPath path);
void Unlock(VFSPath path); void Unlock(VFSPath path);
protected:
std::shared_ptr<FSWatcher> CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
}; };
extern std::shared_ptr<LocalFilesystem> LocalFS; extern std::shared_ptr<LocalFilesystem> LocalFS;
} }

View File

@@ -8,6 +8,7 @@
namespace Tesses::Framework::Filesystem namespace Tesses::Framework::Filesystem
{ {
class StatVFSData { class StatVFSData {
public: public:
uint64_t BlockSize; uint64_t BlockSize;
@@ -29,13 +30,15 @@ namespace Tesses::Framework::Filesystem
static std::vector<std::string> SplitPath(std::string path); static std::vector<std::string> SplitPath(std::string path);
std::vector<std::string> path; std::vector<std::string> path;
VFSPath(); VFSPath();
explicit VFSPath(const char* path) : VFSPath(std::string(path))
{}
VFSPath(std::vector<std::string> path); VFSPath(std::vector<std::string> path);
VFSPath(std::string path); VFSPath(std::string path);
VFSPath(VFSPath p, std::string subent); VFSPath(VFSPath p, std::string subent);
VFSPath(VFSPath p, VFSPath p2); VFSPath(VFSPath p, VFSPath p2);
//does not check for ?
static VFSPath ParseUriPath(std::string path);
VFSPath GetParent() const; VFSPath GetParent() const;
VFSPath CollapseRelativeParents() const; VFSPath CollapseRelativeParents() const;
@@ -117,7 +120,74 @@ namespace Tesses::Framework::Filesystem
VFSPathEnumeratorItterator end(); VFSPathEnumeratorItterator end();
}; };
enum class FSWatcherEventType {
None = 0,
//IN_ACCESS
Accessed=1,
//IN_ATTRIB
AttributeChanged =2,
//IN_CLOSE_WRITE
Writen = 4,
//IN_CLOSE_NOWRITE
Read = 8,
//IN_CREATE
Created = 16,
//IN_DELETE
Deleted = 32,
//IN_DELETE_SELF
WatchEntryDeleted = 64,
//IN_MODIFY
Modified = 128,
//IN_MOVE_SELF
WatchEntryMoved = 256,
//IN_MOVED_FROM
MoveOld = 512,
//IN_MOVED_TO
MoveNew = 1024,
//IN_OPEN
Opened = 2048,
//IN_CLOSE
Closed = Writen | Read,
//IN_MOVE
Moved = MoveOld | MoveNew,
//IN_ALL_EVENTS
All = Accessed | AttributeChanged | Created | Deleted | WatchEntryDeleted | Modified | WatchEntryMoved | Opened | Closed | Moved
};
struct FSWatcherEvent {
//the file or source on move
VFSPath src;
//the dest when moving
VFSPath dest;
FSWatcherEventType type;
bool isDir;
bool IsEvent(FSWatcherEventType e);
std::string ToString();
};
class VFS;
class FSWatcher {
private:
std::shared_ptr<VFS> vfs;
VFSPath path;
protected:
std::atomic<bool> enabled=false;
virtual void SetEnabledImpl(bool enabled);
public:
FSWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
std::function<void(FSWatcherEvent&)> event;
FSWatcherEventType events = FSWatcherEventType::All;
bool GetEnabled();
void SetEnabled(bool val);
std::shared_ptr<VFS> GetFilesystem();
const VFSPath& GetPath();
virtual ~FSWatcher() = default;
static std::shared_ptr<FSWatcher> Create(std::shared_ptr<VFS> vfs, VFSPath path);
};
class VFS { class VFS {
public: public:
@@ -156,8 +226,24 @@ namespace Tesses::Framework::Filesystem
virtual void Lock(VFSPath path); virtual void Lock(VFSPath path);
virtual void Unlock(VFSPath path); virtual void Unlock(VFSPath path);
virtual ~VFS(); virtual ~VFS();
virtual void Close(); virtual void Close();
protected:
virtual std::shared_ptr<FSWatcher> CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
friend class FSWatcher;
}; };
namespace Literals
{
inline VFSPath operator""_tpath(const char* path)
{
return VFSPath(path);
}
}
} }

View File

@@ -23,6 +23,8 @@ namespace Tesses::Framework::Http
HttpDictionary requestHeaders; HttpDictionary requestHeaders;
HttpDictionary responseHeaders; HttpDictionary responseHeaders;
HttpDictionary queryParams; HttpDictionary queryParams;
//used by path
HttpDictionary pathArguments;
std::string path; std::string path;
std::string originalPath; std::string originalPath;
std::string method; std::string method;

View File

@@ -25,6 +25,7 @@ namespace Tesses::Framework::Http
int64_t GetPosition(); int64_t GetPosition();
size_t Read(uint8_t* buffer, size_t len); size_t Read(uint8_t* buffer, size_t len);
size_t Write(const uint8_t* buffer, size_t len); size_t Write(const uint8_t* buffer, size_t len);
void Close();
~HttpStream(); ~HttpStream();
}; };
} }

View File

@@ -0,0 +1,41 @@
#pragma once
#include "HttpServer.hpp"
#include "../Filesystem/VFSFix.hpp"
#include "../Filesystem/VFS.hpp"
namespace Tesses::Framework::Http
{
using ServerRequestHandler = std::function<bool(ServerContext&)>;
class RouteServer : public IHttpServer
{
class RouteServerRoute {
public:
std::vector<std::pair<std::string,bool>> parts;
std::string method;
ServerRequestHandler handler;
RouteServerRoute() = default;
RouteServerRoute(std::string route, std::string method, ServerRequestHandler handler);
bool Equals(Tesses::Framework::Filesystem::VFSPath& path, HttpDictionary& args);
};
std::vector<RouteServerRoute> routes;
std::shared_ptr<IHttpServer> root;
public:
RouteServer() = default;
RouteServer(std::shared_ptr<IHttpServer> root);
void Get(std::string pattern, ServerRequestHandler handler);
void Post(std::string pattern, ServerRequestHandler handler);
void Put(std::string pattern, ServerRequestHandler handler);
void Patch(std::string pattern, ServerRequestHandler handler);
void Delete(std::string pattern, ServerRequestHandler handler);
void Trace(std::string pattern, ServerRequestHandler handler);
void Options(std::string pattern, ServerRequestHandler handler);
void Add(std::string method, std::string pattern, ServerRequestHandler handler);
bool Handle(ServerContext& ctx);
};
}

View File

@@ -9,6 +9,7 @@
#include "Http/ChangeableServer.hpp" #include "Http/ChangeableServer.hpp"
#include "Http/CGIServer.hpp" #include "Http/CGIServer.hpp"
#include "Http/BasicAuthServer.hpp" #include "Http/BasicAuthServer.hpp"
#include "Http/RouteServer.hpp"
#include "Streams/FileStream.hpp" #include "Streams/FileStream.hpp"
#include "Streams/MemoryStream.hpp" #include "Streams/MemoryStream.hpp"
#include "Streams/NetworkStream.hpp" #include "Streams/NetworkStream.hpp"

View File

@@ -10,6 +10,13 @@
#include <utime.h> #include <utime.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#endif #endif
#include "TessesFramework/Threading/Thread.hpp"
#if defined(__linux__)
#include <poll.h>
#include <sys/inotify.h>
#include <unistd.h>
#endif
namespace Tesses::Framework::Filesystem namespace Tesses::Framework::Filesystem
{ {
#if defined(_WIN32) #if defined(_WIN32)
@@ -249,8 +256,184 @@ namespace Tesses::Framework::Filesystem
std::error_code error; std::error_code error;
std::filesystem::remove(VFSPathToSystem(path),error); std::filesystem::remove(VFSPathToSystem(path),error);
} }
#if defined(__linux__)
class INotifyWatcher : public FSWatcher {
std::shared_ptr<Threading::Thread> thrd;
static uint32_t to_linux_mask(FSWatcherEventType flags)
{
uint32_t lflags = 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Accessed) != 0) ? IN_ACCESS : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::AttributeChanged) != 0) ? IN_ATTRIB : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Writen) != 0) ? IN_CLOSE_WRITE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Read) != 0) ? IN_CLOSE_NOWRITE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Created) != 0) ? IN_CREATE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Deleted) != 0) ? IN_DELETE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::WatchEntryDeleted) != 0) ? IN_DELETE_SELF : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Modified) != 0) ? IN_MODIFY : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::WatchEntryMoved) != 0) ? IN_MOVE_SELF : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::MoveOld) != 0) ? IN_MOVED_FROM : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::MoveNew) != 0) ? IN_MOVED_TO : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Opened) != 0) ? IN_OPEN : 0;
return lflags;
}
static FSWatcherEventType from_linux_mask(uint32_t lflags)
{
uint32_t flags = 0;
flags |= ((lflags & IN_ACCESS) != 0) ? (uint32_t)FSWatcherEventType::Accessed : 0;
flags |= ((lflags & IN_ATTRIB) != 0) ? (uint32_t)FSWatcherEventType::AttributeChanged : 0;
flags |= ((lflags & IN_CLOSE_WRITE) != 0) ? (uint32_t)FSWatcherEventType::Writen : 0;
flags |= ((lflags & IN_CLOSE_NOWRITE) != 0) ? (uint32_t)FSWatcherEventType::Read : 0;
flags |= ((lflags & IN_CREATE) != 0) ? (uint32_t)FSWatcherEventType::Created : 0;
flags |= ((lflags & IN_DELETE) != 0) ? (uint32_t)FSWatcherEventType::Deleted : 0;
flags |= ((lflags & IN_DELETE_SELF) != 0) ? (uint32_t)FSWatcherEventType::WatchEntryDeleted : 0;
flags |= ((lflags & IN_MODIFY) != 0) ? (uint32_t)FSWatcherEventType::Modified : 0;
flags |= ((lflags & IN_MOVE_SELF) != 0) ? (uint32_t)FSWatcherEventType::WatchEntryMoved : 0;
flags |= ((lflags & IN_MOVED_FROM) != 0) ? (uint32_t)FSWatcherEventType::MoveOld : 0;
flags |= ((lflags & IN_MOVED_TO) != 0) ? (uint32_t)FSWatcherEventType::MoveNew : 0;
flags |= ((lflags & IN_OPEN) != 0) ? (uint32_t)FSWatcherEventType::Opened : 0;
return (FSWatcherEventType)flags;
}
public:
INotifyWatcher(std::shared_ptr<VFS> vfs, VFSPath path) : FSWatcher(vfs,path)
{
}
protected:
void SetEnabledImpl(bool enabled)
{
if(enabled)
{
int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (fd == -1)
{
throw std::runtime_error("Cannot init inotify");
}
auto str = this->GetFilesystem()->VFSPathToSystem(this->GetPath());
int watch = inotify_add_watch(fd, str.c_str(),to_linux_mask(this->events));
thrd = std::make_shared<Threading::Thread>([this,watch,fd]()-> void {
int cnt = 0;
struct pollfd pfd = {.fd = fd, .events = POLLIN};
std::vector<std::pair<VFSPath,uint32_t>> mvFroms;
char buf[4096]
__attribute__ ((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event;
ssize_t size;
bool fail=false;
FSWatcherEvent evt;
evt.dest = this->GetPath();
while(!fail && this->enabled)
{
cnt = poll(&pfd,1,-1);
if(cnt == -1) break;
if(cnt > 0)
{
if(pfd.revents & POLLIN)
{
for (;;) {
size = read(fd, buf, sizeof(buf));
if (size == -1 && errno != EAGAIN) {
fail=true;
break;
}
if (size <= 0)
break;
for (char *ptr = buf; ptr < buf + size;
ptr += sizeof(struct inotify_event) + event->len) {
event = (const struct inotify_event *) ptr;
VFSPath path = this->GetPath();
if(event->len)
path = path / std::string(event->name, (size_t)event->len);
if(((uint32_t)this->events & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved && event->mask & IN_MOVED_FROM)
{
mvFroms.emplace_back(path,event->cookie);
}
else if(((uint32_t)this->events & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved && event->mask & IN_MOVED_TO)
{
for(auto ittr = mvFroms.begin(); ittr != mvFroms.end(); ittr++)
{
if(ittr->second == event->cookie)
{
evt.src = ittr->first;
mvFroms.erase(ittr);
break;
}
}
evt.isDir = (event->mask & IN_ISDIR);
evt.dest = path;
evt.type = FSWatcherEventType::Moved;
if(this->event)
this->event(evt);
}
else {
evt.isDir = (event->mask & IN_ISDIR);
evt.src = path;
evt.type = from_linux_mask(event->mask);;
if(this->event)
this->event(evt);
}
if(event->mask & IN_MOVE_SELF)
{
close(fd);
return;
}
if(event->mask & IN_DELETE_SELF)
{
close(fd);
return;
}
}
}
}
}
}
close(fd);
});
}
else
{
thrd = nullptr;
}
}
public:
~INotifyWatcher()
{
this->enabled = false;
}
};
#endif
std::shared_ptr<FSWatcher> LocalFilesystem::CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path)
{
#if defined(__linux__)
return std::make_shared<INotifyWatcher>(vfs, path);
#endif
return VFS::CreateWatcher(vfs,path);
}
std::shared_ptr<LocalFilesystem> LocalFS = std::make_shared<LocalFilesystem>(); std::shared_ptr<LocalFilesystem> LocalFS = std::make_shared<LocalFilesystem>();
} }
// C:/Users/Jim/Joel // C:/Users/Jim/Joel

View File

@@ -232,7 +232,8 @@ namespace Tesses::Framework::Filesystem
if(i == this->path.size()-1 && i == toMakeRelativeTo.path.size()-1) if(i == this->path.size()-1 && i == toMakeRelativeTo.path.size()-1)
{ {
VFSPath path({this->path[this->path.size()-1]}); std::vector<std::string> paths{this->path[this->path.size()-1]};
VFSPath path(paths);
path.relative = true; path.relative = true;
return path; return path;
} }
@@ -330,6 +331,35 @@ namespace Tesses::Framework::Filesystem
this->path = p; this->path = p;
} }
VFSPath VFSPath::ParseUriPath(std::string path)
{
std::string builder = {};
VFSPath vpath;
vpath.relative=true;
if(!path.empty() && path[0] == '/') vpath.relative=false;
for(auto item : path)
{
if(item == '/')
{
if(!builder.empty())
{
vpath.path.push_back(builder);
builder.clear();
}
}
else {
builder.push_back(item);
}
}
if(!builder.empty())
{
vpath.path.push_back(builder);
}
return vpath;
}
bool VFSPath::HasExtension() const bool VFSPath::HasExtension() const
{ {
if(this->path.empty()) return false; if(this->path.empty()) return false;
@@ -358,7 +388,8 @@ namespace Tesses::Framework::Filesystem
if(ext.empty()) return; if(ext.empty()) return;
if(ext[0] != '.') if(ext[0] != '.')
{ {
str += '.' + ext; str += '.';
str += ext;
} }
else else
{ {
@@ -377,6 +408,9 @@ namespace Tesses::Framework::Filesystem
if(!str.empty()) if(!str.empty())
{ {
if(str.front() == '/') this->relative=false; if(str.front() == '/') this->relative=false;
#if defined(_WIN32)
if(str.front() == '\\') this->relative=false;
#endif
if(!this->path.empty()) if(!this->path.empty())
{ {
auto firstPartPath = this->path.front(); auto firstPartPath = this->path.front();
@@ -528,4 +562,113 @@ namespace Tesses::Framework::Filesystem
{ {
} }
std::shared_ptr<FSWatcher> VFS::CreateWatcher(std::shared_ptr<VFS> vfs,VFSPath path)
{
return std::make_shared<FSWatcher>(vfs,path);
}
void FSWatcher::SetEnabled(bool enabled)
{
if(this->enabled == enabled) return;
this->enabled = enabled;
this->SetEnabledImpl(enabled);
}
bool FSWatcher::GetEnabled()
{
return this->enabled;
}
void FSWatcher::SetEnabledImpl(bool enabled)
{
}
std::shared_ptr<VFS> FSWatcher::GetFilesystem()
{
return this->vfs;
}
const VFSPath& FSWatcher::GetPath()
{
return this->path;
}
FSWatcher::FSWatcher(std::shared_ptr<VFS> vfs, VFSPath path): vfs(vfs), path(path)
{
}
std::shared_ptr<FSWatcher> FSWatcher::Create(std::shared_ptr<VFS> vfs, VFSPath path)
{
return vfs->CreateWatcher(vfs,path);
}
bool FSWatcherEvent::IsEvent(FSWatcherEventType e)
{
if(e == FSWatcherEventType::All) return this->type != FSWatcherEventType::None;
if(e == FSWatcherEventType::Moved) return ((uint32_t)this->type & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved;
if(e == FSWatcherEventType::Closed) return ((uint32_t)this->type & (uint32_t)FSWatcherEventType::Closed) != 0;
return (uint32_t)this->type & (uint32_t)e;
}
std::string FSWatcherEvent::ToString()
{
if(IsEvent(FSWatcherEventType::Moved))
{
return (this->isDir ? "Moved directory " : "Moved file ") + this->src.ToString() + " -> " + this->dest.ToString();
}
else if(IsEvent(FSWatcherEventType::MoveOld))
{
return (this->isDir ? "Move source directory " : "Move source file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::MoveNew))
{
return (this->isDir ? "Move destination directory " : "Move destination file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Accessed))
{
return (this->isDir ? "Accessed directory " : "Accessed file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::AttributeChanged))
{
return (this->isDir ? "Changed attr on directory " : "Changed attr on file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Writen))
{
return (this->isDir ? "Finished changing directory " : "Finished writing to file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Read))
{
return (this->isDir ? "Finished reading directory " : "Finished reading from file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Created))
{
return (this->isDir ? "Created directory " : "Created file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Deleted))
{
return (this->isDir ? "Deleted directory " : "Deleted file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::WatchEntryDeleted))
{
return (this->isDir ? "Deleted watched directory " : "Deleted watched file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Modified))
{
return (this->isDir ? "Modified directory " : "Modified file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::WatchEntryMoved))
{
return (this->isDir ? "Moved watched directory " : "Moved watched file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Opened))
{
return (this->isDir ? "Opened directory " : "Opened file ") + this->src.ToString();
}
return "";
}
} }

View File

@@ -389,11 +389,8 @@ namespace Tesses::Framework::Http
static bool parseUntillBoundaryEnd(std::shared_ptr<Tesses::Framework::Streams::Stream> src, std::shared_ptr<Tesses::Framework::Streams::Stream> dest, std::string boundary) static bool parseUntillBoundaryEnd(std::shared_ptr<Tesses::Framework::Streams::Stream> src, std::shared_ptr<Tesses::Framework::Streams::Stream> dest, std::string boundary)
{ {
bool hasMore=true; bool hasMore=true;
#if defined(_WIN32)
uint8_t* checkBuffer = new uint8_t[boundary.size()]; uint8_t* checkBuffer = new uint8_t[boundary.size()];
#else
uint8_t checkBuffer[boundary.size()];
#endif
int b; int b;
size_t i = 0; size_t i = 0;
size_t i2 = 0; size_t i2 = 0;
@@ -457,9 +454,8 @@ namespace Tesses::Framework::Http
{ {
dest->Write(buffer,offsetInMem); dest->Write(buffer,offsetInMem);
} }
#if defined(_WIN32) delete[] checkBuffer;
delete checkBuffer;
#endif
return hasMore; return hasMore;
} }
@@ -566,7 +562,12 @@ namespace Tesses::Framework::Http
this->responseHeaders.SetValue("Transfer-Encoding","chunked"); this->responseHeaders.SetValue("Transfer-Encoding","chunked");
this->WriteHeaders(); this->WriteHeaders();
return std::make_shared<HttpStream>(this->strm,length,false,version == "HTTP/1.1"); auto strm = std::make_shared<HttpStream>(this->strm,length,false,version == "HTTP/1.1");
if(method == "HEAD")
{
strm->Close();
}
return strm;
} }
std::shared_ptr<Stream> ServerContext::OpenRequestStream() std::shared_ptr<Stream> ServerContext::OpenRequestStream()
{ {
@@ -788,11 +789,14 @@ namespace Tesses::Framework::Http
this->WithSingleHeader("Content-Range","bytes " + std::to_string(begin) + "-" + std::to_string(end) + "/" + std::to_string(len)); this->WithSingleHeader("Content-Range","bytes " + std::to_string(begin) + "-" + std::to_string(end) + "/" + std::to_string(len));
this->statusCode = PartialContent; this->statusCode = PartialContent;
this->WriteHeaders(); this->WriteHeaders();
if(this->method != "HEAD")
{
strm->Seek(begin,SeekOrigin::Begin); strm->Seek(begin,SeekOrigin::Begin);
uint8_t buffer[1024]; uint8_t buffer[1024];
size_t read=0; size_t read=0;
do { do {
read = sizeof(buffer); read = sizeof(buffer);
myLen = (end - begin)+1; myLen = (end - begin)+1;
@@ -804,7 +808,7 @@ namespace Tesses::Framework::Http
begin += read; begin += read;
} while(read > 0 && !this->strm->EndOfStream()); } while(read > 0 && !this->strm->EndOfStream());
}
} }
else else
@@ -821,6 +825,7 @@ namespace Tesses::Framework::Http
this->WithSingleHeader("Accept-Range","bytes"); this->WithSingleHeader("Accept-Range","bytes");
this->WithSingleHeader("Content-Length",std::to_string(len)); this->WithSingleHeader("Content-Length",std::to_string(len));
this->WriteHeaders(); this->WriteHeaders();
if(this->method != "HEAD")
strm->CopyTo(this->strm); strm->CopyTo(this->strm);
} }
} }
@@ -828,8 +833,10 @@ namespace Tesses::Framework::Http
} }
else else
{ {
auto chunkedStream = this->OpenResponseStream(); auto chunkedStream = this->OpenResponseStream();
if(method != "HEAD")
strm->CopyTo(chunkedStream); strm->CopyTo(chunkedStream);

View File

@@ -27,6 +27,7 @@ namespace Tesses::Framework::Http
} }
bool HttpStream::CanWrite() bool HttpStream::CanWrite()
{ {
if(this->done) return false;
if(this->recv) return false; if(this->recv) return false;
return this->strm->CanWrite(); return this->strm->CanWrite();
} }
@@ -118,6 +119,7 @@ namespace Tesses::Framework::Http
} }
size_t HttpStream::Write(const uint8_t* buff, size_t len) size_t HttpStream::Write(const uint8_t* buff, size_t len)
{ {
if(this->done) return 0;
if(this->recv) return 0; if(this->recv) return 0;
if(this->length == 0) return 0; if(this->length == 0) return 0;
if(this->length > 0) if(this->length > 0)
@@ -153,9 +155,20 @@ namespace Tesses::Framework::Http
} }
} }
} }
void HttpStream::Close()
{
if(this->length == -1 && this->http1_1 && !done && !this->recv)
{
this->done=true;
StreamWriter writer(this->strm);
writer.newline = "\r\n";
writer.WriteLine("0");
writer.WriteLine();
}
}
HttpStream::~HttpStream() HttpStream::~HttpStream()
{ {
if(this->length == -1 && this->http1_1) if(this->length == -1 && this->http1_1 && !done && !this->recv)
{ {
StreamWriter writer(this->strm); StreamWriter writer(this->strm);
writer.newline = "\r\n"; writer.newline = "\r\n";

97
src/Http/RouteServer.cpp Normal file
View File

@@ -0,0 +1,97 @@
#include "TessesFramework/Http/RouteServer.hpp"
namespace Tesses::Framework::Http
{
RouteServer::RouteServerRoute::RouteServerRoute(std::string route, std::string method, ServerRequestHandler handler) : method(method), handler(handler)
{
auto path = Tesses::Framework::Filesystem::VFSPath::ParseUriPath(route);
for(auto item : path.path)
{
if(item.size() > 2 && item[0] == '{' && item[item.size()-1] == '}')
{
this->parts.emplace_back( item.substr(1,item.size()-2),true);
}
else {
this->parts.emplace_back(item,false);
}
}
}
bool RouteServer::RouteServerRoute::Equals(Tesses::Framework::Filesystem::VFSPath& path, HttpDictionary& args)
{
if(path.path.size() != this->parts.size()) return false;
for(size_t i = 0; i < this->parts.size(); i++)
{
auto& part = this->parts[i];
if(part.second)
args.SetValue(part.first, Tesses::Framework::Http::HttpUtils::UrlPathDecode(path.path[i]));
else if(part.first != path.path[i]) return false;
}
return true;
}
RouteServer::RouteServer(std::shared_ptr<IHttpServer> root) : root(root)
{
}
void RouteServer::Add(std::string method, std::string pattern, ServerRequestHandler handler)
{
this->routes.emplace_back(pattern,method,handler);
}
bool RouteServer::Handle(ServerContext& ctx)
{
auto pathArgs = ctx.pathArguments;
auto path = Tesses::Framework::Filesystem::VFSPath::ParseUriPath(ctx.path);
for(auto& svr : this->routes)
{
if(svr.method != ctx.method && !((svr.method == "GET" && ctx.method == "HEAD") || (svr.method == "HEAD" && ctx.method == "GET"))) continue;
ctx.pathArguments = pathArgs;
if(svr.Equals(path, ctx.pathArguments) && svr.handler && svr.handler(ctx))
return true;
}
ctx.pathArguments = pathArgs;
if(this->root)
return this->root->Handle(ctx);
return false;
}
void RouteServer::Get(std::string pattern, ServerRequestHandler handler)
{
Add("GET",pattern,handler);
}
void RouteServer::Post(std::string pattern, ServerRequestHandler handler)
{
Add("POST",pattern,handler);
}
void RouteServer::Put(std::string pattern, ServerRequestHandler handler)
{
Add("PUT",pattern,handler);
}
void RouteServer::Patch(std::string pattern, ServerRequestHandler handler)
{
Add("PATCH",pattern,handler);
}
void RouteServer::Delete(std::string pattern, ServerRequestHandler handler)
{
Add("DELETE",pattern,handler);
}
void RouteServer::Trace(std::string pattern, ServerRequestHandler handler)
{
Add("TRACE",pattern,handler);
}
void RouteServer::Options(std::string pattern, ServerRequestHandler handler)
{
Add("OPTIONS",pattern,handler);
}
}

View File

@@ -47,7 +47,7 @@ extern "C" {
#if defined(AF_UNIX) && !defined(GEKKO) && !defined(__SWITCH__) && !defined(__PS2__) #if defined(AF_UNIX) && !defined(GEKKO) && !defined(__SWITCH__) && !defined(__PS2__)
#include <sys/un.h> #include <sys/un.h>
#endif #endif
#include <sys/poll.h> #include <poll.h>
} }
#endif #endif
#if defined(GEKKO) #if defined(GEKKO)

View File

@@ -48,8 +48,13 @@ namespace Tesses::Framework::Streams {
if(len < 1024) if(len < 1024)
read = len; read = len;
if(read > 0) if(read > 0)
{
read=this->Write(buffer,read); read=this->Write(buffer,read);
if(read == 0)
{
throw std::out_of_range("Failed to write!");
}
}
buffer += read; buffer += read;
@@ -109,7 +114,6 @@ namespace Tesses::Framework::Streams {
read = (size_t)std::min(len-offset,(uint64_t)buffSize); read = (size_t)std::min(len-offset,(uint64_t)buffSize);
read = this->Read(buffer,read); read = this->Read(buffer,read);
strm->WriteBlock(buffer, read); strm->WriteBlock(buffer, read);
offset += read; offset += read;

View File

@@ -712,6 +712,244 @@ if (iResult != 0) {
}
else if(portable_str == "absolute")
{
if(dict2.TryGetValueAsType("user",portable_str))
{
if(portable_str != "system")
{
VFSPath userDir = portable_str;
portable_config.user = userDir.CollapseRelativeParents();
}
}
if(dict2.TryGetValueAsType("documents", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.documents = *(portable_config.user) / "Documents";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.documents = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("downloads", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.downloads = *(portable_config.user) / "Downloads";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.downloads = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("desktop", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.desktop = *(portable_config.user) / "Desktop";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.desktop = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("pictures", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.pictures = *(portable_config.user) / "Pictures";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.pictures = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("videos", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.videos = *(portable_config.user) / "Videos";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.videos = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("music", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.music = *(portable_config.user) / "Music";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.music = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("config", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.config = *(portable_config.user) / "Config";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.config = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("cache", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.cache = *(portable_config.user) / "Cache";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.cache = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("data", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.data = *(portable_config.user) / "Data";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.data = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("state", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.state = *(portable_config.user) / "State";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.state = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("temp", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.temp = *(portable_config.user) / "Temp";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.temp = userDir.CollapseRelativeParents();
}
}
}
} }
} }
} }