ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
Data Structures | Macros | Typedefs | Enumerations | Functions
web.h File Reference

Small web server for esp8266. More...

Go to the source code of this file.

Data Structures

struct  hinfo_t
 
struct  mem_t
 
struct  header_t
 
struct  mime_t
 
struct  rwbuf_t
 

Macros

#define MAX_CONNECTIONS   1
 
#define IO_MAX   512
 

Typedefs

typedef struct espconn espconn_t
 

Enumerations

enum  {
  TOKEN_GET, TOKEN_PUT, TOKEN_POST, TOKEN_HEAD,
  TOKEN_HOST, TOKEN_USER_AGENT, TOKEN_HTTPS, TOKEN_DNT,
  TOKEN_ACCEPT, TOKEN_ACCEPT_LANGUAGE, TOKEN_ACCEPT_ENCODING, TOKEN_CONNECTION,
  TOKEN_REFERER, TOKEN_CONTENT_LENGTH, TOKEN_CONTENT_TYPE, TOKEN_CACHE_CONTROL
}
 
enum  {
  STATUS_OK =200, STATUS_CREATED =201, STATUS_ACCEPTED =202, STATUS_NO_CONTENT =204,
  STATUS_MV_PERM =301, STATUS_MV_TEMP =302, STATUS_NOT_MODIF =304, STATUS_BAD_REQ =400,
  STATUS_UNAUTH =401, STATUS_FORBIDDEN =403, STATUS_NOT_FOUND =404, STATUS_INT_SERR =500,
  STATUS_NOT_IMPL =501, STATUS_BAD_GATEWAY =502, STATUS_SERV_UNAVAIL =503
}
 
enum  {
  PTYPE_TEXT, PTYPE_HTML, PTYPE_PDF, PTYPE_CSS,
  PTYPE_CGI, PTYPE_JS, PTYPE_XML, PTYPE_ICO,
  PTYPE_GIF, PTYPE_JPEG, PTYPE_MPEG, PTYPE_FLASH,
  PTYPE_ERR
}
 

Functions

MEMSPACE void web_sep (void)
 printer seperator More...
 
MEMSPACE int wait_send (rwbuf_t *p)
 Wait for buffer to send for this connection If write_buffer sending flag is set then wait for it to clear. More...
 
MEMSPACE int write_buffer (rwbuf_t *p)
 Socket write buffer for this connection We wait for previous send to complete - then send any new data We do not wait for new data to finish sending - you use flush for that. More...
 
MEMSPACE int write_flush (rwbuf_t *p)
 Write all outstanding data and wait for it to send. More...
 
MEMSPACE int write_byte (rwbuf_t *p, int c)
 Write a byte (buffered) using the rwbuf_t socket buffers for this connection If the buffers are full the socket is written using write_flush. More...
 
MEMSPACE void led_on (int led)
 
MEMSPACE void led_off (int led)
 Turn off virtual LED. More...
 
MEMSPACE void rwbuf_rinit (rwbuf_t *p)
 Initialize socket read status and read index. More...
 
MEMSPACE void rwbuf_winit (rwbuf_t *p)
 Initialize socket send status and write index. More...
 
MEMSPACE void display_ipv4 (char *msg, uint8_t *ip, int port)
 Display IPV4 address. More...
 
MEMSPACE void rwbuf_delete (rwbuf_t *p)
 Delete socket read/write buffers. More...
 
MEMSPACE rwbuf_trwbuf_create (void)
 Create socket read/write buffer for a connection. More...
 
MEMSPACE rwbuf_tfind_connection (espconn_t *conn, int *index, char *msg)
 Find a read/write socket buffer for an espconn connection. More...
 
MEMSPACE rwbuf_tcreate_connection (espconn_t *conn)
 Allocate read/write socket buffers and add it to the working pool. More...
 
MEMSPACE int delete_connection (rwbuf_t *p)
 Delete our main connection structure and connection buffers. More...
 
MEMSPACE void write_len (rwbuf_t *p, char *str, int len)
 Write data using buffered write_byte function. More...
 
MEMSPACE void write_str (rwbuf_t *p, char *str)
 Write string using buffered write_byte function. More...
 
MEMSPACE int vsock_printf (rwbuf_t *p, const char *fmt, va_list va)
 vsock_printf function More...
 
MEMSPACE int sock_printf (rwbuf_t *p, const char *fmt,...)
 sock_printf function More...
 
MEMSPACE int html_msg (rwbuf_t *p, int status, char type, char *fmt,...)
 Send an HTML status message to socket. More...
 
