Microchip® Advanced Software Framework

httpd.c File Reference

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_stateg_hs
 
struct tcp_pcbg_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 int cgi_GetStaus ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Send a JSON string representing the board status.

Parameters
nameString containing the status request.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
-1 failed,0:success.

References HTTP_CONTENT_JSON, http_sendOk(), http_write(), LED_0_PIN, port_pin_get_output_level(), tx_buf, and UNUSED.

static http_handler_t cgi_search ( const char *  name,
HttpCGI table 
)
static

CGI request search engine.

Parameters
nameCGI request name.
tableCGI handler table.
Returns
A valid function handler for the specified CGI request, NULL otherwise.

References CGI_MATCH_EXT, CGI_MATCH_NAME, get_ext(), HttpCGI::handler, and NULL.

Referenced by http_recv().

static int cgi_SetLed ( const char *  name,
char *  recv_buf,
size_t  recv_len 
)
static

Set the led status and send back the new status.

Parameters
nameString containing the status request.
recv_bufReceive buffer.
recv_lenReceive buffer length.
Returns
-1 failed,0:success.

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 const char* get_ext ( const char *  name)
static

Get filename extension type.

Parameters
nameString containing a filename.
Returns
Extension type on success, NULL otherwise.

References NULL.

Referenced by cgi_search(), and http_searchContentType().

static err_t http_accept ( void arg,
struct tcp_pcb pcb,
err_t  err 
)
static

Accept incoming HTTP connection requests.

Parameters
argPointer to structure representing the HTTP state.
pcbPointer to a TCP connection structure.
errConnection status.
Returns
ERR_OK on success.

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 void http_close_conn ( struct tcp_pcb pcb,
struct http_state hs 
)
static

Close HTTP connection.

Parameters
pcbPointer to a TCP connection structure.
hsPointer 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().

static void http_conn_err ( void arg,
err_t  err 
)
static

Callback called on connection error.

Parameters
argPointer to structure representing the HTTP state.
errError code.

References arg, LWIP_UNUSED_ARG, and mem_free().

Referenced by http_accept().

static void http_decodeUrl ( const char *  raw_buf,
size_t  raw_len,
char *  decodec_buf,
size_t  len 
)
static

Decode URL.

Parameters
raw_bufInput buffer.
raw_lenInput buffer length.
decodec_bufOutput decoded buffer.
lenOutput buffer length.

References http_hexToAscii().

Referenced by http_getValue().

static void http_getPageName ( const char *  recv_buf,
size_t  recv_len,
char *  page_name,
size_t  len 
)
static

Get requested page name.

Parameters
recv_bufReceive buffer.
recv_lenReceive buffer length.
page_nameOutput buffer to store requestd page name.
lenOutput buffer length.

Referenced by http_recv().

static int http_getValue ( char *  tolenized_buf,
size_t  tolenized_buf_len,
const char *  key,
char *  value,
size_t  len 
)
static

Get key value from tokenized buffer.

Parameters
tolenized_buf.
tolenized_buf_len.
key.
value
len.

References decoded_str, and http_decodeUrl().

Referenced by cgi_SetLed().

static char http_hexToAscii ( char  first,
char  second 
)
static

ASCII to HEX converter.

Parameters
firstFirst digit.
secondSecond digit.
Returns
String result.

Referenced by http_decodeUrl().

static err_t http_poll ( void arg,
struct tcp_pcb pcb 
)
static

Poll for HTTP data.

Parameters
argPointer to structure representing the HTTP state.
pcbPointer to a TCP connection structure.
Returns
ERR_OK on success, ERR_ABRT otherwise.

References arg, ERR_ABRT, ERR_OK, http_state::file, http_send_data(), NULL, http_state::retries, and tcp_abort().

Referenced by http_accept().

static err_t http_recv ( void arg,
struct tcp_pcb pcb,
struct pbuf p,
err_t  err 
)
static

Core HTTP server receive function.

Handle the request and process it.

Parameters
argPointer to structure representing the HTTP state.
pcbPointer to a TCP connection structure.
pIncoming request.
errConnection status.
Returns
ERR_OK.

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 int http_searchContentType ( const char *  name)
static

