Httpd server.
Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
#include <string.h>
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/tcp.h"
#include "lwipopts.h"
#include "httpd.h"
#include "fs.h"
#include <port.h>
Data Structures | |
struct | http_state |
state info for file sending by http More... | |
Functions | |
static int | cgi_GetStaus (const char *name, char *recv_buf, size_t recv_len) |
Send a JSON string representing the board status. More... | |
static http_handler_t | cgi_search (const char *name, HttpCGI *table) |
CGI request search engine. More... | |
static int | cgi_SetLed (const char *name, char *recv_buf, size_t recv_len) |
Set the led status and send back the new status. More... | |
static const char * | get_ext (const char *name) |
Get filename extension type. More... | |
static err_t | http_accept (void *arg, struct tcp_pcb *pcb, err_t err) |
Accept incoming HTTP connection requests. More... | |
static void | http_close_conn (struct tcp_pcb *pcb, struct http_state *hs) |
Close HTTP connection. More... | |
static void | http_conn_err (void *arg, err_t err) |
Callback called on connection error. More... | |
static void | http_decodeUrl (const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len) |
Decode URL. More... | |
static void | http_getPageName (const char *recv_buf, size_t recv_len, char *page_name, size_t len) |
Get requested page name. More... | |
static int | http_getValue (char *tolenized_buf, size_t tolenized_buf_len, const char *key, char *value, size_t len) |
Get key value from tokenized buffer. More... | |
static char | http_hexToAscii (char first, char second) |
ASCII to HEX converter. More... | |
static err_t | http_poll (void *arg, struct tcp_pcb *pcb) |
Poll for HTTP data. More... | |
static err_t | http_recv (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) |
Core HTTP server receive function. More... | |
static int | http_searchContentType (const char *name) |
Match content type based on filename extension. More... | |
static void | http_send_data (struct tcp_pcb *pcb, struct http_state *hs) |
Send HTTP data. More... | |
static void | http_sendInternalErr (int content_type) |
Send HTTP internal server error code. More... | |
static void | http_sendOk (int content_type) |
Send the HTTP header with the appropriated content type. More... | |
static err_t | http_sent (void *arg, struct tcp_pcb *pcb, u16_t len) |
Callback to handle data transfered. More... | |
static int | http_tokenizeGetRequest (char *raw_buf, size_t raw_len) |
Tokenize a buffer. More... | |
static void | http_write (const char *buf, u32_t len) |
Write HTTP data. More... | |
void | httpd_init (void) |
HTTP server init. More... | |
Variables | |
HttpCGI | cgi_table [] |
CGI table where we associate one callback to one page. More... | |
static char | decoded_str [80] |
Decoded message buffer. More... | |
struct http_state * | g_hs |
struct tcp_pcb * | g_pcb |
the TCP protocol control block More... | |
struct { | |
const char * content | |
const char * key | |
} | http_content_type [] |
http content type indicated with keys More... | |
static const char | http_html_hdr_200 [] = "HTTP/1.0 200 OK\r\n" |
http html header More... | |
static const char | http_html_hdr_404 [] = "HTTP/1.0 404 Not Found\r\n" |
static const char | http_html_hdr_500 [] = "HTTP/1.0 500 Internal Server Error\r\n" |
const char | http_server_error [] |
http error page html More... | |
static char | key_value [80] |
HTTP buffer to store key value. More... | |
static char | req_string [80] |
Handle the incoming client request. More... | |
u8_t | tx_buf [CONFIG_AFSK_TX_BUFLEN] |
|
static |
Send a JSON string representing the board status.
name | String containing the status request. |
recv_buf | Receive buffer. |
recv_len | Receive buffer length. |
References HTTP_CONTENT_JSON, http_sendOk(), http_write(), LED_0_PIN, port_pin_get_output_level(), tx_buf, and UNUSED.
|
static |
CGI request search engine.
name | CGI request name. |
table | CGI handler table. |
References CGI_MATCH_EXT, CGI_MATCH_NAME, get_ext(), HttpCGI::handler, and NULL.
Referenced by http_recv().
|
static |
Set the led status and send back the new status.
name | String containing the status request. |
recv_buf | Receive buffer. |
recv_len | Receive buffer length. |
References CGI_LED_CMD_KEY, CGI_LED_ID_KEY, HTTP_CONTENT_JSON, http_getValue(), http_sendInternalErr(), http_sendOk(), http_tokenizeGetRequest(), http_write(), key_value, LED_0_PIN, port_pin_set_output_level(), tx_buf, and UNUSED.
|
static |
Get filename extension type.
name | String containing a filename. |
References NULL.
Referenced by cgi_search(), and http_searchContentType().
Accept incoming HTTP connection requests.
arg | Pointer to structure representing the HTTP state. |
pcb | Pointer to a TCP connection structure. |
err | Connection status. |
References ERR_MEM, ERR_OK, http_state::file, http_conn_err(), http_poll(), http_recv(), http_state::left, LWIP_UNUSED_ARG, mem_malloc(), NULL, http_state::retries, tcp_arg(), tcp_err(), tcp_poll(), TCP_PRIO_MIN, tcp_recv(), and tcp_setprio().
Referenced by httpd_init().
|
static |
Close HTTP connection.
pcb | Pointer to a TCP connection structure. |
hs | Pointer to structure representing the HTTP state. |
References mem_free(), NULL, tcp_arg(), tcp_close(), tcp_recv(), and tcp_sent().
Referenced by http_recv(), and http_sent().
Callback called on connection error.
arg | Pointer to structure representing the HTTP state. |
err | Error code. |
References arg, LWIP_UNUSED_ARG, and mem_free().
Referenced by http_accept().
|
static |
Decode URL.
raw_buf | Input buffer. |
raw_len | Input buffer length. |
decodec_buf | Output decoded buffer. |
len | Output buffer length. |
References http_hexToAscii().
Referenced by http_getValue().
|
static |
Get requested page name.
recv_buf | Receive buffer. |
recv_len | Receive buffer length. |
page_name | Output buffer to store requestd page name. |
len | Output buffer length. |
Referenced by http_recv().
|
static |
Get key value from tokenized buffer.
tolenized_buf | . |
tolenized_buf_len | . |
key | . |
value | |
len | . |
References decoded_str, and http_decodeUrl().
Referenced by cgi_SetLed().
|
static |
ASCII to HEX converter.
first | First digit. |
second | Second digit. |
Referenced by http_decodeUrl().
Poll for HTTP data.
arg | Pointer to structure representing the HTTP state. |
pcb | Pointer to a TCP connection structure. |
References arg, ERR_ABRT, ERR_OK, http_state::file, http_send_data(), NULL, http_state::retries, and tcp_abort().
Referenced by http_accept().
Core HTTP server receive function.
Handle the request and process it.
arg | Pointer to structure representing the HTTP state. |
pcb | Pointer to a TCP connection structure. |
p | Incoming request. |
err | Connection status. |
References arg, cgi_search(), fs_file::data, ERR_OK, http_state::file, fs_open(), http_close_conn(), HTTP_CONTENT_HTML, HTTP_DEFAULT_PAGE, http_getPageName(), http_html_hdr_404, http_searchContentType(), http_sendInternalErr(), http_sendOk(), http_server_error, http_write(), fs_file::len, pbuf::len, NULL, pbuf::payload, pbuf_free(), req_string, tcp_recved(), and pbuf::tot_len.
Referenced by http_accept().
|
static |
Match content type based on filename extension.
name | String containing a filename. |
References get_ext(), HTTP_CONTENT_CNT, HTTP_CONTENT_JPEG, http_content_type, and key.
Referenced by http_recv().
|
static |
Send HTTP data.
pcb | Pointer to a TCP connection structure. |
hs | Pointer to structure representing the HTTP state. |
References ERR_MEM, ERR_OK, http_state::file, http_state::left, tcp_sndbuf, tcp_write(), and TCP_WRITE_FLAG_COPY.
Referenced by http_poll(), http_sent(), and http_write().
|
static |
Send HTTP internal server error code.
content_type | HTTP content type. |
References content, http_content_type, http_html_hdr_500, and http_write().
Referenced by cgi_SetLed(), and http_recv().
|
static |
Send the HTTP header with the appropriated content type.
content_type | Index in the http_content_type table. |
References content, http_content_type, http_html_hdr_200, and http_write().
Referenced by cgi_GetStaus(), cgi_SetLed(), and http_recv().
Callback to handle data transfered.
arg | Pointer to structure representing the HTTP state. |
pcb | Pointer to a TCP connection structure. |
len | Unused. |
References arg, ERR_OK, http_close_conn(), http_send_data(), http_state::left, LWIP_UNUSED_ARG, and http_state::retries.
Referenced by http_write().
|
static |
Tokenize a buffer.
raw_buf | Buffer to parse. |
raw_len | Buffer length. |
Referenced by cgi_SetLed().
Write HTTP data.
buf | Buffer to write. |
len | Buffer length. |
References http_state::file, http_send_data(), http_sent(), http_state::left, and tcp_sent().
Referenced by cgi_GetStaus(), cgi_SetLed(), http_recv(), http_sendInternalErr(), and http_sendOk().
HTTP server init.
References http_accept(), IP_ADDR_ANY, tcp_accept(), tcp_bind(), tcp_listen, and tcp_new().
Referenced by main().
HttpCGI cgi_table[] |
CGI table where we associate one callback to one page.
const char* content |
Referenced by http_sendInternalErr(), and http_sendOk().
|
static |
Decoded message buffer.
Referenced by http_getValue().
struct http_state* g_hs |
struct tcp_pcb* g_pcb |
the TCP protocol control block
struct { ... } http_content_type[] |
http content type indicated with keys
Referenced by http_searchContentType(), http_sendInternalErr(), and http_sendOk().
|
static |
http html header
Referenced by http_sendOk().
|
static |
Referenced by http_recv().
|
static |
Referenced by http_sendInternalErr().
const char http_server_error[] |
http error page html
Referenced by http_recv().
const char* key |
Referenced by http_searchContentType().
|
static |
HTTP buffer to store key value.
Referenced by cgi_SetLed().
|
static |
Handle the incoming client request.
Referenced by http_recv().
u8_t tx_buf[CONFIG_AFSK_TX_BUFLEN] |
Referenced by cgi_GetStaus(), and cgi_SetLed().