MEMSPACE char * meminit (mem_t *p, char *ptr, int size)
 in memory memory gets function More...
 
MEMSPACE char * memgets (mem_t *p)
 Memory gets function We ASSUME we can replace any
with a \0. More...
 
MEMSPACE char * mime_type (int type)
 return strung pointer for mime type index More...
 
MEMSPACE int file_type (char *name)
 Determin mimetype using file name extension. More...
 
MEMSPACE char * html_status (int status)
 Convert html status into string using http_status table. More...
 
MEMSPACE void init_hinfo (hinfo_t *hi)
 
MEMSPACE int match_headers (char *str, char **p)
 Match GET/POST message headers. More...
 
MEMSPACE char * process_args (hinfo_t *hi, char *ptr)
 Process GET argments or POST message name/value data. HTML encoding is done in place often reducting the size of the result. Convert name=value pairs into null terminated strings. Names and values are each terminated with an EOS by replaceing '?', '&', '=' characters seen while scaning. More...
 
MEMSPACE char * first_arg (hinfo_t *hi)
 Find first POST/GET argument. More...
 
MEMSPACE char * next_arg (hinfo_t *hi)
 Find next POST/GET argument We have to skip a name and a value. More...
 
MEMSPACE char * arg_name (hinfo_t *hi)
 Return the argument name for current argument. More...
 
MEMSPACE char * arg_value (hinfo_t *hi)
 Return the argument value for current argument. More...
 
MEMSPACE char * http_value (hinfo_t *hi, char *str)
 Lookup and argument name and return its value. More...
 
MEMSPACE int is_header (char *str, char **p)
 Does the string look like a header token with a ':' ? More...
 
MEMSPACE char * nextbreak (char *ptr)
 Find next space or ? character. More...
 
MEMSPACE void u5toa (char *ptr, uint16_t num)
 Print a decimal number into a string without an EOS. More...
 
MEMSPACE void html_head (rwbuf_t *p, int status, char type, int len)
 Write HTTP Contenet-Type/Content-Length header. More...
 
MEMSPACE int parse_http_request (rwbuf_t *p, hinfo_t *hi)
 Get arguments for a GET or POST request. More...
 
MEMSPACE int is_cgitoken_char (int c)
 test to see if a character is a valid member of the CGI token character set CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-' More...
 
MEMSPACE int find_cgitoken_start (char *str)
 Find start of CGI token in a string CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-' param[in] *str: string to search. More...
 
MEMSPACE int is_cgitoken (char *str)
 Does the string have a CGI TOKEN at the beginning ? CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-'. More...
 
MEMSPACE int rewrite_cgi_token (rwbuf_t *p, char *src)
 Replace CGI token with CGI result CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-'. More...
 
MEMSPACE void web_task (void)
 Process ALL incoming HTTP requests. More...
 
MEMSPACE void web_init_connections (void)
 
MEMSPACE void web_init (int port)
 Setup WEB server and accept connections. More...
 

Detailed Description

Small web server for esp8266.

Copyright © 2015 Mike Gore, GPL License
You are free to use this code under the terms of GPL
please retain a copy of this notice in any code you use it in.

This is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This software 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file web.h.

Macro Definition Documentation

#define IO_MAX   512

Definition at line 65 of file web.h.

#define MAX_CONNECTIONS   1

Definition at line 31 of file web.h.

Referenced by create_connection(), delete_connection(), find_connection(), setup(), and tcp_accept().

Typedef Documentation

typedef struct espconn espconn_t

Definition at line 27 of file web.h.

Enumeration Type Documentation

anonymous enum
Enumerator
TOKEN_GET 
TOKEN_PUT 
TOKEN_POST 
TOKEN_HEAD 
TOKEN_HOST 
TOKEN_USER_AGENT 
TOKEN_HTTPS 
TOKEN_DNT 
TOKEN_ACCEPT 
TOKEN_ACCEPT_LANGUAGE 
TOKEN_ACCEPT_ENCODING 
TOKEN_CONNECTION 
TOKEN_REFERER 
TOKEN_CONTENT_LENGTH 
TOKEN_CONTENT_TYPE 
TOKEN_CACHE_CONTROL 

Definition at line 68 of file web.h.

anonymous enum
Enumerator
STATUS_OK 
STATUS_CREATED 
STATUS_ACCEPTED 
STATUS_NO_CONTENT 
STATUS_MV_PERM 
STATUS_MV_TEMP 
STATUS_NOT_MODIF 
STATUS_BAD_REQ 
STATUS_UNAUTH 
STATUS_FORBIDDEN 
STATUS_NOT_FOUND 
STATUS_INT_SERR 
STATUS_NOT_IMPL 
STATUS_BAD_GATEWAY 
STATUS_SERV_UNAVAIL 

