Skip to content

Commit

Permalink
HTTP: Add DELETE as new open mode (5,9) to retrieve body/json response
Browse files Browse the repository at this point in the history
This adds new aux1 open commands of:
o 5 = DELETE with no headers
o 9 = DELETE with headers

and allows clients to retrieve the response of the DELETE
in the same way as other open commands on http (i.e. GET/POST/PUT)
using JSON or full READ of body.
  • Loading branch information
markjfisher committed Oct 19, 2023
1 parent 86bd948 commit d0e457c
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 145 deletions.
56 changes: 34 additions & 22 deletions lib/device/sio/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void sioNetwork::sio_open()

newData = (uint8_t *)malloc(NEWDATA_SIZE);

if (!newData)
if (newData == nullptr)
{
Debug_printv("Could not allocate write buffer\n");
sio_error();
Expand Down Expand Up @@ -139,8 +139,11 @@ void sioNetwork::sio_open()
protocolParser = nullptr;
}

if (newData)
if (newData != nullptr)
{
free(newData);
newData = nullptr;
}

sio_error();
return;
Expand All @@ -159,8 +162,11 @@ void sioNetwork::sio_open()
protocolParser = nullptr;
}

if (newData)
if (newData != nullptr)
{
free(newData);
newData = nullptr;
}

sio_error();
return;
Expand Down Expand Up @@ -224,8 +230,11 @@ void sioNetwork::sio_close()
json = nullptr;
}

if (newData)
if (newData != nullptr)
{
free(newData);
newData = nullptr;
}

Debug_printv("After protocol delete %lu\n",esp_get_free_internal_heap_size());
}
Expand Down Expand Up @@ -693,32 +702,35 @@ void sioNetwork::do_inquiry(unsigned char inq_cmd)

// Ask protocol for dstats, otherwise get it locally.
if (protocol != nullptr)
{
inq_dstats = protocol->special_inquiry(inq_cmd);
Debug_printf("protocol special_inquiry returned %d\r\n", inq_dstats);
}

