Files
pico-keys-sdk/src/usb/lwip/rest_server.h
2026-05-18 16:41:27 +02:00

96 lines
2.5 KiB
C

/*
* This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk).
* Copyright (c) 2022 Pol Henarejos.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program 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
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef REST_SERVER_H
#define REST_SERVER_H
#ifdef ENABLE_EMULATION
typedef int err_t;
#define ERR_OK 0
#define ERR_VAL -6
#define ERR_ABRT -13
#else
#include "lwip/err.h"
#endif
#include <stddef.h>
#include <stdint.h>
#include "cJSON.h"
#include <stdbool.h>
#include "mbedtls/ssl.h"
#include "rest.h"
#define REST_PORT 80
#define REST_MAX_CONNS 4
#define REST_MAX_REQUEST_SIZE 1024
#define REST_MAX_METHOD_SIZE 8
#define REST_MAX_CONTENT_TYPE_SIZE 64
#define REST_MAX_PATH_SIZE 192
#define EF_TLS_KEY 0xD500
#define EF_TLS_CERT 0xD501
typedef enum {
REST_CONN_PLAIN = 0x1,
REST_CONN_TLS = 0x2,
REST_CONN_ALL = REST_CONN_PLAIN | REST_CONN_TLS
} rest_conn_type_t;
typedef struct {
#ifdef ENABLE_EMULATION
intptr_t sock;
#else
struct tcp_pcb *pcb;
#endif
size_t request_len;
rest_conn_type_t conn_type;
#ifdef _MSC_VER
char _padding[sizeof(void *) - sizeof(rest_conn_type_t)];
#endif
mbedtls_ssl_context ssl;
unsigned char rx_cipher[REST_MAX_REQUEST_SIZE];
size_t rx_cipher_len;
size_t request_headers_size;
size_t request_content_length;
bool in_use;
bool handshake_done;
bool request_complete;
bool request_dispatched;
bool request_headers_parsed;
#ifndef ENABLE_EMULATION
const char *tx_body;
size_t tx_body_len;
size_t tx_body_sent;
bool tx_pending;
bool tx_body_owned;
#endif
char request[REST_MAX_REQUEST_SIZE + 1];
#ifdef ENABLE_EMULATION
char _padding2[2];
#endif
} rest_conn_t;
err_t rest_server_init(rest_conn_type_t conn_type);
void rest_handle_request(rest_conn_t *conn);
extern int x509_set_random_serial(mbedtls_x509write_cert *crt);
extern int rest_server_error(rest_response_t *response, int status_code, const char *message);
#endif