Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
// SPDX-License-Identifier: LGPL-3.0-or-later
2
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
3
 
4
#pragma once
5
 
6
#include <stddef.h>
7
#include <time.h>
8
 
9
#include <string>
10
#include <utility>
11
 
12
class AsyncStaticWebHandler : public AsyncWebHandler {
13
  using File = fs::File;
14
  using FS = fs::FS;
15
 
16
private:
17
  bool _getFile(AsyncWebServerRequest *request) const;
18
  bool _searchFile(AsyncWebServerRequest *request, const String &path);
19
 
20
protected:
21
  FS _fs;
22
  String _uri;
23
  String _path;
24
  String _default_file;
25
  String _cache_control;
26
  String _last_modified;
27
  AwsTemplateProcessor _callback;
28
  bool _isDir;
29
  bool _tryGzipFirst = true;
30
 
31
public:
32
  AsyncStaticWebHandler(const char *uri, FS &fs, const char *path, const char *cache_control);
33
  bool canHandle(AsyncWebServerRequest *request) const final;
34
  void handleRequest(AsyncWebServerRequest *request) final;
35
  AsyncStaticWebHandler &setTryGzipFirst(bool value);
36
  AsyncStaticWebHandler &setIsDir(bool isDir);
37
  AsyncStaticWebHandler &setDefaultFile(const char *filename);
38
  AsyncStaticWebHandler &setCacheControl(const char *cache_control);
39
 
40
  /**
41
     * @brief Set the Last-Modified time for the object
42
     *
43
     * @param last_modified
44
     * @return AsyncStaticWebHandler&
45
     */
46
  AsyncStaticWebHandler &setLastModified(const char *last_modified);
47
  AsyncStaticWebHandler &setLastModified(struct tm *last_modified);
48
  AsyncStaticWebHandler &setLastModified(time_t last_modified);
49
  // sets to current time. Make sure sntp is running and time is updated
50
  AsyncStaticWebHandler &setLastModified();
51
 
52
  AsyncStaticWebHandler &setTemplateProcessor(AwsTemplateProcessor newCallback);
53
};
54
 
55
class AsyncCallbackWebHandler : public AsyncWebHandler {
56
private:
57
protected:
58
  AsyncURIMatcher _uri;
59
  WebRequestMethodComposite _method;
60
  ArRequestHandlerFunction _onRequest;
61
  ArUploadHandlerFunction _onUpload;
62
  ArBodyHandlerFunction _onBody;
63
  bool _isRegex;
64
 
65
public:
66
  AsyncCallbackWebHandler() : _uri(), _method(AsyncWebRequestMethod::HTTP_ALL), _onRequest(NULL), _onUpload(NULL), _onBody(NULL), _isRegex(false) {}
67
  void setUri(AsyncURIMatcher uri);
68
  void setMethod(WebRequestMethodComposite method) {
69
    _method = std::move(method);
70
  }
71
  void onRequest(ArRequestHandlerFunction fn) {
72
    _onRequest = fn;
73
  }
74
  void onUpload(ArUploadHandlerFunction fn) {
75
    _onUpload = fn;
76
  }
77
  void onBody(ArBodyHandlerFunction fn) {
78
    _onBody = fn;
79
  }
80
 
81
  bool canHandle(AsyncWebServerRequest *request) const final;
82
  void handleRequest(AsyncWebServerRequest *request) final;
83
  void handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) final;
84
  void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) final;
85
  bool isRequestHandlerTrivial() const final {
86
    return !_onRequest;
87
  }
88
};