Skip to content

Cross-platform library which helps to develop web servers or frameworks.

License

Notifications You must be signed in to change notification settings

proj-2019/libsagui

 
 

Repository files navigation

CII Best Practices Language grade: C/C++ Build Status

Overview

Sagui is a cross-platform C library which helps to develop web servers or frameworks. Its core has been developed using the GNU libmicrohttpd, uthash, PCRE2, ZLib and GnuTLS, that's why it is so fast, compact and useful to run on embedded systems.

Features

  • Requests processing through:
    • Event-driven - single-thread + main loop + select/epoll.
    • Threaded - one thread per request.
    • Thread pool - thread pool + select/epoll.
  • High-performance path routing that supports:
    • Regular expressions using PCRE2 syntax.
    • Just-in-time optimization (JIT).
    • Binary search in path entry-points.
  • HTTP compression:
    • Deflate for static contents and streams compression.
    • Gzip for files compression.
  • HTTPS support:
    • TLS 1.3 through GnuTLS library.
  • Dual stack:
    • Single socket for IPv4 and IPv6 support.
  • Basic authentication:
    • For standard login using user name/password.
  • Upload/download streaming by:
    • Payload - for raw data transferring as JSON, XML and other.
    • File - for large data transferring as videos, images, binaries and so on.
  • Dynamic strings:
    • Makes it easy strings operations in C.
  • String map:
    • Fast key-value mapping.
  • And more:
    • Fields, parameters, cookies, headers under hash table structure.
    • Several callbacks for total library customization.

Examples

A minimal HTTP server example:

void req_cb(void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
    sg_httpres_send(res, "Hello world", "text/plain", 200);
}

int main() {
    struct sg_httpsrv *srv = sg_httpsrv_new(req_cb, NULL);
    sg_httpsrv_listen(srv, 8080, false);
    printf("Server running at http:https://localhost:%d\n", sg_httpsrv_port(srv));
    getchar();
    sg_httpsrv_free(srv);
    return 0;
}

The router support is isolated from the HTTP, so it can be used to route any path structure, for example:

void home_cb(void *cls, struct sg_route *route) {
    printf("Home\n");
}

void download_cb(void *cls, struct sg_route *route) {
    printf("Download\n");
}

int main() {
    struct sg_router *router;
    struct sg_route *routes = NULL;
    sg_routes_add(&routes, "/home", home_cb, NULL);
    sg_routes_add(&routes, "/download", download_cb, NULL);
    router = sg_router_new(routes);
    sg_router_dispatch(router, "/home", NULL);
    sg_routes_cleanup(&routes);
    sg_router_free(router);
    return 0;
}

lastly, putting everything together:

struct Holder {
    struct sg_httpreq *req;
    struct sg_httpres *res;
};

void route_home_cb(void *cls, struct sg_route *route) {
    struct Holder *holder = sg_route_user_data(route);
    sg_httpres_send(holder->res, "<html><body>Home</body></html>", "text/html", 200);
}

void route_download_cb(void *cls, struct sg_route *route) {
    struct Holder *holder = sg_route_user_data(route);
    sg_httpres_send(holder->res, "<html><body>Download</body></html>", "text/html", 200);
}

void req_cb(void *cls, struct sg_httpreq *req, struct sg_httpres *res) {
    struct sg_router *router = cls;
    struct Holder holder = {req, res};
    if (sg_router_dispatch(router, sg_httpreq_path(req), &holder) != 0)
        sg_httpres_send(res, "<html><body>404</body></html>", "text/html", 404);
}

int main() {
    struct sg_route *routes = NULL;
    struct sg_router *router;
    struct sg_httpsrv *srv;
    sg_routes_add(&routes, "/home", route_home_cb, NULL);
    sg_routes_add(&routes, "/download", route_download_cb, NULL);
    router = sg_router_new(routes);
    srv = sg_httpsrv_new(req_cb, router);
    sg_httpsrv_listen(srv, 8080, false);
    printf("Server running at http:https://localhost:%d\n", sg_httpsrv_port(srv));
    getchar();
    sg_httpsrv_free(srv);
    sg_routes_cleanup(&routes);
    sg_router_free(router);
    return 0;
}

There are other examples available in the examples/ directory.

Versioning

Starting from the version 1.0.0, Sagui follows the SemVer rules regarding API changes with backwards compatibility and stable ABI across major releases.

Licensing

Sagui is released under GNU Lesser General Public License v3.0. Check the LICENSE file for more details.

GNU Lesser General Public License v3.0

Documentation

The documentation has been written in Doxygen and is available in HTML and PDF.

Downloading

All stable releases are available for download at the releases page. For Windows, the packages libsagui-N.N.N-dll.zip (and their respective GPG signature) contains the compiled DLLs for 32 and 64 bits. For other systems, the packages Source code (tar.gz|zip) contains the library source.

Building/installing

Check the BUILD.md for instructions to build the library, examples, tests and documentation, then, follow the steps in INSTALL.md to install the library from sources on your system.

Compatibility

A typical upgrade of the Sagui library does not break the ABI at all. Take a look at the API/ABI compatibility report to compare most recent library versions.

See also Checking backward API/ABI compatibility of Sagui library versions.

Projects using Sagui

  • Brook framework - Pascal framework which helps to develop web applications.

Contributing

Sagui is totally open source and would not be possible without our contributors. If you want to submit contributions, please fork the project on GitHub and send a pull request. You retain the copyright on your contributions. If you have questions, open a new issue at the issues page. For donations to support this project, please click the botton below.

Support this project

Support

This project is completely self-explanatory, but, if you need a consulting service to integrate it on your project, contact us.

About

Cross-platform library which helps to develop web servers or frameworks.

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 87.6%
  • CMake 12.2%
  • CSS 0.2%