Skip to content

Commit ef39533

Browse files
pfalconAnas Nashif
authored andcommitted
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>
1 parent d3b0cd4 commit ef39533

File tree

9 files changed

+307
-0
lines changed

9 files changed

+307
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Makefile - dumb socket-based http server
2+
3+
#
4+
# Copyright (c) 2017 Linaro Limited
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
9+
BOARD ?= qemu_x86
10+
CONF_FILE ?= prj.conf
11+
12+
include $(ZEPHYR_BASE)/Makefile.inc
13+
14+
ifeq ($(CONFIG_NET_L2_BLUETOOTH), y)
15+
QEMU_EXTRA_FLAGS = -serial unix:/tmp/bt-server-bredr
16+
else
17+
include $(ZEPHYR_BASE)/samples/net/common/Makefile.ipstack
18+
endif
19+
20+
# Support for bin to header conversion
21+
SRC = $(ZEPHYR_BASE)/samples/net/sockets/dumb_http_server/src
22+
include $(ZEPHYR_BASE)/scripts/Makefile.gen
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
socket_dumb_http: src/socket_dumb_http.c
2+
$(CC) $^ -o $@
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
.. _sockets-dumb-http-server-sample:
2+
3+
Socket Dumb HTTP Server
4+
#######################
5+
6+
Overview
7+
********
8+
9+
The sockets/dumb_http_server sample application for Zephyr implements a
10+
skeleton HTTP server using a BSD Sockets compatible API. The purpose of
11+
this sample is to show how it's possible to develop a sockets application
12+
portable to both POSIX and Zephyr. As such, this HTTP server example is
13+
kept very minimal and does not really parse an incoming HTTP request,
14+
just reads and discards it, and always serve a single static page. Even
15+
with such simplification, it is useful as an example of a socket
16+
application which can be accessed via a convention web browser, or to
17+
perform performance/regression testing using existing HTTP testing
18+
tools.
19+
20+
The source code for this sample application can be found at:
21+
:file:`samples/net/sockets/dumb_http_server`.
22+
23+
Requirements
24+
************
25+
26+
- :ref:`networking_with_qemu`
27+
- or, a board with hardware networking
28+
29+
Building and Running
30+
********************
31+
32+
Build the Zephyr version of the sockets/echo application like this:
33+
34+
.. code-block:: console
35+
36+
$ cd $ZEPHYR_BASE/samples/net/sockets/dumb_http_server
37+
$ make pristine
38+
$ make BOARD=<board_to_use>
39+
40+
``board_to_use`` defaults to ``qemu_x86``. In this case, you can run the
41+
application in QEMU using ``make run``. If you used another BOARD, you
42+
will need to consult its documentation for application deployment
43+
instructions. You can read about Zephyr support for specific boards in
44+
the documentation at :ref:`boards`.
45+
46+
After the sample starts, it expects connections at 192.0.2.1, port 8080.
47+
The easiest way to connect is by opening a following URL in a web
48+
browser: http://192.0.2.1:8080/ . You should see a page with content
49+
"Plain-text example.". Alternatively, a tool like ``curl`` can be used:
50+
51+
.. code-block:: console
52+
53+
$ curl http://192.0.2.1:8080/
54+
Plain-text example.
55+
56+
Finally, you can run an HTTP profiling/load tool like Apache Bench
57+
(``ab``) against the server:
58+
59+
$ ab -n10 http://192.0.2.1:8080/
60+
61+
``-n`` parameter specified the number of HTTP requests to issue against
62+
a server.
63+
64+
Running application on POSIX Host
65+
=================================
66+
67+
The same application source code can be built for a POSIX system, e.g.
68+
Linux. (Note: if you look at the source, you will see that the code is
69+
the same except the header files are different for Zephyr vs POSIX.)
70+
71+
To build for a host POSIX OS:
72+
73+
.. code-block:: console
74+
75+
$ make -f Makefile.posix
76+
77+
To run:
78+
79+
.. code-block:: console
80+
81+
$ ./socket_dumb_http
82+
83+
To test, connect to http://127.0.0.1:8080/ , and follow the steps in the
84+
previous section.
85+
86+
As can be seen, the behavior of the application is the same as the Zephyr
87+
version.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# General config
2+
CONFIG_NEWLIB_LIBC=y
3+
4+
# Networking config
5+
CONFIG_NETWORKING=y
6+
CONFIG_NET_IPV4=y
7+
CONFIG_NET_IPV6=n
8+
CONFIG_NET_TCP=y
9+
CONFIG_NET_SOCKETS=y
10+
CONFIG_NET_SOCKETS_POSIX_NAMES=y
11+
12+
# Network driver config
13+
CONFIG_TEST_RANDOM_GENERATOR=y
14+
15+
# Without CONFIG_NET_BUF_LOG printf() doesn't work
16+
CONFIG_NET_BUF_LOG=y
17+
18+
# Network address config
19+
CONFIG_NET_APP_SETTINGS=y
20+
CONFIG_NET_APP_NEED_IPV4=y
21+
CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
22+
CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
23+
24+
# Network debug config
25+
#CONFIG_NET_DEBUG_SOCKETS=y
26+
CONFIG_SYS_LOG_NET_LEVEL=2
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sample:
2+
description: BSD Sockets API dumb HTTP server example
3+
name: socket_dumb_http_server
4+
tests:
5+
- test:
6+
min_ram: 32
7+
build_only: true
8+
tags: net
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
generate_inc_file += \
2+
response_small.html.bin \
3+
response_big.html.bin
4+
5+
include $(ZEPHYR_BASE)/scripts/Makefile.gen
6+
7+
include $(ZEPHYR_BASE)/samples/net/common/Makefile.common
8+
9+
obj-y += socket_dumb_http.o
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
HTTP/1.0 200 OK
2+
Content-Type: text/html; charset=utf-8
3+
4+
<html>
5+
<head>
6+
<style>
7+
body {
8+
background-color: #f3f5f6;
9+
font-family: "sans-serif";
10+
}
11+
</style>
12+
</head>
13+
14+
<body>
15+
16+
<p style="color: red">
17+
Warning: this is a content sample. Proceed to
18+
<a href="https://www.zephyrproject.org/about/what-is-zephyr-project">Zephyr About page</a>
19+
for up to date information.
20+
</p>
21+
22+
<h1>What is Zephyr™ Project?</h1>
23+
24+
<table width="50%">
25+
<tr><td>
26+
<p>
27+
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
28+
</p>
29+
30+
<p>
31+
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.
32+
</p>
33+
34+
<p>
35+
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.
36+
</p>
37+
</td></tr>
38+
39+
</table>
40+
41+
</body></html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
HTTP/1.0 200 OK
2+
Content-Type: text/plain; charset=utf-8
3+
4+
Plain-text example.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2017 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
9+
#ifndef __ZEPHYR__
10+
11+
#include <netinet/in.h>
12+
#include <sys/socket.h>
13+
#include <arpa/inet.h>
14+
#include <unistd.h>
15+
16+
#else
17+
18+
#include <net/socket.h>
19+
#include <kernel.h>
20+
#include <net/net_app.h>
21+
22+
#include <net/buf.h>
23+
24+
#endif
25+
26+
static const char content[] = {
27+
#if 0
28+
#include "response_big.html.bin.inc"
29+
#else
30+
#include "response_small.html.bin.inc"
31+
#endif
32+
};
33+
34+
int main(void)
35+
{
36+
int serv;
37+
struct sockaddr_in bind_addr;
38+
39+
serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
40+
41+
bind_addr.sin_family = AF_INET;
42+
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
43+
bind_addr.sin_port = htons(8080);
44+
bind(serv, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
45+
46+
listen(serv, 5);
47+
48+
while (1) {
49+
struct sockaddr_in client_addr;
50+
socklen_t client_addr_len = sizeof(client_addr);
51+
char addr_str[32];
52+
int req_state = 0;
53+
const char *data;
54+
size_t len;
55+
56+
int client = accept(serv, (struct sockaddr *)&client_addr,
57+
&client_addr_len);
58+
inet_ntop(client_addr.sin_family, &client_addr.sin_addr,
59+
addr_str, sizeof(addr_str));
60+
printf("Connection from %s\n", addr_str);
61+
62+
/* Discard HTTP request (or otherwise client will get
63+
* connection reset error).
64+
*/
65+
while (1) {
66+
char c;
67+
68+
recv(client, &c, 1, 0);
69+
if (req_state == 0 && c == '\r') {
70+
req_state++;
71+
} else if (req_state == 1 && c == '\n') {
72+
req_state++;
73+
} else if (req_state == 2 && c == '\r') {
74+
req_state++;
75+
} else if (req_state == 3 && c == '\n') {
76+
break;
77+
} else {
78+
req_state = 0;
79+
}
80+
}
81+
82+
data = content;
83+
len = sizeof(content);
84+
while (len) {
85+
int sent_len = send(client, data, len, 0);
86+
87+
if (sent_len == -1) {
88+
printf("Error sending data to peer\n");
89+
break;
90+
}
91+
data += sent_len;
92+
len -= sent_len;
93+
}
94+
95+
close(client);
96+
printf("Connection from %s closed\n", addr_str);
97+
98+
#if defined(__ZEPHYR__) && defined(CONFIG_NET_BUF_POOL_USAGE)
99+
struct k_mem_slab *rx, *tx;
100+
struct net_buf_pool *rx_data, *tx_data;
101+
102+
net_pkt_get_info(&rx, &tx, &rx_data, &tx_data);
103+
printf("rx buf: %d, tx buf: %d\n",
104+
rx_data->avail_count, tx_data->avail_count);
105+
#endif
106+
107+
}
108+
}

0 commit comments

Comments
 (0)