Microchip® Advanced Software Framework

netconn_http_stats_example/network/httpserver/httpd.c File Reference

Httpd server.

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

#include <string.h>
#include "lwip/opt.h"
#include "lwip/tcpip.h"
#include "fs.h"
#include "httpd.h"
#include "cgi.h"

Macros

#define HTTP_DEFAULT_PAGE   "index.html"
 

Functions

const char * get_ext (const char *name)
 Get filename extension type. 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_request (void *pvParameters)
 Core HTTP server function processing the request. More...
 
static int http_searchContentType (const char *name)
 Match content type based on filename extension. More...
 
void http_sendInternalErr (struct netconn *client, int content_type)
 Send HTTP internal server error code. More...
 
void http_sendOk (struct netconn *client, int content_type)
 Send the HTTP header with the appropriated content type. More...
 
int http_tokenizeGetRequest (char *raw_buf, size_t raw_len)
 Tokenize a buffer. More...
 

Variables

struct {
   const char *   content
 
   const char *   key
 
http_content_type []
 
static const char http_html_hdr_200 [] = "HTTP/1.0 200 OK\r\n"
 
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 []
 
BoardStatus status
 

#define HTTP_DEFAULT_PAGE   "index.html"

Referenced by http_request().

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

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.

References http_decodeUrl().

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_request ( void *  pvParameters)
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_request().

void http_sendInternalErr ( struct netconn *  client,
int  content_type 
)

Send HTTP internal server error code.

References content, http_content_type, and http_html_hdr_500.

Referenced by http_request().

void http_sendOk ( struct netconn *  client,
int  content_type 
)

Send the HTTP header with the appropriated content type.

Parameters
clientSocket to send data to.
content_typeIndex in the http_content_type table.

References content, http_content_type, and http_html_hdr_200.

Referenced by cgi_chipInfo(), cgi_status(), and http_request().

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.

const char* content
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"},
}

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

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

Referenced by http_sendOk().

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

Referenced by http_request().

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>"

Referenced by http_request().

const char* key

Referenced by http_searchContentType(), and main().

BoardStatus status