// If we didn't get one from protocol, or unsupported, see if supported globally.
if (inq_dstats == 0xFF)
{
switch (inq_cmd)
{
case 0x20:
case 0x21:
case 0x23:
case 0x24:
case 0x2A:
case 0x2B:
case 0x2C:
case 0xFD:
case 0xFE:
case 0x20: // ' ' rename
case 0x21: // '!' delete
case 0x23: // '#' lock
case 0x24: // '$' unlock
case 0x2A: // '*' mkdir
case 0x2B: // '+' rmdir
case 0x2C: // ',' chdir/get prefix
case 0xFD: // login
case 0xFE: // password
inq_dstats = 0x80;
break;
case 0xFC:
case 0xFC: // channel mode
inq_dstats = 0x00;
break;
case 0xFB: // String Processing mode, only in JSON mode
if (channelMode == JSON)
inq_dstats = 0x00;
break;
case 0x30:
case 0x30: // '0' set prefix
inq_dstats = 0x40;
break;
case 'Z': // Set interrupt rate
Expand Down Expand Up @@ -812,15 +824,15 @@ void sioNetwork::sio_special_80()
// Handle commands that exist outside of an open channel.
switch (cmdFrame.comnd)
{
case 0x20: // RENAME
case 0x21: // DELETE
case 0x23: // LOCK
case 0x24: // UNLOCK
case 0x2A: // MKDIR
case 0x2B: // RMDIR
case 0x20: // RENAME ' '
case 0x21: // DELETE '!'
case 0x23: // LOCK '#'
case 0x24: // UNLOCK '$'
case 0x2A: // MKDIR '*'
case 0x2B: // RMDIR '+'
sio_do_idempotent_command_80();
return;
case 0x2C: // CHDIR
case 0x2C: // CHDIR ','
sio_set_prefix();
return;
case 'Q':
Expand Down
5 changes: 3 additions & 2 deletions lib/device/sio/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#define OUTPUT_BUFFER_SIZE 65535
#define SPECIAL_BUFFER_SIZE 256

#define NEWDATA_SIZE 65535

class sioNetwork : public virtualDevice
{

Expand Down Expand Up @@ -257,8 +259,7 @@ class sioNetwork : public virtualDevice
/**
* @brief the write buffer
*/
uint8_t *newData;
#define NEWDATA_SIZE 65535
uint8_t *newData = nullptr;

/**
* Instantiate protocol object
Expand Down
92 changes: 0 additions & 92 deletions lib/fn_esp_http_client/fn_esp_http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,98 +58,6 @@ namespace fujinet

static const char *TAG = "HTTP_CLIENT";

/**
* HTTP Buffer
*/
typedef struct {
char *data; /*!< The HTTP data received from the server */
int len; /*!< The HTTP data len received from the server */
char *raw_data; /*!< The HTTP data after decoding */
int raw_len; /*!< The HTTP data len after decoding */
char *output_ptr; /*!< The destination address of the data to be copied to after decoding */
} esp_http_buffer_t;

/**
* private HTTP Data structure
*/
typedef struct {
http_header_handle_t headers; /*!< http header */
esp_http_buffer_t *buffer; /*!< data buffer as linked list */
int status_code; /*!< status code (integer) */
int content_length; /*!< data length */
int chunk_length; /*!< last chunk length */
int total_chunk_length; /*!< total chunk length */
int data_offset; /*!< offset to http data (Skip header) */
int data_process; /*!< data processed */
int method; /*!< http method */
bool is_chunked;
} esp_http_data_t;

typedef struct {
char *url;
char *scheme;
char *host;
int port;
char *username;
char *password;
char *path;
char *query;
char *cert_pem;
esp_http_client_method_t method;
esp_http_client_auth_type_t auth_type;
esp_http_client_transport_t transport_type;
int max_store_header_size;
} connection_info_t;

typedef enum {
HTTP_STATE_UNINIT = 0,
HTTP_STATE_INIT,
HTTP_STATE_CONNECTED,
HTTP_STATE_REQ_COMPLETE_HEADER,
HTTP_STATE_REQ_COMPLETE_DATA,
HTTP_STATE_RES_COMPLETE_HEADER,
HTTP_STATE_RES_COMPLETE_DATA,
HTTP_STATE_CLOSE
} esp_http_state_t;
/**
* HTTP client class
*/
struct esp_http_client {
int redirect_counter;
int max_redirection_count;
int process_again;
struct http_parser *parser;
struct http_parser_settings *parser_settings;
esp_transport_list_handle_t transport_list;
esp_transport_handle_t transport;
esp_http_data_t *request;
esp_http_data_t *response;
void *user_data;
esp_http_auth_data_t *auth_data;
char *post_data;
char *location;
char *auth_header;
char *current_header_key;
char *current_header_value;
int post_len;
connection_info_t connection_info;
bool is_chunk_complete;
esp_http_state_t state;
http_event_handle_cb event_handler;
int timeout_ms;
int buffer_size_rx;
int buffer_size_tx;
bool disable_auto_redirect;
esp_http_client_event_t event;
int data_written_index;
int data_write_left;
bool first_line_prepared;
int header_index;
bool is_async;
};

typedef struct esp_http_client esp_http_client_t;

static esp_err_t _clear_connection_info(esp_http_client_handle_t client);
/**
* Default settings
Expand Down
96 changes: 95 additions & 1 deletion lib/fn_esp_http_client/fn_esp_http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
#include "esp_err.h"
#include "esp_tls.h"
#include "esp_transport.h"

#include "fn_http_header.h"
#include "fn_http_auth.h"

namespace fujinet
{
Expand Down Expand Up @@ -144,6 +145,99 @@ typedef enum {
HttpStatus_Unauthorized = 401
} HttpStatus_Code;

/**
* HTTP Buffer
*/
typedef struct {
char *data; /*!< The HTTP data received from the server */
int len; /*!< The HTTP data len received from the server */
char *raw_data; /*!< The HTTP data after decoding */
int raw_len; /*!< The HTTP data len after decoding */
char *output_ptr; /*!< The destination address of the data to be copied to after decoding */
} esp_http_buffer_t;

/**
* private HTTP Data structure
*/
typedef struct {
http_header_handle_t headers; /*!< http header */
esp_http_buffer_t *buffer; /*!< data buffer as linked list */
int status_code; /*!< status code (integer) */
int content_length; /*!< data length */
int chunk_length; /*!< last chunk length */
int total_chunk_length; /*!< total chunk length */
int data_offset; /*!< offset to http data (Skip header) */
int data_process; /*!< data processed */
int method; /*!< http method */
bool is_chunked;
} esp_http_data_t;

typedef struct {
char *url;
char *scheme;
char *host;
int port;
char *username;
char *password;
char *path;
char *query;
char *cert_pem;
esp_http_client_method_t method;
esp_http_client_auth_type_t auth_type;
esp_http_client_transport_t transport_type;
int max_store_header_size;
} connection_info_t;

typedef enum {
HTTP_STATE_UNINIT = 0,
HTTP_STATE_INIT,
HTTP_STATE_CONNECTED,
HTTP_STATE_REQ_COMPLETE_HEADER,
HTTP_STATE_REQ_COMPLETE_DATA,
HTTP_STATE_RES_COMPLETE_HEADER,
HTTP_STATE_RES_COMPLETE_DATA,
HTTP_STATE_CLOSE
} esp_http_state_t;
/**
* HTTP client class
*/
struct esp_http_client {
int redirect_counter;
int max_redirection_count;
int process_again;
struct http_parser *parser;
struct http_parser_settings *parser_settings;
esp_transport_list_handle_t transport_list;
esp_transport_handle_t transport;
esp_http_data_t *request;
esp_http_data_t *response;
void *user_data;
esp_http_auth_data_t *auth_data;
char *post_data;
char *location;
char *auth_header;
char *current_header_key;
char *current_header_value;
int post_len;
connection_info_t connection_info;
bool is_chunk_complete;
esp_http_state_t state;
http_event_handle_cb event_handler;
int timeout_ms;
int buffer_size_rx;
int buffer_size_tx;
bool disable_auto_redirect;
esp_http_client_event_t event;
int data_written_index;
int data_write_left;
bool first_line_prepared;
int header_index;
bool is_async;
};

typedef struct esp_http_client esp_http_client_t;


#define ESP_ERR_HTTP_BASE (0x7000) /*!< Starting number of HTTP error codes */
#define ESP_ERR_HTTP_MAX_REDIRECT (ESP_ERR_HTTP_BASE + 1) /*!< The error exceeds the number of HTTP redirects */
#define ESP_ERR_HTTP_CONNECT (ESP_ERR_HTTP_BASE + 2) /*!< Error open the HTTP connection */
Expand Down
Loading

0 comments on commit d0e457c

Please sign in to comment.