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 "../Async/Async.h"
5#include "../AsyncStreams/AsyncRequestStreams.h"
6#include "HttpConnection.h"
7#include "HttpExport.h"
8#include "HttpMultipartParser.h"
9
10namespace SC
11{
13struct SC_HTTP_EXPORT HttpAsyncFileServerOptions
14{
16 StringSpan spaFallbackPath = {};
17
19 bool enableValidators = true;
20
22 bool enableRangeRequests = true;
23
25 bool enableUploads = true;
26
28 size_t maxUploadBytes = 0;
29};
30
40struct SC_HTTP_EXPORT HttpAsyncFileServer
41{
45 struct SC_HTTP_EXPORT Stream
46 {
47 ReadableFileStream readableFileStream;
48 WritableFileStream writableFileStream;
49 AsyncTaskSequence readableFileStreamTask;
50 AsyncTaskSequence writableFileStreamTask;
51 HttpMultipartParser multipartParser;
52 AsyncFileSend asyncFileSend;
53 FileDescriptor sourceFileDescriptor;
54 size_t fileSendOffset = 0;
55 size_t fileSendLength = 0;
56
58 {
59 HttpAsyncFileServer* server = nullptr;
60 Stream* stream = nullptr;
61 HttpConnection* connection = nullptr;
62
63 FileDescriptor currentFd; // TODO: Reuse the above sourceFileDescriptor
64 StringPath currentFilePath;
65 StringSpan currentHeaderName;
67 bool rejectedFileName = false;
68
69 void onData(AsyncBufferView::ID bufferID);
70 } multipartListener;
71
73 {
74 HttpConnection* connection = nullptr;
75
76 void onFinish();
77 } putFileListener;
78 };
79
80 template <int RequestsSize>
81 struct SC_HTTP_EXPORT StreamQueue : public Stream
82 {
83 static_assert(RequestsSize >= 2, "HttpAsyncFileServer::StreamQueue requires RequestsSize >= 2");
84
86 {
87 readableFileStream.setReadQueue(readQueue);
88 writableFileStream.setWriteQueue(writeQueue);
89 }
90 AsyncReadableStream::Request readQueue[RequestsSize];
91 AsyncWritableStream::Request writeQueue[RequestsSize];
92 };
93
95 Result init(ThreadPool& threadPool, AsyncEventLoop& loop, StringSpan directoryToServe);
96
99
101 void setUseAsyncFileSend(bool value);
102
104 [[nodiscard]] bool getUseAsyncFileSend() const { return useAsyncFileSend; }
105
108
112
113 private:
114 bool useAsyncFileSend = true;
115 HttpAsyncFileServerOptions options = {};
116
117 Result putFile(HttpAsyncFileServer::Stream& stream, HttpConnection& connection, StringSpan filePath);
118 Result getFile(HttpAsyncFileServer::Stream& stream, HttpConnection& connection, StringSpan filePath, bool sendBody,
119 bool allowSpaFallback);
120 Result postMultipart(HttpAsyncFileServer::Stream& stream, HttpConnection& connection);
121
122 StringPath directory;
123
124 AsyncEventLoop* eventLoop = nullptr;
125 ThreadPool* threadPool = nullptr;
126 struct Internal;
127};
128
129} // namespace SC
Definition AsyncStreams.h:58
Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEve...
Definition Async.h:1397
Sends file contents to a socket using zero-copy when available (sendfile, TransmitFile).
Definition Async.h:982
Definition AsyncStreams.h:225
An AsyncSequence using a SC::ThreadPool to execute one or more SC::AsyncRequest in a background threa...
Definition Async.h:1124
Definition AsyncStreams.h:363
[UniqueHandleDeclaration2Snippet]
Definition File.h:128
Options controlling optional HttpAsyncFileServer semantics.
Definition HttpAsyncFileServer.h:14
Definition HttpAsyncFileServer.h:82
Definition HttpAsyncFileServer.h:58
Definition HttpAsyncFileServer.h:73
Support class for HttpAsyncFileServer holding file stream and pipeline.
Definition HttpAsyncFileServer.h:46
Http file server statically serves files from a directory.
Definition HttpAsyncFileServer.h:41
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...
Result setOptions(const HttpAsyncFileServerOptions &options)
Sets optional file server behavior.
bool getUseAsyncFileSend() const
Gets whether AsyncFileSend optimization is used for GET requests.
Definition HttpAsyncFileServer.h:104
Http connection abstraction holding both the incoming and outgoing messages in an HTTP transaction.
Definition HttpConnection.h:467
Incremental HTTP multipart/form-data parser.
Definition HttpMultipartParser.h:50
Zero-copy view over common multipart part headers.
Definition HttpMultipartParser.h:33
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