.. _http-server-sample: HTTP Server ########### Overview ******** The HTTP Server sample application for Zephyr implements a basic TCP server on top of the HTTP Server Library that is able to receive HTTP 1.1 requests, parse them and write back the responses. The source code for this sample application can be found at: :file:`samples/net/http_server`. Requirements ************ - Linux machine with wget and the screen terminal emulator - Either QEMU or real device like Freedom Board (FRDM-K64F) - For QEMU see this :ref:`networking_with_qemu` - LAN for testing purposes (Ethernet) Building and Running ******************** Currently, the HTTP Server application is configured to listen at port 80. If you want to modify the http-server sample application, please check the configuration settings in :file:`samples/net/http_server/src/main.c` file and also in the :file:`samples/net/http_server/src/config.h` file. To use QEMU for testing, follow the :ref:`networking_with_qemu` guide. This sample code supports both static and dynamic (DHCPv4) IP addresses that can be defined in the project configuration file: .. code-block:: console CONFIG_NET_APP_MY_IPV6_ADDR="2001:db8::1" CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1" Adding URLs =========== To define a new URL or to change how a URL is processed by the HTTP server, open the :file:`samples/net/http_server/src/main.c` file and locate the following lines: .. code-block:: c http_server_add_default(&http_urls, default_handler); http_server_add_url(&http_urls, "/headers", HTTP_URL_STANDARD); http_server_add_url(&http_urls, "/index.html", HTTP_URL_STANDARD); The first line defines how Zephyr will deal with unknown URLs. In this case, it will respond with a soft HTTP 404 status code, i.e. an HTTP 200 OK status code with a 404 Not Found HTML body. To build this sample on your Linux host computer, open a terminal window, locate the source code of this sample application and type: .. zephyr-app-commands:: :zephyr-app: samples/net/http_server :board: qemu_x86 :goals: run :compact: Sample Output ============= Assume in this example that this HTTP server is configured to listen at IPv4 address 192.168.1.120 and IPv6 address 2001:db8::1 port 80. On your Linux host computer, open a terminal window and type: .. code-block:: console wget 192.168.1.120/index.html wget will show: .. code-block:: console --2017-01-17 00:37:44-- http://192.168.1.120/ Connecting to 192.168.1.120:80... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html] Saving to: 'index.html' The HTML file generated by Zephyr and downloaded by wget is: .. code-block:: html