samples: sockets: Add a dumb HTTP server example
It's dumb, because it doesn't really parse HTTP request, just always sends the same page in response. Even such, it's useful for socket load testing with tools like Apache Bench (ab) and for regression checking in the net subsystem. Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
parent
d3b0cd4827
commit
ef39533365
9 changed files with 307 additions and 0 deletions
22
samples/net/sockets/dumb_http_server/Makefile
Normal file
22
samples/net/sockets/dumb_http_server/Makefile
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# Makefile - dumb socket-based http server
|
||||
|
||||
#
|
||||
# Copyright (c) 2017 Linaro Limited
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
BOARD ?= qemu_x86
|
||||
CONF_FILE ?= prj.conf
|
||||
|
||||
include $(ZEPHYR_BASE)/Makefile.inc
|
||||
|
||||
ifeq ($(CONFIG_NET_L2_BLUETOOTH), y)
|
||||
QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr
|
||||
else
|
||||
include $(ZEPHYR_BASE)/samples/net/common/Makefile.ipstack
|
||||
endif
|
||||
|
||||
# Support for bin to header conversion
|
||||
SRC = $(ZEPHYR_BASE)/samples/net/sockets/dumb_http_server/src
|
||||
include $(ZEPHYR_BASE)/scripts/Makefile.gen
|
||||
2
samples/net/sockets/dumb_http_server/Makefile.posix
Normal file
2
samples/net/sockets/dumb_http_server/Makefile.posix
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
socket_dumb_http: src/socket_dumb_http.c
|
||||
$(CC) $^ -o $@
|
||||
87
samples/net/sockets/dumb_http_server/README.rst
Normal file
87
samples/net/sockets/dumb_http_server/README.rst
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
.. _sockets-dumb-http-server-sample:
|
||||
|
||||
Socket Dumb HTTP Server
|
||||
#######################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
The sockets/dumb_http_server sample application for Zephyr implements a
|
||||
skeleton HTTP server using a BSD Sockets compatible API. The purpose of
|
||||
this sample is to show how it's possible to develop a sockets application
|
||||
portable to both POSIX and Zephyr. As such, this HTTP server example is
|
||||
kept very minimal and does not really parse an incoming HTTP request,
|
||||
just reads and discards it, and always serve a single static page. Even
|
||||
with such simplification, it is useful as an example of a socket
|
||||
application which can be accessed via a convention web browser, or to
|
||||
perform performance/regression testing using existing HTTP testing
|
||||
tools.
|
||||
|
||||
The source code for this sample application can be found at:
|
||||
:file:`samples/net/sockets/dumb_http_server`.
|
||||
|
||||
Requirements
|
||||
************
|
||||
|
||||
- :ref:`networking_with_qemu`
|
||||
- or, a board with hardware networking
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
Build the Zephyr version of the sockets/echo application like this:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ cd $ZEPHYR_BASE/samples/net/sockets/dumb_http_server
|
||||
$ make pristine
|
||||
$ make BOARD=<board_to_use>
|
||||
|
||||
``board_to_use`` defaults to ``qemu_x86``. In this case, you can run the
|
||||
application in QEMU using ``make run``. If you used another BOARD, you
|
||||
will need to consult its documentation for application deployment
|
||||
instructions. You can read about Zephyr support for specific boards in
|
||||
the documentation at :ref:`boards`.
|
||||
|
||||
After the sample starts, it expects connections at 192.0.2.1, port 8080.
|
||||
The easiest way to connect is by opening a following URL in a web
|
||||
browser: http://192.0.2.1:8080/ . You should see a page with content
|
||||
"Plain-text example.". Alternatively, a tool like ``curl`` can be used:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ curl http://192.0.2.1:8080/
|
||||
Plain-text example.
|
||||
|
||||
Finally, you can run an HTTP profiling/load tool like Apache Bench
|
||||
(``ab``) against the server:
|
||||
|
||||
$ ab -n10 http://192.0.2.1:8080/
|
||||
|
||||
``-n`` parameter specified the number of HTTP requests to issue against
|
||||
a server.
|
||||
|
||||
Running application on POSIX Host
|
||||
=================================
|
||||
|
||||
The same application source code can be built for a POSIX system, e.g.
|
||||
Linux. (Note: if you look at the source, you will see that the code is
|
||||
the same except the header files are different for Zephyr vs POSIX.)
|
||||
|
||||
To build for a host POSIX OS:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ make -f Makefile.posix
|
||||
|
||||
To run:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ ./socket_dumb_http
|
||||
|
||||
To test, connect to http://127.0.0.1:8080/ , and follow the steps in the
|
||||
previous section.
|
||||
|
||||
As can be seen, the behavior of the application is the same as the Zephyr
|
||||
version.
|
||||
26
samples/net/sockets/dumb_http_server/prj.conf
Normal file
26
samples/net/sockets/dumb_http_server/prj.conf
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# General config
|
||||
CONFIG_NEWLIB_LIBC=y
|
||||
|
||||
# Networking config
|
||||
CONFIG_NETWORKING=y
|
||||
CONFIG_NET_IPV4=y
|
||||
CONFIG_NET_IPV6=n
|
||||
CONFIG_NET_TCP=y
|
||||
CONFIG_NET_SOCKETS=y
|
||||
CONFIG_NET_SOCKETS_POSIX_NAMES=y
|
||||
|
||||
# Network driver config
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
|
||||
# Without CONFIG_NET_BUF_LOG printf() doesn't work
|
||||
CONFIG_NET_BUF_LOG=y
|
||||
|
||||
# Network address config
|
||||
CONFIG_NET_APP_SETTINGS=y
|
||||
CONFIG_NET_APP_NEED_IPV4=y
|
||||
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
|
||||
CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
|
||||
|
||||
# Network debug config
|
||||
#CONFIG_NET_DEBUG_SOCKETS=y
|
||||
CONFIG_SYS_LOG_NET_LEVEL=2
|
||||
8
samples/net/sockets/dumb_http_server/sample.yaml
Normal file
8
samples/net/sockets/dumb_http_server/sample.yaml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
sample:
|
||||
description: BSD Sockets API dumb HTTP server example
|
||||
name: socket_dumb_http_server
|
||||
tests:
|
||||
- test:
|
||||
min_ram: 32
|
||||
build_only: true
|
||||
tags: net
|
||||
9
samples/net/sockets/dumb_http_server/src/Makefile
Normal file
9
samples/net/sockets/dumb_http_server/src/Makefile
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
generate_inc_file += \
|
||||
response_small.html.bin \
|
||||
response_big.html.bin
|
||||
|
||||
include $(ZEPHYR_BASE)/scripts/Makefile.gen
|
||||
|
||||
include $(ZEPHYR_BASE)/samples/net/common/Makefile.common
|
||||
|
||||
obj-y += socket_dumb_http.o
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
HTTP/1.0 200 OK
|
||||
Content-Type: text/html; charset=utf-8
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<style>
|
||||
body {
|
||||
background-color: #f3f5f6;
|
||||
font-family: "sans-serif";
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<p style="color: red">
|
||||
Warning: this is a content sample. Proceed to
|
||||
<a href="https://www.zephyrproject.org/about/what-is-zephyr-project">Zephyr About page</a>
|
||||
for up to date information.
|
||||
</p>
|
||||
|
||||
<h1>What is Zephyr™ Project?</h1>
|
||||
|
||||
<table width="50%">
|
||||
<tr><td>
|
||||
<p>
|
||||
The Zephyr™ Project, is a Linux Foundation hosted Collaboration Project, an open source collaborative effort uniting leaders from across the industry to build a best-in-breed small, scalable, real-time operating system (RTOS) optimized for resource constrained devices, across multiple architectures. The Zephyr Project’s goal is to establish a neutral project where silicon vendors, OEMs, ODMs, ISVs, and OSVs can contribute technology to reduce the cost and accelerate time to market for developing the billions of devices that will make up the majority of the Internet of Things
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The Zephyr Project is perfect for building simple connected sensors, LED wearables, up to modems and small IoT wireless gateways. Because the Zephyr OS is modular and supports multiple architectures, developers are able to easily tailor an optimal solution to meet their needs. As a true open source project, the community can evolve the project to support new hardware, developer tools, sensor and device drivers. Enhancements in security, device management capabilities, connectivity stacks and file systems can be easily implemented.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The Zephyr kernel is derived from Wind River's commercial VxWorks Microkernel Profile for VxWorks. Microkernel Profile has evolved over 20 years from DSP RTOS technology known as Virtuoso. The RTOS has been used in several commercial applications including satellites, military command and control communications, radar, telecommunications and image processing. The most recent example of the technology’s success is the successful Philae Landing on Comet Churyumov–Gerasimenko and the accompanying Rosetta Orbiter.
|
||||
</p>
|
||||
</td></tr>
|
||||
|
||||
</table>
|
||||
|
||||
</body></html>
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
HTTP/1.0 200 OK
|
||||
Content-Type: text/plain; charset=utf-8
|
||||
|
||||
Plain-text example.
|
||||
108
samples/net/sockets/dumb_http_server/src/socket_dumb_http.c
Normal file
108
samples/net/sockets/dumb_http_server/src/socket_dumb_http.c
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 2017 Linaro Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef __ZEPHYR__
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#else
|
||||
|
||||
#include <net/socket.h>
|
||||
#include <kernel.h>
|
||||
#include <net/net_app.h>
|
||||
|
||||
#include <net/buf.h>
|
||||
|
||||
#endif
|
||||
|
||||
static const char content[] = {
|
||||
#if 0
|
||||
#include "response_big.html.bin.inc"
|
||||
#else
|
||||
#include "response_small.html.bin.inc"
|
||||
#endif
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int serv;
|
||||
struct sockaddr_in bind_addr;
|
||||
|
||||
serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
||||
bind_addr.sin_family = AF_INET;
|
||||
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
bind_addr.sin_port = htons(8080);
|
||||
bind(serv, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
|
||||
|
||||
listen(serv, 5);
|
||||
|
||||
while (1) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_addr_len = sizeof(client_addr);
|
||||
char addr_str[32];
|
||||
int req_state = 0;
|
||||
const char *data;
|
||||
size_t len;
|
||||
|
||||
int client = accept(serv, (struct sockaddr *)&client_addr,
|
||||
&client_addr_len);
|
||||
inet_ntop(client_addr.sin_family, &client_addr.sin_addr,
|
||||
addr_str, sizeof(addr_str));
|
||||
printf("Connection from %s\n", addr_str);
|
||||
|
||||
/* Discard HTTP request (or otherwise client will get
|
||||
* connection reset error).
|
||||
*/
|
||||
while (1) {
|
||||
char c;
|
||||
|
||||
recv(client, &c, 1, 0);
|
||||
if (req_state == 0 && c == '\r') {
|
||||
req_state++;
|
||||
} else if (req_state == 1 && c == '\n') {
|
||||
req_state++;
|
||||
} else if (req_state == 2 && c == '\r') {
|
||||
req_state++;
|
||||
} else if (req_state == 3 && c == '\n') {
|
||||
break;
|
||||
} else {
|
||||
req_state = 0;
|
||||
}
|
||||
}
|
||||
|
||||
data = content;
|
||||
len = sizeof(content);
|
||||
while (len) {
|
||||
int sent_len = send(client, data, len, 0);
|
||||
|
||||
if (sent_len == -1) {
|
||||
printf("Error sending data to peer\n");
|
||||
break;
|
||||
}
|
||||
data += sent_len;
|
||||
len -= sent_len;
|
||||
}
|
||||
|
||||
close(client);
|
||||
printf("Connection from %s closed\n", addr_str);
|
||||
|
||||
#if defined(__ZEPHYR__) && defined(CONFIG_NET_BUF_POOL_USAGE)
|
||||
struct k_mem_slab *rx, *tx;
|
||||
struct net_buf_pool *rx_data, *tx_data;
|
||||
|
||||
net_pkt_get_info(&rx, &tx, &rx_data, &tx_data);
|
||||
printf("rx buf: %d, tx buf: %d\n",
|
||||
rx_data->avail_count, tx_data->avail_count);
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue