NetBurner 3.5.6
PDF Version
HTML Cookie

NetBurner HTML Cookie Example

Overview

The NetBurner HTML Cookie Example is an embedded web application that demonstrates HTTP cookie handling capabilities on NetBurner devices. This application showcases how to set, retrieve, and display cookies in a web browser through an embedded web server.

Three web pages are created: an index page, a page to set the cookie value to "MyCookie", and a page to view the cookie value.

  • Select the "set cookie" link to set the cookie. Note that if you select "show cookie" before setting the value, no value will be displayed.
  • Select the "show cookie" link to view the cookie

A cookie will apply to a "site". In this example a "site" will be the IP address of your NetBurner device. The cookie will be sent as part of the HTML header for any page requested from the "site".

Application Structure

Core Files

  • main.cpp - Main application entry point and network initialization
  • webfuncs.cpp - Web server functionality and cookie handling logic
  • index.html - Web interface with dynamic content display

Main Application (main.cpp)

The main application initializes the NetBurner network stack and starts the web server:

  • Initializes the network stack using init()
  • Enables system diagnostics for debugging
  • Starts HTTP server on default port 80
  • Waits for DHCP network configuration (5 second timeout)
  • Displays application information and NNDK revision
  • Enters main loop with 1-second delays
void UserMain(void *pd)
{
init(); // Initialize network stack
StartHttp(); // Start web server, default port 80
WaitForActiveNetwork(TICKS_PER_SECOND * 5); // Wait for DHCP address
iprintf("Application: %s\r\nNNDK Revision: %s\r\n", AppName, GetReleaseTag());
while (1)
{
OSTimeDly(TICKS_PER_SECOND * 1);
}
}
#define TICKS_PER_SECOND
System clock ticks per second.
Definition constants.h:49
const char * GetReleaseTag()
Returns the NNDK release tag information.
void StartHttp(uint16_t port, bool RunConfigMirror)
Start the HTTP web server. Further documentation in the Initialization section Initialization - Syste...
void init()
System initialization. Ideally called at the beginning of all applications, since the easiest Recover...
void EnableSystemDiagnostics()
Turn on the diagnostic reports from the config page.
bool WaitForActiveNetwork(uint32_t ticks_to_wait=120 *TICKS_PER_SECOND, int interface=-1)
Wait for an active network connection on at least one interface.

Cookie Handling Features

Cookie Display (ShowCookie)

The application includes a function that demonstrates how to parse and display cookies from HTTP requests:

  • Parses HTTP headers to locate "Cookie:" keyword
  • Extracts cookie data from the request
  • Displays cookie content to the requesting socket
  • Handles cases where no cookie is present

Cookie Setting (setCookieGetReqCallback)

The application includes a callback function that sets cookies:

  • Intercepts GET requests to setcookie.html
  • Sends HTTP headers with cookie data ("MyCookie ")
  • Serves the appropriate HTML page
  • Takes complete responsibility for the HTTP response

Technical Implementation

Cookie Parsing Logic

The ShowCookie function implements a simple cookie parser:

  1. Receives the URL and HTTP header data
  2. Searches for the "Cookie:" string in the HTTP headers
  3. Extracts the cookie value until a space character is found
  4. Writes the cookie data back to the client socket

HTTP GET Handler

The application uses NetBurner's callback system to handle specific web requests:

CallBackFunctionPageHandler hIndex("setcookie.html", // Web page to intercept
setCookieGetReqCallback, // Pointer to callback function
tGet, // Type of request, GET
0, // Password level, none
true); // Take responsibility for entire response
Implements the HtmlPageHandler class as a function pointer callback for GET requests.
Definition http.h:326
@ tGet
GET request.
Definition http.h:37

Network Configuration

DHCP Setup

  • Automatically obtains IP address via DHCP
  • Waits up to 5 seconds for network activation
  • Reports network status to debug console

Web Server

  • HTTP server runs on standard port 80
  • Serves static HTML files and dynamic content
  • Handles both GET requests and cookie operations

Usage Instructions

  1. Setup: Deploy the application to a NetBurner device
  2. Network: Ensure the device is connected to a network with DHCP
  3. Access: Open a web browser and navigate to the device's IP address
  4. Set Cookie: Navigate to setcookie.html to set a cookie
  5. View Cookie: Navigate to showcookie.html to display the cookie value

Development Notes

Function Declarations

  • Cookie handling functions must be declared as extern "C" to prevent C++ name mangling
  • This ensures proper linkage with the NetBurner web server system

Debug Output

  • The application outputs debug information to the console
  • URL requests and cookie operations are logged for development purposes

Memory Management

  • Uses stack-based string parsing for cookie extraction
  • No dynamic memory allocation for cookie handling
  • Relies on NetBurner's built-in HTTP handling infrastructure

Dependencies

  • NetBurner NNDK (NetBurner Network Development Kit)
  • Standard NetBurner libraries:

Security Considerations

  • Cookie data is transmitted in plain text
  • No authentication or authorization mechanisms
  • Suitable for development and demonstration purposes
  • Production use should implement additional security measures

Troubleshooting

Common Issues

  1. Network Connection: Ensure DHCP is available and device is connected
  2. Browser Cache: Clear browser cache if cookie behavior seems inconsistent
  3. Debug Output: Monitor console output for network and cookie operation status

Debug Information

The application provides debug output including:

  • Application name and NNDK revision
  • Network status and IP address assignment
  • HTTP request URLs and cookie operations