HTTP Server Middleware Library
Overview

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™

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_http_server_t httpSer_1;
cy_network_interface_t nw_interface_1;
cy_socket_sockaddr_t IP_address_1;
void snippet_http_non_secure_create(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_1.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_1.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_1.object = (void *) &IP_address_1;
nw_interface_1.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_1, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_1 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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
/* Configure the following credentials for a secure HTTP connection */
/* SSL server certificate */
#define SSL_SERVERCERT_PEM \
"-----BEGIN CERTIFICATE-----\n" \
"........base64 data........\n" \
"-----END CERTIFICATE-----"
/* SSL server private key */
#define SSL_SERVERKEY_PEM \
"-----BEGIN RSA PRIVATE KEY-----\n" \
"..........base64 data..........\n" \
"-----END RSA PRIVATE KEY-----"
cy_http_server_t httpSer_2;
cy_network_interface_t nw_interface_2;
cy_socket_sockaddr_t IP_address_2;
void snippet_http_secure_create(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
security_cred.certificate = (uint8_t*) SSL_SERVERCERT_PEM;
security_cred.certificate_length = strlen(SSL_SERVERCERT_PEM);
security_cred.private_key = (uint8_t*) SSL_SERVERKEY_PEM;
security_cred.key_length = strlen(SSL_SERVERKEY_PEM);
security_cred.root_ca_certificate = NULL;
security_cred.root_ca_certificate_length = 0;
/* IP address of the server */
IP_address_2.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_2.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_2.object = (void *) &IP_address_2;
nw_interface_2.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_2, HTTPS_PORT, MAX_SOCKETS, &security_cred, &httpSer_2 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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
/* HTML page sent in response to the client */
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_http_server_t httpSer_3;
cy_network_interface_t nw_interface_3;
cy_socket_sockaddr_t IP_address_3;
void snippet_http_static_reg_resource(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_3.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_3.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_3.object = (void *) &IP_address_3;
nw_interface_3.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_3, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_3 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Register a static resource with the HTTP server */
cy_resource_static_data_t static_resource;
static_resource.data = CAPTIVE_PORTAL_REDIRECT_PAGE;
static_resource.length = sizeof( CAPTIVE_PORTAL_REDIRECT_PAGE );
TestRes = cy_http_server_register_resource( httpSer_3, (uint8_t*) "/get", (uint8_t*)"text/html", CY_STATIC_URL_CONTENT, &static_resource);
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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
/* HTML page sent in response to the client */
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_http_server_t httpSer_4;
cy_network_interface_t nw_interface_4;
cy_socket_sockaddr_t IP_address_4;
static char buffer[BUFFER_LENGTH];
static int length = 0;
int32_t process_handler( const char* url_path, const char* url_parameters, cy_http_response_stream_t* stream,
void* arg, cy_http_message_body_t* http_message_body )
{
memset(buffer, 0, BUFFER_LENGTH);
memcpy( buffer, http_message_body->data, http_message_body->data_length);
length += http_message_body->data_length;
if (http_message_body->data_remaining == 0)
{
cy_http_server_response_stream_write_payload(stream, CAPTIVE_PORTAL_REDIRECT_PAGE, sizeof(CAPTIVE_PORTAL_REDIRECT_PAGE));
length = 0;
memset(buffer, 0, BUFFER_LENGTH);
}
return 0;
}
void snippet_http_dynamic_reg_resource(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_4.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_4.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_4.object = (void *) &IP_address_4;
nw_interface_4.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_4, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_4 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Register a dynamic resource with the HTTP server */
cy_resource_dynamic_data_t dynamic_resource;
dynamic_resource.resource_handler = process_handler;
dynamic_resource.arg = NULL;
TestRes = cy_http_server_register_resource( httpSer_4, (uint8_t*) "/post", (uint8_t*)"text/html", CY_DYNAMIC_URL_CONTENT, &dynamic_resource);
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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_http_server_t httpSer_5;
cy_network_interface_t nw_interface_5;
cy_http_response_stream_t* http_event_stream_5;
cy_socket_sockaddr_t IP_address_5;
int32_t anycloud_process_SSE_handler( const char* url_path, const char* url_parameters,
cy_http_response_stream_t* stream, void* arg,
cy_http_message_body_t* http_message_body )
{
http_event_stream_5 = stream;
/* Enable chunked transfer encoding on the HTTP stream */
cy_http_server_response_stream_write_header( http_event_stream_5, CY_HTTP_200_TYPE, CHUNKED_CONTENT_LENGTH,
CY_HTTP_CACHE_DISABLED, MIME_TYPE_TEXT_EVENT_STREAM );
/* Disable chunked transfer encoding on the HTTP stream after header write is completed */
return 0;
}
void snippet_http_server_event_reg(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_5.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_5.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_5.object = (void *) &IP_address_5;
nw_interface_5.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_5, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_5 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
cy_resource_dynamic_data_t dynamic_sse_resource;
dynamic_sse_resource.resource_handler = anycloud_process_SSE_handler;
dynamic_sse_resource.arg = NULL;
TestRes = cy_http_server_register_resource( httpSer_5, (uint8_t*) "/events", (uint8_t*)"text/event-stream", CY_RAW_DYNAMIC_URL_CONTENT, &dynamic_sse_resource);
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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
/* HTML page sent in response to the client */
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_http_server_t httpSer_6;
cy_network_interface_t nw_interface_6;
cy_socket_sockaddr_t IP_address_6;
void snippet_http_server_start(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_6.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_6.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_6.object = (void *) &IP_address_6;
nw_interface_6.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_6, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_6 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Register a static resource with the HTTP server */
cy_resource_static_data_t static_resource;
static_resource.data = CAPTIVE_PORTAL_REDIRECT_PAGE;
static_resource.length = sizeof( CAPTIVE_PORTAL_REDIRECT_PAGE );
TestRes = cy_http_server_register_resource( httpSer_6, (uint8_t*) "/get", (uint8_t*)"text/html", CY_STATIC_URL_CONTENT, &static_resource );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Start HTTP server */
TestRes = cy_http_server_start( httpSer_6 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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_http_response_stream_t* http_event_stream_7;
cy_http_server_t httpSer_7;
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,
cy_http_response_stream_t* stream, void* arg,
cy_http_message_body_t* http_message_body )
{
http_event_stream_7 = stream;
cy_http_server_response_stream_write_header( http_event_stream_7, CY_HTTP_200_TYPE, CHUNKED_CONTENT_LENGTH,
CY_HTTP_CACHE_DISABLED, MIME_TYPE_TEXT_EVENT_STREAM );
return 0;
}
void send_events()
{
cy_rslt_t result;
if( http_event_stream_7 == NULL )
{
return;
}
result = cy_http_server_response_stream_write_payload( http_event_stream_7, (const void*)EVENT_STREAM_DATA, sizeof( EVENT_STREAM_DATA ) - 1 );
if ( result != CY_RSLT_SUCCESS )
{
http_event_stream_7 = NULL;
return;
}
/* Send message to client */
result = cy_http_server_response_stream_write_payload( http_event_stream_7, SSE_PAYLOAD, sizeof(SSE_PAYLOAD) - 1);
if ( result != CY_RSLT_SUCCESS )
{
http_event_stream_7 = NULL;
return;
}
/* SSE is ended with two line feeds */
result = cy_http_server_response_stream_write_payload( http_event_stream_7, (const void*)LFLF, sizeof( LFLF ) - 1 );
if ( result != CY_RSLT_SUCCESS )
{
http_event_stream_7 = NULL;
return;
}
return;
}
void snippet_http_server_send_events(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_7.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_7.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_7.object = (void *) &IP_address_7;
nw_interface_7.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_7, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_7 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
cy_resource_dynamic_data_t dynamic_sse_resource;
dynamic_sse_resource.resource_handler = process_SSE_handler;
dynamic_sse_resource.arg = NULL;
TestRes = cy_http_server_register_resource( httpSer_7, (uint8_t*) "/events", (uint8_t*)"text/event-stream", CY_RAW_DYNAMIC_URL_CONTENT, &dynamic_sse_resource);
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Start HTTP server */
TestRes = cy_http_server_start( httpSer_7 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Send server events */
if( http_event_stream_7 != NULL )
{
send_events();
}
/* Disconnect event stream */
TestRes = cy_http_server_response_stream_disconnect( http_event_stream_7 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Disconnect all streams */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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
/* HTML page sent in response to the client */
#define CAPTIVE_PORTAL_REDIRECT_PAGE \
"<html><head>" \
"Hello, This is a reference HTTP server application" \
"</head></html>"
cy_http_server_t httpSer_8;
cy_network_interface_t nw_interface_8;
cy_socket_sockaddr_t IP_address_8;
void snippet_http_server_stop(void)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(void)TestRes;
/* IP address of the server */
IP_address_8.ip_address.ip.v4 = SERVER_IP_ADDRESS;
IP_address_8.ip_address.version = CY_SOCKET_IP_VER_V4;
/* Add the IP_address information to the network interface object */
nw_interface_8.object = (void *) &IP_address_8;
nw_interface_8.type = CY_NW_INF_TYPE_WIFI;
/* Initialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
TestRes = cy_http_server_create( &nw_interface_8, HTTP_PORT, MAX_SOCKETS, NULL, &httpSer_8 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Register a static resource with the HTTP server */
cy_resource_static_data_t static_resource;
static_resource.data = CAPTIVE_PORTAL_REDIRECT_PAGE;
static_resource.length = sizeof( CAPTIVE_PORTAL_REDIRECT_PAGE );
cy_http_server_register_resource( httpSer_8, (uint8_t*) "/get", (uint8_t*)"text/html", CY_STATIC_URL_CONTENT, &static_resource );
/* Start HTTP server */
TestRes = cy_http_server_start( httpSer_8 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Stop the HTTP server */
TestRes = cy_http_server_stop( httpSer_8 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Delete the HTTP server object */
TestRes = cy_http_server_delete( httpSer_8 );
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
/* Deinitialize the network socket */
if( TestRes != CY_RSLT_SUCCESS )
{
/* Failure path */
}
}
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)
{
/* Status variables for various operations */
cy_rslt_t TestRes = CY_RSLT_ERROR;
(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;
/* Check if the provided key-value pair is present in the given URL */
TestRes = cy_http_server_match_query_parameter( url, key, value );
if( TestRes != CY_RSLT_SUCCESS )
{
/* URL query failed */
}
/* Retrieve the value and length for a given key from the URL */
TestRes = cy_http_server_get_query_parameter_value( url, key, &value_found, &value_length );
if( TestRes != CY_RSLT_SUCCESS )
{
/* URL query failed */
}
/* Get the number of query parameters found in the URL query string. */
TestRes = cy_http_server_get_query_parameter_count( url, &query_param_count );
if( TestRes != CY_RSLT_SUCCESS )
{
/* URL query failed */
}
}
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.