Match content type based on filename extension.

Parameters
nameString containing a filename.
Returns
Content type index on success, 0 otherwise.

References get_ext(), HTTP_CONTENT_CNT, HTTP_CONTENT_JPEG, http_content_type, and key.

Referenced by http_recv().

static void http_send_data ( struct tcp_pcb pcb,
struct http_state hs 
)
static

Send HTTP data.

Parameters
pcbPointer to a TCP connection structure.
hsPointer 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 void http_sendInternalErr ( int  content_type)
static

Send HTTP internal server error code.

Parameters
content_typeHTTP content type.

References content, http_content_type, http_html_hdr_500, and http_write().

Referenced by cgi_SetLed(), and http_recv().

static void http_sendOk ( int  content_type)
static

Send the HTTP header with the appropriated content type.

Parameters
content_typeIndex 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().

static err_t http_sent ( void arg,
struct tcp_pcb pcb,
u16_t  len 
)
static

Callback to handle data transfered.

Parameters
argPointer to structure representing the HTTP state.
pcbPointer to a TCP connection structure.
lenUnused.
Returns
ERR_OK on success, ERR_ABRT otherwise.

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 int http_tokenizeGetRequest ( char *  raw_buf,
size_t  raw_len 
)
static

Tokenize a buffer.

Parameters
raw_bufBuffer to parse.
raw_lenBuffer length.
Returns
The number of found tokens.

Referenced by cgi_SetLed().

static void http_write ( const char *  buf,
u32_t  len 
)
static

Write HTTP data.

Parameters
bufBuffer to write.
lenBuffer 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().

void httpd_init ( void  )

HTTP server init.

References http_accept(), IP_ADDR_ANY, tcp_accept(), tcp_bind(), tcp_listen, and tcp_new().

Referenced by main().

HttpCGI cgi_table[]
Initial value:
= {
{CGI_MATCH_NAME, "set_led", cgi_SetLed},
}
#define NULL
Definition: def.h:47
static int cgi_SetLed(const char *name, char *recv_buf, size_t recv_len)
Set the led status and send back the new status.
Definition: httpd.c:538
#define CGI_MATCH_NONE
Definition: httpd.h:60
#define CGI_MATCH_NAME
Select item in table if the string is content.
Definition: httpd.h:66
static int cgi_GetStaus(const char *name, char *recv_buf, size_t recv_len)
Send a JSON string representing the board status.
Definition: httpd.c:511

CGI table where we associate one callback to one page.

const char* content
char decoded_str[80]
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[]
Initial value:
= {
{"", "Content-type: application/json\r\n\r\n"},
{"html", "Content-type: text/html\r\n\r\n"},
{"css", "Content-type: text/css\r\n\r\n"},
{"js", "Content-type: text/javascript\r\n\r\n"},
{"png", "Content-type: image/png\r\n\r\n"},
{"jpg", "Content-type: image/jpeg\r\n\r\n"},
{"gif", "Content-type: image/gif\r\n\r\n"},
{"txt", "Content-type: text/plain\r\n\r\n"},
}

http content type indicated with keys

Referenced by http_searchContentType(), http_sendInternalErr(), and http_sendOk().

const char http_html_hdr_200[] = "HTTP/1.0 200 OK\r\n"
static

http html header

Referenced by http_sendOk().

const char http_html_hdr_404[] = "HTTP/1.0 404 Not Found\r\n"
static

Referenced by http_recv().

const char http_html_hdr_500[] = "HTTP/1.0 500 Internal Server Error\r\n"
static

Referenced by http_sendInternalErr().

const char http_server_error[]
Initial value:
= " \
<!DOCTYPE html PUBLIC \"-//IETF//DTD HTML 2.0//EN\"> \
<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \
<title>500 Internal Server error</title></head><body><h1>500 Internal Server error</h1> \
<p>The internal server error was occur while processing the requested page.</p> \
</body></html>"

http error page html

Referenced by http_recv().

const char* key

Referenced by http_searchContentType().

char key_value[80]
static

HTTP buffer to store key value.

Referenced by cgi_SetLed().

char req_string[80]
static

Handle the incoming client request.

Referenced by http_recv().