Definition at line 99 of file web.h.

anonymous enum
Enumerator
PTYPE_TEXT 
PTYPE_HTML 
PTYPE_PDF 
PTYPE_CSS 
PTYPE_CGI 
PTYPE_JS 
PTYPE_XML 
PTYPE_ICO 
PTYPE_GIF 
PTYPE_JPEG 
PTYPE_MPEG 
PTYPE_FLASH 
PTYPE_ERR 

Definition at line 117 of file web.h.

Function Documentation

MEMSPACE char* arg_name ( hinfo_t hi)

Return the argument name for current argument.

Parameters
[in]*hihinfo_t structure with arguments
Returns
argument name

Definition at line 1298 of file web.c.

Referenced by http_value(), and parse_http_request().

MEMSPACE char* arg_value ( hinfo_t hi)

Return the argument value for current argument.

Parameters
[in]*hihinfo_t structure with arguments
Returns
argument value

Definition at line 1346 of file web.c.

Referenced by http_value(), and parse_http_request().

MEMSPACE rwbuf_t* create_connection ( espconn_t conn)

Allocate read/write socket buffers and add it to the working pool.

Parameters
[in]connespconn pointer for this connection
Returns
rwbuf_t pointer to created structure

Definition at line 669 of file web.c.

Referenced by web_data_connect_callback().

MEMSPACE int delete_connection ( rwbuf_t p)

Delete our main connection structure and connection buffers.

Parameters
[in]prwbuf_t pointer

Definition at line 715 of file web.c.

Referenced by web_data_disconnect_callback().

MEMSPACE void display_ipv4 ( char *  msg,
uint8_t ip,
int  port 
)

Display IPV4 address.

Parameters
[in]msguser supplied message
[in]ipIP address as uint8_t [4]
[in]portPort number
Returns
void

Definition at line 470 of file web.c.

Referenced by delete_connection(), find_connection(), web_data_connect_callback(), web_data_disconnect_callback(), web_data_error_callback(), web_data_receive_callback(), and web_data_sent_callback().

MEMSPACE int file_type ( char *  name)

Determin mimetype using file name extension.

Parameters
[in]*namename to test
Returns
index into mimetype table

Definition at line 996 of file web.c.

MEMSPACE int find_cgitoken_start ( char *  str)

Find start of CGI token in a string CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-' param[in] *str: string to search.

Returns
offset of start of token, or -1 if not found

Definition at line 2024 of file web.c.

MEMSPACE rwbuf_t* find_connection ( espconn_t conn,
int *  index,
char *  msg 
)

Find a read/write socket buffer for an espconn connection.

Parameters
[in]connespconn pointer for this connection
[in]*indexreturn index into pool of rwbuf_t connections
[in]*msguser debug message
Returns
rwbuf_t pointer to found connection with *index set, or NULL with *index == -1 on failure

Definition at line 590 of file web.c.

Referenced by web_data_disconnect_callback(), web_data_error_callback(), web_data_receive_callback(), and web_data_sent_callback().

MEMSPACE char* first_arg ( hinfo_t hi)

Find first POST/GET argument.

Parameters
[in]*hihinfo_t structure with arguments
Returns
argument value pair

Definition at line 1235 of file web.c.

Referenced by http_value(), and parse_http_request().

MEMSPACE void html_head ( rwbuf_t p,
int  status,
char  type,
int  len 
)

Write HTTP Contenet-Type/Content-Length header.

Parameters
[in]*prwbuf_t pointer to socket buffer
[in]statushtml status message index
[in]typemimetype index
[in]lenlength of message
Returns
void

Definition at line 1518 of file web.c.

MEMSPACE int html_msg ( rwbuf_t p,
int  status,
char  type,
char *  fmt,
  ... 
)

Send an HTML status message to socket.

Parameters
[in]psocket buffer structure
[in]statusindex into http_status table
[in]typemime type index
[in]fmtprintf format string
[in]...list of arguments
Returns
bytes written, 0 on wrror

Definition at line 853 of file web.c.

MEMSPACE char* html_status ( int  status)

Convert html status into string using http_status table.

Parameters
[in]statushtml status number
Returns
http_status entry mathing status, or 500 Internal Server Error

Definition at line 1030 of file web.c.

Referenced by html_head(), and html_msg().

MEMSPACE char* http_value ( hinfo_t hi,
char *  str 
)

Lookup and argument name and return its value.

