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#include "HttpMultipartParser.h"
7
8namespace SC
9{
19struct SC_COMPILER_EXPORT HttpAsyncFileServer
20{
22 struct SC_COMPILER_EXPORT Stream
23 {
24 ReadableFileStream readableFileStream;
25 WritableFileStream writableFileStream;
26 AsyncTaskSequence readableFileStreamTask;
27 HttpMultipartParser multipartParser;
28 AsyncFileSend asyncFileSend;
29 FileDescriptor sourceFileDescriptor;
30
32 {
33 HttpAsyncFileServer* server = nullptr;
34 Stream* stream = nullptr;
35 HttpAsyncConnectionBase* connection = nullptr;
36
37 FileDescriptor currentFd; // TODO: Reuse the above sourceFileDescriptor
38 StringPath currentFilePath;
39 StringSpan currentFileName;
40 StringSpan currentHeaderName;
41 bool isContentDisposition = false;
42
43 void onData(AsyncBufferView::ID bufferID);
44 } multipartListener;
45 };
46
47 template <int RequestsSize>
48 struct SC_COMPILER_EXPORT StreamQueue : public Stream
49 {
51 {
52 readableFileStream.setReadQueue(readQueue);
53 writableFileStream.setWriteQueue(writeQueue);
54 }
55 AsyncReadableStream::Request readQueue[RequestsSize];
56 AsyncWritableStream::Request writeQueue[RequestsSize];
57 };
58
60 Result init(ThreadPool& threadPool, AsyncEventLoop& loop, StringSpan directoryToServe);
61
64
66 void setUseAsyncFileSend(bool value);
67
69 [[nodiscard]] bool getUseAsyncFileSend() const { return useAsyncFileSend; }
70
74
75 private:
76 bool useAsyncFileSend = true;
77 Result putFile(HttpAsyncFileServer::Stream& stream, HttpConnection& connection, StringSpan filePath);
78 Result getFile(HttpAsyncFileServer::Stream& stream, HttpConnection& connection, StringSpan filePath);
79 Result postMultipart(HttpAsyncFileServer::Stream& stream, HttpConnection& connection);
80
81 StringPath directory;
82
83 AsyncEventLoop* eventLoop = nullptr;
84 ThreadPool* threadPool = nullptr;
85 struct Internal;
86};
87
88} // namespace SC
Definition AsyncStreams.h:52
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1294
Sends file contents to a socket using zero-copy when available (sendfile, TransmitFile).
Definition Async.h:884
Definition AsyncStreams.h:216
An AsyncSequence using a SC::ThreadPool to execute one or more SC::AsyncRequest in a background threa...
Definition Async.h:1024
Definition AsyncStreams.h:339
[UniqueHandleDeclaration2Snippet]
Definition File.h:79
Contains fields used by HttpAsyncServer for each connection.
Definition HttpAsyncServer.h:22
Definition HttpAsyncFileServer.h:49
Definition HttpAsyncFileServer.h:32
Support class for HttpAsyncFileServer holding file stream and pipeline.
Definition HttpAsyncFileServer.h:23
Http file server statically serves files from a directory.
Definition HttpAsyncFileServer.h:20
void setUseAsyncFileSend(bool value)
Controls whether to use AsyncFileSend optimization for GET requests (default: true)
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...
bool getUseAsyncFileSend() const
Gets whether AsyncFileSend optimization is used for GET requests.
Definition HttpAsyncFileServer.h:69
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:132
Incremental HTTP multipart/form-data parser.
Definition HttpMultipartParser.h:15
Uses an SC::AsyncFileRead to stream data from a file.
Definition AsyncRequestStreams.h:77
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:84