Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rxi/sandbird
A tiny embeddable HTTP server written in C89
https://github.com/rxi/sandbird
Last synced: 30 days ago
JSON representation
A tiny embeddable HTTP server written in C89
- Host: GitHub
- URL: https://github.com/rxi/sandbird
- Owner: rxi
- License: mit
- Created: 2015-10-17T16:47:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-10-27T11:12:58.000Z (about 5 years ago)
- Last Synced: 2024-11-05T16:49:20.344Z (about 1 month ago)
- Language: C
- Homepage:
- Size: 21.5 KB
- Stars: 176
- Watchers: 10
- Forks: 27
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-embedded-software - sandbird - Tiny (800sloc) embeddable HTTP server written in C89. (Protocols / Web Server)
README
# Sandbird
A tiny (~800sloc) embeddable HTTP server written in C89, compatible with Linux,
OSX and Windows.## Getting started
Sandbird consists of two files: [sandbird.c](src/sandbird.c?raw=1) and
[sandbird.h](src/sandbird.h?raw=1). Both of these files should be dropped into
an existing project and compiled along with it. Sandbird works asynchronously
and does not use threads, an event handler is invoked when an event -- such as
a request -- occurs.
```c
static int event_handler(sb_Event *e) {
if (e->type == SB_EV_REQUEST) {
sb_send_status(e->stream, 200, "OK");
sb_send_header(e->stream, "Content-Type", "text/plain");
sb_writef(e->stream, "Hello world");
}
return SB_RES_OK;
}
```The server is initialised by setting up a `sb_Options` struct and passing a
pointer to it to `sb_new_server()`. At a minimum the `port` and `handler`
fields of the `sb_Options` struct should be set.
```c
sb_Server *srv;
sb_Options opt;memset(&opt, 0, sizeof(opt));
opt.port = "8000";
opt.handler = event_handler;srv = sb_new_server(&opt);
```If `sb_new_server()` encounters an error, `NULL` is returned -- in this case it
is most likely the socket could not be bound. A non-NULL return value means the
server started successfully and is listening for connections.The poll function must be called on the server object regularly so that it can
handle new and existing connections. The poll function accepts a timeout value
in milliseconds -- if this is set to `0` then the function will not block.
```c
for (;;) {
sb_poll_server(srv, 1000);
}
```When we are done with the server the `sb_close_server()` function can be called
on it. This will close the server's listening socket and free any resources
which were being used.
```c
sb_close_server(srv);
```Some simple example programs can be found in the [example/](example/)
directory.## License
This library is free software; you can redistribute it and/or modify it under
the terms of the MIT license. See [LICENSE](LICENSE) for details.