mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
Merge pull request #1533 from proddy/https_36
keeping branch update. still memory and performance issues
This commit is contained in:
@@ -52,8 +52,8 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@preact/compat": "^17.1.2",
|
"@preact/compat": "^17.1.2",
|
||||||
"@preact/preset-vite": "^2.7.0",
|
"@preact/preset-vite": "^2.7.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.16.0",
|
"@typescript-eslint/eslint-plugin": "^6.17.0",
|
||||||
"@typescript-eslint/parser": "^6.16.0",
|
"@typescript-eslint/parser": "^6.17.0",
|
||||||
"concurrently": "^8.2.2",
|
"concurrently": "^8.2.2",
|
||||||
"eslint": "^8.56.0",
|
"eslint": "^8.56.0",
|
||||||
"eslint-config-airbnb": "^19.0.4",
|
"eslint-config-airbnb": "^19.0.4",
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ const bytesPerLine = 20;
|
|||||||
var totalSize = 0;
|
var totalSize = 0;
|
||||||
|
|
||||||
const generateWWWClass = () =>
|
const generateWWWClass = () =>
|
||||||
`typedef std::function<void(const char *, const char * contentType, const uint8_t * content, size_t len)> RouteRegistrationHandler;
|
`typedef std::function<void(const String &, const String & contentType, const uint8_t * content, size_t len)> RouteRegistrationHandler;
|
||||||
|
// Total size is ${totalSize} bytes
|
||||||
|
|
||||||
class WWWData {
|
class WWWData {
|
||||||
${indent}public:
|
${indent}public:
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
/*
|
||||||
import { debounce } from 'lodash-es';
|
import { debounce } from 'lodash-es';
|
||||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import Sockette from 'sockette';
|
import Sockette from 'sockette';
|
||||||
@@ -89,3 +90,4 @@ export const useWs = <D>(wsUrl: string, wsThrottle = 100) => {
|
|||||||
|
|
||||||
return { connected, data, updateData } as const;
|
return { connected, data, updateData } as const;
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { defineConfig } from 'vite';
|
import { defineConfig, splitVendorChunkPlugin } from 'vite';
|
||||||
import viteTsconfigPaths from 'vite-tsconfig-paths';
|
import viteTsconfigPaths from 'vite-tsconfig-paths';
|
||||||
import preact from '@preact/preset-vite';
|
import preact from '@preact/preset-vite';
|
||||||
import viteImagemin from 'vite-plugin-imagemin';
|
import viteImagemin from 'vite-plugin-imagemin';
|
||||||
@@ -44,6 +44,7 @@ export default defineConfig(({ command, mode }) => {
|
|||||||
plugins: [
|
plugins: [
|
||||||
preact(),
|
preact(),
|
||||||
viteTsconfigPaths(),
|
viteTsconfigPaths(),
|
||||||
|
splitVendorChunkPlugin(),
|
||||||
{
|
{
|
||||||
...viteImagemin({
|
...viteImagemin({
|
||||||
verbose: false,
|
verbose: false,
|
||||||
@@ -112,6 +113,20 @@ export default defineConfig(({ command, mode }) => {
|
|||||||
nameCache: null,
|
nameCache: null,
|
||||||
safari10: false,
|
safari10: false,
|
||||||
toplevel: false
|
toplevel: false
|
||||||
|
},
|
||||||
|
|
||||||
|
rollupOptions: {
|
||||||
|
output: {
|
||||||
|
manualChunks(id: string) {
|
||||||
|
if (id.includes('node_modules')) {
|
||||||
|
// creating a chunk to react routes deps. Reducing the vendor chunk size
|
||||||
|
if (id.includes('react-router-dom') || id.includes('@remix-run') || id.includes('react-router')) {
|
||||||
|
return '@react-router';
|
||||||
|
}
|
||||||
|
return 'vendor';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1689,15 +1689,15 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/eslint-plugin@npm:^6.16.0":
|
"@typescript-eslint/eslint-plugin@npm:^6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/eslint-plugin@npm:6.16.0"
|
resolution: "@typescript-eslint/eslint-plugin@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@eslint-community/regexpp": "npm:^4.5.1"
|
"@eslint-community/regexpp": "npm:^4.5.1"
|
||||||
"@typescript-eslint/scope-manager": "npm:6.16.0"
|
"@typescript-eslint/scope-manager": "npm:6.17.0"
|
||||||
"@typescript-eslint/type-utils": "npm:6.16.0"
|
"@typescript-eslint/type-utils": "npm:6.17.0"
|
||||||
"@typescript-eslint/utils": "npm:6.16.0"
|
"@typescript-eslint/utils": "npm:6.17.0"
|
||||||
"@typescript-eslint/visitor-keys": "npm:6.16.0"
|
"@typescript-eslint/visitor-keys": "npm:6.17.0"
|
||||||
debug: "npm:^4.3.4"
|
debug: "npm:^4.3.4"
|
||||||
graphemer: "npm:^1.4.0"
|
graphemer: "npm:^1.4.0"
|
||||||
ignore: "npm:^5.2.4"
|
ignore: "npm:^5.2.4"
|
||||||
@@ -1710,44 +1710,44 @@ __metadata:
|
|||||||
peerDependenciesMeta:
|
peerDependenciesMeta:
|
||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
checksum: 4bedce948ac3c20492a59813ee5d4f1f2306310857864dfaac2736f6c38e18785002c36844fd64c9fbdf3059fc390b29412be105fd7a118177f1eeeb1eb533f7
|
checksum: f2a5774e9cc03e491a5a488501e5622c7eebd766f5a4fc2c30642864a3b89b0807946bde33a678f326ba7032f3f6a51aa0bf9c2d10adc823804fc9fb47db55a6
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/parser@npm:^6.16.0":
|
"@typescript-eslint/parser@npm:^6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/parser@npm:6.16.0"
|
resolution: "@typescript-eslint/parser@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@typescript-eslint/scope-manager": "npm:6.16.0"
|
"@typescript-eslint/scope-manager": "npm:6.17.0"
|
||||||
"@typescript-eslint/types": "npm:6.16.0"
|
"@typescript-eslint/types": "npm:6.17.0"
|
||||||
"@typescript-eslint/typescript-estree": "npm:6.16.0"
|
"@typescript-eslint/typescript-estree": "npm:6.17.0"
|
||||||
"@typescript-eslint/visitor-keys": "npm:6.16.0"
|
"@typescript-eslint/visitor-keys": "npm:6.17.0"
|
||||||
debug: "npm:^4.3.4"
|
debug: "npm:^4.3.4"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
eslint: ^7.0.0 || ^8.0.0
|
eslint: ^7.0.0 || ^8.0.0
|
||||||
peerDependenciesMeta:
|
peerDependenciesMeta:
|
||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
checksum: 3d941ce345dc2ce29957e2110957662873d514b094b8939923c3281d858c11cd1f9058db862644afe14f68d087770f39a0a1f9e523a2013ed5d2fdf3421b34d0
|
checksum: 2ed0ed4a5b30e953430ce3279df3655af09fa1caed2abf11804d239717daefc32a22864f6620ef57bb9c684c74a99a13241384fea5096e961385e3678fc2e920
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/scope-manager@npm:6.16.0":
|
"@typescript-eslint/scope-manager@npm:6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/scope-manager@npm:6.16.0"
|
resolution: "@typescript-eslint/scope-manager@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@typescript-eslint/types": "npm:6.16.0"
|
"@typescript-eslint/types": "npm:6.17.0"
|
||||||
"@typescript-eslint/visitor-keys": "npm:6.16.0"
|
"@typescript-eslint/visitor-keys": "npm:6.17.0"
|
||||||
checksum: 3360aae4b85f5c31d20ad48d771cc09a6f8f6b1811b00d94f06e55b5a09c610ac75631b1c4edecb3bec682d41351b87e7d14d42bee84aa032064d0e13463035b
|
checksum: fe09c628553c9336e6a36d32c1d34e78ebd20aa02130a6bf535329621ba5a98aaac171f607bc6e4d17b3478c42e7de6476376636897ce3f227c754eb99acd07e
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/type-utils@npm:6.16.0":
|
"@typescript-eslint/type-utils@npm:6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/type-utils@npm:6.16.0"
|
resolution: "@typescript-eslint/type-utils@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@typescript-eslint/typescript-estree": "npm:6.16.0"
|
"@typescript-eslint/typescript-estree": "npm:6.17.0"
|
||||||
"@typescript-eslint/utils": "npm:6.16.0"
|
"@typescript-eslint/utils": "npm:6.17.0"
|
||||||
debug: "npm:^4.3.4"
|
debug: "npm:^4.3.4"
|
||||||
ts-api-utils: "npm:^1.0.1"
|
ts-api-utils: "npm:^1.0.1"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
@@ -1755,23 +1755,23 @@ __metadata:
|
|||||||
peerDependenciesMeta:
|
peerDependenciesMeta:
|
||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
checksum: 5964b87a87252bed278a248eb568902babd7c34defd3af8c3df371926d96aec716f33f1dc14bde170e93f73ed1b0af6e591e647853d0f33f378e2c7b3b73fc5b
|
checksum: dc7938429193acfda61b7282197ec046039e2c4da41cdcddf4daaf300d10229e4e23bb0fcf0503b19c0b99a874849c8a9f5bb35ce106260f56a14106d2b41d8c
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/types@npm:6.16.0":
|
"@typescript-eslint/types@npm:6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/types@npm:6.16.0"
|
resolution: "@typescript-eslint/types@npm:6.17.0"
|
||||||
checksum: 236ca318c2440c95068e5d4d147e2bfed62447775e18695e21c8ca04a341a74d01c37ed2b417629b7bf2fb91ad4fd5e2a6570215d16fc24dd1507ce6973b4e22
|
checksum: 87ab1b5a3270ab34b917c22a2fb90a9ad7d9f3b19d73a337bc9efbe65f924da13482c97e8ccbe3bd3d081aa96039eeff50de41c1da2a2128066429b931cdb21d
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/typescript-estree@npm:6.16.0":
|
"@typescript-eslint/typescript-estree@npm:6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/typescript-estree@npm:6.16.0"
|
resolution: "@typescript-eslint/typescript-estree@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@typescript-eslint/types": "npm:6.16.0"
|
"@typescript-eslint/types": "npm:6.17.0"
|
||||||
"@typescript-eslint/visitor-keys": "npm:6.16.0"
|
"@typescript-eslint/visitor-keys": "npm:6.17.0"
|
||||||
debug: "npm:^4.3.4"
|
debug: "npm:^4.3.4"
|
||||||
globby: "npm:^11.1.0"
|
globby: "npm:^11.1.0"
|
||||||
is-glob: "npm:^4.0.3"
|
is-glob: "npm:^4.0.3"
|
||||||
@@ -1781,34 +1781,34 @@ __metadata:
|
|||||||
peerDependenciesMeta:
|
peerDependenciesMeta:
|
||||||
typescript:
|
typescript:
|
||||||
optional: true
|
optional: true
|
||||||
checksum: 8e1ef03ecabaf3791b11240a51217836dbb74850e458258db77ac5eab5508cd9c63fb671924993d1e7654718c0c857c3550d51ecba0845fe489d143bb858e1b1
|
checksum: 1671b0d2f2fdf07074fb1e2524d61935cec173bd8db6e482cc5b2dcc77aed3ffa831396736ffa0ee2fdbddd8585ae9ca8d6c97bcaea1385b23907a1ec0508f83
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/utils@npm:6.16.0":
|
"@typescript-eslint/utils@npm:6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/utils@npm:6.16.0"
|
resolution: "@typescript-eslint/utils@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@eslint-community/eslint-utils": "npm:^4.4.0"
|
"@eslint-community/eslint-utils": "npm:^4.4.0"
|
||||||
"@types/json-schema": "npm:^7.0.12"
|
"@types/json-schema": "npm:^7.0.12"
|
||||||
"@types/semver": "npm:^7.5.0"
|
"@types/semver": "npm:^7.5.0"
|
||||||
"@typescript-eslint/scope-manager": "npm:6.16.0"
|
"@typescript-eslint/scope-manager": "npm:6.17.0"
|
||||||
"@typescript-eslint/types": "npm:6.16.0"
|
"@typescript-eslint/types": "npm:6.17.0"
|
||||||
"@typescript-eslint/typescript-estree": "npm:6.16.0"
|
"@typescript-eslint/typescript-estree": "npm:6.17.0"
|
||||||
semver: "npm:^7.5.4"
|
semver: "npm:^7.5.4"
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
eslint: ^7.0.0 || ^8.0.0
|
eslint: ^7.0.0 || ^8.0.0
|
||||||
checksum: 84dd02f7c8e47fae699cc222da5cbea08b28c6e1cc7827860430bc86c2a17ee3f86e198a4356902b95930f85785aa662266ea9c476f69bf80c6a5f648e55f9f4
|
checksum: 37c63afcf66124bf84808699997953b8c84a378aa2c490a771b611d82cdac8499c58fac8eeb8378528e97660b59563d99297bfec4b982cd68760b0ffe54aa714
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"@typescript-eslint/visitor-keys@npm:6.16.0":
|
"@typescript-eslint/visitor-keys@npm:6.17.0":
|
||||||
version: 6.16.0
|
version: 6.17.0
|
||||||
resolution: "@typescript-eslint/visitor-keys@npm:6.16.0"
|
resolution: "@typescript-eslint/visitor-keys@npm:6.17.0"
|
||||||
dependencies:
|
dependencies:
|
||||||
"@typescript-eslint/types": "npm:6.16.0"
|
"@typescript-eslint/types": "npm:6.17.0"
|
||||||
eslint-visitor-keys: "npm:^3.4.1"
|
eslint-visitor-keys: "npm:^3.4.1"
|
||||||
checksum: 19e559f14ea0092585a374b8c5f1aca9b6b271fc23909d9857de9cf71a1e1d3abc0afd237e9c02d7a5fbdfe8e3be7853cf9fedf40a6f16bac3495cb7f4e67982
|
checksum: a2aed0e1437fdab8858ab9c7c8e355f8b72a5fa4b0adc54f28b8a2bbc29d4bb93214968ee940f83d013d0a4b83d00cd4eeeb05fb4c2c7d0ead324c6793f7d6d4
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
@@ -1838,8 +1838,8 @@ __metadata:
|
|||||||
"@types/react": "npm:^18.2.46"
|
"@types/react": "npm:^18.2.46"
|
||||||
"@types/react-dom": "npm:^18.2.18"
|
"@types/react-dom": "npm:^18.2.18"
|
||||||
"@types/react-router-dom": "npm:^5.3.3"
|
"@types/react-router-dom": "npm:^5.3.3"
|
||||||
"@typescript-eslint/eslint-plugin": "npm:^6.16.0"
|
"@typescript-eslint/eslint-plugin": "npm:^6.17.0"
|
||||||
"@typescript-eslint/parser": "npm:^6.16.0"
|
"@typescript-eslint/parser": "npm:^6.17.0"
|
||||||
alova: "npm:^2.16.2"
|
alova: "npm:^2.16.2"
|
||||||
async-validator: "npm:^4.2.5"
|
async-validator: "npm:^4.2.5"
|
||||||
concurrently: "npm:^8.2.2"
|
concurrently: "npm:^8.2.2"
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#ifndef ChunkPrinter_h
|
#ifndef ChunkPrinter_h
|
||||||
#define ChunkPrinter_h
|
#define ChunkPrinter_h
|
||||||
|
|
||||||
#include "PsychicCore.h"
|
// #include "PsychicCore.h"
|
||||||
#include "PsychicResponse.h"
|
#include "PsychicResponse.h"
|
||||||
#include <Print.h>
|
#include <Print.h>
|
||||||
|
|
||||||
class ChunkPrinter : public Print {
|
class ChunkPrinter : public Print
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
PsychicResponse *_response;
|
PsychicResponse *_response;
|
||||||
uint8_t *_buffer;
|
uint8_t *_buffer;
|
||||||
@@ -13,24 +14,25 @@ class ChunkPrinter : public Print {
|
|||||||
size_t _pos;
|
size_t _pos;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ChunkPrinter(PsychicResponse * response, uint8_t * buffer, size_t len)
|
ChunkPrinter(PsychicResponse *response, uint8_t *buffer, size_t len) :
|
||||||
: _response(response)
|
_response(response),
|
||||||
, _buffer(buffer)
|
_buffer(buffer),
|
||||||
, _length(len)
|
_length(len),
|
||||||
, _pos(0) {
|
_pos(0)
|
||||||
}
|
{}
|
||||||
|
|
||||||
virtual ~ChunkPrinter() {
|
virtual ~ChunkPrinter() {}
|
||||||
}
|
|
||||||
|
|
||||||
size_t write(uint8_t c) {
|
size_t write(uint8_t c)
|
||||||
|
{
|
||||||
esp_err_t err;
|
esp_err_t err;
|
||||||
|
|
||||||
_buffer[_pos] = c;
|
_buffer[_pos] = c;
|
||||||
_pos++;
|
_pos++;
|
||||||
|
|
||||||
//if we're full, send a chunk
|
//if we're full, send a chunk
|
||||||
if (_pos == _length) {
|
if (_pos == _length)
|
||||||
|
{
|
||||||
_pos = 0;
|
_pos = 0;
|
||||||
|
|
||||||
err = _response->sendChunk(_buffer, _length);
|
err = _response->sendChunk(_buffer, _length);
|
||||||
@@ -41,8 +43,10 @@ class ChunkPrinter : public Print {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void flush() override {
|
virtual void flush() override
|
||||||
if (_pos) {
|
{
|
||||||
|
if (_pos)
|
||||||
|
{
|
||||||
_response->sendChunk(_buffer, _pos);
|
_response->sendChunk(_buffer, _pos);
|
||||||
_pos = 0;
|
_pos = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
#include "PsychicClient.h"
|
#include "PsychicClient.h"
|
||||||
|
#include "PsychicHttpServer.h"
|
||||||
|
#include <lwip/sockets.h>
|
||||||
|
|
||||||
PsychicClient::PsychicClient(httpd_handle_t server, int socket)
|
PsychicClient::PsychicClient(httpd_handle_t server, int socket) :
|
||||||
: _server(server)
|
_server(server),
|
||||||
, _socket(socket)
|
_socket(socket),
|
||||||
, _friend(NULL)
|
_friend(NULL),
|
||||||
, isNew(false) {
|
isNew(false)
|
||||||
}
|
{}
|
||||||
|
|
||||||
PsychicClient::~PsychicClient() {
|
PsychicClient::~PsychicClient() {
|
||||||
}
|
}
|
||||||
@@ -19,14 +21,16 @@ int PsychicClient::socket() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// I'm not sure this is entirely safe to call. I was having issues with race conditions when highly loaded using this.
|
// I'm not sure this is entirely safe to call. I was having issues with race conditions when highly loaded using this.
|
||||||
esp_err_t PsychicClient::close() {
|
esp_err_t PsychicClient::close()
|
||||||
|
{
|
||||||
esp_err_t err = httpd_sess_trigger_close(_server, _socket);
|
esp_err_t err = httpd_sess_trigger_close(_server, _socket);
|
||||||
//PsychicHttpServer::closeCallback(_server, _socket); // call this immediately so the client is taken off the list.
|
//PsychicHttpServer::closeCallback(_server, _socket); // call this immediately so the client is taken off the list.
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress PsychicClient::localIP() {
|
IPAddress PsychicClient::localIP()
|
||||||
|
{
|
||||||
IPAddress address(0,0,0,0);
|
IPAddress address(0,0,0,0);
|
||||||
|
|
||||||
char ipstr[INET6_ADDRSTRLEN];
|
char ipstr[INET6_ADDRSTRLEN];
|
||||||
@@ -46,7 +50,8 @@ IPAddress PsychicClient::localIP() {
|
|||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress PsychicClient::remoteIP() {
|
IPAddress PsychicClient::remoteIP()
|
||||||
|
{
|
||||||
IPAddress address(0,0,0,0);
|
IPAddress address(0,0,0,0);
|
||||||
|
|
||||||
char ipstr[INET6_ADDRSTRLEN];
|
char ipstr[INET6_ADDRSTRLEN];
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
#define PsychicClient_h
|
#define PsychicClient_h
|
||||||
|
|
||||||
#include "PsychicCore.h"
|
#include "PsychicCore.h"
|
||||||
#include "PsychicHttpServer.h"
|
|
||||||
#include <lwip/sockets.h>
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* PsychicClient :: Generic wrapper around the ESP-IDF socket
|
* PsychicClient :: Generic wrapper around the ESP-IDF socket
|
||||||
@@ -24,9 +22,7 @@ class PsychicClient {
|
|||||||
|
|
||||||
bool isNew = false;
|
bool isNew = false;
|
||||||
|
|
||||||
bool operator==(PsychicClient & rhs) const {
|
bool operator==(PsychicClient& rhs) const { return _socket == rhs.socket(); }
|
||||||
return _socket == rhs.socket();
|
|
||||||
}
|
|
||||||
|
|
||||||
httpd_handle_t server();
|
httpd_handle_t server();
|
||||||
int socket();
|
int socket();
|
||||||
|
|||||||
@@ -16,6 +16,10 @@
|
|||||||
#define FILE_CHUNK_SIZE 8 * 1024
|
#define FILE_CHUNK_SIZE 8 * 1024
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef STREAM_CHUNK_SIZE
|
||||||
|
#define STREAM_CHUNK_SIZE 1024
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MAX_UPLOAD_SIZE
|
#ifndef MAX_UPLOAD_SIZE
|
||||||
#define MAX_UPLOAD_SIZE (2048 * 1024) // 2MB
|
#define MAX_UPLOAD_SIZE (2048 * 1024) // 2MB
|
||||||
#endif
|
#endif
|
||||||
@@ -37,8 +41,13 @@
|
|||||||
#include "MD5Builder.h"
|
#include "MD5Builder.h"
|
||||||
#include <UrlEncode.h>
|
#include <UrlEncode.h>
|
||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
|
enum HTTPAuthMethod
|
||||||
|
{
|
||||||
|
BASIC_AUTH,
|
||||||
|
DIGEST_AUTH
|
||||||
|
};
|
||||||
|
|
||||||
String urlDecode(const char *encoded);
|
String urlDecode(const char *encoded);
|
||||||
|
|
||||||
@@ -53,24 +62,30 @@ typedef std::function<bool(PsychicRequest * request)> PsychicRequestFilterFuncti
|
|||||||
// client connect callback
|
// client connect callback
|
||||||
typedef std::function<void(PsychicClient *client)> PsychicClientCallback;
|
typedef std::function<void(PsychicClient *client)> PsychicClientCallback;
|
||||||
|
|
||||||
|
// callback definitions
|
||||||
|
typedef std::function<esp_err_t(PsychicRequest *request)> PsychicHttpRequestCallback;
|
||||||
|
typedef std::function<esp_err_t(PsychicRequest *request, JsonVariant &json)> PsychicJsonRequestCallback;
|
||||||
|
|
||||||
struct HTTPHeader {
|
struct HTTPHeader
|
||||||
|
{
|
||||||
char *field;
|
char *field;
|
||||||
char *value;
|
char *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DefaultHeaders {
|
class DefaultHeaders
|
||||||
|
{
|
||||||
std::list<HTTPHeader> _headers;
|
std::list<HTTPHeader> _headers;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DefaultHeaders() {
|
DefaultHeaders() {}
|
||||||
}
|
|
||||||
|
|
||||||
void addHeader(const String & field, const String & value) {
|
void addHeader(const String &field, const String &value)
|
||||||
|
{
|
||||||
addHeader(field.c_str(), value.c_str());
|
addHeader(field.c_str(), value.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void addHeader(const char * field, const char * value) {
|
void addHeader(const char *field, const char *value)
|
||||||
|
{
|
||||||
HTTPHeader header;
|
HTTPHeader header;
|
||||||
|
|
||||||
// these are just going to stick around forever.
|
// these are just going to stick around forever.
|
||||||
@@ -83,16 +98,15 @@ class DefaultHeaders {
|
|||||||
_headers.push_back(header);
|
_headers.push_back(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::list<HTTPHeader> & getHeaders() {
|
const std::list<HTTPHeader> &getHeaders() { return _headers; }
|
||||||
return _headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete the copy constructor, singleton class
|
// delete the copy constructor, singleton class
|
||||||
DefaultHeaders(DefaultHeaders const &) = delete;
|
DefaultHeaders(DefaultHeaders const &) = delete;
|
||||||
DefaultHeaders &operator=(DefaultHeaders const &) = delete;
|
DefaultHeaders &operator=(DefaultHeaders const &) = delete;
|
||||||
|
|
||||||
// single static class interface
|
// single static class interface
|
||||||
static DefaultHeaders & Instance() {
|
static DefaultHeaders &Instance()
|
||||||
|
{
|
||||||
static DefaultHeaders instance;
|
static DefaultHeaders instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,24 @@
|
|||||||
#include "PsychicEndpoint.h"
|
#include "PsychicEndpoint.h"
|
||||||
|
#include "PsychicHttpServer.h"
|
||||||
|
|
||||||
PsychicEndpoint::PsychicEndpoint()
|
PsychicEndpoint::PsychicEndpoint() :
|
||||||
: _server(NULL)
|
_server(NULL),
|
||||||
, _uri("")
|
_uri(""),
|
||||||
, _method(HTTP_GET)
|
_method(HTTP_GET),
|
||||||
, _handler(NULL) {
|
_handler(NULL)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEndpoint::PsychicEndpoint(PsychicHttpServer * server, http_method method, const char * uri)
|
PsychicEndpoint::PsychicEndpoint(PsychicHttpServer *server, http_method method, const char * uri) :
|
||||||
: _server(server)
|
_server(server),
|
||||||
, _uri(uri)
|
_uri(uri),
|
||||||
, _method(method)
|
_method(method),
|
||||||
, _handler(NULL) {
|
_handler(NULL)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEndpoint * PsychicEndpoint::setHandler(PsychicHandler * handler) {
|
PsychicEndpoint * PsychicEndpoint::setHandler(PsychicHandler *handler)
|
||||||
|
{
|
||||||
//clean up old / default handler
|
//clean up old / default handler
|
||||||
if (_handler != NULL)
|
if (_handler != NULL)
|
||||||
delete _handler;
|
delete _handler;
|
||||||
@@ -28,7 +32,8 @@ PsychicEndpoint * PsychicEndpoint::setHandler(PsychicHandler * handler) {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicHandler * PsychicEndpoint::handler() {
|
PsychicHandler * PsychicEndpoint::handler()
|
||||||
|
{
|
||||||
return _handler;
|
return _handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +41,8 @@ String PsychicEndpoint::uri() {
|
|||||||
return _uri;
|
return _uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicEndpoint::requestCallback(httpd_req_t * req) {
|
esp_err_t PsychicEndpoint::requestCallback(httpd_req_t *req)
|
||||||
|
{
|
||||||
#ifdef ENABLE_ASYNC
|
#ifdef ENABLE_ASYNC
|
||||||
if (is_on_async_worker_thread() == false) {
|
if (is_on_async_worker_thread() == false) {
|
||||||
if (submit_async_req(req, PsychicEndpoint::requestCallback) == ESP_OK) {
|
if (submit_async_req(req, PsychicEndpoint::requestCallback) == ESP_OK) {
|
||||||
@@ -54,8 +60,10 @@ esp_err_t PsychicEndpoint::requestCallback(httpd_req_t * req) {
|
|||||||
PsychicRequest request(self->_server, req);
|
PsychicRequest request(self->_server, req);
|
||||||
|
|
||||||
//make sure we have a handler
|
//make sure we have a handler
|
||||||
if (handler != NULL) {
|
if (handler != NULL)
|
||||||
if (handler->filter(&request) && handler->canHandle(&request)) {
|
{
|
||||||
|
if (handler->filter(&request) && handler->canHandle(&request))
|
||||||
|
{
|
||||||
//check our credentials
|
//check our credentials
|
||||||
if (handler->needsAuthentication(&request))
|
if (handler->needsAuthentication(&request))
|
||||||
return handler->authenticate(&request);
|
return handler->authenticate(&request);
|
||||||
@@ -66,7 +74,8 @@ esp_err_t PsychicEndpoint::requestCallback(httpd_req_t * req) {
|
|||||||
//pass it to our generic handlers
|
//pass it to our generic handlers
|
||||||
else
|
else
|
||||||
return PsychicHttpServer::notFoundHandler(req, HTTPD_500_INTERNAL_SERVER_ERROR);
|
return PsychicHttpServer::notFoundHandler(req, HTTPD_500_INTERNAL_SERVER_ERROR);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
return request.reply(500, "text/html", "No handler registered.");
|
return request.reply(500, "text/html", "No handler registered.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +84,7 @@ PsychicEndpoint * PsychicEndpoint::setFilter(PsychicRequestFilterFunction fn) {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEndpoint *
|
PsychicEndpoint* PsychicEndpoint::setAuthentication(const char *username, const char *password, HTTPAuthMethod method, const char *realm, const char *authFailMsg) {
|
||||||
PsychicEndpoint::setAuthentication(const char * username, const char * password, HTTPAuthMethod method, const char * realm, const char * authFailMsg) {
|
|
||||||
_handler->setAuthentication(username, password, method, realm, authFailMsg);
|
_handler->setAuthentication(username, password, method, realm, authFailMsg);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
@@ -2,13 +2,15 @@
|
|||||||
#define PsychicEndpoint_h
|
#define PsychicEndpoint_h
|
||||||
|
|
||||||
#include "PsychicCore.h"
|
#include "PsychicCore.h"
|
||||||
#include "PsychicHttpServer.h"
|
|
||||||
|
class PsychicHandler;
|
||||||
|
|
||||||
#ifdef ENABLE_ASYNC
|
#ifdef ENABLE_ASYNC
|
||||||
#include "async_worker.h"
|
#include "async_worker.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class PsychicEndpoint {
|
class PsychicEndpoint
|
||||||
|
{
|
||||||
friend PsychicHttpServer;
|
friend PsychicHttpServer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -25,8 +27,7 @@ class PsychicEndpoint {
|
|||||||
PsychicHandler *handler();
|
PsychicHandler *handler();
|
||||||
|
|
||||||
PsychicEndpoint* setFilter(PsychicRequestFilterFunction fn);
|
PsychicEndpoint* setFilter(PsychicRequestFilterFunction fn);
|
||||||
PsychicEndpoint *
|
PsychicEndpoint* setAuthentication(const char *username, const char *password, HTTPAuthMethod method = BASIC_AUTH, const char *realm = "", const char *authFailMsg = "");
|
||||||
setAuthentication(const char * username, const char * password, HTTPAuthMethod method = BASIC_AUTH, const char * realm = "", const char * authFailMsg = "");
|
|
||||||
|
|
||||||
String uri();
|
String uri();
|
||||||
|
|
||||||
|
|||||||
@@ -24,16 +24,17 @@
|
|||||||
// PsychicEventSource - Handler
|
// PsychicEventSource - Handler
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
|
|
||||||
PsychicEventSource::PsychicEventSource()
|
PsychicEventSource::PsychicEventSource() :
|
||||||
: PsychicHandler()
|
PsychicHandler(),
|
||||||
, _onOpen(NULL)
|
_onOpen(NULL),
|
||||||
, _onClose(NULL) {
|
_onClose(NULL)
|
||||||
}
|
{}
|
||||||
|
|
||||||
PsychicEventSource::~PsychicEventSource() {
|
PsychicEventSource::~PsychicEventSource() {
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEventSourceClient * PsychicEventSource::getClient(int socket) {
|
PsychicEventSourceClient * PsychicEventSource::getClient(int socket)
|
||||||
|
{
|
||||||
PsychicClient *client = PsychicHandler::getClient(socket);
|
PsychicClient *client = PsychicHandler::getClient(socket);
|
||||||
|
|
||||||
if (client == NULL)
|
if (client == NULL)
|
||||||
@@ -46,16 +47,19 @@ PsychicEventSourceClient * PsychicEventSource::getClient(PsychicClient * client)
|
|||||||
return getClient(client->socket());
|
return getClient(client->socket());
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicEventSource::handleRequest(PsychicRequest * request) {
|
esp_err_t PsychicEventSource::handleRequest(PsychicRequest *request)
|
||||||
|
{
|
||||||
//start our open ended HTTP response
|
//start our open ended HTTP response
|
||||||
PsychicEventSourceResponse response(request);
|
PsychicEventSourceResponse response(request);
|
||||||
esp_err_t err = response.send();
|
esp_err_t err = response.send();
|
||||||
|
|
||||||
//lookup our client
|
//lookup our client
|
||||||
PsychicClient *client = checkForNewClient(request->client());
|
PsychicClient *client = checkForNewClient(request->client());
|
||||||
if (client->isNew) {
|
if (client->isNew)
|
||||||
|
{
|
||||||
//did we get our last id?
|
//did we get our last id?
|
||||||
if (request->hasHeader("Last-Event-ID")) {
|
if(request->hasHeader("Last-Event-ID"))
|
||||||
|
{
|
||||||
PsychicEventSourceClient *buddy = getClient(client);
|
PsychicEventSourceClient *buddy = getClient(client);
|
||||||
buddy->_lastId = atoi(request->header("Last-Event-ID").c_str());
|
buddy->_lastId = atoi(request->header("Last-Event-ID").c_str());
|
||||||
}
|
}
|
||||||
@@ -90,7 +94,8 @@ void PsychicEventSource::removeClient(PsychicClient * client) {
|
|||||||
|
|
||||||
void PsychicEventSource::openCallback(PsychicClient *client) {
|
void PsychicEventSource::openCallback(PsychicClient *client) {
|
||||||
PsychicEventSourceClient *buddy = getClient(client);
|
PsychicEventSourceClient *buddy = getClient(client);
|
||||||
if (buddy == NULL) {
|
if (buddy == NULL)
|
||||||
|
{
|
||||||
TRACE();
|
TRACE();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -101,7 +106,8 @@ void PsychicEventSource::openCallback(PsychicClient * client) {
|
|||||||
|
|
||||||
void PsychicEventSource::closeCallback(PsychicClient *client) {
|
void PsychicEventSource::closeCallback(PsychicClient *client) {
|
||||||
PsychicEventSourceClient *buddy = getClient(client);
|
PsychicEventSourceClient *buddy = getClient(client);
|
||||||
if (buddy == NULL) {
|
if (buddy == NULL)
|
||||||
|
{
|
||||||
TRACE();
|
TRACE();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -110,7 +116,8 @@ void PsychicEventSource::closeCallback(PsychicClient * client) {
|
|||||||
_onClose(getClient(buddy));
|
_onClose(getClient(buddy));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicEventSource::send(const char * message, const char * event, uint32_t id, uint32_t reconnect) {
|
void PsychicEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect)
|
||||||
|
{
|
||||||
String ev = generateEventMessage(message, event, id, reconnect);
|
String ev = generateEventMessage(message, event, id, reconnect);
|
||||||
for(PsychicClient *c : _clients) {
|
for(PsychicClient *c : _clients) {
|
||||||
((PsychicEventSourceClient*)c->_friend)->sendEvent(ev.c_str());
|
((PsychicEventSourceClient*)c->_friend)->sendEvent(ev.c_str());
|
||||||
@@ -121,9 +128,10 @@ void PsychicEventSource::send(const char * message, const char * event, uint32_t
|
|||||||
// PsychicEventSourceClient
|
// PsychicEventSourceClient
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
|
|
||||||
PsychicEventSourceClient::PsychicEventSourceClient(PsychicClient * client)
|
PsychicEventSourceClient::PsychicEventSourceClient(PsychicClient *client) :
|
||||||
: PsychicClient(client->server(), client->socket())
|
PsychicClient(client->server(), client->socket()),
|
||||||
, _lastId(0) {
|
_lastId(0)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEventSourceClient::~PsychicEventSourceClient(){
|
PsychicEventSourceClient::~PsychicEventSourceClient(){
|
||||||
@@ -149,10 +157,12 @@ void PsychicEventSourceClient::sendEvent(const char * event) {
|
|||||||
/*****************************************/
|
/*****************************************/
|
||||||
|
|
||||||
PsychicEventSourceResponse::PsychicEventSourceResponse(PsychicRequest *request)
|
PsychicEventSourceResponse::PsychicEventSourceResponse(PsychicRequest *request)
|
||||||
: PsychicResponse(request) {
|
: PsychicResponse(request)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicEventSourceResponse::send() {
|
esp_err_t PsychicEventSourceResponse::send() {
|
||||||
|
|
||||||
//build our main header
|
//build our main header
|
||||||
String out = String();
|
String out = String();
|
||||||
out.concat("HTTP/1.1 200 OK\r\n");
|
out.concat("HTTP/1.1 200 OK\r\n");
|
||||||
|
|||||||
@@ -21,8 +21,8 @@
|
|||||||
#define PsychicEventSource_H_
|
#define PsychicEventSource_H_
|
||||||
|
|
||||||
#include "PsychicCore.h"
|
#include "PsychicCore.h"
|
||||||
#include "PsychicClient.h"
|
|
||||||
#include "PsychicHandler.h"
|
#include "PsychicHandler.h"
|
||||||
|
#include "PsychicClient.h"
|
||||||
#include "PsychicResponse.h"
|
#include "PsychicResponse.h"
|
||||||
|
|
||||||
class PsychicEventSource;
|
class PsychicEventSource;
|
||||||
@@ -42,9 +42,7 @@ class PsychicEventSourceClient : public PsychicClient {
|
|||||||
PsychicEventSourceClient(PsychicClient *client);
|
PsychicEventSourceClient(PsychicClient *client);
|
||||||
~PsychicEventSourceClient();
|
~PsychicEventSourceClient();
|
||||||
|
|
||||||
uint32_t lastId() const {
|
uint32_t lastId() const { return _lastId; }
|
||||||
return _lastId;
|
|
||||||
}
|
|
||||||
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
|
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
|
||||||
void sendEvent(const char *event);
|
void sendEvent(const char *event);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -70,58 +70,42 @@ PsychicFileResponse::PsychicFileResponse(PsychicRequest * request, File content,
|
|||||||
addHeader("Content-Disposition", buf);
|
addHeader("Content-Disposition", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicFileResponse::~PsychicFileResponse() {
|
PsychicFileResponse::~PsychicFileResponse()
|
||||||
|
{
|
||||||
if(_content)
|
if(_content)
|
||||||
_content.close();
|
_content.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicFileResponse::_setContentType(const String& path){
|
void PsychicFileResponse::_setContentType(const String& path){
|
||||||
if (path.endsWith(".html"))
|
if (path.endsWith(".html")) _contentType = "text/html";
|
||||||
_contentType = "text/html";
|
else if (path.endsWith(".htm")) _contentType = "text/html";
|
||||||
else if (path.endsWith(".htm"))
|
else if (path.endsWith(".css")) _contentType = "text/css";
|
||||||
_contentType = "text/html";
|
else if (path.endsWith(".json")) _contentType = "application/json";
|
||||||
else if (path.endsWith(".css"))
|
else if (path.endsWith(".js")) _contentType = "application/javascript";
|
||||||
_contentType = "text/css";
|
else if (path.endsWith(".png")) _contentType = "image/png";
|
||||||
else if (path.endsWith(".json"))
|
else if (path.endsWith(".gif")) _contentType = "image/gif";
|
||||||
_contentType = "application/json";
|
else if (path.endsWith(".jpg")) _contentType = "image/jpeg";
|
||||||
else if (path.endsWith(".js"))
|
else if (path.endsWith(".ico")) _contentType = "image/x-icon";
|
||||||
_contentType = "application/javascript";
|
else if (path.endsWith(".svg")) _contentType = "image/svg+xml";
|
||||||
else if (path.endsWith(".png"))
|
else if (path.endsWith(".eot")) _contentType = "font/eot";
|
||||||
_contentType = "image/png";
|
else if (path.endsWith(".woff")) _contentType = "font/woff";
|
||||||
else if (path.endsWith(".gif"))
|
else if (path.endsWith(".woff2")) _contentType = "font/woff2";
|
||||||
_contentType = "image/gif";
|
else if (path.endsWith(".ttf")) _contentType = "font/ttf";
|
||||||
else if (path.endsWith(".jpg"))
|
else if (path.endsWith(".xml")) _contentType = "text/xml";
|
||||||
_contentType = "image/jpeg";
|
else if (path.endsWith(".pdf")) _contentType = "application/pdf";
|
||||||
else if (path.endsWith(".ico"))
|
else if (path.endsWith(".zip")) _contentType = "application/zip";
|
||||||
_contentType = "image/x-icon";
|
else if(path.endsWith(".gz")) _contentType = "application/x-gzip";
|
||||||
else if (path.endsWith(".svg"))
|
else _contentType = "text/plain";
|
||||||
_contentType = "image/svg+xml";
|
|
||||||
else if (path.endsWith(".eot"))
|
|
||||||
_contentType = "font/eot";
|
|
||||||
else if (path.endsWith(".woff"))
|
|
||||||
_contentType = "font/woff";
|
|
||||||
else if (path.endsWith(".woff2"))
|
|
||||||
_contentType = "font/woff2";
|
|
||||||
else if (path.endsWith(".ttf"))
|
|
||||||
_contentType = "font/ttf";
|
|
||||||
else if (path.endsWith(".xml"))
|
|
||||||
_contentType = "text/xml";
|
|
||||||
else if (path.endsWith(".pdf"))
|
|
||||||
_contentType = "application/pdf";
|
|
||||||
else if (path.endsWith(".zip"))
|
|
||||||
_contentType = "application/zip";
|
|
||||||
else if (path.endsWith(".gz"))
|
|
||||||
_contentType = "application/x-gzip";
|
|
||||||
else
|
|
||||||
_contentType = "text/plain";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicFileResponse::send() {
|
esp_err_t PsychicFileResponse::send()
|
||||||
|
{
|
||||||
esp_err_t err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
|
|
||||||
//just send small files directly
|
//just send small files directly
|
||||||
size_t size = getContentLength();
|
size_t size = getContentLength();
|
||||||
if (size < FILE_CHUNK_SIZE) {
|
if (size < FILE_CHUNK_SIZE)
|
||||||
|
{
|
||||||
uint8_t *buffer = (uint8_t *)malloc(size);
|
uint8_t *buffer = (uint8_t *)malloc(size);
|
||||||
int readSize = _content.readBytes((char *)buffer, size);
|
int readSize = _content.readBytes((char *)buffer, size);
|
||||||
|
|
||||||
@@ -129,10 +113,13 @@ esp_err_t PsychicFileResponse::send() {
|
|||||||
err = PsychicResponse::send();
|
err = PsychicResponse::send();
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
/* Retrieve the pointer to scratch buffer for temporary storage */
|
/* Retrieve the pointer to scratch buffer for temporary storage */
|
||||||
char *chunk = (char *)malloc(FILE_CHUNK_SIZE);
|
char *chunk = (char *)malloc(FILE_CHUNK_SIZE);
|
||||||
if (chunk == NULL) {
|
if (chunk == NULL)
|
||||||
|
{
|
||||||
/* Respond with 500 Internal Server Error */
|
/* Respond with 500 Internal Server Error */
|
||||||
httpd_resp_send_err(this->_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
|
httpd_resp_send_err(this->_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
@@ -144,7 +131,8 @@ esp_err_t PsychicFileResponse::send() {
|
|||||||
do {
|
do {
|
||||||
/* Read file in chunks into the scratch buffer */
|
/* Read file in chunks into the scratch buffer */
|
||||||
chunksize = _content.readBytes(chunk, FILE_CHUNK_SIZE);
|
chunksize = _content.readBytes(chunk, FILE_CHUNK_SIZE);
|
||||||
if (chunksize > 0) {
|
if (chunksize > 0)
|
||||||
|
{
|
||||||
err = this->sendChunk((uint8_t *)chunk, chunksize);
|
err = this->sendChunk((uint8_t *)chunk, chunksize);
|
||||||
if (err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
break;
|
break;
|
||||||
@@ -156,7 +144,8 @@ esp_err_t PsychicFileResponse::send() {
|
|||||||
//keep track of our memory
|
//keep track of our memory
|
||||||
free(chunk);
|
free(chunk);
|
||||||
|
|
||||||
if (err == ESP_OK) {
|
if (err == ESP_OK)
|
||||||
|
{
|
||||||
ESP_LOGI(PH_TAG, "File sending complete");
|
ESP_LOGI(PH_TAG, "File sending complete");
|
||||||
this->finishChunking();
|
this->finishChunking();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
class PsychicRequest;
|
class PsychicRequest;
|
||||||
|
|
||||||
class PsychicFileResponse : public PsychicResponse {
|
class PsychicFileResponse: public PsychicResponse
|
||||||
|
{
|
||||||
using File = fs::File;
|
using File = fs::File;
|
||||||
using FS = fs::FS;
|
using FS = fs::FS;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
File _content;
|
File _content;
|
||||||
String _path;
|
String _path;
|
||||||
@@ -17,7 +17,6 @@ class PsychicFileResponse : public PsychicResponse {
|
|||||||
bool _chunked;
|
bool _chunked;
|
||||||
String _contentType;
|
String _contentType;
|
||||||
void _setContentType(const String& path);
|
void _setContentType(const String& path);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PsychicFileResponse(PsychicRequest *request, FS &fs, const String& path, const String& contentType=String(), bool download=false);
|
PsychicFileResponse(PsychicRequest *request, FS &fs, const String& path, const String& contentType=String(), bool download=false);
|
||||||
PsychicFileResponse(PsychicRequest *request, File content, const String& path, const String& contentType=String(), bool download=false);
|
PsychicFileResponse(PsychicRequest *request, File content, const String& path, const String& contentType=String(), bool download=false);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "PsychicRequest.h"
|
#include "PsychicRequest.h"
|
||||||
|
|
||||||
class PsychicEndpoint;
|
class PsychicEndpoint;
|
||||||
|
class PsychicHttpServer;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* HANDLER :: Can be attached to any endpoint or as a generic request handler.
|
* HANDLER :: Can be attached to any endpoint or as a generic request handler.
|
||||||
@@ -32,14 +33,11 @@ class PsychicHandler {
|
|||||||
PsychicHandler* setFilter(PsychicRequestFilterFunction fn);
|
PsychicHandler* setFilter(PsychicRequestFilterFunction fn);
|
||||||
bool filter(PsychicRequest *request);
|
bool filter(PsychicRequest *request);
|
||||||
|
|
||||||
PsychicHandler *
|
PsychicHandler* setAuthentication(const char *username, const char *password, HTTPAuthMethod method = BASIC_AUTH, const char *realm = "", const char *authFailMsg = "");
|
||||||
setAuthentication(const char * username, const char * password, HTTPAuthMethod method = BASIC_AUTH, const char * realm = "", const char * authFailMsg = "");
|
|
||||||
bool needsAuthentication(PsychicRequest *request);
|
bool needsAuthentication(PsychicRequest *request);
|
||||||
esp_err_t authenticate(PsychicRequest *request);
|
esp_err_t authenticate(PsychicRequest *request);
|
||||||
|
|
||||||
virtual bool isWebSocket() {
|
virtual bool isWebSocket() { return false; };
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
PsychicClient * checkForNewClient(PsychicClient *client);
|
PsychicClient * checkForNewClient(PsychicClient *client);
|
||||||
void checkForClosedClient(PsychicClient *client);
|
void checkForClosedClient(PsychicClient *client);
|
||||||
@@ -52,15 +50,11 @@ class PsychicHandler {
|
|||||||
virtual void closeCallback(PsychicClient *client) {};
|
virtual void closeCallback(PsychicClient *client) {};
|
||||||
|
|
||||||
bool hasClient(PsychicClient *client);
|
bool hasClient(PsychicClient *client);
|
||||||
int count() {
|
int count() { return _clients.size(); };
|
||||||
return _clients.size();
|
|
||||||
};
|
|
||||||
const std::list<PsychicClient*>& getClientList();
|
const std::list<PsychicClient*>& getClientList();
|
||||||
|
|
||||||
//derived classes must implement these functions
|
//derived classes must implement these functions
|
||||||
virtual bool canHandle(PsychicRequest * request) {
|
virtual bool canHandle(PsychicRequest *request) { return true; };
|
||||||
return true;
|
|
||||||
};
|
|
||||||
virtual esp_err_t handleRequest(PsychicRequest *request) = 0;
|
virtual esp_err_t handleRequest(PsychicRequest *request) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "PsychicHandler.h"
|
#include "PsychicHandler.h"
|
||||||
#include "PsychicStaticFileHandler.h"
|
#include "PsychicStaticFileHandler.h"
|
||||||
#include "PsychicFileResponse.h"
|
#include "PsychicFileResponse.h"
|
||||||
|
#include "PsychicStreamResponse.h"
|
||||||
#include "PsychicUploadHandler.h"
|
#include "PsychicUploadHandler.h"
|
||||||
#include "PsychicWebSocket.h"
|
#include "PsychicWebSocket.h"
|
||||||
#include "PsychicEventSource.h"
|
#include "PsychicEventSource.h"
|
||||||
|
|||||||
@@ -4,12 +4,13 @@
|
|||||||
#include "PsychicWebHandler.h"
|
#include "PsychicWebHandler.h"
|
||||||
#include "PsychicStaticFileHandler.h"
|
#include "PsychicStaticFileHandler.h"
|
||||||
#include "PsychicWebSocket.h"
|
#include "PsychicWebSocket.h"
|
||||||
|
#include "PsychicJson.h"
|
||||||
#include "WiFi.h"
|
#include "WiFi.h"
|
||||||
#include "PsychicJson.h" // added by proddy
|
|
||||||
|
|
||||||
PsychicHttpServer::PsychicHttpServer()
|
PsychicHttpServer::PsychicHttpServer() :
|
||||||
: _onOpen(NULL)
|
_onOpen(NULL),
|
||||||
, _onClose(NULL) {
|
_onClose(NULL)
|
||||||
|
{
|
||||||
maxRequestBodySize = MAX_REQUEST_BODY_SIZE;
|
maxRequestBodySize = MAX_REQUEST_BODY_SIZE;
|
||||||
maxUploadSize = MAX_UPLOAD_SIZE;
|
maxUploadSize = MAX_UPLOAD_SIZE;
|
||||||
|
|
||||||
@@ -36,7 +37,8 @@ PsychicHttpServer::PsychicHttpServer()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicHttpServer::~PsychicHttpServer() {
|
PsychicHttpServer::~PsychicHttpServer()
|
||||||
|
{
|
||||||
for (auto *client : _clients)
|
for (auto *client : _clients)
|
||||||
delete(client);
|
delete(client);
|
||||||
_clients.clear();
|
_clients.clear();
|
||||||
@@ -52,19 +54,22 @@ PsychicHttpServer::~PsychicHttpServer() {
|
|||||||
delete defaultEndpoint;
|
delete defaultEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicHttpServer::destroy(void * ctx) {
|
void PsychicHttpServer::destroy(void *ctx)
|
||||||
|
{
|
||||||
PsychicHttpServer *temp = (PsychicHttpServer *)ctx;
|
PsychicHttpServer *temp = (PsychicHttpServer *)ctx;
|
||||||
delete temp;
|
delete temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicHttpServer::listen(uint16_t port) {
|
esp_err_t PsychicHttpServer::listen(uint16_t port)
|
||||||
|
{
|
||||||
this->_use_ssl = false;
|
this->_use_ssl = false;
|
||||||
this->config.server_port = port;
|
this->config.server_port = port;
|
||||||
|
|
||||||
return this->_start();
|
return this->_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicHttpServer::_start() {
|
esp_err_t PsychicHttpServer::_start()
|
||||||
|
{
|
||||||
esp_err_t ret;
|
esp_err_t ret;
|
||||||
|
|
||||||
#ifdef ENABLE_ASYNC
|
#ifdef ENABLE_ASYNC
|
||||||
@@ -74,7 +79,8 @@ esp_err_t PsychicHttpServer::_start() {
|
|||||||
|
|
||||||
//fire it up.
|
//fire it up.
|
||||||
ret = _startServer();
|
ret = _startServer();
|
||||||
if (ret != ESP_OK) {
|
if (ret != ESP_OK)
|
||||||
|
{
|
||||||
ESP_LOGE(PH_TAG, "Server start failed (%s)", esp_err_to_name(ret));
|
ESP_LOGE(PH_TAG, "Server start failed (%s)", esp_err_to_name(ret));
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -91,7 +97,8 @@ esp_err_t PsychicHttpServer::_startServer() {
|
|||||||
return httpd_start(&this->server, &this->config);
|
return httpd_start(&this->server, &this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicHttpServer::stop() {
|
void PsychicHttpServer::stop()
|
||||||
|
{
|
||||||
httpd_stop(this->server);
|
httpd_stop(this->server);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,36 +115,20 @@ PsychicEndpoint * PsychicHttpServer::on(const char * uri) {
|
|||||||
return on(uri, HTTP_GET);
|
return on(uri, HTTP_GET);
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEndpoint * PsychicHttpServer::on(const char * uri, PsychicHttpRequestCallback fn) {
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, http_method method)
|
||||||
return on(uri, HTTP_GET, fn);
|
{
|
||||||
}
|
|
||||||
|
|
||||||
PsychicEndpoint * PsychicHttpServer::on(const char * uri, http_method method, PsychicHttpRequestCallback fn) {
|
|
||||||
//these basic requests need a basic web handler
|
|
||||||
PsychicWebHandler * handler = new PsychicWebHandler();
|
|
||||||
handler->onRequest(fn);
|
|
||||||
|
|
||||||
return on(uri, method, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
// added by Proddy
|
|
||||||
PsychicEndpoint * PsychicHttpServer::on(const char * uri, http_method method, PsychicJsonRequestCallback fn) {
|
|
||||||
PsychicJsonHandler * handler = new PsychicJsonHandler(fn);
|
|
||||||
|
|
||||||
return on(uri, method, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
PsychicEndpoint * PsychicHttpServer::on(const char * uri, http_method method) {
|
|
||||||
PsychicWebHandler *handler = new PsychicWebHandler();
|
PsychicWebHandler *handler = new PsychicWebHandler();
|
||||||
|
|
||||||
return on(uri, method, handler);
|
return on(uri, method, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEndpoint * PsychicHttpServer::on(const char * uri, PsychicHandler * handler) {
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, PsychicHandler *handler)
|
||||||
|
{
|
||||||
return on(uri, HTTP_GET, handler);
|
return on(uri, HTTP_GET, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicEndpoint * PsychicHttpServer::on(const char * uri, http_method method, PsychicHandler * handler) {
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, http_method method, PsychicHandler *handler)
|
||||||
|
{
|
||||||
//make our endpoint
|
//make our endpoint
|
||||||
PsychicEndpoint *endpoint = new PsychicEndpoint(this, method, uri);
|
PsychicEndpoint *endpoint = new PsychicEndpoint(this, method, uri);
|
||||||
|
|
||||||
@@ -145,13 +136,18 @@ PsychicEndpoint * PsychicHttpServer::on(const char * uri, http_method method, Ps
|
|||||||
endpoint->setHandler(handler);
|
endpoint->setHandler(handler);
|
||||||
|
|
||||||
// URI handler structure
|
// URI handler structure
|
||||||
httpd_uri_t my_uri{.uri = uri, .method = method, .handler = PsychicEndpoint::requestCallback, .user_ctx = endpoint, .is_websocket = handler->isWebSocket()};
|
httpd_uri_t my_uri {
|
||||||
|
.uri = uri,
|
||||||
|
.method = method,
|
||||||
|
.handler = PsychicEndpoint::requestCallback,
|
||||||
|
.user_ctx = endpoint,
|
||||||
|
.is_websocket = handler->isWebSocket()
|
||||||
|
};
|
||||||
|
|
||||||
// Register endpoint with ESP-IDF server
|
// Register endpoint with ESP-IDF server
|
||||||
esp_err_t ret = httpd_register_uri_handler(this->server, &my_uri);
|
esp_err_t ret = httpd_register_uri_handler(this->server, &my_uri);
|
||||||
if (ret != ESP_OK) {
|
if (ret != ESP_OK)
|
||||||
ESP_LOGE(PH_TAG, "Add endpoint %s failed (%s)", uri, esp_err_to_name(ret)); // modified by proddy
|
ESP_LOGE(PH_TAG, "Add endpoint failed (%s)", esp_err_to_name(ret));
|
||||||
}
|
|
||||||
|
|
||||||
//save it for later
|
//save it for later
|
||||||
_endpoints.push_back(endpoint);
|
_endpoints.push_back(endpoint);
|
||||||
@@ -159,21 +155,53 @@ PsychicEndpoint * PsychicHttpServer::on(const char * uri, http_method method, Ps
|
|||||||
return endpoint;
|
return endpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicHttpServer::onNotFound(PsychicHttpRequestCallback fn) {
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, PsychicHttpRequestCallback fn)
|
||||||
|
{
|
||||||
|
return on(uri, HTTP_GET, fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, http_method method, PsychicHttpRequestCallback fn)
|
||||||
|
{
|
||||||
|
//these basic requests need a basic web handler
|
||||||
|
PsychicWebHandler *handler = new PsychicWebHandler();
|
||||||
|
handler->onRequest(fn);
|
||||||
|
|
||||||
|
return on(uri, method, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, PsychicJsonRequestCallback fn)
|
||||||
|
{
|
||||||
|
return on(uri, HTTP_GET, fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicEndpoint* PsychicHttpServer::on(const char* uri, http_method method, PsychicJsonRequestCallback fn)
|
||||||
|
{
|
||||||
|
//these basic requests need a basic web handler
|
||||||
|
PsychicJsonHandler *handler = new PsychicJsonHandler();
|
||||||
|
handler->onRequest(fn);
|
||||||
|
|
||||||
|
return on(uri, method, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PsychicHttpServer::onNotFound(PsychicHttpRequestCallback fn)
|
||||||
|
{
|
||||||
PsychicWebHandler *handler = new PsychicWebHandler();
|
PsychicWebHandler *handler = new PsychicWebHandler();
|
||||||
handler->onRequest(fn);
|
handler->onRequest(fn);
|
||||||
|
|
||||||
this->defaultEndpoint->setHandler(handler);
|
this->defaultEndpoint->setHandler(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicHttpServer::notFoundHandler(httpd_req_t * req, httpd_err_code_t err) {
|
esp_err_t PsychicHttpServer::notFoundHandler(httpd_req_t *req, httpd_err_code_t err)
|
||||||
|
{
|
||||||
PsychicHttpServer *server = (PsychicHttpServer*)httpd_get_global_user_ctx(req->handle);
|
PsychicHttpServer *server = (PsychicHttpServer*)httpd_get_global_user_ctx(req->handle);
|
||||||
PsychicRequest request(server, req);
|
PsychicRequest request(server, req);
|
||||||
|
|
||||||
//loop through our global handlers and see if anyone wants it
|
//loop through our global handlers and see if anyone wants it
|
||||||
for (auto * handler : server->_handlers) {
|
for(auto *handler: server->_handlers)
|
||||||
|
{
|
||||||
//are we capable of handling this?
|
//are we capable of handling this?
|
||||||
if (handler->filter(&request) && handler->canHandle(&request)) {
|
if (handler->filter(&request) && handler->canHandle(&request))
|
||||||
|
{
|
||||||
//check our credentials
|
//check our credentials
|
||||||
if (handler->needsAuthentication(&request))
|
if (handler->needsAuthentication(&request))
|
||||||
return handler->authenticate(&request);
|
return handler->authenticate(&request);
|
||||||
@@ -191,7 +219,8 @@ esp_err_t PsychicHttpServer::notFoundHandler(httpd_req_t * req, httpd_err_code_t
|
|||||||
return ESP_ERR_HTTPD_INVALID_REQ;
|
return ESP_ERR_HTTPD_INVALID_REQ;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicHttpServer::defaultNotFoundHandler(PsychicRequest * request) {
|
esp_err_t PsychicHttpServer::defaultNotFoundHandler(PsychicRequest *request)
|
||||||
|
{
|
||||||
request->reply(404, "text/html", "That URI does not exist.");
|
request->reply(404, "text/html", "That URI does not exist.");
|
||||||
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -201,7 +230,8 @@ void PsychicHttpServer::onOpen(PsychicClientCallback handler) {
|
|||||||
this->_onOpen = handler;
|
this->_onOpen = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicHttpServer::openCallback(httpd_handle_t hd, int sockfd) {
|
esp_err_t PsychicHttpServer::openCallback(httpd_handle_t hd, int sockfd)
|
||||||
|
{
|
||||||
ESP_LOGI(PH_TAG, "New client connected %d", sockfd);
|
ESP_LOGI(PH_TAG, "New client connected %d", sockfd);
|
||||||
|
|
||||||
//get our global server reference
|
//get our global server reference
|
||||||
@@ -209,7 +239,8 @@ esp_err_t PsychicHttpServer::openCallback(httpd_handle_t hd, int sockfd) {
|
|||||||
|
|
||||||
//lookup our client
|
//lookup our client
|
||||||
PsychicClient *client = server->getClient(sockfd);
|
PsychicClient *client = server->getClient(sockfd);
|
||||||
if (client == NULL) {
|
if (client == NULL)
|
||||||
|
{
|
||||||
client = new PsychicClient(hd, sockfd);
|
client = new PsychicClient(hd, sockfd);
|
||||||
server->addClient(client);
|
server->addClient(client);
|
||||||
}
|
}
|
||||||
@@ -225,16 +256,19 @@ void PsychicHttpServer::onClose(PsychicClientCallback handler) {
|
|||||||
this->_onClose = handler;
|
this->_onClose = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicHttpServer::closeCallback(httpd_handle_t hd, int sockfd) {
|
void PsychicHttpServer::closeCallback(httpd_handle_t hd, int sockfd)
|
||||||
|
{
|
||||||
ESP_LOGI(PH_TAG, "Client disconnected %d", sockfd);
|
ESP_LOGI(PH_TAG, "Client disconnected %d", sockfd);
|
||||||
|
|
||||||
PsychicHttpServer *server = (PsychicHttpServer*)httpd_get_global_user_ctx(hd);
|
PsychicHttpServer *server = (PsychicHttpServer*)httpd_get_global_user_ctx(hd);
|
||||||
|
|
||||||
//lookup our client
|
//lookup our client
|
||||||
PsychicClient *client = server->getClient(sockfd);
|
PsychicClient *client = server->getClient(sockfd);
|
||||||
if (client != NULL) {
|
if (client != NULL)
|
||||||
|
{
|
||||||
//give our handlers a chance to handle a disconnect first
|
//give our handlers a chance to handle a disconnect first
|
||||||
for (PsychicEndpoint * endpoint : server->_endpoints) {
|
for (PsychicEndpoint * endpoint : server->_endpoints)
|
||||||
|
{
|
||||||
PsychicHandler *handler = endpoint->handler();
|
PsychicHandler *handler = endpoint->handler();
|
||||||
handler->checkForClosedClient(client);
|
handler->checkForClosedClient(client);
|
||||||
}
|
}
|
||||||
@@ -245,14 +279,16 @@ void PsychicHttpServer::closeCallback(httpd_handle_t hd, int sockfd) {
|
|||||||
|
|
||||||
//remove it from our list
|
//remove it from our list
|
||||||
server->removeClient(client);
|
server->removeClient(client);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
ESP_LOGE(PH_TAG, "No client record %d", sockfd);
|
ESP_LOGE(PH_TAG, "No client record %d", sockfd);
|
||||||
|
|
||||||
//finally close it out.
|
//finally close it out.
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicStaticFileHandler * PsychicHttpServer::serveStatic(const char * uri, fs::FS & fs, const char * path, const char * cache_control) {
|
PsychicStaticFileHandler* PsychicHttpServer::serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_control)
|
||||||
|
{
|
||||||
PsychicStaticFileHandler* handler = new PsychicStaticFileHandler(uri, fs, path, cache_control);
|
PsychicStaticFileHandler* handler = new PsychicStaticFileHandler(uri, fs, path, cache_control);
|
||||||
this->addHandler(handler);
|
this->addHandler(handler);
|
||||||
|
|
||||||
@@ -296,7 +332,8 @@ bool ON_AP_FILTER(PsychicRequest * request) {
|
|||||||
return WiFi.softAPIP() == request->client()->localIP();
|
return WiFi.softAPIP() == request->client()->localIP();
|
||||||
}
|
}
|
||||||
|
|
||||||
String urlDecode(const char * encoded) {
|
String urlDecode(const char* encoded)
|
||||||
|
{
|
||||||
size_t length = strlen(encoded);
|
size_t length = strlen(encoded);
|
||||||
char* decoded = (char*)malloc(length + 1);
|
char* decoded = (char*)malloc(length + 1);
|
||||||
if (!decoded) {
|
if (!decoded) {
|
||||||
|
|||||||
@@ -4,17 +4,13 @@
|
|||||||
#include "PsychicCore.h"
|
#include "PsychicCore.h"
|
||||||
#include "PsychicClient.h"
|
#include "PsychicClient.h"
|
||||||
#include "PsychicHandler.h"
|
#include "PsychicHandler.h"
|
||||||
#include <ArduinoJson.h> // added by proddy
|
|
||||||
|
|
||||||
class PsychicEndpoint;
|
class PsychicEndpoint;
|
||||||
class PsychicHandler;
|
class PsychicHandler;
|
||||||
class PsychicStaticFileHandler;
|
class PsychicStaticFileHandler;
|
||||||
|
|
||||||
//callback definitions
|
class PsychicHttpServer
|
||||||
typedef std::function<esp_err_t(PsychicRequest * request)> PsychicHttpRequestCallback;
|
{
|
||||||
typedef std::function<esp_err_t(PsychicRequest * request, JsonVariant & json)> PsychicJsonRequestCallback; // added by proddy
|
|
||||||
|
|
||||||
class PsychicHttpServer {
|
|
||||||
protected:
|
protected:
|
||||||
bool _use_ssl = false;
|
bool _use_ssl = false;
|
||||||
std::list<PsychicEndpoint*> _endpoints;
|
std::list<PsychicEndpoint*> _endpoints;
|
||||||
@@ -55,18 +51,17 @@ class PsychicHttpServer {
|
|||||||
PsychicClient* getClient(int socket);
|
PsychicClient* getClient(int socket);
|
||||||
PsychicClient* getClient(httpd_req_t *req);
|
PsychicClient* getClient(httpd_req_t *req);
|
||||||
bool hasClient(int socket);
|
bool hasClient(int socket);
|
||||||
int count() {
|
int count() { return _clients.size(); };
|
||||||
return _clients.size();
|
|
||||||
};
|
|
||||||
const std::list<PsychicClient*>& getClientList();
|
const std::list<PsychicClient*>& getClientList();
|
||||||
|
|
||||||
PsychicEndpoint* on(const char* uri);
|
PsychicEndpoint* on(const char* uri);
|
||||||
PsychicEndpoint* on(const char* uri, http_method method);
|
PsychicEndpoint* on(const char* uri, http_method method);
|
||||||
PsychicEndpoint * on(const char * uri, PsychicHttpRequestCallback onRequest);
|
|
||||||
PsychicEndpoint * on(const char * uri, http_method method, PsychicHttpRequestCallback onRequest);
|
|
||||||
PsychicEndpoint* on(const char* uri, PsychicHandler *handler);
|
PsychicEndpoint* on(const char* uri, PsychicHandler *handler);
|
||||||
PsychicEndpoint* on(const char* uri, http_method method, PsychicHandler *handler);
|
PsychicEndpoint* on(const char* uri, http_method method, PsychicHandler *handler);
|
||||||
PsychicEndpoint * on(const char * uri, http_method method, PsychicJsonRequestCallback onRequest); // added proddy
|
PsychicEndpoint* on(const char* uri, PsychicHttpRequestCallback onRequest);
|
||||||
|
PsychicEndpoint* on(const char* uri, http_method method, PsychicHttpRequestCallback onRequest);
|
||||||
|
PsychicEndpoint* on(const char* uri, PsychicJsonRequestCallback onRequest);
|
||||||
|
PsychicEndpoint* on(const char* uri, http_method method, PsychicJsonRequestCallback onRequest);
|
||||||
|
|
||||||
static esp_err_t notFoundHandler(httpd_req_t *req, httpd_err_code_t err);
|
static esp_err_t notFoundHandler(httpd_req_t *req, httpd_err_code_t err);
|
||||||
static esp_err_t defaultNotFoundHandler(PsychicRequest *request);
|
static esp_err_t defaultNotFoundHandler(PsychicRequest *request);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "PsychicHttpsServer.h"
|
#include "PsychicHttpsServer.h"
|
||||||
|
|
||||||
PsychicHttpsServer::PsychicHttpsServer()
|
PsychicHttpsServer::PsychicHttpsServer() : PsychicHttpServer()
|
||||||
: PsychicHttpServer() {
|
{
|
||||||
//for a SSL server
|
//for a SSL server
|
||||||
ssl_config = HTTPD_SSL_CONFIG_DEFAULT();
|
ssl_config = HTTPD_SSL_CONFIG_DEFAULT();
|
||||||
ssl_config.httpd.open_fn = PsychicHttpServer::openCallback;
|
ssl_config.httpd.open_fn = PsychicHttpServer::openCallback;
|
||||||
@@ -18,10 +18,10 @@ PsychicHttpsServer::PsychicHttpsServer()
|
|||||||
ssl_config.httpd.max_open_sockets = 2;
|
ssl_config.httpd.max_open_sockets = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicHttpsServer::~PsychicHttpsServer() {
|
PsychicHttpsServer::~PsychicHttpsServer() {}
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t PsychicHttpsServer::listen(uint16_t port, const char * cert, const char * private_key) {
|
esp_err_t PsychicHttpsServer::listen(uint16_t port, const char *cert, const char *private_key)
|
||||||
|
{
|
||||||
this->_use_ssl = true;
|
this->_use_ssl = true;
|
||||||
|
|
||||||
this->ssl_config.port_secure = port;
|
this->ssl_config.port_secure = port;
|
||||||
@@ -33,14 +33,16 @@ esp_err_t PsychicHttpsServer::listen(uint16_t port, const char * cert, const cha
|
|||||||
return this->_start();
|
return this->_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicHttpsServer::_startServer() {
|
esp_err_t PsychicHttpsServer::_startServer()
|
||||||
|
{
|
||||||
if (this->_use_ssl)
|
if (this->_use_ssl)
|
||||||
return httpd_ssl_start(&this->server, &this->ssl_config);
|
return httpd_ssl_start(&this->server, &this->ssl_config);
|
||||||
else
|
else
|
||||||
return httpd_start(&this->server, &this->config);
|
return httpd_start(&this->server, &this->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicHttpsServer::stop() {
|
void PsychicHttpsServer::stop()
|
||||||
|
{
|
||||||
if (this->_use_ssl)
|
if (this->_use_ssl)
|
||||||
httpd_ssl_stop(this->server);
|
httpd_ssl_stop(this->server);
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -11,7 +11,8 @@
|
|||||||
|
|
||||||
#define PSY_ENABLE_SSL //you can use this define in your code to enable/disable these features
|
#define PSY_ENABLE_SSL //you can use this define in your code to enable/disable these features
|
||||||
|
|
||||||
class PsychicHttpsServer : public PsychicHttpServer {
|
class PsychicHttpsServer : public PsychicHttpServer
|
||||||
|
{
|
||||||
protected:
|
protected:
|
||||||
bool _use_ssl = false;
|
bool _use_ssl = false;
|
||||||
|
|
||||||
|
|||||||
150
lib/PsychicHttp/src/PsychicJson.DELETEME
Normal file
150
lib/PsychicHttp/src/PsychicJson.DELETEME
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
#include "PsychicJson.h"
|
||||||
|
|
||||||
|
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||||
|
PsychicJsonResponse::PsychicJsonResponse(PsychicRequest *request, bool isArray) : PsychicResponse(request)
|
||||||
|
{
|
||||||
|
setContentType(JSON_MIMETYPE);
|
||||||
|
if (isArray)
|
||||||
|
_root = _jsonBuffer.createArray();
|
||||||
|
else
|
||||||
|
_root = _jsonBuffer.createObject();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
PsychicJsonResponse::PsychicJsonResponse(PsychicRequest *request, bool isArray, size_t maxJsonBufferSize) :
|
||||||
|
PsychicResponse(request),
|
||||||
|
_jsonBuffer(maxJsonBufferSize)
|
||||||
|
{
|
||||||
|
setContentType(JSON_MIMETYPE);
|
||||||
|
if (isArray)
|
||||||
|
_root = _jsonBuffer.createNestedArray();
|
||||||
|
else
|
||||||
|
_root = _jsonBuffer.createNestedObject();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
JsonVariant &PsychicJsonResponse::getRoot() { return _root; }
|
||||||
|
|
||||||
|
size_t PsychicJsonResponse::getLength()
|
||||||
|
{
|
||||||
|
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||||
|
return _root.measureLength();
|
||||||
|
#else
|
||||||
|
return measureJson(_root);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t PsychicJsonResponse::send()
|
||||||
|
{
|
||||||
|
esp_err_t err = ESP_OK;
|
||||||
|
size_t length = getLength();
|
||||||
|
size_t buffer_size;
|
||||||
|
char *buffer;
|
||||||
|
|
||||||
|
DUMP(length);
|
||||||
|
|
||||||
|
//how big of a buffer do we want?
|
||||||
|
if (length < JSON_BUFFER_SIZE)
|
||||||
|
buffer_size = length+1;
|
||||||
|
else
|
||||||
|
buffer_size = JSON_BUFFER_SIZE;
|
||||||
|
|
||||||
|
DUMP(buffer_size);
|
||||||
|
|
||||||
|
buffer = (char *)malloc(buffer_size);
|
||||||
|
if (buffer == NULL) {
|
||||||
|
httpd_resp_send_err(this->_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//send it in one shot or no?
|
||||||
|
if (length < JSON_BUFFER_SIZE)
|
||||||
|
{
|
||||||
|
TRACE();
|
||||||
|
|
||||||
|
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||||
|
_root.printTo(buffer, buffer_size);
|
||||||
|
#else
|
||||||
|
serializeJson(_root, buffer, buffer_size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
this->setContent((uint8_t *)buffer, length);
|
||||||
|
this->setContentType(JSON_MIMETYPE);
|
||||||
|
|
||||||
|
err = PsychicResponse::send();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//helper class that acts as a stream to print chunked responses
|
||||||
|
ChunkPrinter dest(this, (uint8_t *)buffer, buffer_size);
|
||||||
|
|
||||||
|
//keep our headers
|
||||||
|
this->sendHeaders();
|
||||||
|
|
||||||
|
//these commands write to the ChunkPrinter which does the sending
|
||||||
|
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||||
|
_root.printTo(dest);
|
||||||
|
#else
|
||||||
|
serializeJson(_root, dest);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//send the last bits
|
||||||
|
dest.flush();
|
||||||
|
|
||||||
|
//done with our chunked response too
|
||||||
|
err = this->finishChunking();
|
||||||
|
}
|
||||||
|
|
||||||
|
//let the buffer go
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||||
|
PsychicJsonHandler::PsychicJsonHandler() :
|
||||||
|
_onRequest(NULL)
|
||||||
|
{};
|
||||||
|
|
||||||
|
PsychicJsonHandler::PsychicJsonHandler(PsychicJsonRequestCallback onRequest) :
|
||||||
|
_onRequest(onRequest)
|
||||||
|
{}
|
||||||
|
#else
|
||||||
|
PsychicJsonHandler::PsychicJsonHandler(size_t maxJsonBufferSize) :
|
||||||
|
_onRequest(NULL),
|
||||||
|
_maxJsonBufferSize(maxJsonBufferSize)
|
||||||
|
{};
|
||||||
|
|
||||||
|
PsychicJsonHandler::PsychicJsonHandler(PsychicJsonRequestCallback onRequest, size_t maxJsonBufferSize) :
|
||||||
|
_onRequest(onRequest),
|
||||||
|
_maxJsonBufferSize(maxJsonBufferSize)
|
||||||
|
{}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void PsychicJsonHandler::onRequest(PsychicJsonRequestCallback fn) { _onRequest = fn; }
|
||||||
|
|
||||||
|
esp_err_t PsychicJsonHandler::handleRequest(PsychicRequest *request)
|
||||||
|
{
|
||||||
|
//process basic stuff
|
||||||
|
PsychicWebHandler::handleRequest(request);
|
||||||
|
|
||||||
|
if (_onRequest)
|
||||||
|
{
|
||||||
|
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||||
|
DynamicJsonBuffer jsonBuffer;
|
||||||
|
JsonVariant json = jsonBuffer.parse();
|
||||||
|
if (!json.success())
|
||||||
|
return request->reply(400);
|
||||||
|
#else
|
||||||
|
DynamicJsonDocument jsonBuffer(this->_maxJsonBufferSize);
|
||||||
|
DeserializationError error = deserializeJson(jsonBuffer, request->body());
|
||||||
|
if (error)
|
||||||
|
return request->reply(400);
|
||||||
|
|
||||||
|
JsonVariant json = jsonBuffer.as<JsonVariant>();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return _onRequest(request, json);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return request->reply(500);
|
||||||
|
}
|
||||||
@@ -99,7 +99,9 @@ class PsychicJsonResponse : public PsychicResponse {
|
|||||||
|
|
||||||
virtual esp_err_t send() override {
|
virtual esp_err_t send() override {
|
||||||
esp_err_t err = ESP_OK;
|
esp_err_t err = ESP_OK;
|
||||||
size_t length = getLength();
|
// size_t length = getLength();
|
||||||
|
size_t length = JSON_BUFFER_SIZE; // TODO force chunking
|
||||||
|
|
||||||
size_t buffer_size;
|
size_t buffer_size;
|
||||||
char * buffer;
|
char * buffer;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "PsychicRequest.h"
|
#include "PsychicRequest.h"
|
||||||
#include "PsychicResponse.h"
|
#include "http_status.h"
|
||||||
#include <http_status.h>
|
#include "PsychicHttpServer.h"
|
||||||
|
|
||||||
|
|
||||||
PsychicRequest::PsychicRequest(PsychicHttpServer *server, httpd_req_t *req) :
|
PsychicRequest::PsychicRequest(PsychicHttpServer *server, httpd_req_t *req) :
|
||||||
_server(server),
|
_server(server),
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "PsychicHttpServer.h"
|
#include "PsychicHttpServer.h"
|
||||||
#include "PsychicClient.h"
|
#include "PsychicClient.h"
|
||||||
#include "PsychicWebParameter.h"
|
#include "PsychicWebParameter.h"
|
||||||
|
#include "PsychicResponse.h"
|
||||||
|
|
||||||
typedef std::map<String, String> SessionData;
|
typedef std::map<String, String> SessionData;
|
||||||
|
|
||||||
|
|||||||
@@ -2,24 +2,28 @@
|
|||||||
#include "PsychicRequest.h"
|
#include "PsychicRequest.h"
|
||||||
#include <http_status.h>
|
#include <http_status.h>
|
||||||
|
|
||||||
PsychicResponse::PsychicResponse(PsychicRequest * request)
|
PsychicResponse::PsychicResponse(PsychicRequest *request) :
|
||||||
: _request(request)
|
_request(request),
|
||||||
, _code(200)
|
_code(200),
|
||||||
, _status("")
|
_status(""),
|
||||||
, _contentLength(0)
|
_contentLength(0),
|
||||||
, _body("") {
|
_body("")
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicResponse::~PsychicResponse() {
|
PsychicResponse::~PsychicResponse()
|
||||||
|
{
|
||||||
//clean up our header variables. we have to do this since httpd_resp_send doesn't store copies
|
//clean up our header variables. we have to do this since httpd_resp_send doesn't store copies
|
||||||
for (HTTPHeader header : _headers) {
|
for (HTTPHeader header : _headers)
|
||||||
|
{
|
||||||
free(header.field);
|
free(header.field);
|
||||||
free(header.value);
|
free(header.value);
|
||||||
}
|
}
|
||||||
_headers.clear();
|
_headers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicResponse::addHeader(const char * field, const char * value) {
|
void PsychicResponse::addHeader(const char *field, const char *value)
|
||||||
|
{
|
||||||
//these get freed during send()
|
//these get freed during send()
|
||||||
HTTPHeader header;
|
HTTPHeader header;
|
||||||
header.field =(char *)malloc(strlen(field)+1);
|
header.field =(char *)malloc(strlen(field)+1);
|
||||||
@@ -31,7 +35,8 @@ void PsychicResponse::addHeader(const char * field, const char * value) {
|
|||||||
_headers.push_back(header);
|
_headers.push_back(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicResponse::setCookie(const char * name, const char * value, unsigned long secondsFromNow, const char * extras) {
|
void PsychicResponse::setCookie(const char *name, const char *value, unsigned long secondsFromNow, const char *extras)
|
||||||
|
{
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
|
|
||||||
String output;
|
String output;
|
||||||
@@ -41,7 +46,8 @@ void PsychicResponse::setCookie(const char * name, const char * value, unsigned
|
|||||||
if (now < 1700000000)
|
if (now < 1700000000)
|
||||||
output += "; Max-Age=" + String(secondsFromNow);
|
output += "; Max-Age=" + String(secondsFromNow);
|
||||||
//otherwise, set an expiration date
|
//otherwise, set an expiration date
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
time_t expirationTimestamp = now + secondsFromNow;
|
time_t expirationTimestamp = now + secondsFromNow;
|
||||||
|
|
||||||
// Convert the expiration timestamp to a formatted string for the "expires" attribute
|
// Convert the expiration timestamp to a formatted string for the "expires" attribute
|
||||||
@@ -62,33 +68,40 @@ void PsychicResponse::setCookie(const char * name, const char * value, unsigned
|
|||||||
// time_t now = time(nullptr);
|
// time_t now = time(nullptr);
|
||||||
// // Set the cookie with the "expires" attribute
|
// // Set the cookie with the "expires" attribute
|
||||||
|
|
||||||
void PsychicResponse::setCode(int code) {
|
void PsychicResponse::setCode(int code)
|
||||||
|
{
|
||||||
_code = code;
|
_code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicResponse::setContentType(const char * contentType) {
|
void PsychicResponse::setContentType(const char *contentType)
|
||||||
|
{
|
||||||
httpd_resp_set_type(_request->request(), contentType);
|
httpd_resp_set_type(_request->request(), contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicResponse::setContent(const char * content) {
|
void PsychicResponse::setContent(const char *content)
|
||||||
|
{
|
||||||
_body = content;
|
_body = content;
|
||||||
setContentLength(strlen(content));
|
setContentLength(strlen(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicResponse::setContent(const uint8_t * content, size_t len) {
|
void PsychicResponse::setContent(const uint8_t *content, size_t len)
|
||||||
|
{
|
||||||
_body = (char *)content;
|
_body = (char *)content;
|
||||||
setContentLength(len);
|
setContentLength(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * PsychicResponse::getContent() {
|
const char * PsychicResponse::getContent()
|
||||||
|
{
|
||||||
return _body;
|
return _body;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t PsychicResponse::getContentLength() {
|
size_t PsychicResponse::getContentLength()
|
||||||
|
{
|
||||||
return _contentLength;
|
return _contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicResponse::send() {
|
esp_err_t PsychicResponse::send()
|
||||||
|
{
|
||||||
//esp-idf makes you set the whole status.
|
//esp-idf makes you set the whole status.
|
||||||
sprintf(_status, "%u %s", _code, http_status_reason(_code));
|
sprintf(_status, "%u %s", _code, http_status_reason(_code));
|
||||||
httpd_resp_set_status(_request->request(), _status);
|
httpd_resp_set_status(_request->request(), _status);
|
||||||
@@ -106,7 +119,8 @@ esp_err_t PsychicResponse::send() {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicResponse::sendHeaders() {
|
void PsychicResponse::sendHeaders()
|
||||||
|
{
|
||||||
//get our global headers out of the way first
|
//get our global headers out of the way first
|
||||||
for (HTTPHeader header : DefaultHeaders::Instance().getHeaders())
|
for (HTTPHeader header : DefaultHeaders::Instance().getHeaders())
|
||||||
httpd_resp_set_hdr(_request->request(), header.field, header.value);
|
httpd_resp_set_hdr(_request->request(), header.field, header.value);
|
||||||
@@ -116,10 +130,12 @@ void PsychicResponse::sendHeaders() {
|
|||||||
httpd_resp_set_hdr(this->_request->request(), header.field, header.value);
|
httpd_resp_set_hdr(this->_request->request(), header.field, header.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicResponse::sendChunk(uint8_t * chunk, size_t chunksize) {
|
esp_err_t PsychicResponse::sendChunk(uint8_t *chunk, size_t chunksize)
|
||||||
|
{
|
||||||
/* Send the buffer contents as HTTP response chunk */
|
/* Send the buffer contents as HTTP response chunk */
|
||||||
esp_err_t err = httpd_resp_send_chunk(this->_request->request(), (char *)chunk, chunksize);
|
esp_err_t err = httpd_resp_send_chunk(this->_request->request(), (char *)chunk, chunksize);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK)
|
||||||
|
{
|
||||||
ESP_LOGE(PH_TAG, "File sending failed (%s)", esp_err_to_name(err));
|
ESP_LOGE(PH_TAG, "File sending failed (%s)", esp_err_to_name(err));
|
||||||
|
|
||||||
/* Abort sending file */
|
/* Abort sending file */
|
||||||
@@ -132,7 +148,8 @@ esp_err_t PsychicResponse::sendChunk(uint8_t * chunk, size_t chunksize) {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicResponse::finishChunking() {
|
esp_err_t PsychicResponse::finishChunking()
|
||||||
|
{
|
||||||
/* Respond with an empty chunk to signal HTTP response completion */
|
/* Respond with an empty chunk to signal HTTP response completion */
|
||||||
return httpd_resp_send_chunk(this->_request->request(), NULL, 0);
|
return httpd_resp_send_chunk(this->_request->request(), NULL, 0);
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,8 @@
|
|||||||
|
|
||||||
class PsychicRequest;
|
class PsychicRequest;
|
||||||
|
|
||||||
class PsychicResponse {
|
class PsychicResponse
|
||||||
|
{
|
||||||
protected:
|
protected:
|
||||||
PsychicRequest *_request;
|
PsychicRequest *_request;
|
||||||
|
|
||||||
@@ -23,12 +24,8 @@ class PsychicResponse {
|
|||||||
void setCode(int code);
|
void setCode(int code);
|
||||||
|
|
||||||
void setContentType(const char *contentType);
|
void setContentType(const char *contentType);
|
||||||
void setContentLength(int64_t contentLength) {
|
void setContentLength(int64_t contentLength) { _contentLength = contentLength; }
|
||||||
_contentLength = contentLength;
|
int64_t getContentLength(int64_t contentLength) { return _contentLength; }
|
||||||
}
|
|
||||||
int64_t getContentLength(int64_t contentLength) {
|
|
||||||
return _contentLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addHeader(const char *field, const char *value);
|
void addHeader(const char *field, const char *value);
|
||||||
|
|
||||||
|
|||||||
@@ -5,17 +5,11 @@
|
|||||||
/*************************************/
|
/*************************************/
|
||||||
|
|
||||||
PsychicStaticFileHandler::PsychicStaticFileHandler(const char* uri, FS& fs, const char* path, const char* cache_control)
|
PsychicStaticFileHandler::PsychicStaticFileHandler(const char* uri, FS& fs, const char* path, const char* cache_control)
|
||||||
: _fs(fs)
|
: _fs(fs), _uri(uri), _path(path), _default_file("index.html"), _cache_control(cache_control), _last_modified("")
|
||||||
, _uri(uri)
|
{
|
||||||
, _path(path)
|
|
||||||
, _default_file("index.html")
|
|
||||||
, _cache_control(cache_control)
|
|
||||||
, _last_modified("") {
|
|
||||||
// Ensure leading '/'
|
// Ensure leading '/'
|
||||||
if (_uri.length() == 0 || _uri[0] != '/')
|
if (_uri.length() == 0 || _uri[0] != '/') _uri = "/" + _uri;
|
||||||
_uri = "/" + _uri;
|
if (_path.length() == 0 || _path[0] != '/') _path = "/" + _path;
|
||||||
if (_path.length() == 0 || _path[0] != '/')
|
|
||||||
_path = "/" + _path;
|
|
||||||
|
|
||||||
// If path ends with '/' we assume a hint that this is a directory to improve performance.
|
// If path ends with '/' we assume a hint that this is a directory to improve performance.
|
||||||
// However - if it does not end with '/' we, can't assume a file, path can still be a directory.
|
// However - if it does not end with '/' we, can't assume a file, path can still be a directory.
|
||||||
@@ -23,10 +17,8 @@ PsychicStaticFileHandler::PsychicStaticFileHandler(const char * uri, FS & fs, co
|
|||||||
|
|
||||||
// Remove the trailing '/' so we can handle default file
|
// Remove the trailing '/' so we can handle default file
|
||||||
// Notice that root will be "" not "/"
|
// Notice that root will be "" not "/"
|
||||||
if (_uri[_uri.length() - 1] == '/')
|
if (_uri[_uri.length()-1] == '/') _uri = _uri.substring(0, _uri.length()-1);
|
||||||
_uri = _uri.substring(0, _uri.length() - 1);
|
if (_path[_path.length()-1] == '/') _path = _path.substring(0, _path.length()-1);
|
||||||
if (_path[_path.length() - 1] == '/')
|
|
||||||
_path = _path.substring(0, _path.length() - 1);
|
|
||||||
|
|
||||||
// Reset stats
|
// Reset stats
|
||||||
_gzipFirst = false;
|
_gzipFirst = false;
|
||||||
@@ -59,7 +51,8 @@ PsychicStaticFileHandler & PsychicStaticFileHandler::setLastModified(struct tm *
|
|||||||
return setLastModified((const char *)result);
|
return setLastModified((const char *)result);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PsychicStaticFileHandler::canHandle(PsychicRequest * request) {
|
bool PsychicStaticFileHandler::canHandle(PsychicRequest *request)
|
||||||
|
{
|
||||||
if(request->method() != HTTP_GET || !request->uri().startsWith(_uri) )
|
if(request->method() != HTTP_GET || !request->uri().startsWith(_uri) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -69,7 +62,8 @@ bool PsychicStaticFileHandler::canHandle(PsychicRequest * request) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PsychicStaticFileHandler::_getFile(PsychicRequest * request) {
|
bool PsychicStaticFileHandler::_getFile(PsychicRequest *request)
|
||||||
|
{
|
||||||
// Remove the found uri
|
// Remove the found uri
|
||||||
String path = request->uri().substring(_uri.length());
|
String path = request->uri().substring(_uri.length());
|
||||||
|
|
||||||
@@ -96,7 +90,8 @@ bool PsychicStaticFileHandler::_getFile(PsychicRequest * request) {
|
|||||||
|
|
||||||
#define FILE_IS_REAL(f) (f == true && !f.isDirectory())
|
#define FILE_IS_REAL(f) (f == true && !f.isDirectory())
|
||||||
|
|
||||||
bool PsychicStaticFileHandler::_fileExists(const String & path) {
|
bool PsychicStaticFileHandler::_fileExists(const String& path)
|
||||||
|
{
|
||||||
bool fileFound = false;
|
bool fileFound = false;
|
||||||
bool gzipFound = false;
|
bool gzipFound = false;
|
||||||
|
|
||||||
@@ -120,44 +115,46 @@ bool PsychicStaticFileHandler::_fileExists(const String & path) {
|
|||||||
|
|
||||||
bool found = fileFound || gzipFound;
|
bool found = fileFound || gzipFound;
|
||||||
|
|
||||||
if (found) {
|
if (found)
|
||||||
|
{
|
||||||
_filename = path;
|
_filename = path;
|
||||||
|
|
||||||
// Calculate gzip statistic
|
// Calculate gzip statistic
|
||||||
_gzipStats = (_gzipStats << 1) + (gzipFound ? 1 : 0);
|
_gzipStats = (_gzipStats << 1) + (gzipFound ? 1 : 0);
|
||||||
if (_gzipStats == 0x00)
|
if (_gzipStats == 0x00) _gzipFirst = false; // All files are not gzip
|
||||||
_gzipFirst = false; // All files are not gzip
|
else if (_gzipStats == 0xFF) _gzipFirst = true; // All files are gzip
|
||||||
else if (_gzipStats == 0xFF)
|
else _gzipFirst = _countBits(_gzipStats) > 4; // IF we have more gzip files - try gzip first
|
||||||
_gzipFirst = true; // All files are gzip
|
|
||||||
else
|
|
||||||
_gzipFirst = _countBits(_gzipStats) > 4; // IF we have more gzip files - try gzip first
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t PsychicStaticFileHandler::_countBits(const uint8_t value) const {
|
uint8_t PsychicStaticFileHandler::_countBits(const uint8_t value) const
|
||||||
|
{
|
||||||
uint8_t w = value;
|
uint8_t w = value;
|
||||||
uint8_t n;
|
uint8_t n;
|
||||||
for (n = 0; w != 0; n++)
|
for (n=0; w!=0; n++) w&=w-1;
|
||||||
w &= w - 1;
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicStaticFileHandler::handleRequest(PsychicRequest * request) {
|
esp_err_t PsychicStaticFileHandler::handleRequest(PsychicRequest *request)
|
||||||
if (_file == true) {
|
{
|
||||||
|
if (_file == true)
|
||||||
|
{
|
||||||
DUMP(_filename);
|
DUMP(_filename);
|
||||||
|
|
||||||
//is it not modified?
|
//is it not modified?
|
||||||
String etag = String(_file.size());
|
String etag = String(_file.size());
|
||||||
if (_last_modified.length() && _last_modified == request->header("If-Modified-Since")) {
|
if (_last_modified.length() && _last_modified == request->header("If-Modified-Since"))
|
||||||
|
{
|
||||||
DUMP("Last Modified Hit");
|
DUMP("Last Modified Hit");
|
||||||
TRACE();
|
TRACE();
|
||||||
_file.close();
|
_file.close();
|
||||||
request->reply(304); // Not modified
|
request->reply(304); // Not modified
|
||||||
}
|
}
|
||||||
//does our Etag match?
|
//does our Etag match?
|
||||||
else if (_cache_control.length() && request->hasHeader("If-None-Match") && request->header("If-None-Match").equals(etag)) {
|
else if (_cache_control.length() && request->hasHeader("If-None-Match") && request->header("If-None-Match").equals(etag))
|
||||||
|
{
|
||||||
DUMP("Etag Hit");
|
DUMP("Etag Hit");
|
||||||
DUMP(etag);
|
DUMP(etag);
|
||||||
DUMP(_cache_control);
|
DUMP(_cache_control);
|
||||||
@@ -171,7 +168,8 @@ esp_err_t PsychicStaticFileHandler::handleRequest(PsychicRequest * request) {
|
|||||||
response.send();
|
response.send();
|
||||||
}
|
}
|
||||||
//nope, send them the full file.
|
//nope, send them the full file.
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
DUMP("No cache hit");
|
DUMP("No cache hit");
|
||||||
DUMP(_last_modified);
|
DUMP(_last_modified);
|
||||||
DUMP(_cache_control);
|
DUMP(_cache_control);
|
||||||
|
|||||||
@@ -10,12 +10,10 @@
|
|||||||
class PsychicStaticFileHandler : public PsychicWebHandler {
|
class PsychicStaticFileHandler : public PsychicWebHandler {
|
||||||
using File = fs::File;
|
using File = fs::File;
|
||||||
using FS = fs::FS;
|
using FS = fs::FS;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _getFile(PsychicRequest *request);
|
bool _getFile(PsychicRequest *request);
|
||||||
bool _fileExists(const String& path);
|
bool _fileExists(const String& path);
|
||||||
uint8_t _countBits(const uint8_t value) const;
|
uint8_t _countBits(const uint8_t value) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
FS _fs;
|
FS _fs;
|
||||||
File _file;
|
File _file;
|
||||||
@@ -28,7 +26,6 @@ class PsychicStaticFileHandler : public PsychicWebHandler {
|
|||||||
bool _isDir;
|
bool _isDir;
|
||||||
bool _gzipFirst;
|
bool _gzipFirst;
|
||||||
uint8_t _gzipStats;
|
uint8_t _gzipStats;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PsychicStaticFileHandler(const char* uri, FS& fs, const char* path, const char* cache_control);
|
PsychicStaticFileHandler(const char* uri, FS& fs, const char* path, const char* cache_control);
|
||||||
bool canHandle(PsychicRequest *request) override;
|
bool canHandle(PsychicRequest *request) override;
|
||||||
|
|||||||
89
lib/PsychicHttp/src/PsychicStreamResponse.cpp
Normal file
89
lib/PsychicHttp/src/PsychicStreamResponse.cpp
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include "PsychicStreamResponse.h"
|
||||||
|
#include "PsychicResponse.h"
|
||||||
|
#include "PsychicRequest.h"
|
||||||
|
|
||||||
|
PsychicStreamResponse::PsychicStreamResponse(PsychicRequest *request, const String& contentType)
|
||||||
|
: PsychicResponse(request), _buffer(NULL) {
|
||||||
|
|
||||||
|
setContentType(contentType.c_str());
|
||||||
|
addHeader("Content-Disposition", "inline");
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicStreamResponse::PsychicStreamResponse(PsychicRequest *request, const String& contentType, const String& name)
|
||||||
|
: PsychicResponse(request), _buffer(NULL) {
|
||||||
|
|
||||||
|
setContentType(contentType.c_str());
|
||||||
|
|
||||||
|
char buf[26+name.length()];
|
||||||
|
snprintf(buf, sizeof (buf), "attachment; filename=\"%s\"", name);
|
||||||
|
addHeader("Content-Disposition", buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicStreamResponse::~PsychicStreamResponse()
|
||||||
|
{
|
||||||
|
endSend();
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t PsychicStreamResponse::beginSend()
|
||||||
|
{
|
||||||
|
if(_buffer)
|
||||||
|
return ESP_OK;
|
||||||
|
|
||||||
|
//Buffer to hold ChunkPrinter and stream buffer. Using placement new will keep us at a single allocation.
|
||||||
|
_buffer = (uint8_t*)malloc(STREAM_CHUNK_SIZE + sizeof(ChunkPrinter));
|
||||||
|
|
||||||
|
if(!_buffer)
|
||||||
|
{
|
||||||
|
/* Respond with 500 Internal Server Error */
|
||||||
|
httpd_resp_send_err(_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
_printer = new (_buffer) ChunkPrinter(this, _buffer + sizeof(ChunkPrinter), STREAM_CHUNK_SIZE);
|
||||||
|
|
||||||
|
sendHeaders();
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t PsychicStreamResponse::endSend()
|
||||||
|
{
|
||||||
|
esp_err_t err = ESP_OK;
|
||||||
|
|
||||||
|
if(!_buffer)
|
||||||
|
err = ESP_FAIL;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//flush & send remaining data.
|
||||||
|
_printer->flush();
|
||||||
|
err = finishChunking();
|
||||||
|
|
||||||
|
//Free memory
|
||||||
|
_printer->~ChunkPrinter();
|
||||||
|
free(_buffer);
|
||||||
|
_buffer = NULL;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PsychicStreamResponse::flush()
|
||||||
|
{
|
||||||
|
if(_buffer)
|
||||||
|
_printer->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t PsychicStreamResponse::write(uint8_t data)
|
||||||
|
{
|
||||||
|
return _buffer ? _printer->write(data) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t PsychicStreamResponse::copyFrom(Stream &stream)
|
||||||
|
{
|
||||||
|
size_t sentCount = 0;
|
||||||
|
|
||||||
|
if(_buffer)
|
||||||
|
{
|
||||||
|
while(stream.available())
|
||||||
|
sentCount += _printer->write(stream.read());
|
||||||
|
}
|
||||||
|
return sentCount;
|
||||||
|
}
|
||||||
33
lib/PsychicHttp/src/PsychicStreamResponse.h
Normal file
33
lib/PsychicHttp/src/PsychicStreamResponse.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef PsychicStreamResponse_h
|
||||||
|
#define PsychicStreamResponse_h
|
||||||
|
|
||||||
|
#include "PsychicCore.h"
|
||||||
|
#include "PsychicResponse.h"
|
||||||
|
#include "ChunkPrinter.h"
|
||||||
|
|
||||||
|
class PsychicRequest;
|
||||||
|
|
||||||
|
class PsychicStreamResponse : public PsychicResponse, public Print
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
ChunkPrinter *_printer;
|
||||||
|
uint8_t *_buffer;
|
||||||
|
public:
|
||||||
|
|
||||||
|
PsychicStreamResponse(PsychicRequest *request, const String& contentType);
|
||||||
|
PsychicStreamResponse(PsychicRequest *request, const String& contentType, const String& name); //Download
|
||||||
|
|
||||||
|
~PsychicStreamResponse();
|
||||||
|
|
||||||
|
esp_err_t beginSend();
|
||||||
|
esp_err_t endSend();
|
||||||
|
|
||||||
|
virtual void flush() override;
|
||||||
|
|
||||||
|
size_t write(uint8_t data);
|
||||||
|
size_t copyFrom(Stream &stream);
|
||||||
|
|
||||||
|
using Print::write;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PsychicStreamResponse_h
|
||||||
@@ -1,19 +1,27 @@
|
|||||||
#include "PsychicWebHandler.h"
|
#include "PsychicWebHandler.h"
|
||||||
|
|
||||||
PsychicWebHandler::PsychicWebHandler()
|
PsychicWebHandler::PsychicWebHandler() :
|
||||||
: PsychicHandler()
|
PsychicHandler(),
|
||||||
, _requestCallback(NULL) {
|
_requestCallback(NULL),
|
||||||
}
|
_onOpen(NULL),
|
||||||
PsychicWebHandler::~PsychicWebHandler() {
|
_onClose(NULL)
|
||||||
}
|
{}
|
||||||
|
PsychicWebHandler::~PsychicWebHandler() {}
|
||||||
|
|
||||||
bool PsychicWebHandler::canHandle(PsychicRequest *request) {
|
bool PsychicWebHandler::canHandle(PsychicRequest *request) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebHandler::handleRequest(PsychicRequest * request) {
|
esp_err_t PsychicWebHandler::handleRequest(PsychicRequest *request)
|
||||||
|
{
|
||||||
|
//lookup our client
|
||||||
|
PsychicClient *client = checkForNewClient(request->client());
|
||||||
|
if (client->isNew)
|
||||||
|
openCallback(client);
|
||||||
|
|
||||||
/* Request body cannot be larger than a limit */
|
/* Request body cannot be larger than a limit */
|
||||||
if (request->contentLength() > request->server()->maxRequestBodySize) {
|
if (request->contentLength() > request->server()->maxRequestBodySize)
|
||||||
|
{
|
||||||
ESP_LOGE(PH_TAG, "Request body too large : %d bytes", request->contentLength());
|
ESP_LOGE(PH_TAG, "Request body too large : %d bytes", request->contentLength());
|
||||||
|
|
||||||
/* Respond with 400 Bad Request */
|
/* Respond with 400 Bad Request */
|
||||||
@@ -44,3 +52,23 @@ PsychicWebHandler * PsychicWebHandler::onRequest(PsychicHttpRequestCallback fn)
|
|||||||
_requestCallback = fn;
|
_requestCallback = fn;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PsychicWebHandler::openCallback(PsychicClient *client) {
|
||||||
|
if (_onOpen != NULL)
|
||||||
|
_onOpen(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PsychicWebHandler::closeCallback(PsychicClient *client) {
|
||||||
|
if (_onClose != NULL)
|
||||||
|
_onClose(getClient(client));
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicWebHandler * PsychicWebHandler::onOpen(PsychicClientCallback fn) {
|
||||||
|
_onOpen = fn;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
PsychicWebHandler * PsychicWebHandler::onClose(PsychicClientCallback fn) {
|
||||||
|
_onClose = fn;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef PsychicWebHandler_h
|
#ifndef PsychicWebHandler_h
|
||||||
#define PsychicWebHandler_h
|
#define PsychicWebHandler_h
|
||||||
|
|
||||||
#include "PsychicCore.h"
|
// #include "PsychicCore.h"
|
||||||
#include "PsychicHttpServer.h"
|
// #include "PsychicHttpServer.h"
|
||||||
#include "PsychicRequest.h"
|
// #include "PsychicRequest.h"
|
||||||
#include "PsychicHandler.h"
|
#include "PsychicHandler.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -13,6 +13,8 @@
|
|||||||
class PsychicWebHandler : public PsychicHandler {
|
class PsychicWebHandler : public PsychicHandler {
|
||||||
protected:
|
protected:
|
||||||
PsychicHttpRequestCallback _requestCallback;
|
PsychicHttpRequestCallback _requestCallback;
|
||||||
|
PsychicClientCallback _onOpen;
|
||||||
|
PsychicClientCallback _onClose;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PsychicWebHandler();
|
PsychicWebHandler();
|
||||||
@@ -21,6 +23,12 @@ class PsychicWebHandler : public PsychicHandler {
|
|||||||
virtual bool canHandle(PsychicRequest *request) override;
|
virtual bool canHandle(PsychicRequest *request) override;
|
||||||
virtual esp_err_t handleRequest(PsychicRequest *request) override;
|
virtual esp_err_t handleRequest(PsychicRequest *request) override;
|
||||||
PsychicWebHandler * onRequest(PsychicHttpRequestCallback fn);
|
PsychicWebHandler * onRequest(PsychicHttpRequestCallback fn);
|
||||||
|
|
||||||
|
virtual void openCallback(PsychicClient *client);
|
||||||
|
virtual void closeCallback(PsychicClient *client);
|
||||||
|
|
||||||
|
PsychicWebHandler *onOpen(PsychicClientCallback fn);
|
||||||
|
PsychicWebHandler *onClose(PsychicClientCallback fn);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -14,28 +14,12 @@ class PsychicWebParameter {
|
|||||||
bool _isFile;
|
bool _isFile;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PsychicWebParameter(const String & name, const String & value, bool form = false, bool file = false, size_t size = 0)
|
PsychicWebParameter(const String& name, const String& value, bool form=false, bool file=false, size_t size=0): _name(name), _value(value), _size(size), _isForm(form), _isFile(file){}
|
||||||
: _name(name)
|
const String& name() const { return _name; }
|
||||||
, _value(value)
|
const String& value() const { return _value; }
|
||||||
, _size(size)
|
size_t size() const { return _size; }
|
||||||
, _isForm(form)
|
bool isPost() const { return _isForm; }
|
||||||
, _isFile(file) {
|
bool isFile() const { return _isFile; }
|
||||||
}
|
|
||||||
const String & name() const {
|
|
||||||
return _name;
|
|
||||||
}
|
|
||||||
const String & value() const {
|
|
||||||
return _value;
|
|
||||||
}
|
|
||||||
size_t size() const {
|
|
||||||
return _size;
|
|
||||||
}
|
|
||||||
bool isPost() const {
|
|
||||||
return _isForm;
|
|
||||||
}
|
|
||||||
bool isFile() const {
|
|
||||||
return _isFile;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PsychicWebParameter_h
|
#endif //PsychicWebParameter_h
|
||||||
@@ -4,23 +4,27 @@
|
|||||||
/* PsychicWebSocketRequest */
|
/* PsychicWebSocketRequest */
|
||||||
/*************************************/
|
/*************************************/
|
||||||
|
|
||||||
PsychicWebSocketRequest::PsychicWebSocketRequest(PsychicRequest * req)
|
PsychicWebSocketRequest::PsychicWebSocketRequest(PsychicRequest *req) :
|
||||||
: PsychicRequest(req->server(), req->request())
|
PsychicRequest(req->server(), req->request()),
|
||||||
, _client(req->client()) {
|
_client(req->client())
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicWebSocketRequest::~PsychicWebSocketRequest() {
|
PsychicWebSocketRequest::~PsychicWebSocketRequest()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicWebSocketClient * PsychicWebSocketRequest::client() {
|
PsychicWebSocketClient * PsychicWebSocketRequest::client() {
|
||||||
return &_client;
|
return &_client;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketRequest::reply(httpd_ws_frame_t * ws_pkt) {
|
esp_err_t PsychicWebSocketRequest::reply(httpd_ws_frame_t * ws_pkt)
|
||||||
|
{
|
||||||
return httpd_ws_send_frame(this->_req, ws_pkt);
|
return httpd_ws_send_frame(this->_req, ws_pkt);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketRequest::reply(httpd_ws_type_t op, const void * data, size_t len) {
|
esp_err_t PsychicWebSocketRequest::reply(httpd_ws_type_t op, const void *data, size_t len)
|
||||||
|
{
|
||||||
httpd_ws_frame_t ws_pkt;
|
httpd_ws_frame_t ws_pkt;
|
||||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||||
|
|
||||||
@@ -31,7 +35,8 @@ esp_err_t PsychicWebSocketRequest::reply(httpd_ws_type_t op, const void * data,
|
|||||||
return this->reply(&ws_pkt);
|
return this->reply(&ws_pkt);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketRequest::reply(const char * buf) {
|
esp_err_t PsychicWebSocketRequest::reply(const char *buf)
|
||||||
|
{
|
||||||
return this->reply(HTTPD_WS_TYPE_TEXT, buf, strlen(buf));
|
return this->reply(HTTPD_WS_TYPE_TEXT, buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,17 +45,20 @@ esp_err_t PsychicWebSocketRequest::reply(const char * buf) {
|
|||||||
/*************************************/
|
/*************************************/
|
||||||
|
|
||||||
PsychicWebSocketClient::PsychicWebSocketClient(PsychicClient *client)
|
PsychicWebSocketClient::PsychicWebSocketClient(PsychicClient *client)
|
||||||
: PsychicClient(client->server(), client->socket()) {
|
: PsychicClient(client->server(), client->socket())
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicWebSocketClient::~PsychicWebSocketClient() {
|
PsychicWebSocketClient::~PsychicWebSocketClient() {
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_frame_t * ws_pkt) {
|
esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_frame_t * ws_pkt)
|
||||||
|
{
|
||||||
return httpd_ws_send_frame_async(this->server(), this->socket(), ws_pkt);
|
return httpd_ws_send_frame_async(this->server(), this->socket(), ws_pkt);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_type_t op, const void * data, size_t len) {
|
esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_type_t op, const void *data, size_t len)
|
||||||
|
{
|
||||||
httpd_ws_frame_t ws_pkt;
|
httpd_ws_frame_t ws_pkt;
|
||||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||||
|
|
||||||
@@ -61,26 +69,30 @@ esp_err_t PsychicWebSocketClient::sendMessage(httpd_ws_type_t op, const void * d
|
|||||||
return this->sendMessage(&ws_pkt);
|
return this->sendMessage(&ws_pkt);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketClient::sendMessage(const char * buf) {
|
esp_err_t PsychicWebSocketClient::sendMessage(const char *buf)
|
||||||
|
{
|
||||||
return this->sendMessage(HTTPD_WS_TYPE_TEXT, buf, strlen(buf));
|
return this->sendMessage(HTTPD_WS_TYPE_TEXT, buf, strlen(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicWebSocketHandler::PsychicWebSocketHandler()
|
PsychicWebSocketHandler::PsychicWebSocketHandler() :
|
||||||
: PsychicHandler()
|
PsychicHandler(),
|
||||||
, _onOpen(NULL)
|
_onOpen(NULL),
|
||||||
, _onFrame(NULL)
|
_onFrame(NULL),
|
||||||
, _onClose(NULL) {
|
_onClose(NULL)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicWebSocketHandler::~PsychicWebSocketHandler() {
|
PsychicWebSocketHandler::~PsychicWebSocketHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
PsychicWebSocketClient * PsychicWebSocketHandler::getClient(int socket) {
|
PsychicWebSocketClient * PsychicWebSocketHandler::getClient(int socket)
|
||||||
|
{
|
||||||
PsychicClient *client = PsychicHandler::getClient(socket);
|
PsychicClient *client = PsychicHandler::getClient(socket);
|
||||||
if (client == NULL)
|
if (client == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (client->_friend == NULL) {
|
if (client->_friend == NULL)
|
||||||
|
{
|
||||||
DUMP(socket);
|
DUMP(socket);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -105,7 +117,8 @@ void PsychicWebSocketHandler::removeClient(PsychicClient * client) {
|
|||||||
|
|
||||||
void PsychicWebSocketHandler::openCallback(PsychicClient *client) {
|
void PsychicWebSocketHandler::openCallback(PsychicClient *client) {
|
||||||
PsychicWebSocketClient *buddy = getClient(client);
|
PsychicWebSocketClient *buddy = getClient(client);
|
||||||
if (buddy == NULL) {
|
if (buddy == NULL)
|
||||||
|
{
|
||||||
TRACE();
|
TRACE();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -116,7 +129,8 @@ void PsychicWebSocketHandler::openCallback(PsychicClient * client) {
|
|||||||
|
|
||||||
void PsychicWebSocketHandler::closeCallback(PsychicClient *client) {
|
void PsychicWebSocketHandler::closeCallback(PsychicClient *client) {
|
||||||
PsychicWebSocketClient *buddy = getClient(client);
|
PsychicWebSocketClient *buddy = getClient(client);
|
||||||
if (buddy == NULL) {
|
if (buddy == NULL)
|
||||||
|
{
|
||||||
TRACE();
|
TRACE();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -125,16 +139,16 @@ void PsychicWebSocketHandler::closeCallback(PsychicClient * client) {
|
|||||||
_onClose(getClient(buddy));
|
_onClose(getClient(buddy));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PsychicWebSocketHandler::isWebSocket() {
|
bool PsychicWebSocketHandler::isWebSocket() { return true; }
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest * request) {
|
esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest *request)
|
||||||
|
{
|
||||||
//lookup our client
|
//lookup our client
|
||||||
PsychicClient *client = checkForNewClient(request->client());
|
PsychicClient *client = checkForNewClient(request->client());
|
||||||
|
|
||||||
// beginning of the ws URI handler and our onConnect hook
|
// beginning of the ws URI handler and our onConnect hook
|
||||||
if (request->method() == HTTP_GET) {
|
if (request->method() == HTTP_GET)
|
||||||
|
{
|
||||||
if (client->isNew)
|
if (client->isNew)
|
||||||
openCallback(client);
|
openCallback(client);
|
||||||
|
|
||||||
@@ -178,7 +192,8 @@ esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest * request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Text messages are our payload.
|
// Text messages are our payload.
|
||||||
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT) {
|
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT)
|
||||||
|
{
|
||||||
if (this->_onFrame != NULL)
|
if (this->_onFrame != NULL)
|
||||||
ret = this->_onFrame(&wsRequest, &ws_pkt);
|
ret = this->_onFrame(&wsRequest, &ws_pkt);
|
||||||
}
|
}
|
||||||
@@ -186,11 +201,8 @@ esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest * request) {
|
|||||||
//logging housekeeping
|
//logging housekeeping
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
ESP_LOGE(PH_TAG, "httpd_ws_send_frame failed with %s", esp_err_to_name(ret));
|
ESP_LOGE(PH_TAG, "httpd_ws_send_frame failed with %s", esp_err_to_name(ret));
|
||||||
ESP_LOGI(PH_TAG,
|
ESP_LOGI(PH_TAG, "ws_handler: httpd_handle_t=%p, sockfd=%d, client_info:%d", request->server(),
|
||||||
"ws_handler: httpd_handle_t=%p, sockfd=%d, client_info:%d",
|
httpd_req_to_sockfd(request->request()), httpd_ws_get_fd_info(request->server(), httpd_req_to_sockfd(request->request())));
|
||||||
request->server(),
|
|
||||||
httpd_req_to_sockfd(request->request()),
|
|
||||||
httpd_ws_get_fd_info(request->server(), httpd_req_to_sockfd(request->request())));
|
|
||||||
|
|
||||||
//dont forget to release our buffer memory
|
//dont forget to release our buffer memory
|
||||||
free(buf);
|
free(buf);
|
||||||
@@ -213,11 +225,14 @@ PsychicWebSocketHandler * PsychicWebSocketHandler::onClose(PsychicWebSocketClien
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicWebSocketHandler::sendAll(httpd_ws_frame_t * ws_pkt) {
|
void PsychicWebSocketHandler::sendAll(httpd_ws_frame_t * ws_pkt)
|
||||||
for (PsychicClient * client : _clients) {
|
{
|
||||||
|
for (PsychicClient *client : _clients)
|
||||||
|
{
|
||||||
ESP_LOGI(PH_TAG, "Active client (fd=%d) -> sending async message", client->socket());
|
ESP_LOGI(PH_TAG, "Active client (fd=%d) -> sending async message", client->socket());
|
||||||
|
|
||||||
if (client->_friend == NULL) {
|
if (client->_friend == NULL)
|
||||||
|
{
|
||||||
TRACE();
|
TRACE();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -227,7 +242,8 @@ void PsychicWebSocketHandler::sendAll(httpd_ws_frame_t * ws_pkt) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicWebSocketHandler::sendAll(httpd_ws_type_t op, const void * data, size_t len) {
|
void PsychicWebSocketHandler::sendAll(httpd_ws_type_t op, const void *data, size_t len)
|
||||||
|
{
|
||||||
httpd_ws_frame_t ws_pkt;
|
httpd_ws_frame_t ws_pkt;
|
||||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||||
|
|
||||||
@@ -238,6 +254,7 @@ void PsychicWebSocketHandler::sendAll(httpd_ws_type_t op, const void * data, siz
|
|||||||
this->sendAll(&ws_pkt);
|
this->sendAll(&ws_pkt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PsychicWebSocketHandler::sendAll(const char * buf) {
|
void PsychicWebSocketHandler::sendAll(const char *buf)
|
||||||
|
{
|
||||||
this->sendAll(HTTPD_WS_TYPE_TEXT, buf, strlen(buf));
|
this->sendAll(HTTPD_WS_TYPE_TEXT, buf, strlen(buf));
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,8 @@ class PsychicWebSocketClient;
|
|||||||
typedef std::function<void(PsychicWebSocketClient *client)> PsychicWebSocketClientCallback;
|
typedef std::function<void(PsychicWebSocketClient *client)> PsychicWebSocketClientCallback;
|
||||||
typedef std::function<esp_err_t(PsychicWebSocketRequest *request, httpd_ws_frame *frame)> PsychicWebSocketFrameCallback;
|
typedef std::function<esp_err_t(PsychicWebSocketRequest *request, httpd_ws_frame *frame)> PsychicWebSocketFrameCallback;
|
||||||
|
|
||||||
class PsychicWebSocketClient : public PsychicClient {
|
class PsychicWebSocketClient : public PsychicClient
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
PsychicWebSocketClient(PsychicClient *client);
|
PsychicWebSocketClient(PsychicClient *client);
|
||||||
~PsychicWebSocketClient();
|
~PsychicWebSocketClient();
|
||||||
@@ -21,7 +22,8 @@ class PsychicWebSocketClient : public PsychicClient {
|
|||||||
esp_err_t sendMessage(const char *buf);
|
esp_err_t sendMessage(const char *buf);
|
||||||
};
|
};
|
||||||
|
|
||||||
class PsychicWebSocketRequest : public PsychicRequest {
|
class PsychicWebSocketRequest : public PsychicRequest
|
||||||
|
{
|
||||||
private:
|
private:
|
||||||
PsychicWebSocketClient _client;
|
PsychicWebSocketClient _client;
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +1,37 @@
|
|||||||
#include "http_status.h"
|
#include "http_status.h"
|
||||||
|
|
||||||
bool http_informational(int code) {
|
bool http_informational(int code)
|
||||||
|
{
|
||||||
return code >= 100 && code < 200;
|
return code >= 100 && code < 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool http_success(int code) {
|
bool http_success(int code)
|
||||||
|
{
|
||||||
return code >= 200 && code < 300;
|
return code >= 200 && code < 300;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool http_redirection(int code) {
|
bool http_redirection(int code)
|
||||||
|
{
|
||||||
return code >= 300 && code < 400;
|
return code >= 300 && code < 400;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool http_client_error(int code) {
|
bool http_client_error(int code)
|
||||||
|
{
|
||||||
return code >= 400 && code < 500;
|
return code >= 400 && code < 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool http_server_error(int code) {
|
bool http_server_error(int code)
|
||||||
|
{
|
||||||
return code >= 500 && code < 600;
|
return code >= 500 && code < 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool http_failure(int code) {
|
bool http_failure(int code)
|
||||||
|
{
|
||||||
return code >= 400 && code < 600;
|
return code >= 400 && code < 600;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * http_status_group(int code) {
|
const char *http_status_group(int code)
|
||||||
|
{
|
||||||
if (http_informational(code))
|
if (http_informational(code))
|
||||||
return "Informational";
|
return "Informational";
|
||||||
|
|
||||||
@@ -43,8 +50,10 @@ const char * http_status_group(int code) {
|
|||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * http_status_reason(int code) {
|
const char *http_status_reason(int code)
|
||||||
switch (code) {
|
{
|
||||||
|
switch (code)
|
||||||
|
{
|
||||||
/*####### 1xx - Informational #######*/
|
/*####### 1xx - Informational #######*/
|
||||||
case 100:
|
case 100:
|
||||||
return "Continue";
|
return "Continue";
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ void ESP8266React::registerURI() {
|
|||||||
_mqttStatus.registerURI();
|
_mqttStatus.registerURI();
|
||||||
|
|
||||||
_securitySettingsService.registerURI();
|
_securitySettingsService.registerURI();
|
||||||
|
|
||||||
_otaSettingsService.registerURI();
|
_otaSettingsService.registerURI();
|
||||||
|
|
||||||
_restartService.registerURI();
|
_restartService.registerURI();
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class HttpEndpoint {
|
|||||||
|
|
||||||
// register the web server on() endpoints
|
// register the web server on() endpoints
|
||||||
void registerURI() {
|
void registerURI() {
|
||||||
|
// GET
|
||||||
_server->on(_servicePath,
|
_server->on(_servicePath,
|
||||||
HTTP_GET,
|
HTTP_GET,
|
||||||
_securityManager->wrapRequest(
|
_securityManager->wrapRequest(
|
||||||
@@ -57,6 +58,7 @@ class HttpEndpoint {
|
|||||||
},
|
},
|
||||||
_authenticationPredicate));
|
_authenticationPredicate));
|
||||||
|
|
||||||
|
// POST
|
||||||
_server->on(_servicePath,
|
_server->on(_servicePath,
|
||||||
HTTP_POST,
|
HTTP_POST,
|
||||||
_securityManager->wrapCallback(
|
_securityManager->wrapCallback(
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ void NetworkSettingsService::begin() {
|
|||||||
// WiFi.mode(WIFI_OFF);
|
// WiFi.mode(WIFI_OFF);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_MODE_STA); // TODO added otherwise crashes. this is the default.
|
||||||
|
|
||||||
// WiFi.useStaticBuffers(true); // uses 40kb more heap and max alloc, so not recommended
|
// WiFi.useStaticBuffers(true); // uses 40kb more heap and max alloc, so not recommended
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.setAutoReconnect(false);
|
WiFi.setAutoReconnect(false);
|
||||||
|
|
||||||
WiFi.mode(WIFI_MODE_MAX);
|
// WiFi.mode(WIFI_MODE_MAX); // TODO commented out, not sure what use it has
|
||||||
WiFi.mode(WIFI_MODE_NULL);
|
// WiFi.mode(WIFI_MODE_NULL);
|
||||||
|
|
||||||
// WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // default is FAST_SCAN, connect issues in 2.0.14
|
// WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN); // default is FAST_SCAN, connect issues in 2.0.14
|
||||||
// WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // is default, no need to set
|
// WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL); // is default, no need to set
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
/*
|
||||||
#ifndef WebSocketTxRx_h
|
#ifndef WebSocketTxRx_h
|
||||||
#define WebSocketTxRx_h
|
#define WebSocketTxRx_h
|
||||||
|
|
||||||
@@ -107,13 +108,12 @@ class WebSocketTx : virtual public WebSocketConnector<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Broadcasts the payload to the destination, if provided. Otherwise broadcasts to all clients except the origin, if
|
||||||
* Broadcasts the payload to the destination, if provided. Otherwise broadcasts to all clients except the origin, if
|
// specified.
|
||||||
* specified.
|
//
|
||||||
*
|
// Original implementation sent clients their own IDs so they could ignore updates they initiated. This approach
|
||||||
* Original implementation sent clients their own IDs so they could ignore updates they initiated. This approach
|
// simplifies the client and the server implementation but may not be sufficent for all use-cases.
|
||||||
* simplifies the client and the server implementation but may not be sufficent for all use-cases.
|
//
|
||||||
*/
|
|
||||||
void transmitData(AsyncWebSocketClient * client, const String & originId) {
|
void transmitData(AsyncWebSocketClient * client, const String & originId) {
|
||||||
DynamicJsonDocument jsonDocument = DynamicJsonDocument(WebSocketConnector<T>::_bufferSize);
|
DynamicJsonDocument jsonDocument = DynamicJsonDocument(WebSocketConnector<T>::_bufferSize);
|
||||||
JsonObject root = jsonDocument.to<JsonObject>();
|
JsonObject root = jsonDocument.to<JsonObject>();
|
||||||
@@ -214,3 +214,4 @@ class WebSocketTxRx : public WebSocketTx<T>, public WebSocketRx<T> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
*/
|
||||||
110
platformio.ini
110
platformio.ini
@@ -1,13 +1,15 @@
|
|||||||
; PlatformIO Project Configuration File for EMS-ESP
|
; PlatformIO Project Configuration File
|
||||||
; override any settings with your own local ones in pio_local.ini
|
;
|
||||||
|
; Build options: build flags, source filter
|
||||||
|
; Upload options: custom upload port, speed and extra flags
|
||||||
|
; Library options: dependencies, extra library storages
|
||||||
|
; Advanced options: extra scripting
|
||||||
|
;
|
||||||
|
; Please visit documentation for the other options and examples
|
||||||
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
; default_envs = esp32_4M
|
|
||||||
default_envs = lolin_s3
|
default_envs = lolin_s3
|
||||||
; default_envs = esp32_16M
|
|
||||||
; default_envs = standalone
|
|
||||||
; default_envs = https
|
|
||||||
|
|
||||||
extra_configs =
|
extra_configs =
|
||||||
factory_settings.ini
|
factory_settings.ini
|
||||||
pio_local.ini
|
pio_local.ini
|
||||||
@@ -16,14 +18,8 @@ extra_configs =
|
|||||||
core_build_flags =
|
core_build_flags =
|
||||||
-D ARDUINO_ARCH_ESP32=1
|
-D ARDUINO_ARCH_ESP32=1
|
||||||
-D ESP32=1
|
-D ESP32=1
|
||||||
; -std=gnu++17
|
|
||||||
|
|
||||||
; core_unbuild_flags = -std=gnu++11
|
|
||||||
core_unbuild_flags =
|
core_unbuild_flags =
|
||||||
|
|
||||||
; my_build_flags is set in pio_local.ini
|
|
||||||
my_build_flags =
|
my_build_flags =
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
${common.core_build_flags}
|
${common.core_build_flags}
|
||||||
${factory_settings.build_flags}
|
${factory_settings.build_flags}
|
||||||
@@ -33,13 +29,13 @@ build_flags =
|
|||||||
-D ARDUINOJSON_ENABLE_STD_STRING=1
|
-D ARDUINOJSON_ENABLE_STD_STRING=1
|
||||||
-D ARDUINOJSON_USE_DOUBLE=0
|
-D ARDUINOJSON_USE_DOUBLE=0
|
||||||
-D ARDUINOTRACE_ENABLE=0
|
-D ARDUINOTRACE_ENABLE=0
|
||||||
; -D CONFIG_UART_ISR_IN_IRAM
|
-D CONFIG_ETH_ENABLED
|
||||||
|
-D ARDUINOTRACE_ENABLE=0
|
||||||
unbuild_flags =
|
unbuild_flags =
|
||||||
${common.core_unbuild_flags}
|
${common.core_unbuild_flags}
|
||||||
|
|
||||||
[espressi32_base]
|
[espressi32_base]
|
||||||
platform = espressif32@6.5.0
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
build_flags = ${common.build_flags}
|
build_flags = ${common.build_flags}
|
||||||
@@ -49,14 +45,7 @@ extra_scripts =
|
|||||||
scripts/rename_fw.py
|
scripts/rename_fw.py
|
||||||
|
|
||||||
[espressi32_base_tasmota]
|
[espressi32_base_tasmota]
|
||||||
; use Tasmota's libary which removes some libs (like mbedtsl) and increases available heap
|
platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.03/platform-espressif32-2023.10.03.zip
|
||||||
; platform = https://github.com/tasmota/platform-espressif32.git ; latest development
|
|
||||||
; latest release with WiFi_secure.h
|
|
||||||
platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.03/platform-espressif32-2023.10.03.zip ; latest stable
|
|
||||||
; latest arduino 2.xx release:
|
|
||||||
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.11.01/platform-espressif32.zip
|
|
||||||
; latest arduino 3.0/IDF 5.1.(alpha 3):
|
|
||||||
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.12.10/platform-espressif32.zip
|
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
build_flags =
|
build_flags =
|
||||||
@@ -73,16 +62,12 @@ monitor_filters = esp32_exception_decoder
|
|||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
build_type = release
|
build_type = release
|
||||||
lib_ldf_mode = chain+
|
lib_ldf_mode = chain+
|
||||||
; board_build.flash_mode = qio
|
|
||||||
|
|
||||||
check_tool = cppcheck, clangtidy
|
check_tool = cppcheck, clangtidy
|
||||||
check_severity = high, medium
|
check_severity = high, medium
|
||||||
check_flags =
|
check_flags =
|
||||||
cppcheck: --std=c++11 -v
|
cppcheck: --std=c++11 -v
|
||||||
clangtidy: --checks=-*,clang-analyzer-*,performance-*
|
clangtidy: --checks=-*,clang-analyzer-*,performance-*
|
||||||
|
|
||||||
; build for GitHub Actions CI
|
|
||||||
; the Web interface is built seperately
|
|
||||||
[env:ci]
|
[env:ci]
|
||||||
extends = espressi32_base_tasmota
|
extends = espressi32_base_tasmota
|
||||||
extra_scripts = scripts/rename_fw.py
|
extra_scripts = scripts/rename_fw.py
|
||||||
@@ -134,8 +119,6 @@ build_flags =
|
|||||||
${espressi32_base_tasmota.build_flags}
|
${espressi32_base_tasmota.build_flags}
|
||||||
'-DEMSESP_DEFAULT_BOARD_PROFILE="C3MINI"'
|
'-DEMSESP_DEFAULT_BOARD_PROFILE="C3MINI"'
|
||||||
|
|
||||||
; lolin C3 mini v1 needs special wifi init.
|
|
||||||
; https://www.wemos.cc/en/latest/c3/c3_mini_1_0_0.html#about-wifi
|
|
||||||
[env:lolin_c3_mini_v1]
|
[env:lolin_c3_mini_v1]
|
||||||
extends = espressi32_base_tasmota
|
extends = espressi32_base_tasmota
|
||||||
board = lolin_c3_mini
|
board = lolin_c3_mini
|
||||||
@@ -169,33 +152,18 @@ build_flags =
|
|||||||
'-DEMSESP_DEFAULT_BOARD_PROFILE="S32S3"'
|
'-DEMSESP_DEFAULT_BOARD_PROFILE="S32S3"'
|
||||||
|
|
||||||
[env:https]
|
[env:https]
|
||||||
; use Tasmota's libary which removes some libs (like mbedtsl) and increases available heap
|
platform = espressif32
|
||||||
; platform = https://github.com/tasmota/platform-espressif32.git ; latest development
|
|
||||||
;
|
|
||||||
; latest release with WiFi_secure.h
|
|
||||||
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.10.03/platform-espressif32-2023.10.03.zip ; latest stable
|
|
||||||
;
|
|
||||||
; latest arduino 2.xx release:
|
|
||||||
; platform = https://github.com/tasmota/platform-espressif32/releases/download/2023.11.01/platform-espressif32.zip
|
|
||||||
;
|
|
||||||
; latest arduino 3.0/IDF 5.1.(alpha 3):
|
|
||||||
; platform = https://github.com/platformio/platform-espressif32.git
|
|
||||||
; platform = https://github.com/Jason2866/platform-espressif32.git#Arduino/IDF5
|
|
||||||
; platform_packages = https://github.com/espressif/arduino-esp32.git#3.0.0-alpha2
|
|
||||||
platform = espressif32@6.5.0
|
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
board_build.f_cpu = 240000000L
|
board_build.f_cpu = 240000000L
|
||||||
board_upload.flash_size = 4MB
|
board_upload.flash_size = 4MB
|
||||||
board_build.partitions = esp32_partition_4M.csv
|
board_build.partitions = esp32_partition_4M.csv
|
||||||
; board_upload.flash_size = 16MB
|
|
||||||
; board_build.partitions = esp32_partition_16M.csv
|
|
||||||
board_upload.use_1200bps_touch = false
|
board_upload.use_1200bps_touch = false
|
||||||
board_upload.wait_for_upload_port = true
|
board_upload.wait_for_upload_port = true
|
||||||
upload_port = /dev/ttyUSB0
|
upload_port = /dev/ttyUSB0
|
||||||
extra_scripts =
|
extra_scripts =
|
||||||
; pre:scripts/build_interface.py
|
pre:scripts/build_interface.py
|
||||||
scripts/rename_fw.py
|
scripts/rename_fw.py
|
||||||
build_unflags = ${common.unbuild_flags}
|
build_unflags = ${common.unbuild_flags}
|
||||||
build_flags =
|
build_flags =
|
||||||
@@ -206,17 +174,14 @@ build_flags =
|
|||||||
-D NO_GLOBAL_ARDUINOOTA
|
-D NO_GLOBAL_ARDUINOOTA
|
||||||
-D ARDUINOJSON_ENABLE_STD_STRING=1
|
-D ARDUINOJSON_ENABLE_STD_STRING=1
|
||||||
-D ARDUINOJSON_USE_DOUBLE=0
|
-D ARDUINOJSON_USE_DOUBLE=0
|
||||||
; -D ARDUINOTRACE_ENABLE=1
|
-D ARDUINOTRACE_ENABLE=1
|
||||||
-D ARDUINOTRACE_ENABLE=0
|
|
||||||
-D TASMOTA_SDK
|
-D TASMOTA_SDK
|
||||||
; -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_WARN
|
-D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_WARN
|
||||||
-D EMSESP_TEST
|
-D EMSESP_TEST
|
||||||
-D EMSESP_DEBUG
|
-D EMSESP_DEBUG
|
||||||
-D CONFIG_ETH_ENABLED
|
-D CONFIG_ETH_ENABLED
|
||||||
; -D BOARD_HAS_PSRAM
|
|
||||||
'-DEMSESP_DEFAULT_BOARD_PROFILE="Test"'
|
'-DEMSESP_DEFAULT_BOARD_PROFILE="Test"'
|
||||||
|
|
||||||
; to build and run: pio run -e standalone -t exec
|
|
||||||
[env:standalone]
|
[env:standalone]
|
||||||
platform = native
|
platform = native
|
||||||
build_flags =
|
build_flags =
|
||||||
@@ -226,8 +191,6 @@ build_flags =
|
|||||||
-lpthread
|
-lpthread
|
||||||
-std=gnu++11 -Og -ggdb
|
-std=gnu++11 -Og -ggdb
|
||||||
build_src_flags =
|
build_src_flags =
|
||||||
; -Wall -Wextra -Werror -Wswitch-enum -Wno-unused-parameter -Wno-inconsistent-missing-override -Wno-unused-lambda-capture -Wno-sign-compare
|
|
||||||
; -Wall -Wextra -Werror
|
|
||||||
-Wno-missing-braces
|
-Wno-missing-braces
|
||||||
-I./lib_standalone
|
-I./lib_standalone
|
||||||
-I./lib/ArduinoJson/src
|
-I./lib/ArduinoJson/src
|
||||||
@@ -251,3 +214,42 @@ build_src_filter =
|
|||||||
+<../lib/espMqttClient/src/Transport>
|
+<../lib/espMqttClient/src/Transport>
|
||||||
lib_compat_mode = off
|
lib_compat_mode = off
|
||||||
lib_ldf_mode = off
|
lib_ldf_mode = off
|
||||||
|
|
||||||
|
[factory_settings]
|
||||||
|
build_flags =
|
||||||
|
-D FACTORY_WIFI_SSID=\"\"
|
||||||
|
-D FACTORY_WIFI_PASSWORD=\"\"
|
||||||
|
-D FACTORY_WIFI_HOSTNAME=\"ems-esp\"
|
||||||
|
|
||||||
|
-D FACTORY_AP_PROVISION_MODE=AP_MODE_DISCONNECTED
|
||||||
|
-D FACTORY_AP_SSID=\"ems-esp\"
|
||||||
|
-D FACTORY_AP_PASSWORD=\"ems-esp-neo\"
|
||||||
|
-D FACTORY_AP_LOCAL_IP=\"192.168.4.1\"
|
||||||
|
-D FACTORY_AP_GATEWAY_IP=\"192.168.4.1\"
|
||||||
|
-D FACTORY_AP_SUBNET_MASK=\"255.255.255.0\"
|
||||||
|
|
||||||
|
-D FACTORY_ADMIN_USERNAME=\"admin\"
|
||||||
|
-D FACTORY_ADMIN_PASSWORD=\"admin\"
|
||||||
|
-D FACTORY_GUEST_USERNAME=\"guest\"
|
||||||
|
-D FACTORY_GUEST_PASSWORD=\"guest\"
|
||||||
|
|
||||||
|
-D FACTORY_NTP_ENABLED=false
|
||||||
|
-D FACTORY_NTP_TIME_ZONE_LABEL=\"Europe/Amsterdam\"
|
||||||
|
-D FACTORY_NTP_TIME_ZONE_FORMAT=\"CET-1CEST,M3.5.0,M10.5.0/3\"
|
||||||
|
-D FACTORY_NTP_SERVER=\"time.google.com\"
|
||||||
|
|
||||||
|
-D FACTORY_OTA_PORT=8266
|
||||||
|
-D FACTORY_OTA_PASSWORD=\"ems-esp-neo\"
|
||||||
|
-D FACTORY_OTA_ENABLED=false
|
||||||
|
|
||||||
|
-D FACTORY_MQTT_ENABLED=false
|
||||||
|
-D FACTORY_MQTT_HOST=\"\"
|
||||||
|
-D FACTORY_MQTT_PORT=1883
|
||||||
|
-D FACTORY_MQTT_USERNAME=\"\"
|
||||||
|
-D FACTORY_MQTT_PASSWORD=\"\"
|
||||||
|
-D FACTORY_MQTT_CLIENT_ID=\"ems-esp\"
|
||||||
|
-D FACTORY_MQTT_KEEP_ALIVE=60
|
||||||
|
-D FACTORY_MQTT_CLEAN_SESSION=false
|
||||||
|
-D FACTORY_MQTT_MAX_TOPIC_LENGTH=128
|
||||||
|
|
||||||
|
-D FACTORY_JWT_SECRET=\"ems-esp-neo\"
|
||||||
|
|||||||
@@ -1405,24 +1405,26 @@ EMSESP::EMSESP()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add web server endpoint
|
// add web server endpoint
|
||||||
void EMSESP::handler(const char * uri, const char * contentType, const uint8_t * content, size_t len) {
|
void EMSESP::handler(const String & uri, const String & contentType, const uint8_t * content, size_t len) {
|
||||||
PsychicHttpRequestCallback fn = [contentType, content, len](PsychicRequest * request) {
|
PsychicHttpRequestCallback fn = [contentType, content, len](PsychicRequest * request) {
|
||||||
PsychicResponse response(request);
|
PsychicResponse response(request);
|
||||||
response.setCode(200);
|
response.setCode(200);
|
||||||
response.setContentType(contentType);
|
response.setContentType(contentType.c_str());
|
||||||
response.addHeader("Content-Encoding", "gzip");
|
|
||||||
// response.addHeader("Content-Encoding", "br"); // Brotli - only works over HTTPS
|
// response.addHeader("Content-Encoding", "br"); // Brotli - only works over HTTPS
|
||||||
|
response.addHeader("Content-Encoding", "gzip");
|
||||||
|
response.addHeader("Cache-Control", "public, immutable, max-age=31536000");
|
||||||
response.setContent(content, len);
|
response.setContent(content, len);
|
||||||
return response.send();
|
return response.send();
|
||||||
};
|
};
|
||||||
|
|
||||||
PsychicWebHandler * handler = new PsychicWebHandler();
|
PsychicWebHandler * handler = new PsychicWebHandler();
|
||||||
handler->onRequest(fn);
|
handler->onRequest(fn);
|
||||||
webServer.on(uri, HTTP_GET, handler);
|
webServer.on(uri.c_str(), HTTP_GET, handler);
|
||||||
|
|
||||||
// Set default end-point for all non matching requests
|
// Set default end-point for all non matching requests
|
||||||
// this is easier than using webServer.onNotFound()
|
// this is easier than using webServer.onNotFound()
|
||||||
if (strcmp(uri, "/index.html") == 0) {
|
if (uri.equals("/index.html")) {
|
||||||
|
// if (strcmp(uri, "/index.html") == 0) {
|
||||||
webServer.defaultEndpoint->setHandler(handler);
|
webServer.defaultEndpoint->setHandler(handler);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1435,14 +1437,43 @@ void EMSESP::setupWeb() {
|
|||||||
// esp8266React services has 13
|
// esp8266React services has 13
|
||||||
// custom projects has around 23
|
// custom projects has around 23
|
||||||
webServer.config.max_uri_handlers = 80;
|
webServer.config.max_uri_handlers = 80;
|
||||||
// webServer.config.uri_match_fn = NULL; // don't use wildcards
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
#define HTTPD_DEFAULT_CONFIG() { \
|
||||||
|
.task_priority = tskIDLE_PRIORITY+5, \
|
||||||
|
.stack_size = 4096, \
|
||||||
|
.core_id = tskNO_AFFINITY, \
|
||||||
|
.server_port = 80, \
|
||||||
|
.ctrl_port = 32768, \
|
||||||
|
.max_open_sockets = 7, \
|
||||||
|
.max_uri_handlers = 8, \
|
||||||
|
.max_resp_headers = 8, \
|
||||||
|
.backlog_conn = 5, \
|
||||||
|
.lru_purge_enable = false, \
|
||||||
|
.recv_wait_timeout = 5, \
|
||||||
|
.send_wait_timeout = 5, \
|
||||||
|
.global_user_ctx = NULL, \
|
||||||
|
.global_user_ctx_free_fn = NULL, \
|
||||||
|
.global_transport_ctx = NULL, \
|
||||||
|
.global_transport_ctx_free_fn = NULL, \
|
||||||
|
.enable_so_linger = false, \
|
||||||
|
.linger_timeout = 0, \
|
||||||
|
.open_fn = NULL, \
|
||||||
|
.close_fn = NULL, \
|
||||||
|
.uri_match_fn = NULL \
|
||||||
|
*/
|
||||||
|
|
||||||
|
// TODO remove experimental stuff
|
||||||
|
// webServer.config.uri_match_fn = NULL; // don't use wildcards
|
||||||
|
// webServer.config.stack_size = 8192; // default is 4096, we had 2x8192 in AsyncTCP
|
||||||
|
// webServer.config.max_open_sockets = 3;
|
||||||
// webServer.config.task_priority = uxTaskPriorityGet(nullptr); // seems to make it slightly slower
|
// webServer.config.task_priority = uxTaskPriorityGet(nullptr); // seems to make it slightly slower
|
||||||
|
// webServer.config.task_priority = 5; // same as AsyncTCP, but looks like even slower
|
||||||
|
// webServer.config.lru_purge_enable = true; // makes no diff. is as in https://github.com/espressif/esp-idf/blob/master/examples/protocols/http_server/simple/main/main.c
|
||||||
|
|
||||||
// TODO add support for https
|
// TODO add support for https
|
||||||
webServer.listen(80); // start the web server
|
webServer.listen(80); // start the web server
|
||||||
|
|
||||||
DefaultHeaders::Instance().addHeader("Server", "EMS-ESP");
|
|
||||||
|
|
||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
WWWData::registerRoutes(handler); // add webServer.on() endpoints from the generated web code
|
WWWData::registerRoutes(handler); // add webServer.on() endpoints from the generated web code
|
||||||
@@ -1562,6 +1593,9 @@ void EMSESP::start() {
|
|||||||
analogsensor_.start(); // Analog external sensors
|
analogsensor_.start(); // Analog external sensors
|
||||||
webLogService.start(); // apply settings to weblog service
|
webLogService.start(); // apply settings to weblog service
|
||||||
|
|
||||||
|
// set hostname on web server
|
||||||
|
DefaultHeaders::Instance().addHeader("Server", system_.hostname().c_str()); // TODO use hostname
|
||||||
|
|
||||||
// Load our library of known devices into stack mem. Names are stored in Flash memory
|
// Load our library of known devices into stack mem. Names are stored in Flash memory
|
||||||
device_library_ = {
|
device_library_ = {
|
||||||
#include "device_library.h"
|
#include "device_library.h"
|
||||||
|
|||||||
@@ -241,7 +241,8 @@ class EMSESP {
|
|||||||
static WebSchedulerService webSchedulerService;
|
static WebSchedulerService webSchedulerService;
|
||||||
static WebCustomEntityService webCustomEntityService;
|
static WebCustomEntityService webCustomEntityService;
|
||||||
|
|
||||||
static void handler(const char * uri, const char * contentType, const uint8_t * content, size_t len);
|
static void handler(const String & uri, const String & contentType, const uint8_t * content, size_t len);
|
||||||
|
// static void handler(const char * uri, const char * contentType, const uint8_t * content, size_t len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::string device_tostring(const uint8_t device_id);
|
static std::string device_tostring(const uint8_t device_id);
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
#define EMSESP_APP_VERSION "3.6.5-https.1"
|
#define EMSESP_APP_VERSION "3.6.5-https.2"
|
||||||
|
|||||||
@@ -23,27 +23,19 @@ using namespace std::placeholders;
|
|||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
WebLogService::WebLogService(PsychicHttpServer * server, SecurityManager * securityManager)
|
WebLogService::WebLogService(PsychicHttpServer * server, SecurityManager * securityManager)
|
||||||
// TODO fix event source
|
: _server(server)
|
||||||
: // : _events(EVENT_SOURCE_LOG_PATH)
|
|
||||||
_server(server)
|
|
||||||
, _securityManager(securityManager) {
|
, _securityManager(securityManager) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void WebLogService::registerURI() {
|
void WebLogService::registerURI() {
|
||||||
// TODO fix event source
|
// eventsource
|
||||||
/*
|
_server->on(EVENT_SOURCE_LOG_PATH, &_events);
|
||||||
_events_.onOpen([](PsychicEventSourceClient * client) {
|
|
||||||
Serial.printf("[eventsource] connection #%u connected from %s\n", client->socket(), client->remoteIP().toString());
|
|
||||||
client->send("Hello user!", NULL, millis(), 1000);
|
|
||||||
});
|
|
||||||
_events_.onClose([](PsychicEventSourceClient * client) {
|
|
||||||
Serial.printf("[eventsource] connection #%u closed from %s\n", client->socket(), client->remoteIP().toString());
|
|
||||||
});
|
|
||||||
_server->on(EVENT_SOURCE_LOG_PATH, &events_);
|
|
||||||
*/
|
|
||||||
|
|
||||||
// post
|
// TODO this doesn't work with PsychicEventSource
|
||||||
|
// _server->on(EVENT_SOURCE_LOG_PATH, &_events)->setFilter(_securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||||
|
// _events.setFilter(_securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||||
|
|
||||||
|
// POST
|
||||||
_server->on(LOG_SETTINGS_PATH, HTTP_POST, [this](PsychicRequest * request, JsonVariant & json) {
|
_server->on(LOG_SETTINGS_PATH, HTTP_POST, [this](PsychicRequest * request, JsonVariant & json) {
|
||||||
auto && body = json.as<JsonObject>();
|
auto && body = json.as<JsonObject>();
|
||||||
|
|
||||||
@@ -59,9 +51,7 @@ void WebLogService::registerURI() {
|
|||||||
return request->reply(200); // OK
|
return request->reply(200); // OK
|
||||||
});
|
});
|
||||||
|
|
||||||
_events.setFilter(_securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
// GET settings
|
||||||
|
|
||||||
// get settings
|
|
||||||
_server->on(LOG_SETTINGS_PATH, HTTP_GET, [this](PsychicRequest * request) {
|
_server->on(LOG_SETTINGS_PATH, HTTP_GET, [this](PsychicRequest * request) {
|
||||||
PsychicJsonResponse response = PsychicJsonResponse(request, false, EMSESP_JSON_SIZE_SMALL);
|
PsychicJsonResponse response = PsychicJsonResponse(request, false, EMSESP_JSON_SIZE_SMALL);
|
||||||
JsonObject root = response.getRoot();
|
JsonObject root = response.getRoot();
|
||||||
@@ -72,6 +62,7 @@ void WebLogService::registerURI() {
|
|||||||
return response.send();
|
return response.send();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// /rest/fetchLog
|
||||||
// for bring back the whole log - is a command, hence a POST
|
// for bring back the whole log - is a command, hence a POST
|
||||||
// send the complete log buffer to the API, not filtering on log level
|
// send the complete log buffer to the API, not filtering on log level
|
||||||
// done by resetting the pointer
|
// done by resetting the pointer
|
||||||
@@ -79,9 +70,6 @@ void WebLogService::registerURI() {
|
|||||||
log_message_id_tail_ = 0;
|
log_message_id_tail_ = 0;
|
||||||
return request->reply(200);
|
return request->reply(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO this can be removed when ported over
|
|
||||||
// server->addHandler(&events_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the log service with INFO level
|
// start the log service with INFO level
|
||||||
@@ -232,9 +220,9 @@ char * WebLogService::messagetime(char * out, const uint64_t t, const size_t buf
|
|||||||
// send to web eventsource
|
// send to web eventsource
|
||||||
void WebLogService::transmit(const QueuedLogMessage & message) {
|
void WebLogService::transmit(const QueuedLogMessage & message) {
|
||||||
DynamicJsonDocument jsonDocument(EMSESP_JSON_SIZE_LARGE);
|
DynamicJsonDocument jsonDocument(EMSESP_JSON_SIZE_LARGE);
|
||||||
if (jsonDocument.capacity() == 0) {
|
// if (jsonDocument.capacity() == 0) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
JsonObject logEvent = jsonDocument.to<JsonObject>();
|
JsonObject logEvent = jsonDocument.to<JsonObject>();
|
||||||
char time_string[25];
|
char time_string[25];
|
||||||
|
|
||||||
@@ -244,47 +232,13 @@ void WebLogService::transmit(const QueuedLogMessage & message) {
|
|||||||
logEvent["n"] = message.content_->name;
|
logEvent["n"] = message.content_->name;
|
||||||
logEvent["m"] = message.content_->text;
|
logEvent["m"] = message.content_->text;
|
||||||
|
|
||||||
size_t len = measureJson(jsonDocument);
|
size_t len = measureJson(jsonDocument) + 1;
|
||||||
char * buffer = new char[len + 1];
|
char * buffer = (char *)malloc(len);
|
||||||
if (buffer) {
|
if (buffer != NULL) {
|
||||||
serializeJson(jsonDocument, buffer, len + 1);
|
serializeJson(jsonDocument, buffer, len);
|
||||||
_events.send(buffer, "message", message.id_);
|
_events.send(buffer, "message", message.id_);
|
||||||
}
|
}
|
||||||
delete[] buffer;
|
free(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send the complete log buffer to the API, not filtering on log level
|
|
||||||
// done by resetting the pointer
|
|
||||||
// esp_err_t WebLogService::fetchLog(PsychicRequest * request) {
|
|
||||||
// log_message_id_tail_ = 0;
|
|
||||||
// request->send(200);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return the current value settings after a GET
|
|
||||||
// esp_err_t WebLogService::getValues(PsychicRequest * request) {
|
|
||||||
// auto * response = new AsyncJsonResponse(false, EMSESP_JSON_SIZE_SMALL);
|
|
||||||
// JsonObject root = response->getRoot();
|
|
||||||
// root["level"] = log_level();
|
|
||||||
// root["max_messages"] = maximum_log_messages();
|
|
||||||
// root["compact"] = compact();
|
|
||||||
// response->setLength();
|
|
||||||
// request->send(response);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sets the values like level after a POST
|
|
||||||
// esp_err_t WebLogService::setValues(PsychicRequest * request, JsonVariant & json) {
|
|
||||||
// auto && body = json.as<JsonObject>();
|
|
||||||
|
|
||||||
// uuid::log::Level level = body["level"];
|
|
||||||
// log_level(level);
|
|
||||||
|
|
||||||
// uint8_t max_messages = body["max_messages"];
|
|
||||||
// maximum_log_messages(max_messages);
|
|
||||||
|
|
||||||
// bool comp = body["compact"];
|
|
||||||
// compact(comp);
|
|
||||||
|
|
||||||
// return request->reply(200); // OK
|
|
||||||
// }
|
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
@@ -50,7 +50,6 @@ class WebLogService : public uuid::log::Handler {
|
|||||||
private:
|
private:
|
||||||
SecurityManager * _securityManager;
|
SecurityManager * _securityManager;
|
||||||
PsychicHttpServer * _server;
|
PsychicHttpServer * _server;
|
||||||
|
|
||||||
PsychicEventSource _events;
|
PsychicEventSource _events;
|
||||||
|
|
||||||
class QueuedLogMessage {
|
class QueuedLogMessage {
|
||||||
|
|||||||
Reference in New Issue
Block a user