Skip to content

Web server simplifications and handers #7429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
15141be
First stab ad simplyfing webserver auth and adding a handler.
dirkx Oct 29, 2022
0b4cf21
Merge branch 'espressif:master' into WebServerSimplificationsAndHanders
dirkx Nov 4, 2022
df0c189
Tweaks after testing against docs and latest Library tree
dirkx Nov 4, 2022
09a982e
Add documentatin for callback handler
dirkx Nov 4, 2022
15f4da7
Bodge to allow things to compile without the dependencies
dirkx Nov 4, 2022
48814ad
Remove dependency on sodium to make it compile with 4.4
dirkx Nov 4, 2022
f1282fc
Fix hex conversion
dirkx Nov 4, 2022
30b08d6
Move some common HEX functions into a static HEX class, remove those …
dirkx Nov 5, 2022
38b0778
Remove some duplicated code
dirkx Nov 5, 2022
6ca0c7d
Add simplfiied HEXBuilder under MD5Bulder to CMakefile.
dirkx Nov 5, 2022
a8265c3
Merge branch 'master' into WebServerSimplificationsAndHanders
dirkx Nov 9, 2022
524c017
Merge branch 'master' into WebServerSimplificationsAndHanders
SuGlider Dec 16, 2022
50200e3
Merge branch 'master' into WebServerSimplificationsAndHanders
SuGlider Dec 19, 2022
6548923
Merge branch 'master' into WebServerSimplificationsAndHanders
dirkx May 6, 2023
bb77987
Merge branch 'master' into pr/7429
lucasssvaz Jan 8, 2024
dcd64e1
Update for 3.0.0 and QoL improvements
lucasssvaz Jan 8, 2024
6813672
Remove examples that depend on external libraries
lucasssvaz Jan 8, 2024
c3ff7e0
Skip H2 testing
lucasssvaz Jan 9, 2024
d24668e
Formatting improvements
lucasssvaz Jan 9, 2024
ffdd841
Move builders examples to Utilities folder
lucasssvaz Jan 11, 2024
dc1a920
Fix indentation
lucasssvaz Jan 11, 2024
289f59d
Add HashBuilder abstract class
lucasssvaz Jan 11, 2024
81663f5
Add SHA1Builder
lucasssvaz Jan 11, 2024
85b2f68
Fix comment
lucasssvaz Jan 11, 2024
9ff23e7
Fix whitespace
lucasssvaz Jan 12, 2024
5631cf4
Fix crashes and improve log messages
lucasssvaz Jan 16, 2024
48f97ea
Merge remote-tracking branch 'origin/master' into pr/7429
lucasssvaz Jan 16, 2024
05fd5c5
Fix indentation for webserver
lucasssvaz Jan 16, 2024
d0ecd1f
Merge branch 'master' into WebServerSimplificationsAndHanders
lucasssvaz Jan 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ set(CORE_SRCS
cores/esp32/Esp.cpp
cores/esp32/FunctionalInterrupt.cpp
cores/esp32/HardwareSerial.cpp
cores/esp32/HEXBuilder.cpp
cores/esp32/IPAddress.cpp
cores/esp32/libb64/cdecode.c
cores/esp32/libb64/cencode.c
cores/esp32/main.cpp
cores/esp32/MD5Builder.cpp
cores/esp32/Print.cpp
cores/esp32/SHA1Builder.cpp
cores/esp32/stdlib_noniso.c
cores/esp32/Stream.cpp
cores/esp32/StreamString.cpp
Expand Down
71 changes: 71 additions & 0 deletions cores/esp32/HEXBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp32 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <HEXBuilder.h>

static uint8_t hex_char_to_byte(uint8_t c)
{
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0x10; // unknown char is 16
}

size_t HEXBuilder::hex2bytes(unsigned char * out, size_t maxlen, String &in) {
return hex2bytes(out, maxlen, in.c_str());
}

size_t HEXBuilder::hex2bytes(unsigned char * out, size_t maxlen, const char * in) {
size_t len = 0;
for(;*in;in++) {
uint8_t c = hex_char_to_byte(*in);
// Silently skip anything unknown.
if (c > 15)
continue;

if (len & 1) {
if (len/2 < maxlen)
out[len/2] |= c;
} else {
if (len/2 < maxlen)
out[len/2] = c<<4;
}
len++;
}
return (len + 1)/2;
}

size_t HEXBuilder::bytes2hex(char * out, size_t maxlen, const unsigned char * in, size_t len) {
for(size_t i = 0; i < len; i++) {
if (i*2 + 1 < maxlen) {
sprintf(out + (i * 2), "%02x", in[i]);
}
}
return len * 2 + 1;
}

String HEXBuilder::bytes2hex(const unsigned char * in, size_t len) {
size_t maxlen = len * 2 + 1;
char * out = (char *) malloc(maxlen);
if (!out) return String();
bytes2hex(out, maxlen, in, len);
String ret = String(out);
free(out);
return ret;
}
34 changes: 34 additions & 0 deletions cores/esp32/HEXBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp32 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef HEXBuilder_h
#define HEXBuilder_h

#include <WString.h>
#include <Stream.h>

class HEXBuilder {
public:
static size_t hex2bytes(unsigned char * out, size_t maxlen, String & in);
static size_t hex2bytes(unsigned char * out, size_t maxlen, const char * in);

static String bytes2hex(const unsigned char * in, size_t len);
static size_t bytes2hex(char * out, size_t maxlen, const unsigned char * in, size_t len);
};
#endif
60 changes: 60 additions & 0 deletions cores/esp32/HashBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef HashBuilder_h
#define HashBuilder_h

#include <WString.h>
#include <Stream.h>

#include "HEXBuilder.h"

class HashBuilder : public HEXBuilder
{
public:
virtual ~HashBuilder() {}
virtual void begin() = 0;

virtual void add(uint8_t* data, size_t len) = 0;
virtual void add(const char* data)
{
add((uint8_t*)data, strlen(data));
}
virtual void add(char* data)
{
add((const char*)data);
}
virtual void add(String data)
{
add(data.c_str());
}

virtual void addHexString(const char* data) = 0;
virtual void addHexString(char* data)
{
addHexString((const char*)data);
}
virtual void addHexString(String data)
{
addHexString(data.c_str());
}

virtual bool addStream(Stream& stream, const size_t maxLen) = 0;
virtual void calculate() = 0;
virtual void getBytes(uint8_t* output) = 0;
virtual void getChars(char* output) = 0;
virtual String toString() = 0;
};

#endif
23 changes: 6 additions & 17 deletions cores/esp32/MD5Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,30 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <HEXBuilder.h>
#include <MD5Builder.h>

static uint8_t hex_char_to_byte(uint8_t c)
{
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
}

void MD5Builder::begin(void)
{
memset(_buf, 0x00, ESP_ROM_MD5_DIGEST_LEN);
esp_rom_md5_init(&_ctx);
}

void MD5Builder::add(uint8_t * data, uint16_t len)
void MD5Builder::add(uint8_t * data, size_t len)
{
esp_rom_md5_update(&_ctx, data, len);
}

void MD5Builder::addHexString(const char * data)
{
uint16_t i, len = strlen(data);
size_t len = strlen(data);
uint8_t * tmp = (uint8_t*)malloc(len/2);
if(tmp == NULL) {
return;
}
for(i=0; i<len; i+=2) {
uint8_t high = hex_char_to_byte(data[i]);
uint8_t low = hex_char_to_byte(data[i+1]);
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
}
hex2bytes(tmp, len/2, data);
add(tmp, len/2);
free(tmp);
}
Expand Down Expand Up @@ -105,9 +96,7 @@ void MD5Builder::getBytes(uint8_t * output)

void MD5Builder::getChars(char * output)
{
for(uint8_t i = 0; i < ESP_ROM_MD5_DIGEST_LEN; i++) {
sprintf(output + (i * 2), "%02x", _buf[i]);
}
bytes2hex(output, ESP_ROM_MD5_DIGEST_LEN*2+1, _buf, ESP_ROM_MD5_DIGEST_LEN);
}

String MD5Builder::toString(void)
Expand Down
58 changes: 23 additions & 35 deletions cores/esp32/MD5Builder.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
/*
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This file is part of the esp32 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
Expand All @@ -15,51 +15,39 @@
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified 10 Jan 2024 by Lucas Saavedra Vaz (Use abstract class HashBuilder)
*/
#ifndef __ESP8266_MD5_BUILDER__
#define __ESP8266_MD5_BUILDER__
#ifndef MD5Builder_h
#define MD5Builder_h

#include <WString.h>
#include <Stream.h>

#include "esp_system.h"
#include "esp_rom_md5.h"

class MD5Builder
#include "HashBuilder.h"

class MD5Builder : public HashBuilder
{
private:
md5_context_t _ctx;
uint8_t _buf[ESP_ROM_MD5_DIGEST_LEN];
public:
void begin(void);
void add(uint8_t * data, uint16_t len);
void add(const char * data)
{
add((uint8_t*)data, strlen(data));
}
void add(char * data)
{
add((const char*)data);
}
void add(String data)
{
add(data.c_str());
}
void addHexString(const char * data);
void addHexString(char * data)
{
addHexString((const char*)data);
}
void addHexString(String data)
{
addHexString(data.c_str());
}
bool addStream(Stream & stream, const size_t maxLen);
void calculate(void);
void getBytes(uint8_t * output);
void getChars(char * output);
String toString(void);
};
void begin(void) override;

using HashBuilder::add;
void add(uint8_t * data, size_t len) override;

using HashBuilder::addHexString;
void addHexString(const char * data) override;

bool addStream(Stream & stream, const size_t maxLen) override;
void calculate(void) override;
void getBytes(uint8_t * output) override;
void getChars(char * output) override;
String toString(void) override;
};

#endif
Loading