Giter Club home page Giter Club logo

http-c's Introduction

http-c

This is a short, hopefully comprehensible, HTTP server written in C intended to be used as example code for students.

Some notable characteristics about this implementation:

  • The code has been written to run on Linux, using epoll (learn about select, poll, kqueue, io_uring somewhere else.)
  • This is an implementation of HTTP 1.0
  • This is a single-process, single-threaded, co-operative implementation, we wont be using fork, pthreads, tracing gc's nor any off-brand continouations library.
  • We are using c11 (c17) not c98.
  • We are not hiding any business logic behind macros or global variables.

Requirements

  • bear
  • clang

Building

Use Bear to generate a compile_commands.json this is used by clangd for development.

bear -- clang -std=c17  -I./http-c/src  -o ./httpc-server ./src/main.c

If you do not want to install bear or configure clangd then running the cc command which is given to the bear command will compile the example for you

Example

#include <stdio.h>
#include "httpc.h"

struct httpc_response_t index(struct httpc_request_t *req)
{
    struct httpc_response_t response = httpc_response_ok();

    size_t n_hit = (size_t)(req->context);
    req->context = (void *)(n_hit + 1);

    uint8_t *buf = calloc(1024, sizof(uint8_t));
    int n_trunc = snprintf(buf, 1024, "Hello, World! %d", n_hit);

    response->body = httpc_response_string_owned(buf);

    return response;
}

int main()
{
    struct httpc_server_t server = {0};
    if (httpc_server_new(8080, &server) != 0)
    {
        perror("fatal: failed to create server");
        return EXIT_FAILURE;
    }

    size_t hits = 0;
    httpc_server_add_route_with_context("/", &index, &hits);

    for (;;)
    {
        if (httpc_server_await_once(&server, 1) != 0) // timeout is 1 millisecond.
        {
            perror("fatal: while waiting on httpc server to handle I/O");
            httpc_server_close(&server);
            return EXIT_FAILURE;
        }
    }

    httpc_server_close(&server);

    return EXIT_SUCCESS;
}

http-c's People

Contributors

mental32 avatar

Stargazers

Jamar Powell avatar Federico Damián Schonborn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.