Parameters
[in]*hihinfo_t structure with arguments
[in]*strstring to lookup
Returns
argument value

Definition at line 1422 of file web.c.

MEMSPACE void init_hinfo ( hinfo_t hi)

Initilize hinfo_t structure

Parameters
[in]*hihinfo_t structure pointer to initialize
Returns
hinfo_t structure pointer to initialize

Definition at line 1055 of file web.c.

Referenced by parse_http_request().

MEMSPACE int is_cgitoken ( char *  str)

Does the string have a CGI TOKEN at the beginning ? CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-'.

/**

Returns
size of token or -1 on fail

Definition at line 2051 of file web.c.

MEMSPACE int is_cgitoken_char ( int  c)

test to see if a character is a valid member of the CGI token character set CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-'

Parameters
[in]ccharacter to test
Returns
1 if the character is a CGI token or 0 if not

Definition at line 1999 of file web.c.

MEMSPACE int is_header ( char *  str,
char **  p 
)

Does the string look like a header token with a ':' ?

Parameters
[in]*strstring to test
[in]**pstring pointer to set on match
Returns
1 if it looks like a header, otherwise 0

Definition at line 1451 of file web.c.

Referenced by parse_http_request().

MEMSPACE void led_off ( int  led)

Turn off virtual LED.

Parameters
[in]ledled to turn off
Returns
void

Definition at line 384 of file web.c.

MEMSPACE void led_on ( int  led)

Definition at line 374 of file web.c.

MEMSPACE int match_headers ( char *  str,
char **  p 
)

Match GET/POST message headers.

Parameters
[in]*strstring to patch
[in]**ppoints past matched string on sucess
Returns
header index or -1 on no match

Definition at line 1077 of file web.c.

Referenced by parse_http_request().

MEMSPACE char* memgets ( mem_t p)

Memory gets function We ASSUME we can replace any
with a \0.

Parameters
[in]pstructure with size and offset used by mem_gets()
Returns
pointer to string

Definition at line 952 of file web.c.

Referenced by parse_http_request().

MEMSPACE char* meminit ( mem_t p,
char *  ptr,
int  size 
)

in memory memory gets function

Parameters
[in]pstructure with size and offset used by mem_gets()
[in]ptrstart of memory area
[in]sizesize of memory area
Returns
pointer to start of memory area (ptr)

Definition at line 933 of file web.c.

Referenced by parse_http_request().

MEMSPACE char* mime_type ( int  type)

return strung pointer for mime type index

Parameters
[in]typeindex into mim_type table
Returns
pointer to string, if out of range uses index PTYPE_ERR

Definition at line 983 of file web.c.

Referenced by html_head(), and html_msg().

MEMSPACE char* next_arg ( hinfo_t hi)

Find next POST/GET argument We have to skip a name and a value.

Parameters
[in]*hihinfo_t structure with arguments
Returns
argument value pair

Definition at line 1247 of file web.c.

Referenced by http_value(), and parse_http_request().

MEMSPACE char* nextbreak ( char *  ptr)

Find next space or ? character.

Parameters
[in]*ptrstring to search
Returns
first space or ? character

Definition at line 1476 of file web.c.

Referenced by parse_http_request().

MEMSPACE int parse_http_request ( rwbuf_t p,
hinfo_t hi 
)

Get arguments for a GET or POST request.

Parameters
[in]*prwbuf_t pointer to socket buffer
[in]*hiheader structure of parsed result
Returns
0 on error, 1 on success

We must respond with Connection: keep-alive now

Definition at line 1535 of file web.c.

MEMSPACE char* process_args ( hinfo_t hi,
char *  ptr 
)

Process GET argments or POST message name/value data. HTML encoding is done in place often reducting the size of the result. Convert name=value pairs into null terminated strings. Names and values are each terminated with an EOS by replaceing '?', '&', '=' characters seen while scaning.

Parameters
[in]*hihinfo_t structure to fill
[in]*ptrGET arguments or PUT message body.
Returns
*ptr pointer that points to just past the header area.

Definition at line 1116 of file web.c.

Referenced by parse_http_request().

MEMSPACE int rewrite_cgi_token ( rwbuf_t p,
char *  src 
)

Replace CGI token with CGI result CGI tokens have the following syntax @ They start with "@_" and end with "_@" "@_" must be first two characters of string May have upper and lower case letters, numbers and '-'.

Parameters
[in]*psocket stream
[in]*strstring with token, example @
Returns
length of replaced text or 0 if no CGI handler was matched

Definition at line 2093 of file web.c.

MEMSPACE rwbuf_t* rwbuf_create ( void  )

Create socket read/write buffer for a connection.

Returns
void

Definition at line 526 of file web.c.

Referenced by create_connection().

MEMSPACE void rwbuf_delete ( rwbuf_t p)

Delete socket read/write buffers.

Parameters
[in]prwbuf_t pointer to buffer to delete
Returns
void

Definition at line 487 of file web.c.

Referenced by delete_connection(), and rwbuf_create().

MEMSPACE void rwbuf_rinit ( rwbuf_t p)

Initialize socket read status and read index.

Parameters
[in]prwbuf_t pointer
Returns
void

Definition at line 439 of file web.c.

Referenced by rwbuf_create(), rwbuf_delete(), and web_data_receive_callback().

MEMSPACE void rwbuf_winit ( rwbuf_t p)

Initialize socket send status and write index.

Parameters
[in]prwbuf_t pointer
Returns
void

Definition at line 453 of file web.c.

Referenced by rwbuf_create(), rwbuf_delete(), and web_data_sent_callback().

MEMSPACE int sock_printf ( rwbuf_t p,
const char *  fmt,
  ... 
)

sock_printf function

Parameters
[in]psocket buffer structure
[in]fmtprintf format string
[in]...list of arguments
Returns
bytes written

Definition at line 830 of file web.c.

Referenced by html_head().

MEMSPACE void u5toa ( char *  ptr,
uint16_t  num 
)

Print a decimal number into a string without an EOS.

Parameters
[in]*ptrbuffer to write number to
[in]numnumber to convet
Returns
void

Definition at line 1497 of file web.c.

Referenced by html_msg().

MEMSPACE int vsock_printf ( rwbuf_t p,
const char *  fmt,
va_list  va 
)

vsock_printf function

Parameters
[in]psocket buffer structure
[in]fmtprintf format string
[in]vava_list of arguments
Returns
bytes written

Definition at line 808 of file web.c.

Referenced by sock_printf().

MEMSPACE int wait_send ( rwbuf_t p)

Wait for buffer to send for this connection If write_buffer sending flag is set then wait for it to clear.

Parameters
[in]*prwbuf_t pointer for this socket buffer
Returns
sent buffer size or -1 on error

Definition at line 195 of file web.c.

Referenced by write_buffer(), and write_flush().

MEMSPACE void web_init ( int  port)

Setup WEB server and accept connections.

Parameters
[in]portport number to run web server on
Returns
void

Definition at line 2490 of file web.c.

Referenced by setup().

MEMSPACE void web_init_connections ( void  )

Definition at line 2475 of file web.c.

MEMSPACE void web_sep ( void  )

printer seperator

Returns
void

Definition at line 182 of file web.c.

Referenced by tcp_accept().

MEMSPACE void web_task ( void  )

Process ALL incoming HTTP requests.

See also
process_requests()
Returns
void

Definition at line 2441 of file web.c.

Referenced by loop_wrapper().

MEMSPACE int write_buffer ( rwbuf_t p)

Socket write buffer for this connection We wait for previous send to complete - then send any new data We do not wait for new data to finish sending - you use flush for that.

Parameters
[in]*prwbuf_t pointer for this socket buffer
Returns
size of data in buffer or -1 on error

Definition at line 266 of file web.c.

Referenced by write_flush().

MEMSPACE int write_byte ( rwbuf_t p,
int  c 
)

Write a byte (buffered) using the rwbuf_t socket buffers for this connection If the buffers are full the socket is written using write_flush.

Parameters
[in]*prwbuf_t pointer for this socket buffer
[in]1on success 0 on fail

Definition at line 339 of file web.c.

Referenced by _write_byte_fn(), write_len(), and write_str().

MEMSPACE int write_flush ( rwbuf_t p)

Write all outstanding data and wait for it to send.

Parameters
[in]*prwbuf_t pointer for this socket buffer
Returns
void

Definition at line 323 of file web.c.

Referenced by write_byte().

MEMSPACE void write_len ( rwbuf_t p,
char *  str,
int  len 
)

Write data using buffered write_byte function.

Parameters
[in]*prwbuf_t pointer for this socket buffer
[in]*strdata buffer to write
[in]lennumber of bytes to write
Returns
void

Definition at line 758 of file web.c.

Referenced by html_msg().

MEMSPACE void write_str ( rwbuf_t p,
char *  str 
)

Write string using buffered write_byte function.

Parameters
[in]*prwbuf_t pointer for this socket buffer
[in]*str0 terminated string to write
Returns
void

Definition at line 774 of file web.c.