• Stars
    star
    160
  • Rank 234,703 (Top 5 %)
  • Language
    C
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pure C Asynchronous HTTP/IO library providing websockets, SSL, routing, reverse proxy.

IWNET

Pure C asynchronous HTTP framework providing websockets client/server, SSL, reverse proxy and routing.

Works on Linux, macOS, FreeBSD

Build from sources

Prerequisites

  • Linux, macOS or FreeBSD
  • CMake 3.12 or greater
  • gcc or clang compiler
  • GNU Make or Ninja

Building

git clone https://github.com/Softmotions/iwnet.git

mkdir -p ./iwnet/build && cd ./iwnet/build

cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_EXAMPLES=ON

make 

IWSTART

IWSTART is an automatic CMake initial project generator for C projects based on iowow / iwnet / ejdb2 libs.

https://github.com/Softmotions/iwstart

Used by

Asynchronous HTTP Framework

Examples

Simple echo server

  ./echo_http_server --ssl
 
  curl -k -XPUT -d'Hello' https://localhost:8080/echo
  Hello
  I'm an echo web server

echo_http_server.c

#include "iwn_wf.h"

#include <iowow/iwconv.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <errno.h>

static struct iwn_poller *poller;
static struct iwn_wf_ctx *ctx;

static void _on_signal(int signo) {
  fprintf(stderr, "\nExiting...\n");
  iwn_poller_shutdown_request(poller);
}

static int _handle_echo(struct iwn_wf_req *req, void *user_data) {
  fprintf(stderr, "Echo handler called\n");
  iwn_http_response_printf(req->http, 200, "text/plain", "%.*s\n%s\n",
                           (int) req->body_len, req->body, (char*) user_data);
  return IWN_WF_RES_PROCESSED;
}

int main(int argc, char *argv[]) {
  signal(SIGPIPE, SIG_IGN);
  if (  signal(SIGTERM, _on_signal) == SIG_ERR
     || signal(SIGINT, _on_signal) == SIG_ERR) {
    return EXIT_FAILURE;
  }

  iwrc rc = 0;
  bool ssl = false;
  int port = 8080;

  for (int i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "--ssl") == 0) {
      ssl = true;
    } else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc) {
      port = iwatoi(argv[i + 1]);
    }
  }
  RCC(rc, finish, iw_init());              // Init iowow runtime, logging, etc..
  RCC(rc, finish, iwn_wf_create(0, &ctx)); // Create web server context
  RCC(rc, finish, iwn_wf_route(&(struct iwn_wf_route) {
    .ctx = ctx,
    .pattern = "/echo",
    .handler = _handle_echo,
    .user_data = "I'm an echo web server",
    .flags = IWN_WF_PUT | IWN_WF_POST
  }, 0));

  RCC(rc, finish, iwn_poller_create(0, 0, &poller));

  struct iwn_wf_server_spec spec = {
    .listen = "localhost",
    .port   = port,
    .poller = poller,
  };
  if (ssl) {
    spec.ssl.private_key = "./server-eckey.pem";
    spec.ssl.private_key_len = -1;
    spec.ssl.certs = "./server-ecdsacert.pem";
    spec.ssl.certs_len = -1;
  }

  // Print out a routes configuration.
  iwn_wf_route_print(ctx->root, stderr);
  // Configure HTTP server.
  RCC(rc, finish, iwn_wf_server(&spec, ctx));
  fprintf(stderr,
          "\nOpen terminal and run:\n\tcurl -k -XPUT -d'Hello' %s://%s:%d\n",
          (ssl ? "https" : "http"),
          spec.listen,
          spec.port);

  // Start fds poller reactor.
  iwn_poller_poll(poller);

finish:
  iwn_poller_destroy(&poller);
  if (rc) {
    iwlog_ecode_error3(rc);
    return EXIT_FAILURE;
  } else {
    return EXIT_SUCCESS;
  }
}

Todo list REST API server

todolist_http_server.c

