Microchip® Advanced Software Framework

httpd.c File Reference

Httpd server.

Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.

#include "lwipopts.h"
#include "lwip/ip.h"
#include <string.h>
#include "lwip/tcp.h"
#include "fs.h"
#include "afec.h"
#include "httpd.h"
#include "cgi.h"

Data Structures

struct  http_state
 state info for file sending by http More...
 

Macros

#define HTTP_DEFAULT_PAGE   "index.html"
 
#define STEP_AT_27C   144 * 4096 / 330
 

Functions

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...
 
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...
 
void http_init (void)
 HTTP server init. 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...
 
void http_sendInternalErr (int content_type)
 Send HTTP internal server error code. More...
 
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...
 
int http_tokenizeGetRequest (char *raw_buf, size_t raw_len)
 Tokenize a buffer. More...
 
void http_write (const char *buf, u32_t len)
 Write HTTP data. More...
 

Variables

static char decoded_str [80]
 
struct http_stateg_hs
 
struct tcp_pcbg_pcb
 
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 req_string [80]
 Handle the incoming client request. More...
 
BoardStatus status
 board status info More...
 

#define HTTP_DEFAULT_PAGE   "index.html"

Referenced by http_recv().

#define STEP_AT_27C   144 * 4096 / 330

Referenced by http_accept().

const char* get_ext ( const char *  name)

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 afec_channel_get_value(), AFEC_TEMPERATURE_SENSOR, ERR_MEM, ERR_OK, http_state::file, http_conn_err(), http_poll(), http_recv(), BoardStatus::internal_temp, http_state::left, LWIP_UNUSED_ARG, mem_malloc(), NULL, http_state::retries, STEP_AT_27C, tcp_arg(), tcp_err(), tcp_poll(), TCP_PRIO_MIN, tcp_recv(), and tcp_setprio().

Referenced by http_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().

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.

Parameters
tolenized_buf.
tolenized_buf_len.
key.
value
len.

References decoded_str, and http_decodeUrl().

Referenced by cgi_displayMsg(), and cgi_led().

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

void http_init ( void  )

HTTP server init.

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

Referenced by init_ethernet().

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 requests and process them.

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

References arg, cgi_search(), cgi_table, 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().

void http_sendInternalErr ( int  content_type)

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_displayMsg(), cgi_led(), and http_recv().

void http_sendOk ( int  content_type)

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_chipInfo(), cgi_displayMsg(), cgi_echo(), cgi_led(), cgi_ledStatus(), cgi_resistor(), cgi_status(), cgi_temp(), cgi_uptime(), 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().

int http_tokenizeGetRequest ( char *  raw_buf,
size_t  raw_len 
)

Tokenize a buffer.

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

Referenced by cgi_displayMsg(), and cgi_led().

void http_write ( const char *  buf,
u32_t  len 
)

const char* content
char decoded_str[80]
static

Referenced by http_getValue().

struct http_state* g_hs
struct tcp_pcb* g_pcb

Referenced by cgi_status().

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 req_string[80]
static

Handle the incoming client request.

Referenced by http_recv().