Initiated

This commit is contained in:
2019-02-23 21:06:34 +01:00
commit 4b137d7c55
38 changed files with 4378 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
/* ----------------------------------------------------------------------------
* aisl.h - header file for AISL library, part of AISLing Technology
*
* Copyright (c) 2017 by Löwenware Ltd. (https://lowenware.com/)
*
* Authors and maintainers:
* Ilja Kartaschoff <ik@lowenware.com>
*
* DOCUMENTATION
* This file is not designed to be used as a documentation, but for looking at
* the precise values of constants and definitions.
* Please, for documentation refer to web page https://lowenware.com/aisling/ or
* file READEME.md from library source package.
*
* LICENSE and DISCLAIMER
*
* -------------------------------------------------------------------------- */
#ifndef _AISL_H_
#define _AISL_H_
/* system includes ---------------------------------------------------------- */
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <arpa/inet.h>
/* aisl includes ------------------------------------------------------------ */
#include <aisl/status.h>
#include <aisl/event.h>
#include <aisl/stream.h>
#include <aisl/handle.h>
#include <aisl/http.h>
/* Control calls ------------------------------------------------------------ */
/* DEPRECATED, use aisl_handle_new instead
* */
aisl_status_t
aisl_init();
/* DEPRECATED, use aisl_handle_free instead
* */
void
aisl_release();
/* Tell library what socket should be opened. Could be called multiple times.
* This function only save passed data. In fact, sockets are being opened only
* inside aisl_run loop.
* @address : host or IP to listen
* @port : port to listen
* */
aisl_status_t
aisl_select(const char *address, int port);
/* Start main loop
* @result : exit code
* */
aisl_status_t
aisl_run( int * flags );
/* Event calls -------------------------------------------------------------- */
/* Add callback to be executed after timeout. If callback function will return
* true, callback will be kept in main loop and raised again, otherwise it will
* be removed
* @cb : callback function: bool callback (void * u_data)
* @usec : delay in milliseconds
* @data : user-defined data to be passed to callback
* */
aisl_status_t
aisl_delay(aisl_callback_t cb, uint32_t msec, void *u_data);
/* Add event listener
* @source : pointer to event source
* @e_id : event identifier
* @cb : callback to be executed
* */
aisl_status_t
aisl_listen(void *source, aisl_event_t e_id, aisl_callback_t cb);
/* Raise event
* @source : pointer to event source data
* @e_id : event identifier
* @... : custom event data
* @result : true if event was handled, false otherwise
* */
bool
aisl_raise(void *source, aisl_event_t e_id, ... );
/* input stream functions --------------------------------------------------- */
const char *
aisl_header_get(aisl_stream_t stream, const char *key);
const char *
aisl_header_get_by_index(aisl_stream_t stream, const char **key, uint32_t i);
/* -------------------------------------------------------------------------- */
#endif
+128
View File
@@ -0,0 +1,128 @@
#ifndef _AISL_EVENT_H_
#define _AISL_EVENT_H_
#include <stdbool.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <aisl/stream.h>
#include <aisl/status.h>
#include <aisl/http.h>
/* -------------------------------------------------------------------------- */
typedef unsigned int aisl_event_t;
/* -------------------------------------------------------------------------- */
typedef enum
{
AISL_SERVER_OPEN = 100,
AISL_SERVER_ERROR = 190,
AISL_CLIENT_CONNECT = 200,
AISL_CLIENT_DISCONNECT = 210,
AISL_CLIENT_TIMEOUT = 220,
AISL_STREAM_OPEN = 300, /* 5 - headers recieved */
AISL_STREAM_HEADER = 310, /* 5 - headers recieved */
AISL_STREAM_INPUT = 320, /* 6 - chunk of data transmission */
AISL_STREAM_REQUEST = 330, /* 7 - data received, response required */
AISL_STREAM_OUTPUT = 340,/* event for long-size responses optimal handling */
AISL_STREAM_CLOSE = 350,
AISL_STREAM_ERROR = 390, /* 8 - bad request */
AISL_EVENTS_CUSTOM = 999
} aisl_event_id_t;
/* -------------------------------------------------------------------------- */
/* AISL_SERVER_OPEN event handler */
typedef bool
(*aisl_server_open_t)( aisl_server_t server,
int flags );
/* AISL_SERVER_ERROR event handler */
typedef bool
(*aisl_server_error_t)( aisl_server_t server,
int flags,
const char * details );
/* AISL_CLIENT_CONNECT event handler */
typedef bool
(*aisl_client_connect_t)( aisl_server_t server,
aisl_client_t client );
/* AISL_CLIENT_DISCONNECT event handler */
typedef bool
(*aisl_client_disconnect_t)( aisl_server_t server,
aisl_client_t client );
/* AISL_CLIENT_DISCONNECT event handler */
typedef bool
(*aisl_client_timeout_t)( aisl_server_t server,
aisl_client_t client );
/* AISL_STREAM_OPEN event handler */
typedef bool
(*aisl_stream_open_t)( aisl_stream_t s,
aisl_http_method_t method,
const char * path,
const char * query );
typedef bool
(*aisl_stream_header_t)( aisl_stream_t s,
const char * key,
const char * value );
/* AISL_STREAM_INPUT event handler */
typedef bool
(*aisl_stream_input_t)( aisl_stream_t s,
char * data,
int len );
/* AISL_STREAM_REQUEST event handler */
typedef bool
(*aisl_stream_request_t)( aisl_stream_t s );
/* AISL_STREAM_OUTPUT event handler */
typedef bool
(*aisl_stream_output_t)( aisl_stream_t s,
uint32_t buffer_space );
typedef bool
(*aisl_stream_close_t)( aisl_stream_t s );
/* AISL_STREAM_ERROR event handler */
typedef bool
(*aisl_stream_error_t)( aisl_stream_t s,
const char * details );
/* CUSTOM event_handler */
typedef bool
(*aisl_custom_event_t)( void * source,
va_list vl );
/* on delay timeout */
typedef bool
(*aisl_delay_timeout_t)( void * u_data );
/* -------------------------------------------------------------------------- */
/* type for event callbacks to use in structures and function prototypes */
typedef bool
(* aisl_callback_t) (void);
/* cast callback as aisl_callback_t */
#define AISL_CALLBACK(x) ((aisl_callback_t) x)
/* -------------------------------------------------------------------------- */
const char *
aisl_event_get_text( aisl_event_t e_id );
/* -------------------------------------------------------------------------- */
#endif
+76
View File
@@ -0,0 +1,76 @@
#ifndef _AISL_HANDLE_H_
#define _AISL_HANDLE_H_
#include <aisl/status.h>
#include <aisl/event.h>
/* -------------------------------------------------------------------------- */
#define AISL_FLAG_SSL (1<<0)
/* -------------------------------------------------------------------------- */
typedef struct aisl_handle * aisl_handle_t;
/* -------------------------------------------------------------------------- */
aisl_handle_t
aisl_handle_new(size_t min_clients, size_t buffer_size);
/* -------------------------------------------------------------------------- */
aisl_handle_t
aisl_handle_from_stream( aisl_stream_t s );
/* -------------------------------------------------------------------------- */
void
aisl_handle_free( aisl_handle_t self );
/* -------------------------------------------------------------------------- */
aisl_status_t
aisl_bind( aisl_handle_t self, const char * address, int port, int flags );
/* -------------------------------------------------------------------------- */
aisl_status_t
aisl_set_ssl( aisl_handle_t self, const char * server_name,
const char * key_file,
const char * crt_file );
/* -------------------------------------------------------------------------- */
aisl_status_t
aisl_set_callback( aisl_handle_t self,
void * source,
aisl_event_t e_id,
aisl_callback_t cb );
/* -------------------------------------------------------------------------- */
bool
aisl_raise_event( aisl_handle_t self,
void * source,
aisl_event_t e_id,
... );
/* -------------------------------------------------------------------------- */
aisl_status_t
aisl_run_cycle( aisl_handle_t self );
/* -------------------------------------------------------------------------- */
const char *
aisl_handle_get_error( aisl_handle_t self );
/* -------------------------------------------------------------------------- */
int
aisl_sleep( aisl_handle_t self, unsigned long usec );
/* -------------------------------------------------------------------------- */
#endif
+104
View File
@@ -0,0 +1,104 @@
#ifndef _AISL_HTTP_H_
#define _AISL_HTTP_H_
/* -------------------------------------------------------------------------- */
typedef enum
{
AISL_HTTP_1_0,
AISL_HTTP_1_1,
AISL_HTTP_2_0
} aisl_http_version_t;
/* -------------------------------------------------------------------------- */
typedef enum {
AISL_HTTP_GET,
AISL_HTTP_PUT,
AISL_HTTP_POST,
AISL_HTTP_HEAD,
AISL_HTTP_TRACE,
AISL_HTTP_DELETE,
AISL_HTTP_OPTIONS,
AISL_HTTP_CONNECT,
AISL_HTTP_PRI
} aisl_http_method_t;
/* -------------------------------------------------------------------------- */
typedef enum
{
/* informational ------------------------------ */
AISL_HTTP_CONTINUE = 100,
AISL_HTTP_SWITCHING_PROTOCOLS,
/* Successful --------------------------------- */
AISL_HTTP_OK = 200,
AISL_HTTP_CREATED,
AISL_HTTP_ACCEPTED,
AISL_HTTP_NON_AUTHORITATIVE_INFORMATION,
AISL_HTTP_NO_CONTENT,
AISL_HTTP_RESET_CONTENT,
AISL_HTTP_PARTIAL_CONTENT,
/* redirection -------------------------------- */
AISL_HTTP_MULTIPLE_CHOICES = 300,
AISL_HTTP_MOVED_PERMANENTLY,
AISL_HTTP_FOUND,
AISL_HTTP_SEE_OTHER,
AISL_HTTP_NOT_MODIFIED,
AISL_HTTP_USE_PROXY,
AISL_HTTP_UNUSED,
AISL_HTTP_TEMPORARY_REDIRECT,
/* client error ------------------------------- */
AISL_HTTP_BAD_REQUEST = 400,
AISL_HTTP_UNAUTHORIZED,
AISL_HTTP_PAYMENT_REQUIRED,
AISL_HTTP_FORBIDDEN,
AISL_HTTP_NOT_FOUND,
AISL_HTTP_METHOD_NOT_ALLOWED,
AISL_HTTP_NOT_ACCEPTABLE,
AISL_HTTP_PROXY_AUTHENTICATION_REQUIRED,
AISL_HTTP_REQUEST_TIMEOUT,
AISL_HTTP_CONFLICT,
AISL_HTTP_GONE,
AISL_HTTP_LENGTH_REQUIRED,
AISL_HTTP_PRECONDITION_FAILED,
AISL_HTTP_REQUEST_ENTITY_TOO_LARGE,
AISL_HTTP_REQUEST_URI_TOO_LONG,
AISL_HTTP_UNSUPPORTED_MEDIA_TYPE,
AISL_HTTP_REQUESTED_RANGE_NOT_SATISFIABLE,
AISL_HTTP_EXPECTATION_FAILED,
/* server error ------------------------------- */
AISL_HTTP_INTERNAL_SERVER_ERROR = 500,
AISL_HTTP_NOT_IMPLEMENTED,
AISL_HTTP_BAD_GATEWAY,
AISL_HTTP_SERVICE_UNAVAILABLE,
AISL_HTTP_GATEWAY_TIMEOUT,
AISL_HTTP_VERSION_NOT_SUPPORTED
} aisl_http_response_t;
/* -------------------------------------------------------------------------- */
const char *
aisl_http_version_to_string(aisl_http_version_t version);
/* -------------------------------------------------------------------------- */
const char *
aisl_http_response_to_string(aisl_http_response_t code);
/* -------------------------------------------------------------------------- */
const char *
aisl_http_secure_to_string( int is_secure );
/* -------------------------------------------------------------------------- */
const char *
aisl_http_method_to_string( aisl_http_method_t method );
/* -------------------------------------------------------------------------- */
#endif
+24
View File
@@ -0,0 +1,24 @@
#ifndef _AISL_STATUS_H_
#define _AISL_STATUS_H_
/* -------------------------------------------------------------------------- */
typedef enum {
AISL_EXTCALL_ERROR = -3,
AISL_SYSCALL_ERROR = -2,
AISL_MALLOC_ERROR = -1,
AISL_SUCCESS = 0,
AISL_IDLE = 1
} aisl_status_t;
/* -------------------------------------------------------------------------- */
const char *
aisl_status_to_string(aisl_status_t status);
/* -------------------------------------------------------------------------- */
#endif
+172
View File
@@ -0,0 +1,172 @@
#ifndef _AISL_STREAM_H_
#define _AISL_STREAM_H_
#include <arpa/inet.h>
/* Library statuses */
/* HTTP requests */
#include <aisl/http.h>
#include <aisl/status.h>
/* -------------------------------------------------------------------------- */
typedef struct sockaddr_in * aisl_server_t;
typedef struct sockaddr_in * aisl_client_t;
struct aisl_stream
{
/* DO NOT USE PROPERTIES DIRECTLY IN NEW CODE */
struct sockaddr_in *client;
const char *host;
const char *path;
const char *query;
const char *scheme;
void *u_ptr; /* pointer to bind custom data to stream */
aisl_http_method_t request_method;
};
/* pointer to stream descriptor */
typedef struct aisl_stream * aisl_stream_t;
/* -------------------------------------------------------------------------- */
/* start response to client
* this function should be called before any header or content function
* necessary protocol headers as well as Content-Type and Content-Length
* will be set automaticaly
* @stream : stream instance
* @status : HTTP response status code (default 200)
* @content_type : string with content type (default text/html), NULL -> no
* @content_length : length of content, 0 = no content
* */
aisl_status_t
aisl_response(aisl_stream_t stream, aisl_http_response_t status_code,
const char *content_type,
uint32_t content_length);
/* send all buffered data to client
* ALL responses should always be finished with calling of this method
* @stream : stream instance
* */
aisl_status_t
aisl_flush(aisl_stream_t stream);
/* header functions --------------------------------------------------------- */
/* add custom header to stream
* this function should be called before content functions
* @stream : stream instance
* @key : header key string
* @value : header value string
* */
int
aisl_header(aisl_stream_t stream, const char *key, const char *value);
/* add custom header to stream
* this function should be called before content functions
* @stream : stream instance
* @key : header key string
* @f_value : value format string, same as for printf function
* @... : arguments according to f_value string
* */
int
aisl_header_printf(aisl_stream_t stream, const char *key,
const char *f_value, ...);
/* add custom header to stream
* this function should be called before content functions
* @stream : stream instance
* @key : header key string
* @f_value : value format string, same as for printf function
* @args : arguments macro according to f_value string
* */
int
aisl_header_vprintf(aisl_stream_t stream, const char *key,
const char *format,
va_list args);
/* data response functions -------------------------------------------------- */
/* response formated data to client
* @stream : stream instance
* @format : format string, same as for printf
* @... : arguments according to format string
* @result : number of responed bytes
* */
int
aisl_printf(aisl_stream_t stream, const char *format, ...);
/* response formated data to client
* @stream : stream instance
* @format : format string, same as for printf
* @args : arguments macro according to format string
* @result : number of responed bytes
* */
int
aisl_vprintf(aisl_stream_t stream, const char *format, va_list args);
/* response characters to client
* @stream : stream instance
* @data : characters to be sent
* @d_len : number of characters to send
* @result : number of responed bytes
* */
int
aisl_write(aisl_stream_t s, const char *data, int d_len);
/* response string to client
* @string : string to be sent
* @stream : stream instance
* @result : number of responed bytes
* */
int
aisl_puts(const char *string, aisl_stream_t stream);
/* -------------------------------------------------------------------------- */
void
aisl_cancel(aisl_stream_t s);
/* -------------------------------------------------------------------------- */
bool
aisl_is_secure(aisl_stream_t s);
/* -------------------------------------------------------------------------- */
void *
aisl_get_context(aisl_stream_t s);
/* -------------------------------------------------------------------------- */
void
aisl_set_context(aisl_stream_t s, void * u_ptr);
/* -------------------------------------------------------------------------- */
aisl_client_t
aisl_get_client(aisl_stream_t s);
/* -------------------------------------------------------------------------- */
aisl_server_t
aisl_get_server(aisl_stream_t s);
/* -------------------------------------------------------------------------- */
aisl_http_version_t
aisl_get_http_version(aisl_stream_t s);
/* -------------------------------------------------------------------------- */
void
aisl_reject( aisl_stream_t s);
/* -------------------------------------------------------------------------- */
#endif