This library provides the HTTP Server implementation that can work on Infineon MCUs with Wi-Fi connectivity.It supports RESTful methods such as GET, PUT, and POST for the client to communicate with this HTTP Server library.
Features
- Supports Wi-Fi and Ethernet connections.
- Supports HTTP/1.1 protocol version.
- Secure [with TLS security] and non-secure modes of connection.
- Supports RESTful HTTP methods: GET, PUT, and POST.
- Handles various resource content types such as HTML, Plain, and JSON.
- Capable of handling content payload greater than the MTU size using the Content-Length HTTP header. This feature is supported only for
CY_RAW_DYNAMIC_URL_CONTENT
and CY_DYNAMIC_URL_CONTENT
content types
- Supports chunked encoding for GET and POST methods.
Note: For a POST request, chunked encoding is supported only for the data that is less than a single MTU; Content-Length headers are recommended for larger data.
* Supports Server-Sent Events (SSE). SSE is a server push technology, enabling an HTTP client (for example, a browser or any device running an HTTP client) to receive automatic updates from the HTTP server via the HTTP connection.
Supported Platforms
ModusToolbox™
Supported Frameworks
- ModusToolbox™ environment: In this environment the HTTP Server Library uses the abstraction-rtos library that provides the RTOS abstraction API and uses the secure-sockets library for implementing socket functions.
Quick Start
This library is supported on ModusToolbox™ framework and below section provides information on how to build this library in ModusToolbox™ framework.
ModusToolbox™
- To use http-server library with Wi-Fi kits on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull http-server library and wifi-core-freertos-lwip-mbedtls library which will internally pull secure-sockets, wifi-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull wifi-core-freertos-lwip-mbedtls and http-server libraries create the following *.mtb* files in deps folder.
- To use http-server library with Ethernet kits on FreeRTOS, lwIP, and Mbed TLS combination, the application should pull http-server library and ethernet-core-freertos-lwip-mbedtls library which will internally pull secure-sockets, ethernet-connection-manager, FreeRTOS, lwIP, Mbed TLS and other dependent modules. To pull ethernet-core-freertos-lwip-mbedtls and http-server libraries create the following *.mtb* files in deps folder.
- Review and make the required changes to the pre-defined configuration files.
- The configuration files are bundled with the wifi-core-freertos-lwip-mbedtls library for FreeRTOS, lwIP, and Mbed TLS. See README.md for more details.
- If the application is using bundle library then the configuration files are in the bundle library. For example if the application is using Wi-Fi core freertos lwip mbedtls bundle library, the configuration files are in
wifi-core-freertos-lwip-mbedtls/configs
folder. Similarly if the application is using Ethernet Core FreeRTOS lwIP mbedtls library, the configuration files are in ethernet-core-freertos-lwip-mbedtls/configs
folder.
- Define following COMPONENTS in the application's Makefile for the HTTP Server Library. For additional information, see the "Quick Start" section in README.md.
COMPONENTS=FREERTOS MBEDTLS LWIP SECURE_SOCKETS
- HTTP Server Library disables all the debug log messages by default. To enable log messages, the application must perform the following:
- Add
ENABLE_HTTP_SERVER_LOGS
macro to the DEFINES in the code example's Makefile. The Makefile entry would look like as follows: DEFINES+=ENABLE_HTTP_SERVER_LOGS
- Call the
cy_log_init()
function provided by the cy-log module. cy-log is part of the connectivity-utilities library. See connectivity-utilities library API documentation for cy-log details.
- Define the following macro in application's Makefile to configure the maximum number of HTTP server resources to 'N':
#define MAX_NUMBER_OF_HTTP_SERVER_RESOURCES
Max number of resources supported by the HTTP server.
Definition: cy_http_server.h:124
- Define the following macro in application's Makefile to configure the socket receive timeout in 'N' milliseconds. Default value is set to 10ms.
#define HTTP_SERVER_SOCKET_RECEIVE_TIMEOUT
Socket receive timeout in milliseconds.
Definition: cy_http_server.h:131
Code Snippets
This section provides code snippets for this library on ModusToolbox™ framework. The code snippets given under this section uses C APIs.
- ModusToolbox™ - Snippets for HTTP Server create, start, stop, and delete (C implementation).
ModusToolbox™
These code snippets demonstrate the initialization of the configuration structures required for the operation such as creating a secure or non-secure connection and registering a static or dynamic resource with the HTTP server.
Code Snippet 1: Creation and Initialization of Handle for Non-Secure HTTP Server
Creates an HTTP server handle for a non-secure HTTP connection using the cy_http_server_create API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
cy_network_interface_t nw_interface_1;
cy_socket_sockaddr_t IP_address_1;
void snippet_http_non_secure_create(void)
{
(void)TestRes;
IP_address_1.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_1.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_1.object = (void *) &IP_address_1;
nw_interface_1.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_create(cy_network_interface_t *interface, uint16_t port, uint16_t max_connection, cy_https_server_security_info_t *security_info, cy_http_server_t *server_handle)
Creates a HTTP server instance and initializes its members based on the supplied arguments.
cy_rslt_t cy_http_server_network_init(void)
One-time initialization function for network sockets implementation.
#define CY_RSLT_ERROR
HTTP server generic error.
Definition: cy_http_server.h:117
void * cy_http_server_t
HTTP server handle.
Definition: cy_http_server.h:249
Code Snippet 2: Creation and Initialization of Handle for Secure HTTP Server
Creates an HTTP server handle for a secure HTTP connection using the cy_http_server_create API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTPS_PORT 443
#define MAX_SOCKETS 2
#define SSL_SERVERCERT_PEM \
"-----BEGIN CERTIFICATE-----\n" \
"........base64 data........\n" \
"-----END CERTIFICATE-----"
#define SSL_SERVERKEY_PEM \
"-----BEGIN RSA PRIVATE KEY-----\n" \
"..........base64 data..........\n" \
"-----END RSA PRIVATE KEY-----"
cy_network_interface_t nw_interface_2;
cy_socket_sockaddr_t IP_address_2;
void snippet_http_secure_create(void)
{
(void)TestRes;
security_cred.
certificate = (uint8_t*) SSL_SERVERCERT_PEM;
security_cred.
private_key = (uint8_t*) SSL_SERVERKEY_PEM;
security_cred.
key_length = strlen(SSL_SERVERKEY_PEM);
IP_address_2.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_2.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_2.object = (void *) &IP_address_2;
nw_interface_2.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
HTTP server security info.
Definition: cy_http_server.h:293
uint16_t root_ca_certificate_length
Root CA certificate length excluding 'null' termination character.
Definition: cy_http_server.h:299
uint16_t certificate_length
HTTP server certificate length excluding 'null' termination character.
Definition: cy_http_server.h:297
uint8_t * private_key
HTTP server private key (base64 encoded)
Definition: cy_http_server.h:294
uint8_t * certificate
HTTP server certificate.
Definition: cy_http_server.h:296
uint16_t key_length
HTTP server private key length excluding 'null' termination character.
Definition: cy_http_server.h:295
uint8_t * root_ca_certificate
Root CA certificate to verify client certificate.
Definition: cy_http_server.h:298
Code Snippet 3: Registering a Static Resource
Registers a static resource with the HTTP server using the cy_http_server_register_resource API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_network_interface_t nw_interface_3;
cy_socket_sockaddr_t IP_address_3;
void snippet_http_static_reg_resource(void)
{
(void)TestRes;
IP_address_3.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_3.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_3.object = (void *) &IP_address_3;
nw_interface_3.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
static_resource.
data = CAPTIVE_PORTAL_REDIRECT_PAGE;
static_resource.
length =
sizeof( CAPTIVE_PORTAL_REDIRECT_PAGE );
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_register_resource(cy_http_server_t server_handle, uint8_t *url, uint8_t *mime_type, cy_url_resource_type url_resource_type, void *resource_data)
Used to register a resource(static/dynamic) with the HTTP server.
@ CY_STATIC_URL_CONTENT
Page is constant data in memory-addressable area.
Definition: cy_http_server.h:237
Static HTTP resource info.
Definition: cy_http_server.h:311
uint32_t length
The length in bytes of the page/file.
Definition: cy_http_server.h:313
const void * data
A pointer to the data for the page/file resource.
Definition: cy_http_server.h:312
Code Snippet 4: Registering a Dynamic Resource
Registers a dynamic resource with the HTTP server using the cy_http_server_register_resource API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
#define BUFFER_LENGTH 1500
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_network_interface_t nw_interface_4;
cy_socket_sockaddr_t IP_address_4;
static char buffer[BUFFER_LENGTH];
static int length = 0;
{
memset(buffer, 0, BUFFER_LENGTH);
{
length = 0;
memset(buffer, 0, BUFFER_LENGTH);
}
return 0;
}
void snippet_http_dynamic_reg_resource(void)
{
(void)TestRes;
IP_address_4.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_4.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_4.object = (void *) &IP_address_4;
nw_interface_4.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
dynamic_resource.
arg = NULL;
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_response_stream_write_payload(cy_http_response_stream_t *stream, const void *data, uint32_t length)
Writes data to the HTTP stream.
@ CY_DYNAMIC_URL_CONTENT
Page is dynamically generated by a url_processor_t type function.
Definition: cy_http_server.h:238
HTTP message structure that gets passed to dynamic URL processor functions.
Definition: cy_http_server.h:258
uint16_t data_length
Data length in current packet
Definition: cy_http_server.h:260
const uint8_t * data
Packet data in message body
Definition: cy_http_server.h:259
uint32_t data_remaining
Data yet to be consumed
Definition: cy_http_server.h:261
Context structure for HTTP server stream Users should not access these values - they are provided her...
Definition: cy_http_server.h:273
Dynamic HTTP resource info.
Definition: cy_http_server.h:304
void * arg
Argument to be passed to the generator function
Definition: cy_http_server.h:306
url_processor_t resource_handler
The function that will handle requests for this page.
Definition: cy_http_server.h:305
Code Snippet 5: Registering a Server-Sent Event (SSE)
Registers a Server-Sent Event using the cy_http_server_register_resource API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
#define CHUNKED_CONTENT_LENGTH 0
cy_network_interface_t nw_interface_5;
cy_socket_sockaddr_t IP_address_5;
int32_t anycloud_process_SSE_handler( const char* url_path, const char* url_parameters,
{
http_event_stream_5 = stream;
return 0;
}
void snippet_http_server_event_reg(void)
{
(void)TestRes;
IP_address_5.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_5.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_5.object = (void *) &IP_address_5;
nw_interface_5.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
dynamic_sse_resource.
arg = NULL;
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_response_stream_disable_chunked_transfer(cy_http_response_stream_t *stream)
Disables chunked transfer encoding on the HTTP stream.
cy_rslt_t cy_http_server_response_stream_enable_chunked_transfer(cy_http_response_stream_t *stream)
Enables chunked transfer encoding on the HTTP stream.
cy_rslt_t cy_http_server_response_stream_write_header(cy_http_response_stream_t *stream, cy_http_status_codes_t status_code, uint32_t content_length, cy_http_cache_t cache_type, cy_http_mime_type_t mime_type)
Writes HTTP header to the HTTP stream provided.
@ CY_RAW_DYNAMIC_URL_CONTENT
Same as CY_DYNAMIC_URL_CONTENT, but the HTTP header must be supplied as part of the content.
Definition: cy_http_server.h:241
@ CY_HTTP_CACHE_DISABLED
Do not cache previously fetched resources.
Definition: cy_http_server.h:189
@ CY_HTTP_200_TYPE
OK.
Definition: cy_http_server.h:214
Code Snippet 6: HTTP Server Start
Starts the HTTP server using the cy_http_server_start API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_network_interface_t nw_interface_6;
cy_socket_sockaddr_t IP_address_6;
void snippet_http_server_start(void)
{
(void)TestRes;
IP_address_6.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_6.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_6.object = (void *) &IP_address_6;
nw_interface_6.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
static_resource.
data = CAPTIVE_PORTAL_REDIRECT_PAGE;
static_resource.
length =
sizeof( CAPTIVE_PORTAL_REDIRECT_PAGE );
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_start(cy_http_server_t server_handle)
Starts a HTTP server daemon (web server)
Code Snippet 7: Sending HTTP Server Events
Sends HTTP server events using the cy_http_server_response_stream_write_header API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
#define SSE_PAYLOAD "Hello, This is server sent events \r\n"
#define LFLF "\n\n"
#define EVENT_STREAM_DATA "data: "
#define CHUNKED_CONTENT_LENGTH 0
cy_network_interface_t nw_interface_7;
cy_socket_sockaddr_t IP_address_7;
int32_t process_SSE_handler( const char* url_path, const char* url_parameters,
{
http_event_stream_7 = stream;
return 0;
}
void send_events()
{
cy_rslt_t result;
if( http_event_stream_7 == NULL )
{
return;
}
if ( result != CY_RSLT_SUCCESS )
{
http_event_stream_7 = NULL;
return;
}
if ( result != CY_RSLT_SUCCESS )
{
http_event_stream_7 = NULL;
return;
}
if ( result != CY_RSLT_SUCCESS )
{
http_event_stream_7 = NULL;
return;
}
return;
}
void snippet_http_server_send_events(void)
{
(void)TestRes;
IP_address_7.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_7.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_7.object = (void *) &IP_address_7;
nw_interface_7.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
dynamic_sse_resource.
arg = NULL;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( http_event_stream_7 != NULL )
{
send_events();
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_response_stream_disconnect(cy_http_response_stream_t *stream)
Queues a disconnect request to the HTTP server.
cy_rslt_t cy_http_server_response_stream_disconnect_all(cy_http_server_t server_handle)
Disconnects all the HTTP streams associated with the given server.
Code Snippet 8: HTTP Server Stop
Stops the HTTP server using the cy_http_server_stop API function.
#define MAKE_IPV4_ADDRESS(a, b, c, d) ((((uint32_t) d) << 24) | \
(((uint32_t) c) << 16) | \
(((uint32_t) b) << 8) |\
((uint32_t) a))
#define SERVER_IP_ADDRESS MAKE_IPV4_ADDRESS(192, 168, 18, 9)
#define HTTP_PORT 80
#define MAX_SOCKETS 2
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_network_interface_t nw_interface_8;
cy_socket_sockaddr_t IP_address_8;
void snippet_http_server_stop(void)
{
(void)TestRes;
IP_address_8.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_8.ip_address.version = CY_SOCKET_IP_VER_V4;
nw_interface_8.object = (void *) &IP_address_8;
nw_interface_8.type = CY_NW_INF_TYPE_WIFI;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
static_resource.
data = CAPTIVE_PORTAL_REDIRECT_PAGE;
static_resource.
length =
sizeof( CAPTIVE_PORTAL_REDIRECT_PAGE );
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_delete(cy_http_server_t server_handle)
Deletes the given HTTP server instance and resources allocated for the instance by the cy_http_server...
cy_rslt_t cy_http_server_network_deinit(void)
One-time deinitialization function for Secure Sockets implementation.
cy_rslt_t cy_http_server_stop(cy_http_server_t server_handle)
Stops a HTTP server daemon (web server) Before calling this API function, API cy_http_server_start mu...
Code Snippet 9: HTTP URL query
Search a key-value pair, fetch the value of a key, and get the number of queries from the given URL using URL-processing functions.
void snippet_http_server_url_query(void)
{
(void)TestRes;
const char url[] = "https://example.com/?product=shirt&color=blue&newuser&size=m\n";
const char key[] = "product\n";
const char value[] = "shirt\n";
char *value_found = NULL;
uint32_t value_length = 0;
uint32_t query_param_count = 0;
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
cy_rslt_t cy_http_server_get_query_parameter_count(const char *url_query, uint32_t *count)
Returns the number of parameters found in the URL query string.
cy_rslt_t cy_http_server_match_query_parameter(const char *url_query, const char *parameter_key, const char *parameter_value)
Checks whether the given parameter key-value pair is present in the given URL query.
cy_rslt_t cy_http_server_get_query_parameter_value(const char *url_query, const char *parameter_key, char **parameter_value, uint32_t *value_length)
Searches for a parameter (key-value pair) in a URL query string and returns a pointer to the value.