./todolist_http_server --ssl
0001 [root:*] 
0002   [/todo> ALL
0003     {create:/([a-zA-z]+[a-z0-9A-Z]*)] PUT
0004     {get:/([0-9]+)] GET
0005     {remove:/([0-9]+)] DELETE
0006     [list:*] GET
0007     [done:*] POST

 Create a new 'Hello' todo entry:
	curl -k -XPUT -d'Say Hello' https://localhost:8080/todo/Hello
	curl -k -XPUT -d'Say Hello' https://localhost:8080/todo/Hello?done=1

 List all todo list items:
	curl -k https://localhost:8080/todo

 Get task #1 details:
	curl -k https://localhost:8080/todo/1

 Remove task #2 from todo list:
	curl -k -XDELETE https://localhost:8080/todo/2

 Update done status of task #2:
	curl -k -XPOST -d'id=2&done=1' https://localhost:8080/todo

More examples

Real life large project - Wirow video-conferencing server

You may find many helpful code examples by looking into framework test code

License


MIT License

Copyright (c) 2012-2022 Softmotions Ltd <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

ejdb

๐Ÿ‚ EJDB2 โ€” Embeddable JSON Database engine C library. Simple XPath like query language (JQL). Websockets / Android / iOS / React Native / Flutter / Java / Dart / Node.js bindings. Docker image.
C
1,394
star
2

iowow

A C utility library and persistent key/value storage engine
C
268
star
3

ejdb-node

C++
37
star
4

ncms

Java CMS engine. Host and develop multiple websites inside a single instance through the GUI and benefit from features like A/B testing, affiliate tracking tools, and a high performance template engine with CSS stylesheets processing & scripts minification.
JavaScript
33
star
5

ejdb-python

Project archived in favor of ejdb2. EJDB Python binding
Python
28
star
6

sphinx_material_theme

Sphinx material theme. Based on http://materializecss.com framework
JavaScript
23
star
7

ejdb-java

Project archived in favor of ejdb2
Java
15
star
8

ejdb-csharp

Project archived in favor of ejdb2. EJDB C# .Net binding
C#
13
star
9

vim-dark-frost-theme

Dark Frost color theme for Vim
HTML
12
star
10

antfs

Python file selector based on Ant patterns
Python
5
star
11

spring-boot-starter-cayenne

Spring boot starter for Apache Cayenne
Java
4
star
12

ejdb-lua

Project archived in favor of ejdb2. EJDB Lua binding
C
4
star
13

json_at

Dart implementation of RFC 6901 JSON pointer
Dart
4
star
14

acdb

Clang's C/C++ compile_commands.json generator for Arduino projects
C
4
star
15

iwstart

Boilerplate generator for C projects based on iowow, iwnet, ejdb2 libraries
CMake
3
star
16

nwk-ejdb-address-book

Project archived in favor of ejdb2. Simple address book demo implemented with EJDB (http://ejdb.org) and NW.js (http://nwjs.io)
HTML
3
star
17

einstein-web

Einstein puzzle
JavaScript
3
star
18

ejdb-ruby

Project archived in favor of ejdb2. Ruby binding for EJDB database engine
C
3
star
19

nativescript-plugin-google-signin-button

A NativeScript plugin to create native Google sign-in button
TypeScript
2
star
20

piumarta-peg

C
2
star
21

einstein-mobile

JavaScript
2
star
22

EJDB2Swift

EJDB2 Swift binding for iOS OSX Linux
Swift
2
star
23

BearSSL

BearSSL fork with own patches
C
2
star
24

aws4

AWS API client library in pure C
C
2
star
25

safe_config

Fork of https://pub.dartlang.org/packages/safe_config
Dart
1
star
26

jsflex

Java
1
star
27

coc-class-css

Vim/Neovim CoC CSS class completion plugin
TypeScript
1
star
28

qx-softmotions

Reusable http://qooxdoo.org components used in various Softmotions projects
JavaScript
1
star
29

riot7

Riot.js 2.x integration with Framework7
JavaScript
1
star
30

softmotions-java-commons

Reusable java components used in various Softmotions projects
Java
1
star
31

dadata-sugesstions-angular

AngularJS DaData suggestions module
JavaScript
1
star