aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: d522bcf3be57983363cc0ebb76f3f2faef471846 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# libweb, a simple and lightweight web framework

`libweb` is a simple and lightweight implementation of a web server, written in
C99 plus POSIX.1-2008 extensions, that can be integrated into applications.

## Disclaimer

Intentionally, `libweb` does not share some of the philosophical views from the
[suckless project](https://suckless.org). However, it still strives towards
portability, minimalism, simplicity and efficiency.

## Features

- Small and portable HTTP/1.1 server implementation, with support for
`GET`, `HEAD`, `PUT` and `POST`.
- Provides a interface to set up user-defined callbacks depending on
the operation (see example below).
- Transport-agnostic implementation.
    - While a POSIX socket, TCP-based implementation is already
    provided, the HTTP interface can be mapped to any other reliable
    transport layer.
- Supports [`multiform/form-data`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST)
, which makes it useful to transfer multiple files with a single `POST`
request.
- [A library](include/libweb/html.h) to write HTML programmatically.

### TLS

In order to maintain simplicity and reduce the risk for security bugs, `libweb`
does **not** implement TLS support. Instead, this should be provided by a
reverse proxy, such as [`caddy`](https://caddyserver.com/).

### Root permissions

`libweb` does not require root permissions. So, in order to avoid the
risk for security bugs, **please do not run `libweb` as `root`**.

## Requirements

- A POSIX environment.
- [`dynstr`](https://gitea.privatedns.org/xavi/dynstr)
(provided as a `git` submodule).
- CMake (optional).

### Ubuntu / Debian

#### Mandatory packages

```sh
sudo apt install build-essential
```

#### Optional packages

```sh
sudo apt install cmake
```

### Alpine Linux

#### Mandatory packages

```sh
apk add make gcc musl-dev
```

#### Optional packages

```sh
apk add cmake
```

## How to use
### Build

Two build environments are provided for `libweb` - feel free to choose any of
them:

- A mostly POSIX-compliant [`Makefile`](Makefile).
- A [`CMakeLists.txt`](CMakeLists.txt).

`libweb` can be built using the standard build process:

#### Make

```sh
$ make
```

This would generate a static library, namely `libweb.a`, on the project
top-level directory. Applications can then call the top-level `Makefile` by
the use of recursive `make`. For example, assuming `libweb` is contained on a
subdirectory:

```make
libweb/libweb.a:
    +cd libweb && $(MAKE)
```

Additionally, `libweb` can be installed using the `install` target. A
custom prefix can be assigned via the `PREFIX` variable:

```sh
$ make PREFIX=$HOME/libweb-prefix install
```

By default, `PREFIX` is assigned to `/usr/local`.

#### CMake

```sh
$ mkdir build/
$ cd build/
$ cmake ..
$ cmake --build .
```

A CMake target, also called `libweb`, is created. This makes it possible
to integrate `libweb` into CMake projects via `add_subdirectory` and
`target_link_libraries`. For example:

```cmake
project(example)
add_executable(${PROJECT_NAME} main.c)
add_subdirectory(libweb)
target_link_libraries(${PROJECT_NAME} PRIVATE web)
```

Additionally, `libweb` can be installed using the standard procedure
in CMake. As usual, a custom prefix can be assigned via the
`CMAKE_INSTALL_PREFIX` variable:

```sh
$ cmake --install build/ -DCMAKE_INSTALL_PREFIX=$HOME/libweb-prefix
```

### Examples

[A directory](examples) with examples shows how `libweb` can be used by
applications. These can be built from the top-level directory with:

```sh
$ make examples
```

In the case of CMake builds, examples are built by default. This can be turned
off by assigning `BUILD_EXAMPLES` to `OFF` or `0`:

```sh
$ mkdir build/
$ cd build/
$ cmake .. -DBUILD_EXAMPLES=OFF
$ cmake --build .
```

## Why this project?

Originally, `libweb` was part of the
[`slcl`](https://gitea.privatedns.org/xavi/slcl) project, a lightweight
cloud solution also written in C99 plus POSIX extensions. However, there
always was a clear separation between application logic and the underlying
HTTP/1.1 server implementation and other surrounding utilities.

Therefore, it made sense to keep all these components on a separate
repository that `slcl` could depend on. Additionally, this would also
benefit other applications interested in this implementation.

### Seriously, why _yet another_ new HTTP/1.1 implementation?

- Popular web server implementations, such as
[`apache`](https://httpd.apache.org/) or [`nginx`](https://nginx.net) are
standalone applications that can be configured to run other
applications in order to generate dynamic content, via a standard
interface called
[Common Gateway Interface](https://en.wikipedia.org/wiki/Common_Gateway_Interface)
, or CGI for short.
    - However, those are vastly complex tools with many features and
    options, whereas simplicity was one of the key design goals for
    `slcl`.
    - Additionally, tools such as `apache` or `nginx` place
    configuration files into `/etc`, which makes it harder to run
    multiple instances on the same machine. While not a strict
    requirement from `slcl`, it was desirable to keep configuration as
    simple as possible for administrators.
- The [`onion`](https://github.com/davidmoreno/onion) project, which
does follow the HTTP library concept, was initially considered for
`slcl`, but has a larger scope than `libweb`, and again simplicity was
essential for `slcl`.
- And, after all, it was a good excuse to learn about HTTP/1.1.

## License

```
libweb, a simple and lightweight web framework.
Copyright (C) 2023-2026 libweb contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
```

Also, see [`LICENSE`](LICENSE).