Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
HttpAsyncFileServer.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../AsyncStreams/AsyncRequestStreams.h"
5#include "HttpAsyncServer.h"
6
7namespace SC
8{
18struct SC_COMPILER_EXPORT HttpAsyncFileServer
19{
21 struct SC_COMPILER_EXPORT Stream
22 {
23 ReadableFileStream readableFileStream;
24 WritableFileStream writableFileStream;
25 AsyncTaskSequence readableFileStreamTask;
26 };
27
28 template <int RequestsSize>
29 struct SC_COMPILER_EXPORT StreamQueue : public Stream
30 {
32 {
33 readableFileStream.setReadQueue(readQueue);
34 writableFileStream.setWriteQueue(writeQueue);
35 }
36 AsyncReadableStream::Request readQueue[RequestsSize];
37 AsyncWritableStream::Request writeQueue[RequestsSize];
38 };
39
41 Result init(ThreadPool& threadPool, AsyncEventLoop& loop, StringSpan directoryToServe);
42
45
49
50 private:
51 Result putFile(HttpAsyncFileServer::Stream& stream, HttpConnection& connection, StringSpan filePath);
52 Result getFile(HttpAsyncFileServer::Stream& stream, HttpConnection& connection, StringSpan filePath);
53
54 StringPath directory;
55
56 AsyncEventLoop* eventLoop = nullptr;
57 ThreadPool* threadPool = nullptr;
58 struct Internal;
59};
60
61} // namespace SC
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1196
Definition AsyncStreams.h:213
An AsyncSequence using a SC::ThreadPool to execute one or more SC::AsyncRequest in a background threa...
Definition Async.h:926
Definition AsyncStreams.h:316
Definition HttpAsyncFileServer.h:30
Support class for HttpAsyncFileServer holding file stream and pipeline.
Definition HttpAsyncFileServer.h:22
Http file server statically serves files from a directory.
Definition HttpAsyncFileServer.h:19
Result init(ThreadPool &threadPool, AsyncEventLoop &loop, StringSpan directoryToServe)
Initialize the web server on the given file system directory to serve.
Result close()
Removes any reference to the arguments passed during init.
Result handleRequest(HttpAsyncFileServer::Stream &stream, HttpConnection &connection)
Handles the request, serving the requested file (GET) or creating a new one (PUT/POST) Call this meth...
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:120
Uses an SC::AsyncFileRead to stream data from a file.
Definition AsyncRequestStreams.h:62
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
Pre-sized char array holding enough space to represent a file system path.
Definition StringPath.h:42
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37
Simple thread pool that executes tasks in a fixed number of worker threads.
Definition ThreadPool.h:38
Uses an SC::AsyncFileWrite to stream data to a file.
Definition AsyncRequestStreams.h:68