Switch to new coding style

This commit is contained in:
Ilja Kartašov 2019-04-19 21:43:02 +02:00
parent ee90cbc37c
commit 12be39c747
28 changed files with 2466 additions and 2213 deletions

View File

@ -24,14 +24,14 @@
static void static void
hello_world(aisl_evt_t const evt, void * p_ctx) hello_world(const struct aisl_evt *evt, void *p_ctx)
{ {
if (evt->code != AISL_EVENT_STREAM_REQUEST) if (evt->code != AISL_EVENT_STREAM_REQUEST)
return; return;
aisl_status_t status; AislStatus status;
aisl_stream_t s = evt->source; AislStream s = evt->source;
const char html[] = const char html[] =
"<html>" "<html>"
@ -44,11 +44,11 @@ hello_world(aisl_evt_t const evt, void * p_ctx)
"</body>" "</body>"
"</html>"; "</html>";
status = aisl_response(s, AISL_HTTP_OK, sizeof(html)-1); status = aisl_response(s, AISL_HTTP_OK, sizeof (html)-1);
if (status == AISL_SUCCESS) if (status == AISL_SUCCESS)
{ {
if (aisl_write(s, html, sizeof(html)-1) != -1) if (aisl_write(s, html, sizeof (html)-1) != -1)
{ {
aisl_flush(s); aisl_flush(s);
} }
@ -63,8 +63,8 @@ hello_world(aisl_evt_t const evt, void * p_ctx)
int int
main(int argc, char ** argv) main(int argc, char ** argv)
{ {
aisl_t aisl; /**< AISL instance pointer */ AislInstance aisl; /**< AISL instance pointer */
aisl_status_t status; /**< AISL status code */ AislStatus status; /**< AISL status code */
struct aisl_cfg cfg = AISL_CFG_DEFAULT; struct aisl_cfg cfg = AISL_CFG_DEFAULT;
struct aisl_cfg_srv srv = { struct aisl_cfg_srv srv = {
.host = "0.0.0.0", .host = "0.0.0.0",

View File

@ -1,10 +1,16 @@
/* /******************************************************************************
* <aisl/aisl.h>
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
* *
* Project homepage: https://lowenware.com/aisl/ ******************************************************************************/
/**
* @file aisl/aisl.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Meta header file of AISL
* *
* @see https://lowenware.com/aisl/
*/ */
#ifndef AISL_H_17EF1616_A00F_49C9_92B6_273AB13BF279 #ifndef AISL_H_17EF1616_A00F_49C9_92B6_273AB13BF279

View File

@ -1,10 +1,16 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/** /**
* @file aisl/client.h * @file aisl/client.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of #AislCLient functions
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * @see https://lowenware.com/aisl/
*
* Project homepage: https://lowenware.com/aisl/
*
*/ */
#ifndef AISL_CLIENT_H_A6C37DCF_2183_4F22_A5A0_668311757A08 #ifndef AISL_CLIENT_H_A6C37DCF_2183_4F22_A5A0_668311757A08
@ -14,50 +20,50 @@
/** /**
* @brief Gets #aisl_server_t instance associated with client. * @brief Gets #AislServer instance associated with client.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @return an associated #aisl_server_t pointer. * @return an associated #AislServer pointer.
*/ */
aisl_server_t AislServer
aisl_client_get_server(aisl_client_t client); aisl_client_get_server(AislClient client);
/** /**
* @brief Gets security connection status. * @brief Gets security connection status.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @return true if SSL is enabled and false if disabled. * @return true if SSL is enabled and false if disabled.
*/ */
bool bool
aisl_client_is_secure(aisl_client_t client); aisl_client_is_secure(AislClient client);
/** /**
* @brief Gets client's connection state. * @brief Gets client's connection state.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @return true if client is online and false if is offline. * @return true if client is online and false if is offline.
*/ */
bool bool
aisl_client_is_online(aisl_client_t client); aisl_client_is_online(AislClient client);
/** /**
* @brief Forcefully closes client's connection. * @brief Forcefully closes client's connection.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
*/ */
void void
aisl_client_disconnect(aisl_client_t client); aisl_client_disconnect(AislClient client);
/** /**
* @brief Gets HTTP protocol version. * @brief Gets HTTP protocol version.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @return HTTP protocol version * @return HTTP protocol version
*/ */
aisl_http_version_t AislHttpVersion
aisl_client_get_http_version(aisl_client_t client); aisl_client_get_http_version(AislClient client);
void void
aisl_client_get_address( aisl_client_t client, struct sockaddr_in * address); aisl_client_get_address(AislClient client, struct sockaddr_in *address);
#endif /* !AISL_CLIENT_H */ #endif /* !AISL_CLIENT_H */

View File

@ -1,10 +1,16 @@
/* /******************************************************************************
* <aisl/config.h>
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
* *
* Project homepage: https://lowenware.com/aisl/ ******************************************************************************/
/**
* @file aisl/config.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of AISL configuration structures
* *
* @see https://lowenware.com/aisl/
*/ */
#ifndef AISL_CONFIG_H_DB67A89B_5CAF_4A5F_AEB1_6DB9F84827D6 #ifndef AISL_CONFIG_H_DB67A89B_5CAF_4A5F_AEB1_6DB9F84827D6
@ -12,8 +18,7 @@
#include <aisl/types.h> #include <aisl/types.h>
#define AISL_CFG_DEFAULT \ #define AISL_CFG_DEFAULT { \
{ \
.callback = NULL \ .callback = NULL \
, .p_ctx = NULL \ , .p_ctx = NULL \
, .srv = NULL \ , .srv = NULL \
@ -27,34 +32,27 @@
} \ } \
struct aisl_cfg_srv struct aisl_cfg_srv {
{
const char * host; const char * host;
uint16_t port; uint16_t port;
bool secure; bool secure;
}; };
typedef struct aisl_cfg_srv * aisl_cfg_srv_t; struct aisl_cfg_ssl {
struct aisl_cfg_ssl
{
const char * host; const char * host;
const char * key_file; const char * key_file;
const char * crt_file; const char * crt_file;
}; };
typedef struct aisl_cfg_ssl * aisl_cfg_ssl_t;
struct aisl_cfg struct aisl_cfg
{ {
/* event handlers */ /* event handlers */
aisl_callback_t callback; AislCallback callback;
void * p_ctx; void *p_ctx;
aisl_cfg_srv_t srv; struct aisl_cfg_srv *srv;
aisl_cfg_ssl_t ssl; struct aisl_cfg_ssl *ssl;
int srv_cnt; int srv_cnt;
int ssl_cnt; int ssl_cnt;
@ -65,6 +63,4 @@ struct aisl_cfg
int client_silence_timeout; int client_silence_timeout;
}; };
typedef struct aisl_cfg * aisl_cfg_t;
#endif /* !AISL_CONFIG_H */ #endif /* !AISL_CONFIG_H */

View File

@ -1,10 +1,17 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/** /**
* @file aisl/instance.h * @file aisl/instance.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of #AislInstance functions
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * @see https://lowenware.com/aisl/
*
* Project homepage: https://lowenware.com/aisl/
*
*/ */
#ifndef AISL_INSTANCE_H_60576F41_454C_4189_B91A_F40501132230 #ifndef AISL_INSTANCE_H_60576F41_454C_4189_B91A_F40501132230
@ -21,39 +28,39 @@
* @brief Allocates new AISL instance. * @brief Allocates new AISL instance.
* *
* @param cfg a pointer to #aisl_cfg_t structure. * @param cfg a pointer to #aisl_cfg_t structure.
* @return an #aisl_t instance pointer. * @return an #AislInstance instance pointer.
*/ */
aisl_t AislInstance
aisl_new( aisl_cfg_t cfg ); aisl_new(const struct aisl_cfg *cfg);
/** /**
* @brief Frees previously allocated pointer of AISL instance. * @brief Frees previously allocated pointer of AISL instance.
* @param instance a pointer to #aisl_t instance. * @param instance a pointer to #AislInstance instance.
*/ */
void void
aisl_free( aisl_t instance ); aisl_free(AislInstance instance);
/** /**
* @brief A core function doing all the library routines. * @brief A core function doing all the library routines.
* Designed to be called inside application main loop * Designed to be called inside application main loop
* @param instance a pointer to #aisl_t instance. * @param instance a pointer to #AislInstance instance.
* @return #aisl_status_t code. * @return #AislStatus code.
*/ */
aisl_status_t AislStatus
aisl_run_cycle( aisl_t instance ); aisl_run_cycle(AislInstance instance);
/** /**
* @brief Function to sleep CPU if nothing to do. * @brief Function to sleep CPU if nothing to do.
* Calls select on all the opened sockets inside. * Calls select on all the opened sockets inside.
* @param instance a pointer to #aisl_t instance. * @param instance a pointer to #AislInstance instance.
* @param usec a number of miliseconds to wait for any data on sockets. * @param usec a number of miliseconds to wait for any data on sockets.
* @return #aisl_status_t code. * @return #AislStatus code.
*/ */
aisl_status_t AislStatus
aisl_sleep( aisl_t instance, uint32_t usec ); aisl_sleep(AislInstance instance, uint32_t usec);
#endif /* !AISL_INSTANCE_H */ #endif /* !AISL_INSTANCE_H */

View File

@ -1,10 +1,16 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/** /**
* @file aisl/server.h * @file aisl/server.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of #AislServer functions
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * @see https://lowenware.com/aisl/
*
* Project homepage: https://lowenware.com/aisl/
*
*/ */
#ifndef AISL_SERVER_H_CC564608_7A05_4B31_9E7E_32750BC60768 #ifndef AISL_SERVER_H_CC564608_7A05_4B31_9E7E_32750BC60768
@ -15,28 +21,28 @@
/** /**
* @brief Function to get appropriate AISL instance pointer from server pointer. * @brief Function to get appropriate AISL instance pointer from server pointer.
* @param server an #aisl_server_t pointer. * @param server an #AislServer pointer.
* @return an #aisl_t instance pointer. * @return an #AislInstance instance pointer.
*/ */
aisl_t AislInstance
aisl_server_get_instance( aisl_server_t server ); aisl_server_get_instance(AislServer server);
/** /**
* @brief Copies server listen address information to sockaddr_in structure. * @brief Copies server listen address information to sockaddr_in structure.
* @param server an #aisl_server_t pointer. * @param server an #AislServer pointer.
* @param address a pointer to sockaddr_in structure. * @param address a pointer to sockaddr_in structure.
*/ */
void void
aisl_server_get_address( aisl_server_t server, struct sockaddr_in * address); aisl_server_get_address(AislServer server, struct sockaddr_in *address);
/** /**
* @brief Function to get on and off status of SSL for the #aisl_server_t. * @brief Function to get on and off status of SSL for the #AislServer.
* @param server an #aisl_server_t pointer. * @param server an #AislServer pointer.
* @return a boolean value representing SSL enabled/disabled state. * @return a boolean value representing SSL enabled/disabled state.
*/ */
bool bool
aisl_server_get_ssl( aisl_server_t server ); aisl_server_get_ssl(AislServer server);
#endif /* !AISL_SERVER_H */ #endif /* !AISL_SERVER_H */

View File

@ -1,12 +1,17 @@
/* /******************************************************************************
* <aisl/stream.h>
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
* *
* Project homepage: https://lowenware.com/aisl/ ******************************************************************************/
*
*/
/**
* @file aisl/stream.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of #AislStream functions
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISL_STREAM_H_4D8EB622_3CE0_4F1B_AC1F_B27CCB5C2EDC #ifndef AISL_STREAM_H_4D8EB622_3CE0_4F1B_AC1F_B27CCB5C2EDC
#define AISL_STREAM_H_4D8EB622_3CE0_4F1B_AC1F_B27CCB5C2EDC #define AISL_STREAM_H_4D8EB622_3CE0_4F1B_AC1F_B27CCB5C2EDC
@ -15,96 +20,87 @@
#include <aisl/types.h> #include <aisl/types.h>
/* Stream helpers */
bool bool
aisl_is_secure( aisl_stream_t stream ); aisl_is_secure(AislStream stream);
aisl_client_t AislClient
aisl_get_client( aisl_stream_t stream ); aisl_get_client(AislStream stream);
aisl_server_t AislServer
aisl_get_server( aisl_stream_t stream ); aisl_get_server(AislStream stream);
aisl_http_version_t AislHttpVersion
aisl_get_http_version( aisl_stream_t stream ); aisl_get_http_version(AislStream stream);
aisl_t AislInstance
aisl_stream_get_instance( aisl_stream_t s ); aisl_stream_get_instance(AislStream s);
/* Context functions */
void * void *
aisl_get_context( aisl_stream_t stream ); aisl_get_context(AislStream stream);
void void
aisl_set_context( aisl_stream_t stream, void * context ); aisl_set_context(AislStream stream, void *context);
/* Stream control functions */ AislStatus
aisl_flush(AislStream stream);
aisl_status_t
aisl_flush( aisl_stream_t stream );
void void
aisl_reject( aisl_stream_t stream ); aisl_reject(AislStream stream);
/* Response functions */ AislStatus
aisl_response(AislStream stream,
aisl_status_t AislHttpResponse status_code,
aisl_response( aisl_stream_t stream, uint64_t content_length);
aisl_http_response_t status_code,
uint64_t content_length );
int int
aisl_header( aisl_stream_t stream, const char *key, const char *value ); aisl_header(AislStream stream, const char *key, const char *value );
int int
aisl_header_printf( aisl_stream_t stream, aisl_header_printf(AislStream stream,
const char * key, const char *key,
const char * format, const char *format,
... ); ... );
int int
aisl_header_vprintf( aisl_stream_t stream, aisl_header_vprintf(AislStream stream,
const char * key, const char *key,
const char * format, const char *format,
va_list args ); va_list args );
int int
aisl_printf( aisl_stream_t stream, const char * format, ... ); aisl_printf(AislStream stream, const char *format, ...);
int int
aisl_vprintf( aisl_stream_t stream, const char * format, va_list args ); aisl_vprintf(AislStream stream, const char *format, va_list args);
int int
aisl_write( aisl_stream_t stream, const char * data, int d_len ); aisl_write(AislStream stream, const char *data, int d_len);
int int
aisl_puts( const char * str_data, aisl_stream_t stream ); aisl_puts(const char *str_data, AislStream stream);
void void
aisl_set_output_event(aisl_stream_t stream, bool value); aisl_set_output_event(AislStream stream, bool value);
bool bool
aisl_get_output_event(aisl_stream_t stream); aisl_get_output_event(AislStream stream);
#endif /* !AISL_STREAM_H */ #endif /* !AISL_STREAM_H */

View File

@ -1,10 +1,16 @@
/* /******************************************************************************
* <aisl/types.h>
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
* *
* Project homepage: https://lowenware.com/aisl/ ******************************************************************************/
/**
* @file aisl/types.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of AISL types
* *
* @see https://lowenware.com/aisl/
*/ */
#ifndef AISL_TYPES_H_86A9DBA7_C0E6_4CF4_8A64_DAAD4A81031B #ifndef AISL_TYPES_H_86A9DBA7_C0E6_4CF4_8A64_DAAD4A81031B
@ -20,26 +26,25 @@
#define AISL_AUTO_LENGTH (~0) #define AISL_AUTO_LENGTH (~0)
/* type casts */ /** type casts */
#define AISL_CALLBACK(x) ((aisl_callback_t) x) #define AISL_CALLBACK(x) ((AislCallback) x)
/* AISL Instance */ /** AISL Instance */
typedef struct aisl * aisl_t; typedef struct aisl_instance * AislInstance;
/* HTTP(s) Server */ /** HTTP(s) Server */
typedef struct aisl_server * aisl_server_t; typedef struct aisl_server * AislServer;
/* HTTP(s) Client */ /** HTTP(s) Client */
typedef struct aisl_client * aisl_client_t; typedef struct aisl_client * AislClient;
/* Server<->Client Stream */ /** Server<->Client Stream */
typedef struct aisl_stream * aisl_stream_t; typedef struct aisl_stream * AislStream;
/* status return codes */ /** status return codes */
typedef enum typedef enum {
{
AISL_INPUT_ERROR = -4 AISL_INPUT_ERROR = -4
, AISL_EXTCALL_ERROR = -3 , AISL_EXTCALL_ERROR = -3
, AISL_SYSCALL_ERROR = -2 , AISL_SYSCALL_ERROR = -2
@ -47,35 +52,30 @@ typedef enum
, AISL_SUCCESS = 0 , AISL_SUCCESS = 0
, AISL_IDLE = 1 , AISL_IDLE = 1
} AislStatus;
} aisl_status_t;
#ifndef WITHOUT_STRINGIFIERS #ifndef WITHOUT_STRINGIFIERS
const char * const char *
aisl_status_to_string(aisl_status_t status); aisl_status_to_string(AislStatus status);
#endif #endif
/* Generic HTTP Enumerations */ /** Generic HTTP Enumerations */
typedef enum {
typedef enum
{
AISL_HTTP_0_9 = 0x0009 AISL_HTTP_0_9 = 0x0009
, AISL_HTTP_1_0 = 0x0100 , AISL_HTTP_1_0 = 0x0100
, AISL_HTTP_1_1 = 0x0101 , AISL_HTTP_1_1 = 0x0101
, AISL_HTTP_2_0 = 0x0200 , AISL_HTTP_2_0 = 0x0200
} AislHttpVersion;
} aisl_http_version_t;
const char * const char *
aisl_http_version_to_string( aisl_http_version_t version ); aisl_http_version_to_string(AislHttpVersion version);
typedef enum typedef enum {
{
AISL_HTTP_METHOD_UNKNOWN AISL_HTTP_METHOD_UNKNOWN
, AISL_HTTP_GET , AISL_HTTP_GET
, AISL_HTTP_PUT , AISL_HTTP_PUT
@ -85,18 +85,15 @@ typedef enum
, AISL_HTTP_DELETE , AISL_HTTP_DELETE
, AISL_HTTP_OPTIONS , AISL_HTTP_OPTIONS
, AISL_HTTP_CONNECT , AISL_HTTP_CONNECT
, AISL_HTTP_PRI , AISL_HTTP_PRI
} AislHttpMethod;
} aisl_http_method_t;
const char * const char *
aisl_http_method_to_string( aisl_http_method_t method ); aisl_http_method_to_string( AislHttpMethod method );
typedef enum typedef enum {
{
AISL_HTTP_CONTINUE = 100 AISL_HTTP_CONTINUE = 100
, AISL_HTTP_SWITCHING_PROTOCOLS , AISL_HTTP_SWITCHING_PROTOCOLS
@ -142,19 +139,16 @@ typedef enum
, AISL_HTTP_SERVICE_UNAVAILABLE , AISL_HTTP_SERVICE_UNAVAILABLE
, AISL_HTTP_GATEWAY_TIMEOUT , AISL_HTTP_GATEWAY_TIMEOUT
, AISL_HTTP_VERSION_NOT_SUPPORTED , AISL_HTTP_VERSION_NOT_SUPPORTED
} AislHttpResponse;
} aisl_http_response_t;
const char * const char *
aisl_http_response_to_string( aisl_http_response_t code ); aisl_http_response_to_string( AislHttpResponse code );
/* Events */ /** Codes of AISL events */
typedef enum {
typedef enum AISL_EVENT_SERVER_READY = 100
{
AISL_EVENT_SERVER_OPEN = 100
, AISL_EVENT_SERVER_ERROR = 190 , AISL_EVENT_SERVER_ERROR = 190
, AISL_EVENT_CLIENT_CONNECT = 200 , AISL_EVENT_CLIENT_CONNECT = 200
@ -167,59 +161,47 @@ typedef enum
, AISL_EVENT_STREAM_OUTPUT = 340 , AISL_EVENT_STREAM_OUTPUT = 340
, AISL_EVENT_STREAM_CLOSE = 350 , AISL_EVENT_STREAM_CLOSE = 350
, AISL_EVENT_STREAM_ERROR = 390 , AISL_EVENT_STREAM_ERROR = 390
} AislEvent;
} aisl_evt_code_t;
struct aisl_evt struct aisl_evt {
{ void *source;
void * source; AislEvent code;
aisl_evt_code_t code; AislStatus status;
aisl_status_t status;
}; };
typedef struct aisl_evt * aisl_evt_t;
/* void type event callback */ /* void type event callback */
typedef void typedef void
(* aisl_callback_t) (aisl_evt_t const evt, void * p_ctx); (* AislCallback) (const struct aisl_evt *evt, void *ctx);
struct aisl_evt_stream_open struct aisl_evt_open {
{
struct aisl_evt evt; struct aisl_evt evt;
const char * path; const char *path;
const char * query; const char *query;
aisl_http_method_t http_method; AislHttpMethod http_method;
}; };
typedef struct aisl_evt_stream_open * aisl_evt_stream_open_t;
struct aisl_evt_header {
struct aisl_evt_stream_header
{
struct aisl_evt evt; struct aisl_evt evt;
const char * key; const char *key;
const char * value; const char *value;
}; };
typedef struct aisl_evt_stream_header * aisl_evt_stream_header_t;
struct aisl_evt_input {
struct aisl_evt_stream_input
{
struct aisl_evt evt; struct aisl_evt evt;
const char * data; const char *data;
int32_t size; int32_t size;
}; };
typedef struct aisl_evt_stream_input * aisl_evt_stream_input_t;
#ifndef WITHOUT_STRINGIFIERS #ifndef WITHOUT_STRINGIFIERS
const char * const char *
aisl_evt_code_to_string( aisl_evt_code_t evt_code ); aisl_event_to_string(AislEvent evt);
#endif #endif

539
src/) Normal file
View File

@ -0,0 +1,539 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef AISL_WITHOUT_SSL
#include <openssl/err.h>
#endif
#include <aisl/aisl.h>
#include "debug.h"
#include "stream.h"
#include "http.h"
#include "server.h"
#include "instance.h"
#include "client.h"
#define FLAG_KEEPALIVE (1<<0)
#define FLAG_HANDSHAKE (1<<1)
#define FLAG_CAN_READ (1<<2)
#define FLAG_CAN_WRITE (1<<3)
#define BUFFER_SIZE (16*1024)
static void
aisl_client_close(AislClient client, AislStatus status)
{
if (client->fd != -1)
{
aisl_raise(
client->server->instance
, (void *)client
, AISL_EVENT_CLIENT_DISCONNECT
, status
);
close(client->fd);
shutdown(client->fd, SHUT_RDWR);
client->fd = -1;
}
}
static AislStatus
aisl_client_parse(AislClient client, char * data, int32_t size)
{
AislStatus result = AISL_SUCCESS;
AislStream s = client->stream;
ParserStatus p = HTTP_PARSER_SUCCESS;
int32_t bytes_left = size;
switch (client->http_version)
{
case AISL_HTTP_0_9:
case AISL_HTTP_1_0:
case AISL_HTTP_1_1:
/* s = client->stream; */
while ( p == HTTP_PARSER_SUCCESS )
{
switch ( aisl_stream_get_state(s) )
{
case AISL_STREAM_STATE_IDLE:
p = http_10_parse_request(data, &size, client->stream);
break;
case AISL_STREAM_STATE_WAIT_HEADER:
p = http_10_parse_header(data, &size, client->stream);
break;
case AISL_STREAM_STATE_WAIT_BODY:
p = http_10_parse_body(data, &size, client->stream);
break;
default: /* has input data, but request was already parsed */
p = HTTP_PARSER_ERROR;
continue;
}
// size now has number of parsed bytes
data += size;
bytes_left -= size;
size = bytes_left;
}
break;
case AISL_HTTP_2_0:
break;
}
switch(p)
{
case HTTP_PARSER_READY:
client->flags &= ~FLAG_CAN_READ;
client->flags |= FLAG_CAN_WRITE;
aisl_raise(
client->server->instance
, (void *) s
, AISL_EVENT_STREAM_REQUEST
, result
);
break;
case HTTP_PARSER_ERROR:
/* reply Bad Request here */
client->stream->http_response = AISL_HTTP_BAD_REQUEST;
aisl_raise(
client->server->instance
, (void *) s
, AISL_EVENT_STREAM_ERROR
, result
);
aisl_client_close(client, result);
return result;
default:
break;
}
if (size)
buffer_shift(&client->in, client->in.used - size); /* reset buffer */
return result;
}
/* In HTTP 2.0 client->stream will be NULL if stream related data was completely
* parsed. If it is not NULL, then stream expects additional data -> same like
* in mono stream HTTP 1.0
*/
static AislStatus
aisl_client_input(AislClient client)
{
int l;
char * data = &client->in.data[ client->in.used ];
int32_t size = client->in.size - client->in.used;
#ifndef AISL_WITHOUT_SSL
if (client->ssl)
{
DPRINTF("SSL_read");
if ( !(client->flags & FLAG_HANDSHAKE) )
{
if ( (l = SSL_accept(client->ssl)) != 1 )
{
l = SSL_get_error(client->ssl, l);
if (l == SSL_ERROR_WANT_READ || l == SSL_ERROR_WANT_WRITE)
return AISL_IDLE;
DPRINTF("SSL handshake fail: %s\n", ERR_error_string(l, NULL) );
aisl_client_close(client, AISL_EXTCALL_ERROR);
return AISL_EXTCALL_ERROR;
}
client->flags &= ~FLAG_HANDSHAKE;
}
l = SSL_read(client->ssl, data, size) ;
}
else
#endif
l = recv( client->fd, data, size, 0);
if (l > 0)
{
DPRINTF("%d bytes received from client", l);
data = client->in.data;
size = client->in.used + l;
client->in.used = size;
return aisl_client_parse(client, data, size);
}
else if (l<0)
{
#ifndef AISL_WITHOUT_SSL
if (client->ssl)
{
if (SSL_get_error(client->ssl, l) == SSL_ERROR_WANT_READ)
return AISL_IDLE;
}
else
#endif
{
if(errno == EWOULDBLOCK)
return AISL_IDLE;
DPRINTF("client - %s", strerror(errno));
}
}
/* both: client disconnect + on read error */
/* todo: raise client error here */
aisl_client_close(client, AISL_SYSCALL_ERROR);
return AISL_SYSCALL_ERROR;
}
static AislStatus
aisl_client_output(AislClient client)
{
int l;
char * data;
AislStream s = client->stream;
/* while stream is not flushed, we should raise event */
if( aisl_get_output_event(s) )
{
/* in case of chunked output ( subscription for AISL_STREAM_OUTPUT event )
* stream buffer will be initialized with OUTPUT_BUFFER_SIZE size, but
* buffer->size will be used to carry amount of stored bytes
* */
l = aisl_stream_get_buffer_space(s);
/*
if (bsz < OUTPUT_BUFFER_SIZE)
{
if (buffer_clear(s->buffer, OUTPUT_BUFFER_SIZE) == 0)
return false;
s->buffer->size = bsz;
bsz = OUTPUT_BUFFER_SIZE;
}
*/
if ( !(l < aisl_stream_get_buffer_size(s) / 2) )
{
aisl_raise(
client->server->instance
, (void*)s
, AISL_EVENT_STREAM_OUTPUT
, AISL_SUCCESS
);
}
}
data = aisl_stream_get_data(s, &l);
if ( !l )
return AISL_IDLE;
#ifdef AISL_WITHOUT_SSL
l = send( client->fd, data, l, 0);
#else
l = (client->ssl) ?
SSL_write(client->ssl, data, l) :
send( client->fd, data, l, 0);
#endif
if (l > 0)
{
aisl_stream_shift(s, l);
/*
if (s->state == STREAM_RESPONSE_READY && / * flushed * /
s->buffer->size == 0) / * all sent * /
*/
if ( aisl_stream_is_done(s) )
{
/* buffer_clear(s->buffer, 0); */
/* data has been sent */
if (client->flags & FLAG_KEEPALIVE)
{
aisl_stream_free(s);
client->stream = aisl_stream_new(client, client->next_id++);
if (client->stream != NULL )
return AISL_SUCCESS;
/* in case of malloc error it will not be error as long as request was
* handled and we just close the connection.
*/
}
aisl_client_close(client, AISL_SUCCESS);
}
return AISL_SUCCESS;
}
/* l < 0 */
#ifndef AISL_WITHOUT_SSL
if (client->ssl)
{
if ( SSL_get_error(client->ssl, l) == SSL_ERROR_WANT_WRITE )
return AISL_IDLE;
}
else
#endif
{
if (errno == EWOULDBLOCK)
return AISL_IDLE;
}
aisl_client_close(client, AISL_SYSCALL_ERROR);
return AISL_SYSCALL_ERROR;
}
AislClient
aisl_client_new( AislServer server,
int fd,
struct sockaddr_in * addr )
{
AislClient client;
AislStream stream;
if ( (client = calloc(1, sizeof (struct aisl_client))) != NULL )
{
DPRINTF("client alocated");
memcpy(&client->address, addr, sizeof (struct sockaddr_in));
client->server = server;
client->fd = fd;
client->next_id = 2;
client->http_version = AISL_HTTP_1_0;
client->timestamp = time(NULL);
client->flags = FLAG_KEEPALIVE | FLAG_HANDSHAKE | FLAG_CAN_READ;
if (buffer_init(&client->in, 2*BUFFER_SIZE) != -1)
{
DPRINTF("client buffer alocated");
memcpy(&client->out, &client->in, sizeof (struct buffer));
stream = aisl_stream_new(client, 0);
if (stream != NULL)
{
client->stream = stream;
DPRINTF("client stream alocated");
#ifdef AISL_WITHOUT_SSL
return client;
#else
SSL_CTX * ssl_ctx;
if ( !server->ssl )
return client;
ssl_ctx = aisl_get_ssl_ctx(server->instance, NULL);
if ((client->ssl = SSL_new(ssl_ctx)) != NULL )
{
SSL_set_fd(client->ssl, fd);
return client;
}
#endif
}
}
aisl_client_free(client);
}
return NULL;
}
void
aisl_client_free(AislClient client)
{
aisl_client_close(client, AISL_SUCCESS);
#ifndef AISL_WITHOUT_SSL
if (client->ssl)
SSL_free(client->ssl);
#endif
if (client->in.data)
free(client->in.data);
/* out buffer is a shared part of input buffer, so no need to free it */
if (client->stream)
aisl_stream_free(client->stream);
free(client);
}
AislStatus
AislClientouch(AislClient client, int32_t timeout)
{
AislStatus result = AISL_IDLE,
status = AISL_IDLE;
/* input */
if (client->flags & FLAG_CAN_READ)
{
if ( (result = aisl_client_input(client)) < 0 )
return result;
}
/* output */
if (client->flags & FLAG_CAN_WRITE)
{
if ( (status = aisl_client_output(client)) < 0 )
return status;
}
/*
if ((client->http_version==AISL_HTTP_2_0 || s->state<STREAM_REQUEST_READY) &&
(client_input(client)) ) result = true;
*/
/* output */
/*
s = list_index(client->streams, client->ostream);
if ( s->flags & (STREAM_FLAG_OUTPUT_READY | STREAM_FLAG_OUTPUT_CHUNKED) )
result = client_output(client);
*/
/* update timestamp */
if (result == AISL_IDLE)
result = status;
if (result == AISL_SUCCESS)
client->timestamp = time(NULL);
else
{
time_t now;
time(&now);
if ( !(now - client->timestamp < timeout) )
{
aisl_client_close(client, result);
}
}
return result;
}
int
aisl_client_get_socket(AislClient client)
{
return client->fd;
}
bool
aisl_client_get_keepalive(AislClient client)
{
return (client->flags & FLAG_KEEPALIVE);
}
void
aisl_client_set_keepalive(AislClient client, bool value)
{
if (value)
client->flags |= FLAG_KEEPALIVE;
else
client->flags &= ~FLAG_KEEPALIVE;
}
/* API Level ---------------------------------------------------------------- */
__attribute__ ((visibility ("default") ))
AislServer
aisl_client_get_server(AislClient client)
{
return client->server;
}
__attribute__ ((visibility ("default") ))
bool
aisl_client_is_secure(AislClient client)
{
#ifdef AISL_WITHOUT_SSL
return false;
#else
return (client->ssl == NULL) ? false : true;
#endif
}
__attribute__ ((visibility ("default") ))
bool
aisl_client_is_online(AislClient client)
{
return (client->fd == -1) ? false : true;
}
__attribute__ ((visibility ("default") ))
void
aisl_client_disconnect(AislClient client)
{
aisl_client_close(client, AISL_SUCCESS);
}
__attribute__ ((visibility ("default") ))
AislHttpVersion
aisl_client_get_http_version(AislClient client)
{
return client->http_version;
}
__attribute__ ((visibility ("default") ))
void
aisl_client_get_address( AislClient client, struct sockaddr_in * address)
{
memcpy(address, &client->address, sizeof (struct sockaddr_in));
}

View File

@ -19,39 +19,35 @@
static int32_t static int32_t
buffer_set_size(buffer_t buffer, int32_t new_size) buffer_set_size(struct buffer *buffer, int32_t new_size)
{ {
if ( new_size != buffer->size ) if (new_size != buffer->size) {
{ char *data;
if (new_size)
{ if (new_size) {
int32_t s = new_size / 1024; int32_t s = new_size / 1024;
if ( new_size % 1024 ) if ( new_size % 1024 ) {
{
new_size = (s+1) * 1024; new_size = (s+1) * 1024;
} }
} } else {
else
new_size = 16*1024; new_size = 16*1024;
}
char * data = realloc(buffer->data, new_size); if ((data = realloc(buffer->data, new_size)) != NULL) {
if (data)
{
buffer->data = data; buffer->data = data;
buffer->size = new_size; buffer->size = new_size;
} } else {
else
new_size = -1; new_size = -1;
} }
}
return new_size; return new_size;
} }
int32_t int32_t
buffer_init( buffer_t buffer, int32_t size ) buffer_init(struct buffer *buffer, int32_t size)
{ {
if ( (size = buffer_set_size(buffer, size)) != -1) if ( (size = buffer_set_size(buffer, size)) != -1)
buffer->used = 0; buffer->used = 0;
@ -61,10 +57,9 @@ buffer_init( buffer_t buffer, int32_t size )
void void
buffer_release( buffer_t buffer ) buffer_release(struct buffer *buffer)
{ {
if (buffer->data) if (buffer->data) {
{
free(buffer->data); free(buffer->data);
buffer->data = NULL; buffer->data = NULL;
} }
@ -75,30 +70,29 @@ buffer_release( buffer_t buffer )
static int32_t static int32_t
buffer_move_offset( buffer_t buffer, int32_t offset, int32_t size ) buffer_move_offset(struct buffer *buffer, int32_t offset, int32_t size)
{ {
int32_t to_move = buffer->used - offset; int32_t to_move = buffer->used - offset;
if (to_move < 0) if (to_move < 0) {
return -1; return -1;
else if (to_move) } else if (to_move) {
memmove(&buffer->data[offset+size], &buffer->data[offset], to_move); memmove(&buffer->data[offset+size], &buffer->data[offset], to_move);
}
return size; return size;
} }
int32_t int32_t
buffer_insert( buffer_t buffer buffer_insert(struct buffer *buffer,
, int32_t offset int32_t offset,
, const char * data const char *data,
, int32_t size ) int32_t size)
{ {
int32_t result; int32_t result;
if ( (result = buffer_set_size(buffer, size)) != -1) if ( (result = buffer_set_size(buffer, size)) != -1) {
{ if ((result = buffer_move_offset(buffer, offset, size)) != -1) {
if ((result = buffer_move_offset(buffer, offset, size)) != -1)
{
memcpy(&buffer->data[offset], data, size); memcpy(&buffer->data[offset], data, size);
buffer->used += result; buffer->used += result;
} }
@ -109,36 +103,33 @@ buffer_insert( buffer_t buffer
int32_t int32_t
buffer_append_printf( buffer_t buffer, const char * format, ... ) buffer_append_printf(struct buffer *buffer, const char *format, ...)
{ {
int32_t result; int32_t result;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
result = buffer_append_vprintf( buffer, format, args); result = buffer_append_vprintf(buffer, format, args);
va_end(args); va_end(args);
return result; return result;
} }
int32_t int32_t
buffer_append_vprintf( buffer_t buffer, const char * format, va_list args ) buffer_append_vprintf(struct buffer *buffer, const char *format, va_list args)
{ {
int32_t space = buffer->size - buffer->used, int32_t space, result;
result;
va_list cp_args; va_list cp_args;
va_copy(cp_args, args); va_copy(cp_args, args);
space = buffer->size - buffer->used,
result = vsnprintf(&buffer->data[buffer->used], space, format, args);
result = vsnprintf( &buffer->data[buffer->used], space, format, args ); if (result < space) { /* enough space */
if ( result < space ) /* enough space */
{
buffer->used += result; buffer->used += result;
} } else {
else result = buffer_set_size(buffer, buffer->size + result - space);
{ if (result != -1)
if ((result = buffer_set_size(buffer, buffer->size + result - space)) != -1)
result = buffer_append_vprintf(buffer, format, cp_args); result = buffer_append_vprintf(buffer, format, cp_args);
} }
va_end(cp_args); va_end(cp_args);
@ -147,14 +138,15 @@ buffer_append_vprintf( buffer_t buffer, const char * format, va_list args )
} }
int32_t int32_t
buffer_append( buffer_t buffer, const char * data, int32_t size ) buffer_append(struct buffer *buffer, const char *data, int32_t size)
{ {
int32_t used = buffer->used, int32_t used, space;
used = buffer->used,
space = buffer->size - used; space = buffer->size - used;
if ( size > space ) /* enough space */ if (size > space) { /* enough space */
{ if (buffer_set_size(buffer, buffer->size + size - space) == -1)
if ( buffer_set_size(buffer, buffer->size + size - space) == -1)
return -1; return -1;
} }
@ -166,22 +158,18 @@ buffer_append( buffer_t buffer, const char * data, int32_t size )
int32_t int32_t
buffer_shift( buffer_t buffer, int32_t offset ) buffer_shift(struct buffer *buffer, int32_t offset)
{ {
int32_t used = buffer->used - offset; int32_t used = buffer->used - offset;
if (offset > 0) if (offset > 0) {
{ if (offset < used) {
if (offset < used)
{
memmove(buffer->data, &buffer->data[offset], used); memmove(buffer->data, &buffer->data[offset], used);
} } else {
else
used = 0; used = 0;
}
buffer->used = used; buffer->used = used;
} }
return used; return used;
} }

View File

@ -6,9 +6,9 @@
******************************************************************************/ ******************************************************************************/
/** /**
* @file buffer.h * @file src/buffer.h
* @author Ilja Kartašov <ik@lowenware.com> * @author Ilja Kartašov <ik@lowenware.com>
* @brief Buffer module header file * @brief Declarations of struct buffer and functions
* *
* @see https://lowenware.com/aisl/ * @see https://lowenware.com/aisl/
*/ */
@ -20,42 +20,39 @@
#include <stdarg.h> #include <stdarg.h>
struct buffer struct buffer {
{ char *data;
char * data;
int32_t size; int32_t size;
int32_t used; int32_t used;
}; };
typedef struct buffer * buffer_t;
int32_t int32_t
buffer_init( buffer_t buffer, int32_t size ); buffer_init(struct buffer *bs, int32_t size);
void void
buffer_release( buffer_t buffer ); buffer_release(struct buffer *bs );
int32_t int32_t
buffer_insert(buffer_t buffer, int32_t offset, const char * data, int32_t size); buffer_insert(struct buffer *bs, int32_t offset, const char *data, int32_t size);
int32_t int32_t
buffer_append_printf( buffer_t buffer, const char * format, ... ); buffer_append_printf(struct buffer *bs, const char *format, ...);
int32_t int32_t
buffer_append_vprintf( buffer_t buffer, const char * format, va_list args ); buffer_append_vprintf(struct buffer *bs, const char *format, va_list args);
int32_t int32_t
buffer_append( buffer_t buffer, const char * data, int32_t size ); buffer_append(struct buffer *bs, const char *data, int32_t size);
int32_t int32_t
buffer_shift( buffer_t buffer, int32_t size ); buffer_shift(struct buffer *bs, int32_t size);

View File

@ -27,16 +27,11 @@
static void static void
aisl_client_close(aisl_client_t client, aisl_status_t status) aisl_client_close(AislClient client, AislStatus status)
{ {
if (client->fd != -1) if (client->fd != -1) {
{ aisl_raise(client->server->instance, (void *)client,
aisl_raise( AISL_EVENT_CLIENT_DISCONNECT, status );
client->server->instance
, (void *)client
, AISL_EVENT_CLIENT_DISCONNECT
, status
);
close(client->fd); close(client->fd);
shutdown(client->fd, SHUT_RDWR); shutdown(client->fd, SHUT_RDWR);
@ -45,29 +40,21 @@ aisl_client_close(aisl_client_t client, aisl_status_t status)
} }
static aisl_status_t static AislStatus
aisl_client_parse(aisl_client_t client, char * data, int32_t size) aisl_client_parse(AislClient client, char *data, int32_t size)
{ {
aisl_status_t result = AISL_SUCCESS; AislStatus result = AISL_SUCCESS;
aisl_stream_t s = client->stream; AislStream s = client->stream;
http_parser_t p = HTTP_PARSER_SUCCESS; ParserStatus p = HTTP_PARSER_SUCCESS;
int32_t bytes_left = size; int32_t bytes_left = size;
switch (client->http_version) switch (client->http_version) {
{
case AISL_HTTP_0_9: case AISL_HTTP_0_9:
case AISL_HTTP_1_0: case AISL_HTTP_1_0:
case AISL_HTTP_1_1: case AISL_HTTP_1_1:
while (p == HTTP_PARSER_SUCCESS) {
/* s = client->stream; */ switch (aisl_stream_get_state(s)) {
while ( p == HTTP_PARSER_SUCCESS )
{
switch ( aisl_stream_get_state(s) )
{
case AISL_STREAM_STATE_IDLE: case AISL_STREAM_STATE_IDLE:
p = http_10_parse_request(data, &size, client->stream); p = http_10_parse_request(data, &size, client->stream);
break; break;
@ -89,43 +76,28 @@ aisl_client_parse(aisl_client_t client, char * data, int32_t size)
bytes_left -= size; bytes_left -= size;
size = bytes_left; size = bytes_left;
} }
break; break;
case AISL_HTTP_2_0: case AISL_HTTP_2_0:
break; break;
} }
switch(p) switch(p) {
{
case HTTP_PARSER_READY: case HTTP_PARSER_READY:
client->flags &= ~FLAG_CAN_READ; client->flags &= ~FLAG_CAN_READ;
client->flags |= FLAG_CAN_WRITE; client->flags |= FLAG_CAN_WRITE;
aisl_raise( aisl_raise(client->server->instance, (void *)s, AISL_EVENT_STREAM_REQUEST,
client->server->instance result);
, (void *) s
, AISL_EVENT_STREAM_REQUEST
, result
);
break; break;
case HTTP_PARSER_ERROR: case HTTP_PARSER_ERROR:
/* reply Bad Request here */ /* reply Bad Request here */
client->stream->http_response = AISL_HTTP_BAD_REQUEST; client->stream->http_response = AISL_HTTP_BAD_REQUEST;
aisl_raise(client->server->instance, (void *)s, AISL_EVENT_STREAM_ERROR,
aisl_raise( result);
client->server->instance
, (void *) s
, AISL_EVENT_STREAM_ERROR
, result
);
aisl_client_close(client, result); aisl_client_close(client, result);
return result; return result;
default: default:
@ -143,22 +115,18 @@ aisl_client_parse(aisl_client_t client, char * data, int32_t size)
* parsed. If it is not NULL, then stream expects additional data -> same like * parsed. If it is not NULL, then stream expects additional data -> same like
* in mono stream HTTP 1.0 * in mono stream HTTP 1.0
*/ */
static aisl_status_t static AislStatus
aisl_client_input(aisl_client_t client) aisl_client_input(AislClient client)
{ {
int l; int l;
char *data = &client->in.data[ client->in.used ];
char * data = &client->in.data[ client->in.used ];
int32_t size = client->in.size - client->in.used; int32_t size = client->in.size - client->in.used;
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
if (client->ssl) if (client->ssl) {
{
DPRINTF("SSL_read"); DPRINTF("SSL_read");
if ( !(client->flags & FLAG_HANDSHAKE) ) if (!(client->flags & FLAG_HANDSHAKE)) {
{ if ( (l = SSL_accept(client->ssl)) != 1 ) {
if ( (l = SSL_accept(client->ssl)) != 1 )
{
l = SSL_get_error(client->ssl, l); l = SSL_get_error(client->ssl, l);
if (l == SSL_ERROR_WANT_READ || l == SSL_ERROR_WANT_WRITE) if (l == SSL_ERROR_WANT_READ || l == SSL_ERROR_WANT_WRITE)
@ -169,41 +137,34 @@ aisl_client_input(aisl_client_t client)
aisl_client_close(client, AISL_EXTCALL_ERROR); aisl_client_close(client, AISL_EXTCALL_ERROR);
return AISL_EXTCALL_ERROR; return AISL_EXTCALL_ERROR;
} }
client->flags &= ~FLAG_HANDSHAKE; client->flags &= ~FLAG_HANDSHAKE;
} }
l = SSL_read(client->ssl, data, size);
l = SSL_read(client->ssl, data, size) ; } else
}
else
#endif #endif
l = recv( client->fd, data, size, 0);
if (l > 0)
{ {
l = recv( client->fd, data, size, 0);
}
if (l > 0) {
DPRINTF("%d bytes received from client", l); DPRINTF("%d bytes received from client", l);
data = client->in.data; data = client->in.data;
size = client->in.used + l; size = client->in.used + l;
client->in.used = size; client->in.used = size;
return aisl_client_parse(client, data, size); return aisl_client_parse(client, data, size);
} } else if (l<0) {
else if (l<0)
{
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
if (client->ssl) if (client->ssl) {
{
if (SSL_get_error(client->ssl, l) == SSL_ERROR_WANT_READ) if (SSL_get_error(client->ssl, l) == SSL_ERROR_WANT_READ)
return AISL_IDLE; return AISL_IDLE;
} } else
else
#endif #endif
{ {
if(errno == EWOULDBLOCK) if(errno == EWOULDBLOCK)
return AISL_IDLE; return AISL_IDLE;
DPRINTF("client - %s", strerror(errno)); DPRINTF("client - %s", strerror(errno));
} }
} }
@ -216,17 +177,16 @@ aisl_client_input(aisl_client_t client)
} }
static aisl_status_t static AislStatus
aisl_client_output(aisl_client_t client) aisl_client_output(AislClient client)
{ {
int l; int l;
char * data; char *data;
aisl_stream_t s = client->stream; AislStream s = client->stream;
/* while stream is not flushed, we should raise event */ /* while stream is not flushed, we should raise event */
if( aisl_get_output_event(s) ) if(aisl_get_output_event(s)) {
{
/* in case of chunked output ( subscription for AISL_STREAM_OUTPUT event ) /* in case of chunked output ( subscription for AISL_STREAM_OUTPUT event )
* stream buffer will be initialized with OUTPUT_BUFFER_SIZE size, but * stream buffer will be initialized with OUTPUT_BUFFER_SIZE size, but
* buffer->size will be used to carry amount of stored bytes * buffer->size will be used to carry amount of stored bytes
@ -244,14 +204,9 @@ aisl_client_output(aisl_client_t client)
} }
*/ */
if ( !(l < aisl_stream_get_buffer_size(s) / 2) ) if (!(l < aisl_stream_get_buffer_size(s) / 2)) {
{ aisl_raise(client->server->instance, (void*)s, AISL_EVENT_STREAM_OUTPUT,
aisl_raise( AISL_SUCCESS);
client->server->instance
, (void*)s
, AISL_EVENT_STREAM_OUTPUT
, AISL_SUCCESS
);
} }
} }
@ -268,22 +223,11 @@ aisl_client_output(aisl_client_t client)
send( client->fd, data, l, 0); send( client->fd, data, l, 0);
#endif #endif
if (l > 0) if (l > 0) {
{
aisl_stream_shift(s, l); aisl_stream_shift(s, l);
if ( aisl_stream_is_done(s) ) {
/*
if (s->state == STREAM_RESPONSE_READY && / * flushed * /
s->buffer->size == 0) / * all sent * /
*/
if ( aisl_stream_is_done(s) )
{
/* buffer_clear(s->buffer, 0); */
/* data has been sent */ /* data has been sent */
if (client->flags & FLAG_KEEPALIVE) {
if (client->flags & FLAG_KEEPALIVE)
{
aisl_stream_free(s); aisl_stream_free(s);
client->stream = aisl_stream_new(client, client->next_id++); client->stream = aisl_stream_new(client, client->next_id++);
@ -303,36 +247,30 @@ aisl_client_output(aisl_client_t client)
/* l < 0 */ /* l < 0 */
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
if (client->ssl) if (client->ssl) {
{ if (SSL_get_error(client->ssl, l) == SSL_ERROR_WANT_WRITE)
if ( SSL_get_error(client->ssl, l) == SSL_ERROR_WANT_WRITE )
return AISL_IDLE; return AISL_IDLE;
} } else
else
#endif #endif
{ {
if (errno == EWOULDBLOCK) if (errno == EWOULDBLOCK)
return AISL_IDLE; return AISL_IDLE;
} }
aisl_client_close(client, AISL_SYSCALL_ERROR); aisl_client_close(client, AISL_SYSCALL_ERROR);
return AISL_SYSCALL_ERROR; return AISL_SYSCALL_ERROR;
} }
aisl_client_t AislClient
aisl_client_new( aisl_server_t server, aisl_client_new(AislServer server, int fd, struct sockaddr_in *addr)
int fd,
struct sockaddr_in * addr )
{ {
aisl_client_t client; AislClient client;
aisl_stream_t stream; AislStream stream;
if ( (client = calloc(1, sizeof(struct aisl_client))) != NULL ) if ((client = calloc(1, sizeof (struct aisl_client))) != NULL) {
{
DPRINTF("client alocated"); DPRINTF("client alocated");
memcpy(&client->address, addr, sizeof(struct sockaddr_in)); memcpy(&client->address, addr, sizeof (struct sockaddr_in));
client->server = server; client->server = server;
client->fd = fd; client->fd = fd;
client->next_id = 2; client->next_id = 2;
@ -340,51 +278,40 @@ aisl_client_new( aisl_server_t server,
client->timestamp = time(NULL); client->timestamp = time(NULL);
client->flags = FLAG_KEEPALIVE | FLAG_HANDSHAKE | FLAG_CAN_READ; client->flags = FLAG_KEEPALIVE | FLAG_HANDSHAKE | FLAG_CAN_READ;
if (buffer_init(&client->in, 2*BUFFER_SIZE) != -1) if (buffer_init(&client->in, 2*BUFFER_SIZE) != -1) {
{
DPRINTF("client buffer alocated"); DPRINTF("client buffer alocated");
memcpy(&client->out, &client->in, sizeof(struct buffer)); memcpy(&client->out, &client->in, sizeof (struct buffer));
stream = aisl_stream_new(client, 0); stream = aisl_stream_new(client, 0);
if (stream != NULL) if (stream != NULL) {
{
client->stream = stream; client->stream = stream;
DPRINTF("client stream alocated"); DPRINTF("client stream alocated");
#ifdef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
if (server->ssl) {
SSL_CTX * ssl_ctx = aisl_get_ssl_ctx(server->instance, NULL);
return client; if ((client->ssl = SSL_new(ssl_ctx)) != NULL ) {
#else
SSL_CTX * ssl_ctx;
if ( !server->ssl )
return client;
ssl_ctx = aisl_get_ssl_ctx(server->instance, NULL);
if ((client->ssl = SSL_new(ssl_ctx)) != NULL )
{
SSL_set_fd(client->ssl, fd); SSL_set_fd(client->ssl, fd);
return client; return client;
} }
} else {
return client;
}
#else
return client;
#endif #endif
} }
} }
aisl_client_free(client); aisl_client_free(client);
} }
return NULL; return NULL;
} }
void void
aisl_client_free(aisl_client_t client) aisl_client_free(AislClient client)
{ {
aisl_client_close(client, AISL_SUCCESS); aisl_client_close(client, AISL_SUCCESS);
@ -396,8 +323,6 @@ aisl_client_free(aisl_client_t client)
if (client->in.data) if (client->in.data)
free(client->in.data); free(client->in.data);
/* out buffer is a shared part of input buffer, so no need to free it */
if (client->stream) if (client->stream)
aisl_stream_free(client->stream); aisl_stream_free(client->stream);
@ -405,77 +330,59 @@ aisl_client_free(aisl_client_t client)
} }
aisl_status_t AislStatus
aisl_client_touch(aisl_client_t client, int32_t timeout) aisl_client_touch(AislClient client, int32_t timeout)
{ {
aisl_status_t result = AISL_IDLE, AislStatus result, status;
result = AISL_IDLE;
status = AISL_IDLE; status = AISL_IDLE;
/* input */ /* input */
if (client->flags & FLAG_CAN_READ) if (client->flags & FLAG_CAN_READ) {
{
if ( (result = aisl_client_input(client)) < 0 ) if ( (result = aisl_client_input(client)) < 0 )
return result; return result;
} }
/* output */ /* output */
if (client->flags & FLAG_CAN_WRITE) if (client->flags & FLAG_CAN_WRITE) {
{
if ( (status = aisl_client_output(client)) < 0 ) if ( (status = aisl_client_output(client)) < 0 )
return status; return status;
} }
/*
if ((client->http_version==AISL_HTTP_2_0 || s->state<STREAM_REQUEST_READY) &&
(client_input(client)) ) result = true;
*/
/* output */
/*
s = list_index(client->streams, client->ostream);
if ( s->flags & (STREAM_FLAG_OUTPUT_READY | STREAM_FLAG_OUTPUT_CHUNKED) )
result = client_output(client);
*/
/* update timestamp */
if (result == AISL_IDLE) if (result == AISL_IDLE)
result = status; result = status;
if (result == AISL_SUCCESS) if (result != AISL_SUCCESS) {
client->timestamp = time(NULL);
else
{
time_t now; time_t now;
time(&now); time(&now);
if ( !(now - client->timestamp < timeout) ) if (!(now - client->timestamp < timeout)) {
{
aisl_client_close(client, result); aisl_client_close(client, result);
} }
} else {
client->timestamp = time(NULL);
} }
return result; return result;
} }
int int
aisl_client_get_socket(aisl_client_t client) aisl_client_get_socket(AislClient client)
{ {
return client->fd; return client->fd;
} }
bool bool
aisl_client_get_keepalive(aisl_client_t client) aisl_client_get_keepalive(AislClient client)
{ {
return (client->flags & FLAG_KEEPALIVE); return (client->flags & FLAG_KEEPALIVE);
} }
void void
aisl_client_set_keepalive(aisl_client_t client, bool value) aisl_client_set_keepalive(AislClient client, bool value)
{ {
if (value) if (value)
client->flags |= FLAG_KEEPALIVE; client->flags |= FLAG_KEEPALIVE;
@ -488,8 +395,8 @@ aisl_client_set_keepalive(aisl_client_t client, bool value)
/* API Level ---------------------------------------------------------------- */ /* API Level ---------------------------------------------------------------- */
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_server_t AislServer
aisl_client_get_server(aisl_client_t client) aisl_client_get_server(AislClient client)
{ {
return client->server; return client->server;
} }
@ -497,7 +404,7 @@ aisl_client_get_server(aisl_client_t client)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
bool bool
aisl_client_is_secure(aisl_client_t client) aisl_client_is_secure(AislClient client)
{ {
#ifdef AISL_WITHOUT_SSL #ifdef AISL_WITHOUT_SSL
return false; return false;
@ -509,7 +416,7 @@ aisl_client_is_secure(aisl_client_t client)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
bool bool
aisl_client_is_online(aisl_client_t client) aisl_client_is_online(AislClient client)
{ {
return (client->fd == -1) ? false : true; return (client->fd == -1) ? false : true;
} }
@ -517,15 +424,15 @@ aisl_client_is_online(aisl_client_t client)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_client_disconnect(aisl_client_t client) aisl_client_disconnect(AislClient client)
{ {
aisl_client_close(client, AISL_SUCCESS); aisl_client_close(client, AISL_SUCCESS);
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_http_version_t AislHttpVersion
aisl_client_get_http_version(aisl_client_t client) aisl_client_get_http_version(AislClient client)
{ {
return client->http_version; return client->http_version;
} }
@ -533,7 +440,7 @@ aisl_client_get_http_version(aisl_client_t client)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_client_get_address( aisl_client_t client, struct sockaddr_in * address) aisl_client_get_address(AislClient client, struct sockaddr_in *address)
{ {
memcpy(address, &client->address, sizeof(struct sockaddr_in)); memcpy(address, &client->address, sizeof (struct sockaddr_in));
} }

View File

@ -1,3 +1,17 @@
/******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file client.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of aisl_client structure and functions
*
* @see https://lowenware.com/aisl/
*/
#ifndef AISL_CLIENT_H_164FE6B2_E5D4_4968_B50F_823E30E8F777 #ifndef AISL_CLIENT_H_164FE6B2_E5D4_4968_B50F_823E30E8F777
#define AISL_CLIENT_H_164FE6B2_E5D4_4968_B50F_823E30E8F777 #define AISL_CLIENT_H_164FE6B2_E5D4_4968_B50F_823E30E8F777
@ -6,86 +20,83 @@
#include <aisl/client.h> #include <aisl/client.h>
#include "buffer.h" #include "buffer.h"
#define AISL_CLIENT(x) ((aisl_client_t) x) #define AISL_CLIENT(x) ((AislClient) x)
struct aisl_client struct aisl_client {
{
struct sockaddr_in address; /**< Client's address structure. */ struct sockaddr_in address; /**< Client's address structure. */
aisl_server_t server; /**< Server instance. */
struct buffer in; /**< Client's input buffer. */ struct buffer in; /**< Client's input buffer. */
struct buffer out; /**< Client's output buffer. */ struct buffer out; /**< Client's output buffer. */
AislServer server; /**< Server instance. */
AislStream stream; /**< Pending client's stream. */
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
SSL * ssl; /**< SSL pointer for HTTPS. */ SSL *ssl; /**< SSL pointer for HTTPS. */
#endif #endif
time_t timestamp; /**< Last communication timestamp. */ time_t timestamp; /**< Last communication timestamp. */
aisl_stream_t stream; /**< Pending client's stream. */
int next_id; /**< Stream id generator (even). */ int next_id; /**< Stream id generator (even). */
int flags; /**< Client's flag bitmask. */ int flags; /**< Client's flag bitmask. */
int fd; /**< Client's socket descriptor. */ int fd; /**< Client's socket descriptor. */
AislHttpVersion http_version; /**< Client's http_version version. */
aisl_http_version_t http_version; /**< Client's http_version version. */
}; };
/** /**
* @brief Constructor for #aisl_client_t instance. * @brief Constructor for #AislClient instance.
* @param server an #aisl_server_t instance pointer. * @param server an #AislServer instance pointer.
* @param fd a client socket descriptor. * @param fd a client socket descriptor.
* @param addr a pointer to client's address structure. * @param addr a pointer to client's address structure.
* @param ssl_ctx a pointer to SSL context or NULL if encryption is disabled * @param ssl_ctx a pointer to SSL context or NULL if encryption is disabled
*/ */
aisl_client_t AislClient
aisl_client_new( aisl_server_t server, aisl_client_new(AislServer server,
int fd, int fd,
struct sockaddr_in * addr ); struct sockaddr_in *addr );
/** /**
* @brief Destructor for #aisl_client_t instance. * @brief Destructor for #AislClient instance.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
*/ */
void void
aisl_client_free(aisl_client_t client); aisl_client_free(AislClient client);
/** /**
* @brief Does all HTTP client routines. * @brief Does all HTTP client routines.
* Reads and parses requests, writes responses. * Reads and parses requests, writes responses.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @param timeout an allowed client silence time in seconds. * @param timeout an allowed client silence time in seconds.
* @return #aisl_status_t code. * @return #AislStatus code.
*/ */
aisl_status_t AislStatus
aisl_client_touch(aisl_client_t client, int32_t timeout); aisl_client_touch(AislClient client, int32_t timeout);
/** /**
* @Brief Checks if client is about to keep connection alive. * @Brief Checks if client is about to keep connection alive.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @return true if keepalive mode is on, otherwise false. * @return true if keepalive mode is on, otherwise false.
*/ */
bool bool
aisl_client_get_keepalive(aisl_client_t client); aisl_client_get_keepalive(AislClient client);
/** /**
* @Brief Sets if connection with client must be kept alive. * @Brief Sets if connection with client must be kept alive.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @param value a true to enable keepalive mode, false to disable. * @param value a true to enable keepalive mode, false to disable.
*/ */
void void
aisl_client_set_keepalive(aisl_client_t client, bool value); aisl_client_set_keepalive(AislClient client, bool value);
/** /**
* @brief Gets socket descriptor associated with #aisl_client_t instance. * @brief Gets socket descriptor associated with #AislClient instance.
* @param client an #aisl_client_t instance pointer. * @param client an #AislClient instance pointer.
* @return a client socket descriptor. * @return a client socket descriptor.
*/ */
int int
aisl_client_get_socket(aisl_client_t client); aisl_client_get_socket(AislClient client);
#endif /* !AISL_CLIENT_H */ #endif /* !AISL_CLIENT_H */

View File

@ -20,33 +20,14 @@
#include "debug.h" #include "debug.h"
#include "http.h" #include "http.h"
struct http_request
{
char * method;
char * schema;
char * host;
char * port;
char * path;
char * version;
char * newline;
int32_t method_len;
int32_t schema_len;
int32_t host_len;
int32_t port_len;
int32_t path_len;
int32_t version_len;
};
typedef struct http_request * http_request_t; static AislHttpMethod
http_method_from_string(const char *method, int32_t length)
static aisl_http_method_t
http_method_from_string( const char * method, int32_t length )
{ {
int i; int i;
aisl_http_method_t methods[3] = {0, 0, 0}; AislHttpMethod methods[3] = {0, 0, 0};
switch(length) switch(length) {
{
case 3: case 3:
methods[0] = AISL_HTTP_GET; methods[0] = AISL_HTTP_GET;
methods[1] = AISL_HTTP_PUT; methods[1] = AISL_HTTP_PUT;
@ -72,8 +53,7 @@ http_method_from_string( const char * method, int32_t length )
break; break;
} }
for (i=0; i<sizeof(methods)/sizeof(aisl_http_method_t); i++) for (i=0; i<sizeof (methods)/sizeof (AislHttpMethod); i++) {
{
if (!(methods[i])) if (!(methods[i]))
break; break;
@ -85,11 +65,10 @@ http_method_from_string( const char * method, int32_t length )
} }
static aisl_http_version_t static AislHttpVersion
http_version_from_string(const char * version_string) http_version_from_string(const char *version_string)
{ {
if (strncmp(version_string, "HTTP/", 5)==0) if (strncmp(version_string, "HTTP/", 5)==0) {
{
if (strncmp(&version_string[5], "0.9", 3)==0) return AISL_HTTP_0_9; if (strncmp(&version_string[5], "0.9", 3)==0) return AISL_HTTP_0_9;
if (strncmp(&version_string[5], "1.0", 3)==0) return AISL_HTTP_1_0; if (strncmp(&version_string[5], "1.0", 3)==0) return AISL_HTTP_1_0;
if (strncmp(&version_string[5], "1.1", 3)==0) return AISL_HTTP_1_1; if (strncmp(&version_string[5], "1.1", 3)==0) return AISL_HTTP_1_1;
@ -101,8 +80,8 @@ http_version_from_string(const char * version_string)
/* Library Level */ /* Library Level */
http_parser_t ParserStatus
http_10_parse_request(char * data, int32_t * p_size, aisl_stream_t stream) http_10_parse_request(char *data, int32_t *p_size, AislStream stream)
{ {
/* STEP 1. Split data according to HTTP request format /* STEP 1. Split data according to HTTP request format
* *
@ -118,24 +97,23 @@ http_10_parse_request(char * data, int32_t * p_size, aisl_stream_t stream)
* | +------------------------------------------------------------ uri * | +------------------------------------------------------------ uri
* +---------------------------------------------------------------- method * +---------------------------------------------------------------- method
*/ */
char * uri = NULL, char *uri = NULL,
* uri_end = NULL, *uri_end = NULL,
* host = NULL, *host = NULL,
* port = NULL, *port = NULL,
* path = NULL, *path = NULL,
* query = NULL, *query = NULL,
* version = NULL, *version = NULL,
* newline = NULL, *newline = NULL,
* method = data, *method = data,
* method_end = NULL; *method_end = NULL;
aisl_http_method_t http_method; AislHttpMethod http_method;
aisl_http_version_t http_version; AislHttpVersion http_version;
int32_t size = *p_size; int32_t size = *p_size;
while(!newline && size--) while(!newline && size--) {
{
switch(*data) switch(*data)
{ {
case ' ': case ' ':
@ -155,14 +133,13 @@ http_10_parse_request(char * data, int32_t * p_size, aisl_stream_t stream)
break; break;
case '/': case '/':
if (!path && data > host) if (!path && data > host) {
{
path = data; path = data;
if (!uri) if (!uri)
uri = path; uri = path;
} } else if (version && data-version != 4) {
else if (version && data-version != 4)
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
}
break; break;
case '?': case '?':
@ -188,19 +165,16 @@ http_10_parse_request(char * data, int32_t * p_size, aisl_stream_t stream)
version = data; version = data;
else if (version && data-version > 7) else if (version && data-version > 7)
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
} }
data++; data++;
} }
/* STEP 2. Verifly splitting was completed */ /* STEP 2. Verifly splitting was completed */
/* Was request sent? */ /* Was request sent? */
if (!newline) if (!newline)
return HTTP_PARSER_HUNGRY; return HTTP_PARSER_HUNGRY;
/* Check mandatory parts presence */ /* Check mandatory parts presence */
if (!method_end || !path || !uri_end || !version) if (!method_end || !path || !uri_end || !version)
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
@ -216,15 +190,13 @@ http_10_parse_request(char * data, int32_t * p_size, aisl_stream_t stream)
if ((http_version = http_version_from_string(version))==0) if ((http_version = http_version_from_string(version))==0)
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
if (query) if (query) {
{
*(query-1)=0; *(query-1)=0;
} } else {
else
query = uri_end; query = uri_end;
}
if (host) if (host) {
{
if (strncmp(uri, "http://", 7) || strncmp(uri, "https://", 8)) if (strncmp(uri, "http://", 7) || strncmp(uri, "https://", 8))
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
@ -233,40 +205,35 @@ http_10_parse_request(char * data, int32_t * p_size, aisl_stream_t stream)
} }
stream->client->http_version = http_version; stream->client->http_version = http_version;
aisl_stream_set_request(stream, http_method, path, query); aisl_stream_set_request(stream, http_method, path, query);
if (host) if (host)
aisl_stream_set_header(stream, "host", host); aisl_stream_set_header(stream, "host", host);
/* how many characters has been read */ /* how many characters has been read */
*(p_size)-=size; *(p_size)-=size;
return HTTP_PARSER_SUCCESS; return HTTP_PARSER_SUCCESS;
} }
http_parser_t ParserStatus
http_10_parse_header(char * data, int32_t * p_size, aisl_stream_t stream) http_10_parse_header(char *data, int32_t *p_size, AislStream stream)
{ {
int32_t size = *p_size; int32_t size = *p_size;
char * key = data, char *key = data,
* colon = NULL, *colon = NULL,
* val = NULL, *val = NULL,
* val_end = NULL, *val_end = NULL,
* newline = NULL; *newline = NULL;
while(!newline && size-- ) while(!newline && size-- ) {
{ switch(*data) {
switch(*data)
{
case ' ': case ' ':
if (val && !val_end) if (val && !val_end)
val_end = data; val_end = data;
break; break;
case ':': case ':':
if (!colon) if (!colon) {
{
if (colon == key) if (colon == key)
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
@ -283,19 +250,15 @@ http_10_parse_header(char * data, int32_t * p_size, aisl_stream_t stream)
break; break;
default: default:
if (!colon) if (!colon) {
{
*data = tolower(*data); *data = tolower(*data);
} } else if (!val) {
else if (!val)
{
if (colon) if (colon)
val = data; val = data;
} }
if (val_end) if (val_end)
val_end = NULL; val_end = NULL;
} }
data++; data++;
} }
@ -303,35 +266,30 @@ http_10_parse_header(char * data, int32_t * p_size, aisl_stream_t stream)
if (!newline) if (!newline)
return HTTP_PARSER_HUNGRY; return HTTP_PARSER_HUNGRY;
if (colon && val && val_end) if (colon && val && val_end) {
{
*colon = 0; *colon = 0;
*val_end = 0; *val_end = 0;
aisl_stream_set_header(stream, key, val); aisl_stream_set_header(stream, key, val);
*p_size -= size; *p_size -= size;
return HTTP_PARSER_SUCCESS; return HTTP_PARSER_SUCCESS;
} else if (newline == key || (newline == key+1 && *key == '\r')) {
return (aisl_stream_set_end_of_headers(stream) == 0) ?
HTTP_PARSER_READY : HTTP_PARSER_SUCCESS;
} }
else if (newline == key || (newline == key+1 && *key == '\r'))
{
return (aisl_stream_set_end_of_headers(stream) == 0) ? HTTP_PARSER_READY :
HTTP_PARSER_SUCCESS;
}
return HTTP_PARSER_ERROR; return HTTP_PARSER_ERROR;
} }
http_parser_t ParserStatus
http_10_parse_body(char * data, int32_t * p_size, aisl_stream_t stream) http_10_parse_body(char *data, int32_t *p_size, AislStream stream)
{ {
switch (aisl_stream_set_body(stream, data, *p_size)) switch (aisl_stream_set_body(stream, data, *p_size)) {
{ case 0:
case 0: return HTTP_PARSER_READY; return HTTP_PARSER_READY;
case -1: return HTTP_PARSER_ERROR; case -1:
default: return HTTP_PARSER_SUCCESS; return HTTP_PARSER_ERROR;
default:
return HTTP_PARSER_SUCCESS;
} }
} }
@ -340,14 +298,17 @@ http_10_parse_body(char * data, int32_t * p_size, aisl_stream_t stream)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
const char * const char *
aisl_http_version_to_string(aisl_http_version_t version) aisl_http_version_to_string(AislHttpVersion version)
{ {
switch (version) switch (version) {
{ case AISL_HTTP_0_9:
case AISL_HTTP_0_9: return "HTTP/0.9"; return "HTTP/0.9";
case AISL_HTTP_1_0: return "HTTP/1.0"; case AISL_HTTP_1_0:
case AISL_HTTP_1_1: return "HTTP/1.1"; return "HTTP/1.0";
case AISL_HTTP_2_0: return "HTTP/2.0"; case AISL_HTTP_1_1:
return "HTTP/1.1";
case AISL_HTTP_2_0:
return "HTTP/2.0";
} }
return ""; return "";
} }
@ -355,14 +316,12 @@ aisl_http_version_to_string(aisl_http_version_t version)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
const char * const char *
aisl_http_response_to_string(aisl_http_response_t code) aisl_http_response_to_string(AislHttpResponse code)
{ {
switch (code) switch (code) {
{
/* most common for faster behavior */ /* most common for faster behavior */
case AISL_HTTP_OK: return "OK"; case AISL_HTTP_OK: return "OK";
case AISL_HTTP_MOVED_PERMANENTLY: return "Moved Permanently"; case AISL_HTTP_MOVED_PERMANENTLY: return "Moved Permanently";
/* informational */ /* informational */
case AISL_HTTP_CONTINUE: return "Continue"; case AISL_HTTP_CONTINUE: return "Continue";
case AISL_HTTP_SWITCHING_PROTOCOLS: return "Switching Protocols"; case AISL_HTTP_SWITCHING_PROTOCOLS: return "Switching Protocols";
@ -407,19 +366,16 @@ aisl_http_response_to_string(aisl_http_response_t code)
case AISL_HTTP_SERVICE_UNAVAILABLE: return "Service Unavailable"; case AISL_HTTP_SERVICE_UNAVAILABLE: return "Service Unavailable";
case AISL_HTTP_GATEWAY_TIMEOUT: return "Gateway Timeout"; case AISL_HTTP_GATEWAY_TIMEOUT: return "Gateway Timeout";
case AISL_HTTP_VERSION_NOT_SUPPORTED: return "HTTP Version Not Supported"; case AISL_HTTP_VERSION_NOT_SUPPORTED: return "HTTP Version Not Supported";
} }
return ""; return "";
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
const char * const char *
aisl_http_method_to_string( aisl_http_method_t method ) aisl_http_method_to_string( AislHttpMethod method )
{ {
switch(method) switch(method) {
{
case AISL_HTTP_GET: return "GET"; case AISL_HTTP_GET: return "GET";
case AISL_HTTP_PUT: return "PUT"; case AISL_HTTP_PUT: return "PUT";
case AISL_HTTP_POST: return "POST"; case AISL_HTTP_POST: return "POST";
@ -428,14 +384,8 @@ aisl_http_method_to_string( aisl_http_method_t method )
case AISL_HTTP_DELETE: return "DELETE"; case AISL_HTTP_DELETE: return "DELETE";
case AISL_HTTP_OPTIONS: return "OPTIONS"; case AISL_HTTP_OPTIONS: return "OPTIONS";
case AISL_HTTP_CONNECT: return "CONNECT"; case AISL_HTTP_CONNECT: return "CONNECT";
case AISL_HTTP_PRI: return "PRI"; case AISL_HTTP_PRI: return "PRI";
case AISL_HTTP_METHOD_UNKNOWN: break; case AISL_HTTP_METHOD_UNKNOWN: break;
} }
return ""; return "";
} }

View File

@ -18,26 +18,24 @@
#include <aisl/types.h> #include <aisl/types.h>
typedef enum typedef enum {
{
HTTP_PARSER_SUCCESS HTTP_PARSER_SUCCESS
, HTTP_PARSER_READY , HTTP_PARSER_READY
, HTTP_PARSER_HUNGRY , HTTP_PARSER_HUNGRY
, HTTP_PARSER_ERROR , HTTP_PARSER_ERROR
} ParserStatus;
} http_parser_t;
http_parser_t ParserStatus
http_10_parse_request(char * data, int32_t * size, aisl_stream_t stream); http_10_parse_request(char *data, int32_t *size, AislStream stream);
http_parser_t ParserStatus
http_10_parse_header(char * data, int32_t * size, aisl_stream_t stream); http_10_parse_header(char *data, int32_t *size, AislStream stream);
http_parser_t ParserStatus
http_10_parse_body(char * data, int32_t * size, aisl_stream_t stream); http_10_parse_body(char *data, int32_t *size, AislStream stream);
#endif /* !AISL_HTTP_H */ #endif /* !AISL_HTTP_H */

View File

@ -22,10 +22,8 @@
#include "debug.h" #include "debug.h"
#include "str-utils.h" #include "str-utils.h"
#include "buffer.h" #include "buffer.h"
#include "client.h" #include "client.h"
#include "server.h" #include "server.h"
//#include "globals.h"
#include "stream.h" #include "stream.h"
#include "instance.h" #include "instance.h"
@ -34,41 +32,34 @@
static uint32_t m_instances = 0; static uint32_t m_instances = 0;
static struct aisl_ssl *
static const aisl_ssl_t aisl_new_ssl(AislInstance instance, const struct aisl_cfg_ssl *cfg_ssl)
aisl_new_ssl( aisl_t instance, const aisl_cfg_ssl_t cfg_ssl)
{ {
SSL_CTX * ssl_ctx = NULL; SSL_CTX *ssl_ctx = NULL;
aisl_ssl_t * list = instance->ssl, struct aisl_ssl **list, *ssl;
ssl;
list = instance->ssl;
/* lookup for existing contexts */ /* lookup for existing contexts */
while( (ssl = *list) ) while ((ssl = *list)) {
{
if (ssl->key_file && strcmp(ssl->key_file, cfg_ssl->key_file)==0 && if (ssl->key_file && strcmp(ssl->key_file, cfg_ssl->key_file)==0 &&
ssl->crt_file && strcmp(ssl->crt_file, cfg_ssl->crt_file)==0 ) ssl->crt_file && strcmp(ssl->crt_file, cfg_ssl->crt_file)==0
{ ) {
ssl_ctx = ssl->ctx; ssl_ctx = ssl->ctx;
break; break;
} }
list++; list++;
} }
ssl = aisl_ssl_new( cfg_ssl->host, ssl = aisl_ssl_new(cfg_ssl->host, cfg_ssl->key_file, cfg_ssl->crt_file,
cfg_ssl->key_file, ssl_ctx);
cfg_ssl->crt_file,
ssl_ctx );
if (ssl) if (ssl) {
{ if (!ssl_ctx && !aisl_ssl_get_ctx(ssl, (void*) instance)) {
if ( !ssl_ctx && !aisl_ssl_get_ctx(ssl, (void*) instance))
{
aisl_ssl_free(ssl); aisl_ssl_free(ssl);
ssl = NULL; ssl = NULL;
} }
} }
return ssl; return ssl;
} }
@ -78,45 +69,43 @@ aisl_new_ssl( aisl_t instance, const aisl_cfg_ssl_t cfg_ssl)
/* Initialization functions */ /* Initialization functions */
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_t AislInstance
aisl_new( aisl_cfg_t cfg ) aisl_new(const struct aisl_cfg *cfg)
{ {
aisl_t instance; int i;
AislInstance instance;
/* allocate root structure */ /* allocate root structure */
if ( !(instance = calloc(1, sizeof(struct aisl))) ) if (!(instance = calloc(1, sizeof (struct aisl_instance))))
goto finally; goto finally;
/* allocate servers */ /* allocate servers */
if ( !(instance->srv = calloc(cfg->srv_cnt+1, sizeof(aisl_server_t))) ) if (!(instance->srv = calloc(cfg->srv_cnt+1, sizeof (AislServer))))
goto release; goto release;
for (int i=0; i<cfg->srv_cnt; i++) for (i = 0; i < cfg->srv_cnt; i++) {
{
DPRINTF("new srv %d", i); DPRINTF("new srv %d", i);
if (!(instance->srv[i] = aisl_server_new(&cfg->srv[i], instance))) if (!(instance->srv[i] = aisl_server_new(&cfg->srv[i], instance)))
goto release; goto release;
} }
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
if ((m_instances++) == 0) if ((m_instances++) == 0) {
{
SSL_load_error_strings(); SSL_load_error_strings();
OpenSSL_add_ssl_algorithms(); OpenSSL_add_ssl_algorithms();
} }
if ( !(instance->ssl = calloc(cfg->ssl_cnt+1, sizeof(aisl_ssl_t))) ) if (!(instance->ssl = calloc(cfg->ssl_cnt+1, sizeof (struct aisl_ssl))))
goto release; goto release;
for (int i=0; i<cfg->ssl_cnt; i++) for (i=0; i<cfg->ssl_cnt; i++) {
{
DPRINTF("new ssl %d", i); DPRINTF("new ssl %d", i);
if (!(instance->ssl[i] = aisl_new_ssl(instance, &cfg->ssl[i]))) if (!(instance->ssl[i] = aisl_new_ssl(instance, &cfg->ssl[i])))
goto release; goto release;
} }
#endif #endif
if ( list_init(&instance->client_spool, cfg->client_spool_size) == -1 ) if (list_init(&instance->client_spool, cfg->client_spool_size) == -1)
goto release; goto release;
instance->accept_limit = cfg->client_accept_limit; instance->accept_limit = cfg->client_accept_limit;
@ -137,42 +126,33 @@ finally:
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_free( aisl_t instance ) aisl_free(AislInstance instance)
{ {
if (instance->srv) if (instance->srv) {
{ AislServer * srv = instance->srv;
aisl_server_t * srv = instance->srv;
while (*srv) while (*srv) {
{ aisl_server_free(*(srv++));
aisl_server_free(*srv);
srv++;
} }
free(instance->srv); free(instance->srv);
} }
list_release(&instance->client_spool, (list_destructor_t) aisl_client_free ); list_release(&instance->client_spool, (list_destructor_t)aisl_client_free);
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
if (instance->ssl) if (instance->ssl) {
{ struct aisl_ssl **ssl = instance->ssl;
aisl_ssl_t * ssl = instance->ssl;
while (*ssl) while (*ssl) {
{ aisl_ssl_free(*(ssl++));
aisl_ssl_free(*ssl);
ssl++;
} }
free(instance->ssl); free(instance->ssl);
} }
if ((--m_instances) == 0) if ((--m_instances) == 0) {
{
EVP_cleanup(); EVP_cleanup();
} }
#endif #endif
free(instance); free(instance);
@ -182,24 +162,20 @@ aisl_free( aisl_t instance )
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
SSL_CTX * SSL_CTX *
aisl_get_ssl_ctx( aisl_t instance, const char * host ) aisl_get_ssl_ctx(AislInstance instance, const char * host)
{ {
aisl_ssl_t * list = instance->ssl, struct aisl_ssl **list, *ssl;
ssl;
if (host) list = instance->ssl;
{
while ( (ssl = *list) ) if (host) {
{ while ((ssl = *list)) {
if (str_cmpi(ssl->host, host) == 0) if (str_cmpi(ssl->host, host) == 0) {
{
return ssl->ctx; return ssl->ctx;
} }
list++; list++;
} }
} }
return NULL; return NULL;
} }
@ -207,12 +183,12 @@ aisl_get_ssl_ctx( aisl_t instance, const char * host )
void void
aisl_raise_evt( aisl_t instance, aisl_evt_t const evt ) aisl_raise_evt(AislInstance instance, const struct aisl_evt *evt)
{ {
#ifdef AISL_WITHOUT_STRINGIFIERS #ifdef AISL_WITHOUT_STRINGIFIERS
DPRINTF("! %d", evt->code); DPRINTF("! %d", evt->code);
#else #else
DPRINTF("! %s", aisl_evt_code_to_string(evt->code)); DPRINTF("! %s", aisl_event_to_string(evt->code));
#endif #endif
if (instance->callback) if (instance->callback)
@ -221,10 +197,10 @@ aisl_raise_evt( aisl_t instance, aisl_evt_t const evt )
void void
aisl_raise( aisl_t instance, aisl_raise(AislInstance instance,
void * source, void *source,
aisl_evt_code_t code, AislEvent code,
aisl_status_t status ) AislStatus status)
{ {
struct aisl_evt evt; struct aisl_evt evt;
@ -237,98 +213,82 @@ aisl_raise( aisl_t instance,
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_status_t AislStatus
aisl_run_cycle( aisl_t instance ) aisl_run_cycle(AislInstance instance)
{ {
aisl_status_t result = AISL_IDLE; AislStatus result = AISL_IDLE;
AislServer *list, srv;
AislClient cli;
int32_t i;
aisl_server_t * list = instance->srv, list = instance->srv;
srv;
aisl_client_t cli;
while ( (srv = *list) ) while ((srv = *list)) {
{
cli = NULL; cli = NULL;
if (aisl_server_touch(srv, &cli) != AISL_IDLE) if (aisl_server_touch(srv, &cli) != AISL_IDLE)
result = AISL_SUCCESS; result = AISL_SUCCESS;
if (cli) if (cli) {
{
DPRINTF("Accepted %p", (void*)cli); DPRINTF("Accepted %p", (void*)cli);
if ( list_append(&instance->client_spool, cli) == -1 ) if (list_append(&instance->client_spool, cli) == -1)
aisl_client_free(cli); aisl_client_free(cli);
} }
list++; list++;
} }
for (int32_t i=0; i < instance->client_spool.count; i++) for (i=0; i < instance->client_spool.count; i++) {
{
cli = LIST_INDEX(instance->client_spool, i); cli = LIST_INDEX(instance->client_spool, i);
if (aisl_client_touch(cli, instance->silence_timeout) != AISL_IDLE) if (aisl_client_touch(cli, instance->silence_timeout) != AISL_IDLE)
result = AISL_SUCCESS; result = AISL_SUCCESS;
/* if (aisl_client_is_timed_out( c, instance->silence_timeout ) ) if (!aisl_client_is_online(cli)) {
aisl_raise( instance, c, AISL_EVENT_CLIENT_TIMEOUT );
*/
if ( !aisl_client_is_online(cli) )
{
aisl_client_free( cli ); aisl_client_free( cli );
list_remove_index(&instance->client_spool, i); list_remove_index(&instance->client_spool, i);
} }
} }
return result; return result;
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_status_t AislStatus
aisl_sleep( aisl_t instance, uint32_t usec ) aisl_sleep(AislInstance instance, uint32_t usec)
{ {
int maxfd=0, AislServer *list, srv;
sd; int sd, maxfd = 0;
size_t i; size_t i;
struct timeval timeout = {0,usec}; struct timeval timeout = {0,usec};
memset(&timeout, 0, sizeof(struct timeval)); memset(&timeout, 0, sizeof (struct timeval));
timeout.tv_usec = usec; timeout.tv_usec = usec;
fd_set fs; fd_set fs;
FD_ZERO (&fs); FD_ZERO (&fs);
aisl_server_t * list = instance->srv, list = instance->srv;
srv;
while ( (srv = *list) ) while ((srv = *list)) {
{
sd = aisl_server_get_socket(srv); sd = aisl_server_get_socket(srv);
if (sd != -1) if (sd != -1) {
{
FD_SET(sd, &fs); FD_SET(sd, &fs);
if (sd > maxfd) maxfd = sd; if (sd > maxfd) maxfd = sd;
} }
list++; list++;
} }
for (i=0; i<instance->client_spool.count; i++) for (i=0; i<instance->client_spool.count; i++) {
{ AislClient c = LIST_INDEX(instance->client_spool, i);
aisl_client_t c = LIST_INDEX(instance->client_spool, i);
sd = aisl_client_get_socket(c); sd = aisl_client_get_socket(c);
if (sd != -1) if (sd != -1) {
{
FD_SET(sd, &fs); FD_SET(sd, &fs);
if (sd > maxfd) maxfd = sd; if (sd > maxfd) maxfd = sd;
} }
} }
switch ( select(maxfd+1, &fs, NULL, NULL, &timeout) ) switch (select(maxfd+1, &fs, NULL, NULL, &timeout)) {
{
case -1: case -1:
return AISL_SYSCALL_ERROR; return AISL_SYSCALL_ERROR;
@ -339,4 +299,3 @@ aisl_sleep( aisl_t instance, uint32_t usec )
return AISL_SUCCESS; return AISL_SUCCESS;
} }
} }

View File

@ -1,10 +1,16 @@
/* /******************************************************************************
* src/instance.h
* *
* Copyright (C) 2019 Ilja Kartašov <ik@lowenware.com> * Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
* *
* Project homepage: https://lowenware.com/aisl/ ******************************************************************************/
/**
* @file src/instance.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief Declarations of aisl_instance structure and functions
* *
* @see https://lowenware.com/aisl/
*/ */
#ifndef AISL_INSTANCE_H_814CF474_A646_45B7_B6B2_3F4C7BEFA484 #ifndef AISL_INSTANCE_H_814CF474_A646_45B7_B6B2_3F4C7BEFA484
@ -19,15 +25,14 @@
#include "list.h" #include "list.h"
struct aisl struct aisl_instance {
{ AislServer *srv;
aisl_server_t * srv;
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
aisl_ssl_t * ssl; struct aisl_ssl * *ssl;
#endif #endif
struct list client_spool; struct list client_spool;
aisl_callback_t callback; AislCallback callback;
void * p_ctx; void *p_ctx;
uint32_t accept_limit; uint32_t accept_limit;
uint32_t silence_timeout; uint32_t silence_timeout;
@ -38,28 +43,28 @@ struct aisl
#ifndef AISL_WITHOUT_SSL #ifndef AISL_WITHOUT_SSL
/** /**
* @brief Gets SSL context for appropriate server name. * @brief Gets SSL context for appropriate server name.
* @param instance a pointer to #aisl_t instance. * @param instance a pointer to #AislInstance instance.
* @param server_name a null-terminated string with server name or NULL. * @param server_name a null-terminated string with server name or NULL.
* @return a pointer to SSL context * @return a pointer to SSL context
*/ */
SSL_CTX * SSL_CTX *
aisl_get_ssl_ctx( aisl_t instance, const char * server_name ); aisl_get_ssl_ctx(AislInstance instance, const char *server_name);
#endif #endif
/** /**
* @brief Raises event from source. * @brief Raises event from source.
* @param instance a pointer to #aisl_t instance. * @param instance a pointer to #AislInstance instance.
* @param evt a pointer to event structure. * @param evt a pointer to event structure.
*/ */
void void
aisl_raise_evt( aisl_t instance, aisl_evt_t const evt ); aisl_raise_evt(AislInstance instance, const struct aisl_evt *evt);
void void
aisl_raise( aisl_t instance, aisl_raise(AislInstance instance,
void * source, void *source,
aisl_evt_code_t code, AislEvent code,
aisl_status_t status ); AislStatus status);
#endif /* !AISL_INSTANCE_H */ #endif /* !AISL_INSTANCE_H */

View File

@ -18,33 +18,29 @@
#include "list.h" #include "list.h"
int32_t int32_t
list_init(list_t list, int32_t size) list_init(struct list *list, int32_t size)
{ {
if ((list->data = calloc(size, sizeof(void*))) != NULL) if ((list->data = calloc(size, sizeof (void*))) != NULL) {
{
list->size = size; list->size = size;
list->count = 0; list->count = 0;
return 0; return 0;
} }
return -1; return -1;
} }
void void
list_release(list_t list, list_destructor_t destructor) list_release(struct list *list, list_destructor_t destructor)
{ {
if (list->data) if (list->data) {
{ if (destructor) {
if (destructor)
{
int32_t i; int32_t i;
for (i=0; i<list->count; i++)
{
void * ptr;
if ( (ptr = list->data[i]) != NULL) for (i=0; i<list->count; i++) {
destructor( list->data[i] ); void *ptr;
if ((ptr = list->data[i]) != NULL)
destructor(list->data[i]);
} }
} }
free(list->data); free(list->data);
@ -53,20 +49,19 @@ list_release(list_t list, list_destructor_t destructor)
int32_t int32_t
list_append(list_t list, void * entry) list_append(struct list *list, void *entry)
{ {
int32_t pos = list->count; int32_t pos = list->count;
DPRINTF("pos = %d", pos); DPRINTF("pos = %d", pos);
if ( !(pos < list->size) ) if (!(pos < list->size)) {
{
DPRINTF("extending, size = %d", list->size); DPRINTF("extending, size = %d", list->size);
void ** new_list; void **new_list;
int32_t new_size = pos + 1; int32_t new_size = pos + 1;
DPRINTF("extending, ptr = %p", (void*)list->data); DPRINTF("extending, ptr = %p", (void*)list->data);
if ( (new_list = realloc( list->data, new_size*sizeof(void*) )) == NULL ) if ((new_list = realloc( list->data, new_size*sizeof (void*) )) == NULL)
return -1; return -1;
list->data = new_list; list->data = new_list;
@ -81,18 +76,16 @@ list_append(list_t list, void * entry)
void * void *
list_remove_index(list_t list, int32_t index) list_remove_index(struct list *list, int32_t index)
{ {
void * result; void *result;
if (index < list->count) if (index < list->count) {
{
int32_t i, c = --list->count; int32_t i, c = --list->count;
result = list->data[index]; result = list->data[index];
for (i=index; i<c; i++) for (i = index; i<c; i++) {
{
list->data[i]=list->data[i+1]; list->data[i]=list->data[i+1];
} }
} }

View File

@ -6,11 +6,11 @@
******************************************************************************/ ******************************************************************************/
/** /**
* @file list.h * @file src/list.h
* @author Ilja Kartašov <ik@lowenware.com> * @author Ilja Kartašov <ik@lowenware.com>
* @brief List module header file * @brief List module header file
* *
* @see https://lowenware.com/ * @see https://lowenware.com/aisl/
*/ */
#ifndef AISL_LIST_H_21495B65_111D_40F7_840F_CC50D9D324A1 #ifndef AISL_LIST_H_21495B65_111D_40F7_840F_CC50D9D324A1
@ -21,34 +21,31 @@
#define LIST_INDEX(L, I) ( L.data[ I ] ) #define LIST_INDEX(L, I) ( L.data[ I ] )
struct list struct list {
{ void **data;
void ** data;
int32_t size; int32_t size;
int32_t count; int32_t count;
}; };
typedef struct list * list_t;
typedef void typedef void
(* list_destructor_t)(void * list_item); (* list_destructor_t)(void *list_item);
int32_t int32_t
list_init(list_t list, int32_t size); list_init(struct list *lst, int32_t size);
void void
list_release(list_t list, list_destructor_t destructor); list_release(struct list *lst, list_destructor_t destructor);
int32_t int32_t
list_append(list_t list, void * entry); list_append(struct list *lst, void * entry);
void * void *
list_remove_index(list_t list, int32_t index); list_remove_index(struct list *lst, int32_t index);
#endif /* !AISL_LIST_H */ #endif /* !AISL_LIST_H */

View File

@ -6,15 +6,12 @@
#include <unistd.h> #include <unistd.h>
#include <netdb.h> #include <netdb.h>
#include <fcntl.h> #include <fcntl.h>
#include <openssl/ssl.h>
#ifdef __APPLE__ #ifdef __APPLE__
#include <sys/ioctl.h> #include <sys/ioctl.h>
#endif #endif
#include <openssl/ssl.h>
#include "debug.h" #include "debug.h"
#include "str-utils.h" #include "str-utils.h"
#include "instance.h" #include "instance.h"
@ -24,36 +21,33 @@
/** /**
* @brief Creates TCP server socket, binds to address and starts to listen. * @brief Creates TCP server socket, binds to address and starts to listen.
* @param server a pointer to #aisl_server_t instance. * @param server a pointer to #AislServer instance.
* @return #aisl_status_t code. * @return #AislStatus code.
*/ */
static aisl_status_t static AislStatus
aisl_server_open(aisl_server_t server) aisl_server_open(AislServer server)
{ {
int fd, s_opt = 1; int fd, s_opt = 1;
fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (fd != -1) if (fd != -1) {
{ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&s_opt,
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&s_opt, sizeof(int)); sizeof (int));
#ifdef __APPLE__ #ifdef __APPLE__
ioctl(fd, FIONBIO, (char *)&s_opt); ioctl(fd, FIONBIO, (char *)&s_opt);
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
#endif #endif
s_opt = sizeof(struct sockaddr_in); s_opt = sizeof (struct sockaddr_in);
if (bind(fd, (struct sockaddr *) &server->address, s_opt)==0) if (bind(fd, (struct sockaddr *) &server->address, s_opt) == 0) {
{ if (listen(fd, SOMAXCONN) == 0) {
if (listen(fd, SOMAXCONN) == 0)
{
server->fd = fd; server->fd = fd;
return AISL_SUCCESS; return AISL_SUCCESS;
} }
} }
close(fd); close(fd);
} }
@ -63,87 +57,71 @@ aisl_server_open(aisl_server_t server)
/** /**
* @brief Tries to accept a new client. * @brief Tries to accept a new client.
* @param server a pointer to #aisl_server_t instance. * @param server a pointer to #AislServer instance.
* @param p_client a pointer to store #aisl_client_t instance pointer. * @param p_client a pointer to store #AislClient instance pointer.
* @return #aisl_status_t code. * @return #AislStatus code.
*/ */
static aisl_status_t static AislStatus
aisl_server_accept( aisl_server_t server, aisl_server_accept(AislServer server, AislClient *p_client )
aisl_client_t * p_client )
{ {
int fd; int fd;
struct sockaddr_in addr; struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in); socklen_t len = sizeof (struct sockaddr_in);
fd = accept(server->fd, (struct sockaddr *) &addr, &len); fd = accept(server->fd, (struct sockaddr *)&addr, &len);
if (fd != -1) if (fd != -1) {
{
int flags; int flags;
DPRINTF("accepted fd=%d", fd); DPRINTF("accepted fd=%d", fd);
if ((flags = fcntl(fd, F_GETFL, 0)) != -1) if ((flags = fcntl(fd, F_GETFL, 0)) != -1) {
{
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) == 0) if (fcntl(fd, F_SETFL, flags) == 0) {
{
return (!(*p_client = aisl_client_new(server, fd, &addr))) ? return (!(*p_client = aisl_client_new(server, fd, &addr))) ?
AISL_MALLOC_ERROR : AISL_SUCCESS; AISL_MALLOC_ERROR : AISL_SUCCESS;
} }
} }
close(fd); close(fd);
} } else if (errno == EWOULDBLOCK) {
else if (errno == EWOULDBLOCK)
return AISL_IDLE; return AISL_IDLE;
}
return AISL_SYSCALL_ERROR; return AISL_SYSCALL_ERROR;
} }
/* Library Level ------------------------------------------------------------ */ /* Library Level ------------------------------------------------------------ */
aisl_status_t AislStatus
aisl_server_touch( aisl_server_t server, aisl_server_touch(AislServer server, AislClient *p_client)
aisl_client_t * p_client )
{ {
aisl_status_t result; AislStatus result;
if (server->fd == -1) if (server->fd == -1) {
{ if ((result = aisl_server_open(server)) != AISL_IDLE) {
aisl_raise(server->instance, server, ((result == AISL_SUCCESS) ?
if ((result = aisl_server_open(server)) != AISL_IDLE) AISL_EVENT_SERVER_READY : AISL_EVENT_SERVER_ERROR), result);
{
aisl_raise(
server->instance,
server,
((result == AISL_SUCCESS) ? AISL_EVENT_SERVER_OPEN
: AISL_EVENT_SERVER_ERROR),
result
);
} }
} } else {
else
result = aisl_server_accept(server, p_client); result = aisl_server_accept(server, p_client);
}
return result; return result;
} }
int int
aisl_server_get_socket( aisl_server_t server ) aisl_server_get_socket(AislServer server)
{ {
return server->fd; return server->fd;
} }
aisl_server_t AislServer
aisl_server_new(aisl_cfg_srv_t const cfg_srv, aisl_t instance) aisl_server_new(const struct aisl_cfg_srv *cfg_srv, AislInstance instance)
{ {
aisl_server_t server; AislServer server;
if ( (server = calloc(1, sizeof(struct aisl_server))) != NULL ) if ((server = calloc(1, sizeof (struct aisl_server))) != NULL) {
{
server->instance = instance; server->instance = instance;
server->fd = -1; server->fd = -1;
server->address.sin_family = AF_INET; server->address.sin_family = AF_INET;
@ -151,22 +129,18 @@ aisl_server_new(aisl_cfg_srv_t const cfg_srv, aisl_t instance)
server->address.sin_port = htons(cfg_srv->port); server->address.sin_port = htons(cfg_srv->port);
server->ssl = cfg_srv->secure; server->ssl = cfg_srv->secure;
} }
return server; return server;
} }
void void
aisl_server_free(aisl_server_t server) aisl_server_free(AislServer server)
{ {
if (server) if (server) {
{ if ( server->fd != -1) {
if ( server->fd != -1)
{
close(server->fd); close(server->fd);
server->fd=-1; server->fd=-1;
} }
free(server); free(server);
} }
} }
@ -177,9 +151,9 @@ aisl_server_free(aisl_server_t server)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_server_get_address( aisl_server_t server, struct sockaddr_in * address) aisl_server_get_address(AislServer server, struct sockaddr_in *address)
{ {
memcpy(address, &server->address, sizeof(struct sockaddr_in)); memcpy(address, &server->address, sizeof (struct sockaddr_in));
} }
@ -187,7 +161,7 @@ aisl_server_get_address( aisl_server_t server, struct sockaddr_in * address)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
bool bool
aisl_server_get_ssl( aisl_server_t server ) aisl_server_get_ssl(AislServer server)
{ {
return server->ssl; return server->ssl;
} }

View File

@ -1,4 +1,17 @@
/* /******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file dummy.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*//*
* @file src/server.h * @file src/server.h
* *
* Copyright (c) 2017-2019 by Löwenware Ltd. * Copyright (c) 2017-2019 by Löwenware Ltd.
@ -13,60 +26,58 @@
#include <aisl/config.h> #include <aisl/config.h>
#include <aisl/server.h> #include <aisl/server.h>
#define AISL_SERVER(x) ((aisl_server_t) x) #define AISL_SERVER(x) ((AislServer) x)
/** /**
* @brief HTTP(s) server data structure represented by #aisl_server_t pointer. * @brief HTTP(s) server data structure represented by #AislServer pointer.
*/ */
struct aisl_server struct aisl_server {
{
struct sockaddr_in address; /**< TCP server address to listen to. */ struct sockaddr_in address; /**< TCP server address to listen to. */
aisl_t instance; /**< Associated AISL instance pointer. */ AislInstance instance; /**< Associated AISL instance pointer. */
int fd; /**< System socket descriptor. */ int fd; /**< System socket descriptor. */
bool ssl; /**< SSL enabled/disabled flag. */ bool ssl; /**< SSL enabled/disabled flag. */
}; };
/** /**
* @brief Allocates and instance of #aisl_server_t. * @brief Allocates and instance of #AislServer.
* @param cfg_srv a pointer to server configuration structure. * @param cfg_srv a pointer to server configuration structure.
* @param instance a pointer to #aisl_t instance. * @param instance a pointer to #AislInstance instance.
* @return a pointer to #aisl_server_t instance. * @return a pointer to #AislServer instance.
*/ */
aisl_server_t AislServer
aisl_server_new(aisl_cfg_srv_t const cfg_srv, aisl_t instance); aisl_server_new(const struct aisl_cfg_srv *cfg_srv, AislInstance instance);
/** /**
* @brief Frees memory allocated for #aisl_server_t instance. * @brief Frees memory allocated for #AislServer instance.
* @param server a pointer to #aisl_server_t instance. * @param server a pointer to #AislServer instance.
*/ */
void void
aisl_server_free(aisl_server_t server); aisl_server_free(AislServer server);
/** /**
* @brief Does server routines. * @brief Does server routines.
* Tries to open server if it was not opened yet, otherwise tries to accept a * Tries to open server if it was not opened yet, otherwise tries to accept a
* new client connecting to the server. * new client connecting to the server.
* @param server a pointer to #aisl_server_t instance. * @param server a pointer to #AislServer instance.
* @param p_client a pointer to store #aisl_client_t instance pointer. * @param p_client a pointer to store #AislClient instance pointer.
* @return #aisl_status_t code: * @return #AislStatus code:
* - AISL_SUCCESS if client connected, * - AISL_SUCCESS if client connected,
* - AISL_IDLE if there is no client to connect, * - AISL_IDLE if there is no client to connect,
* - AISL_SYSCALL_ERROR if error occured. * - AISL_SYSCALL_ERROR if error occured.
*/ */
aisl_status_t AislStatus
aisl_server_touch( aisl_server_t server, aisl_server_touch(AislServer server, AislClient *p_client);
aisl_client_t * p_client );
/** /**
* @brief Gets a socket descriptor associated with HTTP client. * @brief Gets a socket descriptor associated with HTTP client.
* @param server a pointer to #aisl_server_t instance. * @param server a pointer to #AislServer instance.
* @return a client socket descriptor. * @return a client socket descriptor.
*/ */
int int
aisl_server_get_socket(aisl_server_t server); aisl_server_get_socket(AislServer server);
#endif /* !AISL_SERVER_H */ #endif /* !AISL_SERVER_H */

View File

@ -15,16 +15,13 @@
static int static int
aisl_ssl_on_get_ctx( SSL * ssl, int * ptr, void * instance ) aisl_ssl_on_get_ctx(SSL *ssl, int *ptr, void *instance )
{ {
const char * server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
SSL_CTX *ctx = aisl_get_ssl_ctx((AislInstance) instance, server_name);
SSL_CTX * ctx = aisl_get_ssl_ctx( (aisl_t) instance, server_name );
if (ctx) if (ctx)
{
SSL_set_SSL_CTX(ssl, ctx); SSL_set_SSL_CTX(ssl, ctx);
}
(void)ptr; (void)ptr;
@ -33,14 +30,12 @@ aisl_ssl_on_get_ctx( SSL * ssl, int * ptr, void * instance )
SSL_CTX * SSL_CTX *
aisl_ssl_get_ctx(aisl_ssl_t ssl, void * p_instance) aisl_ssl_get_ctx(struct aisl_ssl *ssl, void *p_instance)
{ {
SSL_CTX * ctx; SSL_CTX * ctx;
if ( (ctx = SSL_CTX_new(SSLv23_server_method())) != NULL ) if ((ctx = SSL_CTX_new(SSLv23_server_method())) != NULL) {
{
SSL_CTX_set_ecdh_auto(ctx, 1); SSL_CTX_set_ecdh_auto(ctx, 1);
SSL_CTX_set_tlsext_servername_callback( ctx, aisl_ssl_on_get_ctx ); SSL_CTX_set_tlsext_servername_callback( ctx, aisl_ssl_on_get_ctx );
SSL_CTX_set_tlsext_servername_arg( ctx, p_instance ); SSL_CTX_set_tlsext_servername_arg( ctx, p_instance );
@ -60,29 +55,22 @@ except:
return NULL; return NULL;
} }
aisl_ssl_t struct aisl_ssl *
aisl_ssl_new( const char * key_file, aisl_ssl_new(const char *key_file,
const char * crt_file, const char *crt_file,
const char * host, const char *host,
SSL_CTX * ctx ) SSL_CTX *ctx)
{ {
aisl_ssl_t ssl; struct aisl_ssl *ssl;
if ((ssl = calloc(1, sizeof(struct aisl_ssl))) != NULL) if ((ssl = calloc(1, sizeof (struct aisl_ssl))) != NULL) {
{ if ((ssl->host = str_copy( host ? host : "*" )) != NULL) {
if ((ssl->host = str_copy( host ? host : "*" )) != NULL) if (ctx) {
{
if (ctx)
{
ssl->ctx = ctx; ssl->ctx = ctx;
return ssl; return ssl;
} } else {
else if ((ssl->key_file = str_copy(key_file)) != NULL) {
{ if ((ssl->crt_file = str_copy(crt_file)) != NULL) {
if ((ssl->key_file = str_copy(key_file)) != NULL)
{
if ((ssl->crt_file = str_copy(crt_file)) != NULL)
{
return ssl; return ssl;
} }
} }
@ -90,19 +78,17 @@ aisl_ssl_new( const char * key_file,
} }
aisl_ssl_free(ssl); aisl_ssl_free(ssl);
} }
return NULL; return NULL;
} }
void void
aisl_ssl_free( aisl_ssl_t ssl ) aisl_ssl_free( struct aisl_ssl *ssl )
{ {
if (ssl->host) if (ssl->host)
free(ssl->host); free(ssl->host);
if (ssl->key_file) if (ssl->key_file) {
{
free(ssl->key_file); free(ssl->key_file);
SSL_CTX_free(ssl->ctx); SSL_CTX_free(ssl->ctx);
} }
@ -113,5 +99,4 @@ aisl_ssl_free( aisl_ssl_t ssl )
free(ssl); free(ssl);
} }
#endif #endif

View File

@ -1,4 +1,17 @@
/* /******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file dummy.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*//*
* src/ssl.h * src/ssl.h
* *
* Copyright (C) 2019 Ilja Kartašov <ik@lowenware.com> * Copyright (C) 2019 Ilja Kartašov <ik@lowenware.com>
@ -15,30 +28,27 @@
#include <openssl/ssl.h> #include <openssl/ssl.h>
struct aisl_ssl struct aisl_ssl {
{ char *key_file;
char * key_file; char *crt_file;
char * crt_file; char *host;
char * host; SSL_CTX *ctx;
SSL_CTX * ctx;
}; };
typedef struct aisl_ssl * aisl_ssl_t;
struct aisl_ssl *
aisl_ssl_t aisl_ssl_new( const char *key_file,
aisl_ssl_new( const char * key_file, const char *crt_file,
const char * crt_file, const char *host,
const char * host, SSL_CTX *ctx );
SSL_CTX * ctx );
SSL_CTX * SSL_CTX *
aisl_ssl_get_ctx(aisl_ssl_t ssl, void * p_instance); aisl_ssl_get_ctx(struct aisl_ssl *ssl, void *p_instance);
void void
aisl_ssl_free( aisl_ssl_t ssl ); aisl_ssl_free(struct aisl_ssl *ssl);
#endif /* !AISL_SSL_H */ #endif /* !AISL_SSL_H */

View File

@ -20,11 +20,11 @@
char * char *
str_copy(const char * source) str_copy(const char *source)
{ {
int l = strlen(source); int l = strlen(source);
char * result = malloc(l+1); char *result = malloc(l+1);
if (result) if (result)
strcpy(result, source); strcpy(result, source);
@ -34,12 +34,11 @@ str_copy(const char * source)
int int
str_cmpi(const char * s1, const char * s2) str_cmpi(const char *s1, const char *s2)
{ {
char c1, c2, r = 0; char c1, c2, r = 0;
do do {
{
c1 = tolower(*(s1++)); c1 = tolower(*(s1++));
c2 = tolower(*(s2++)); c2 = tolower(*(s2++));

View File

@ -18,9 +18,9 @@
char * char *
str_copy(const char * source); str_copy(const char *source);
int int
str_cmpi(const char * anycase, const char * lowcase); str_cmpi(const char *anycase, const char *lowcase);
#endif /* !STR_UTILS_H */ #endif /* !STR_UTILS_H */

View File

@ -21,16 +21,11 @@
/* Library level */ /* Library level */
static void static void
aisl_stream_reset(aisl_stream_t stream, bool initial) aisl_stream_reset(AislStream stream, bool initial)
{ {
if (!initial) if (!initial) {
{ aisl_raise(aisl_stream_get_instance(stream), (void*) stream,
aisl_raise( AISL_EVENT_STREAM_CLOSE, AISL_SUCCESS);
aisl_stream_get_instance(stream)
, (void*) stream
, AISL_EVENT_STREAM_CLOSE
, AISL_SUCCESS
);
} }
buffer_release(&stream->buffer); buffer_release(&stream->buffer);
@ -44,24 +39,22 @@ aisl_stream_reset(aisl_stream_t stream, bool initial)
} }
aisl_stream_t AislStream
aisl_stream_new(aisl_client_t client, int id) aisl_stream_new(AislClient client, int id)
{ {
aisl_stream_t stream = calloc(1, sizeof(struct aisl_stream)); AislStream stream = calloc(1, sizeof (struct aisl_stream));
if (stream) if (stream) {
{
stream->id = id; stream->id = id;
stream->client = client; stream->client = client;
aisl_stream_reset(stream, true); aisl_stream_reset(stream, true);
} }
return stream; return stream;
} }
void void
aisl_stream_free(aisl_stream_t stream) aisl_stream_free(AislStream stream)
{ {
aisl_stream_reset(stream, false); aisl_stream_reset(stream, false);
free(stream); free(stream);
@ -69,21 +62,21 @@ aisl_stream_free(aisl_stream_t stream)
int32_t int32_t
aisl_stream_get_buffer_space(aisl_stream_t stream) aisl_stream_get_buffer_space(AislStream stream)
{ {
return stream->buffer.size - stream->buffer.used; return stream->buffer.size - stream->buffer.used;
} }
int32_t int32_t
aisl_stream_get_buffer_size(aisl_stream_t stream) aisl_stream_get_buffer_size(AislStream stream)
{ {
return stream->buffer.size; return stream->buffer.size;
} }
char * char *
aisl_stream_get_data(aisl_stream_t stream, int32_t * p_length) aisl_stream_get_data(AislStream stream, int32_t *p_length)
{ {
*p_length = stream->buffer.used; *p_length = stream->buffer.used;
@ -92,42 +85,38 @@ aisl_stream_get_data(aisl_stream_t stream, int32_t * p_length)
void void
aisl_stream_shift(aisl_stream_t stream, int32_t offset) aisl_stream_shift(AislStream stream, int32_t offset)
{ {
buffer_shift(&stream->buffer, offset); buffer_shift(&stream->buffer, offset);
} }
bool bool
aisl_stream_is_done(aisl_stream_t stream) aisl_stream_is_done(AislStream stream)
{ {
return (!stream->buffer.used && stream->state == AISL_STREAM_STATE_DONE); return (!stream->buffer.used && stream->state == AISL_STREAM_STATE_DONE);
} }
aisl_stream_state_t AislStreamState
aisl_stream_get_state(aisl_stream_t stream) aisl_stream_get_state(AislStream stream)
{ {
return stream->state; return stream->state;
} }
void void
aisl_stream_set_request( aisl_stream_t stream, aisl_stream_set_request(AislStream stream,
aisl_http_method_t http_method, AislHttpMethod http_method,
const char * path, const char *path,
const char * query ) const char *query)
{ {
struct aisl_evt_stream_open on_open; struct aisl_evt_open on_open;
stream->state = AISL_STREAM_STATE_WAIT_HEADER; stream->state = AISL_STREAM_STATE_WAIT_HEADER;
DPRINTF( DPRINTF("%s -> path: %s, query: %s", aisl_http_method_to_string(http_method),
"%s -> path: %s, query: %s" path, query);
, aisl_http_method_to_string(http_method)
, path
, query
);
on_open.evt.code = AISL_EVENT_STREAM_OPEN; on_open.evt.code = AISL_EVENT_STREAM_OPEN;
on_open.evt.source = (void *) stream; on_open.evt.source = (void *) stream;
@ -136,27 +125,24 @@ aisl_stream_set_request( aisl_stream_t stream,
on_open.path = path; on_open.path = path;
on_open.query = query; on_open.query = query;
aisl_raise_evt(aisl_stream_get_instance(stream), (aisl_evt_t)&on_open); aisl_raise_evt(aisl_stream_get_instance(stream), (struct aisl_evt *)&on_open);
} }
void void
aisl_stream_set_header( aisl_stream_t stream, aisl_stream_set_header(AislStream stream, const char *key, const char *value)
const char * key,
const char * value )
{ {
struct aisl_evt_stream_header on_header; struct aisl_evt_header on_header;
if (stream->state != AISL_STREAM_STATE_WAIT_HEADER) if (stream->state != AISL_STREAM_STATE_WAIT_HEADER)
return; return;
if (strcmp(key, "content-length")==0) if (strcmp(key, "content-length") == 0) {
stream->content_length = strtoll(value, NULL, 10); stream->content_length = strtoll(value, NULL, 10);
else if (strcmp(key, "connection")==0) } else if (strcmp(key, "connection") == 0) {
aisl_client_set_keepalive( aisl_client_set_keepalive(stream->client,
stream->client (str_cmpi(value, "close")==0) ? false : true);
, (str_cmpi(value, "close")==0) ? false : true }
);
DPRINTF("%s: %s", key, value); DPRINTF("%s: %s", key, value);
@ -166,44 +152,40 @@ aisl_stream_set_header( aisl_stream_t stream,
on_header.key = key; on_header.key = key;
on_header.value = value; on_header.value = value;
aisl_raise_evt(aisl_stream_get_instance(stream), (aisl_evt_t)&on_header); aisl_raise_evt(aisl_stream_get_instance(stream),
(struct aisl_evt *) &on_header);
} }
int int
aisl_stream_set_end_of_headers( aisl_stream_t stream ) aisl_stream_set_end_of_headers(AislStream stream)
{ {
int result; int result;
if (stream->state == AISL_STREAM_STATE_WAIT_HEADER) if (stream->state == AISL_STREAM_STATE_WAIT_HEADER) {
{
stream->state = AISL_STREAM_STATE_WAIT_BODY; stream->state = AISL_STREAM_STATE_WAIT_BODY;
result = (stream->content_length == 0); result = (stream->content_length == 0);
} } else {
else
result = 2; result = 2;
}
return result; return result;
} }
int int
aisl_stream_set_body( aisl_stream_t stream, const char * data, int32_t size ) aisl_stream_set_body(AislStream stream, const char *data, int32_t size)
{ {
int result; int result;
if (stream->state == AISL_STREAM_STATE_WAIT_BODY) if (stream->state == AISL_STREAM_STATE_WAIT_BODY) {
{ if (!(stream->content_length < size)) {
if ( !(stream->content_length < size) ) struct aisl_evt_input on_input;
{
struct aisl_evt_stream_input on_input;
if (stream->content_length == 0) if (stream->content_length == 0) {
{
stream->state = AISL_STREAM_STATE_READY; stream->state = AISL_STREAM_STATE_READY;
result = 0; result = 0;
} } else {
else
result = 1; result = 1;
}
on_input.evt.code = AISL_EVENT_STREAM_INPUT; on_input.evt.code = AISL_EVENT_STREAM_INPUT;
on_input.evt.source = (void *) stream; on_input.evt.source = (void *) stream;
@ -211,14 +193,14 @@ aisl_stream_set_body( aisl_stream_t stream, const char * data, int32_t size )
on_input.data = data; on_input.data = data;
on_input.size = size; on_input.size = size;
aisl_raise_evt(stream->client->server->instance, (aisl_evt_t)&on_input); aisl_raise_evt(stream->client->server->instance,
} (struct aisl_evt *) &on_input);
else } else {
result = -1; result = -1;
} }
else } else {
result = 2; result = 2;
}
return result; return result;
} }
@ -226,7 +208,7 @@ aisl_stream_set_body( aisl_stream_t stream, const char * data, int32_t size )
/* Why it was here? /* Why it was here?
static int static int
aisl_stream_write(aisl_stream_t stream, const char * data, uint32_t d_len) aisl_stream_write(AislStream stream, const char * data, uint32_t d_len)
{ {
return buffer_add( &stream->buffer, data, d_len); return buffer_add( &stream->buffer, data, d_len);
} }
@ -234,7 +216,7 @@ aisl_stream_write(aisl_stream_t stream, const char * data, uint32_t d_len)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_cancel(aisl_stream_t stream) aisl_cancel(AislStream stream)
{ {
aisl_client_close( stream->client ); aisl_client_close( stream->client );
} }
@ -242,7 +224,7 @@ aisl_cancel(aisl_stream_t stream)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void * void *
aisl_get_context(aisl_stream_t s) aisl_get_context(AislStream s)
{ {
return s->u_ptr; return s->u_ptr;
} }
@ -250,31 +232,31 @@ aisl_get_context(aisl_stream_t s)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_set_context(aisl_stream_t s, void * u_ptr) aisl_set_context(AislStream s, void *u_ptr)
{ {
s->u_ptr = u_ptr; s->u_ptr = u_ptr;
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_client_t AislClient
aisl_get_client(aisl_stream_t s) aisl_get_client(AislStream s)
{ {
return s->client; return s->client;
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_server_t AislServer
aisl_get_server(aisl_stream_t s) aisl_get_server(AislStream s)
{ {
return aisl_client_get_server(s->client); return aisl_client_get_server(s->client);
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_http_version_t AislHttpVersion
aisl_get_http_version(aisl_stream_t s) aisl_get_http_version(AislStream s)
{ {
return aisl_client_get_http_version(s->client); return aisl_client_get_http_version(s->client);
} }
@ -282,31 +264,28 @@ aisl_get_http_version(aisl_stream_t s)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_reject(aisl_stream_t s) aisl_reject(AislStream s)
{ {
aisl_client_disconnect( s->client ); aisl_client_disconnect( s->client );
} }
static aisl_status_t static AislStatus
aisl_start_response(aisl_stream_t stream) aisl_start_response(AislStream stream)
{ {
return aisl_response( stream return aisl_response(stream, AISL_HTTP_OK, AISL_AUTO_LENGTH);
, AISL_HTTP_OK
, AISL_AUTO_LENGTH );
} }
static aisl_status_t static AislStatus
aisl_stream_close_headers(aisl_stream_t stream) aisl_stream_close_headers(AislStream stream)
{ {
int32_t l; int32_t l;
if (aisl_start_response(stream) == AISL_MALLOC_ERROR) if (aisl_start_response(stream) == AISL_MALLOC_ERROR)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
if (!(stream->flags & FLAG_SERVER_HEADER_SENT)) if (!(stream->flags & FLAG_SERVER_HEADER_SENT)) {
{
l = buffer_append( &stream->buffer, "Server: AISL\r\n", 14); l = buffer_append( &stream->buffer, "Server: AISL\r\n", 14);
if (l == -1) if (l == -1)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
@ -314,24 +293,21 @@ aisl_stream_close_headers(aisl_stream_t stream)
stream->flags |= FLAG_SERVER_HEADER_SENT; stream->flags |= FLAG_SERVER_HEADER_SENT;
} }
if (!(stream->flags & FLAG_CONTENT_TYPE_HEADER_SENT)) if (!(stream->flags & FLAG_CONTENT_TYPE_HEADER_SENT)) {
{ l = buffer_append(&stream->buffer,
l = buffer_append( &stream->buffer "Content-type: text/html; encoding=utf-8\r\n", 41);
, "Content-type: text/html; encoding=utf-8\r\n"
, 41);
if (l == -1) if (l == -1)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
stream->flags |= FLAG_CONTENT_TYPE_HEADER_SENT; stream->flags |= FLAG_CONTENT_TYPE_HEADER_SENT;
} }
if (!(stream->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) if (!(stream->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) {
{ if (stream->content_length == AISL_AUTO_LENGTH) {
if (stream->content_length == AISL_AUTO_LENGTH) l = buffer_append_printf(&stream->buffer, "Content-length: %"PRIu64"\r\n",
{ stream->content_length);
l = buffer_append_printf( &stream->buffer
, "Content-length: %"PRIu64"\r\n"
, stream->content_length );
if (l == -1) if (l == -1)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
@ -339,12 +315,10 @@ aisl_stream_close_headers(aisl_stream_t stream)
} }
} }
if (!(stream->flags & FLAG_CONNECTION_HEADER_SENT)) if (!(stream->flags & FLAG_CONNECTION_HEADER_SENT)) {
{ l = buffer_append_printf(&stream->buffer, "Connection: %s\r\n",
l = buffer_append_printf( &stream->buffer (aisl_client_get_keepalive(stream->client) ? "keepalive" : "close"));
, "Connection: %s\r\n"
, (aisl_client_get_keepalive(stream->client) ?
"keepalive" : "close"));
if (l == -1) if (l == -1)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
@ -355,7 +329,6 @@ aisl_stream_close_headers(aisl_stream_t stream)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
stream->body_offset = stream->buffer.used; stream->body_offset = stream->buffer.used;
stream->state = AISL_STREAM_STATE_SEND_BODY; stream->state = AISL_STREAM_STATE_SEND_BODY;
return AISL_SUCCESS; return AISL_SUCCESS;
@ -363,35 +336,28 @@ aisl_stream_close_headers(aisl_stream_t stream)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_status_t AislStatus
aisl_response( aisl_stream_t stream aisl_response(AislStream stream, AislHttpResponse rs_code, uint64_t c_len)
, aisl_http_response_t status_code
, uint64_t content_length )
{ {
int32_t l; int32_t l;
/* check if those headers were already sent */ /* check if those headers were already sent */
if (stream->state > AISL_STREAM_STATE_READY) return AISL_IDLE; if (stream->state > AISL_STREAM_STATE_READY)
return AISL_IDLE;
stream->http_response = status_code; stream->http_response = rs_code;
stream->content_length = content_length; stream->content_length = c_len;
buffer_init( &stream->buffer buffer_init(&stream->buffer, (c_len != AISL_AUTO_LENGTH) ? c_len : 0);
, (content_length != AISL_AUTO_LENGTH) ? content_length : 0);
l = buffer_append_printf( l = buffer_append_printf(&stream->buffer, "%s %d %s\r\n",
&stream->buffer aisl_http_version_to_string(stream->client->http_version), rs_code,
, "%s %d %s\r\n" aisl_http_response_to_string(rs_code));
, aisl_http_version_to_string(stream->client->http_version)
, status_code
, aisl_http_response_to_string(status_code)
);
if ( l == -1 ) if (l == -1)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
stream->head_offset = l; stream->head_offset = l;
stream->state = AISL_STREAM_STATE_SEND_HEADER; stream->state = AISL_STREAM_STATE_SEND_HEADER;
return AISL_SUCCESS; return AISL_SUCCESS;
@ -399,22 +365,16 @@ aisl_response( aisl_stream_t stream
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_status_t AislStatus
aisl_flush(aisl_stream_t s) aisl_flush(AislStream s)
{ {
if (!(s->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) if (!(s->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) {
{
char hdr[ 40 ]; char hdr[ 40 ];
uint64_t c_len = s->buffer.used - s->body_offset; uint64_t c_len = s->buffer.used - s->body_offset;
int32_t l; int32_t l;
l = snprintf(hdr, sizeof(hdr), "Content-length: %"PRIu64"\r\n", c_len); l = snprintf(hdr, sizeof (hdr), "Content-length: %"PRIu64"\r\n", c_len);
l = buffer_insert(&s->buffer, s->body_offset - 2, hdr, l);
l = buffer_insert( &s->buffer
, s->body_offset - 2
, hdr
, l );
if (l == -1) if (l == -1)
return AISL_MALLOC_ERROR; return AISL_MALLOC_ERROR;
@ -430,54 +390,42 @@ aisl_flush(aisl_stream_t s)
static int32_t static int32_t
aisl_stream_verify_header( aisl_stream_t stream, aisl_stream_verify_header(AislStream stream, const char *key, const char *value)
const char * key,
const char * value )
{ {
if (stream->state < AISL_STREAM_STATE_SEND_HEADER) if (stream->state < AISL_STREAM_STATE_SEND_HEADER) {
{
if (aisl_start_response(stream) != AISL_SUCCESS) if (aisl_start_response(stream) != AISL_SUCCESS)
return -1; return -1;
} } else if (stream->state > AISL_STREAM_STATE_SEND_HEADER) {
else if (stream->state > AISL_STREAM_STATE_SEND_HEADER)
return 0; return 0;
}
if (!(stream->flags & FLAG_CONNECTION_HEADER_SENT)) if (!(stream->flags & FLAG_CONNECTION_HEADER_SENT)) {
{ if (str_cmpi(key, "connection")==0) {
if (str_cmpi(key, "connection")==0)
{
stream->flags |= FLAG_CONNECTION_HEADER_SENT; stream->flags |= FLAG_CONNECTION_HEADER_SENT;
if (value) if (value) {
{ aisl_client_set_keepalive(stream->client,
aisl_client_set_keepalive( stream->client (str_cmpi(value, "keepalive") == 0));
, (str_cmpi(value, "keepalive")==0) );
} }
return 1; return 1;
} }
} }
if (!(stream->flags & FLAG_CONTENT_TYPE_HEADER_SENT)) if (!(stream->flags & FLAG_CONTENT_TYPE_HEADER_SENT)) {
{ if (str_cmpi(key, "content-type") == 0) {
if (str_cmpi(key, "content-type")==0)
{
stream->flags |= FLAG_CONTENT_TYPE_HEADER_SENT; stream->flags |= FLAG_CONTENT_TYPE_HEADER_SENT;
return 1; return 1;
} }
} }
if (!(stream->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) if (!(stream->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) {
{ if (str_cmpi(key, "content-length") == 0) {
if (str_cmpi(key, "content-length")==0)
{
stream->flags |= FLAG_CONTENT_LENGTH_HEADER_SENT; stream->flags |= FLAG_CONTENT_LENGTH_HEADER_SENT;
return 1; return 1;
} }
} }
if (!(stream->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) if (!(stream->flags & FLAG_CONTENT_LENGTH_HEADER_SENT)) {
{ if (str_cmpi(key, "content-length")==0) {
if (str_cmpi(key, "content-length")==0)
{
stream->flags |= FLAG_CONTENT_LENGTH_HEADER_SENT; stream->flags |= FLAG_CONTENT_LENGTH_HEADER_SENT;
return 1; return 1;
} }
@ -488,18 +436,14 @@ aisl_stream_verify_header( aisl_stream_t stream,
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int32_t int32_t
aisl_header(aisl_stream_t stream, const char *key, const char *value) aisl_header(AislStream stream, const char *key, const char *value)
{ {
int32_t result; int32_t result;
if ( (result = aisl_stream_verify_header( stream, key, value )) != 1) if ( (result = aisl_stream_verify_header( stream, key, value )) != 1)
return result; return result;
result = buffer_append_printf( &stream->buffer result = buffer_append_printf(&stream->buffer, "%s: %s\r\n", key, value);
, "%s: %s\r\n"
, key
, value );
return result; return result;
@ -531,16 +475,13 @@ aisl_header(aisl_stream_t stream, const char *key, const char *value)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int32_t int32_t
aisl_header_printf(aisl_stream_t stream, const char *key, aisl_header_printf(AislStream stream, const char *key, const char *format, ...)
const char *format, ...)
{ {
int32_t result; int32_t result;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
result = aisl_header_vprintf( stream, key, format, args ); result = aisl_header_vprintf( stream, key, format, args );
va_end(args); va_end(args);
return result; return result;
@ -549,68 +490,53 @@ aisl_header_printf(aisl_stream_t stream, const char *key,
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int32_t int32_t
aisl_header_vprintf(aisl_stream_t stream, const char *key, aisl_header_vprintf(AislStream stream,
const char *key,
const char *format, const char *format,
va_list args) va_list args)
{ {
int32_t result, int32_t result, l;
l;
if ( (result = aisl_stream_verify_header( stream, key, NULL )) != 1) if ( (result = aisl_stream_verify_header( stream, key, NULL )) != 1)
return result; return result;
result = buffer_append_printf( &stream->buffer, "%s: ", key ); result = buffer_append_printf( &stream->buffer, "%s: ", key );
if (result != -1) if (result != -1) {
{
l = buffer_append_vprintf( &stream->buffer, format, args ); l = buffer_append_vprintf( &stream->buffer, format, args );
if (l != -1) if (l != -1) {
{
result += l; result += l;
if ((l = buffer_append(&stream->buffer, "\r\n", 2)) != -1) if ((l = buffer_append(&stream->buffer, "\r\n", 2)) != -1) {
{
result += l; result += l;
return result; return result;
} }
} }
} }
return -1; return -1;
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int int
aisl_printf(aisl_stream_t stream, const char *format, ...) aisl_printf(AislStream stream, const char *format, ...)
{ {
int result;
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
result = aisl_vprintf(stream, format, arg);
int result = aisl_vprintf(stream, format, arg);
va_end(arg); va_end(arg);
/* No need to update length there, because vprintf do that
*
* if (stream->c_length_unknown)
stream->c_length += result;
*/
return result; return result;
} }
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int32_t int32_t
aisl_vprintf(aisl_stream_t stream, const char *format, va_list args) aisl_vprintf(AislStream stream, const char *format, va_list args)
{ {
if (stream->state < AISL_STREAM_STATE_SEND_BODY) if (stream->state < AISL_STREAM_STATE_SEND_BODY) {
{
if (aisl_stream_close_headers(stream) != AISL_SUCCESS) if (aisl_stream_close_headers(stream) != AISL_SUCCESS)
return -1; return -1;
} }
@ -620,13 +546,13 @@ aisl_vprintf(aisl_stream_t stream, const char *format, va_list args)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int32_t int32_t
aisl_write(aisl_stream_t stream, const char *data, int32_t d_len) aisl_write(AislStream stream, const char *data, int32_t d_len)
{ {
if (stream->state < AISL_STREAM_STATE_SEND_BODY) if (stream->state < AISL_STREAM_STATE_SEND_BODY) {
{
if (aisl_stream_close_headers(stream) != AISL_SUCCESS) if (aisl_stream_close_headers(stream) != AISL_SUCCESS)
return -1; return -1;
} }
if (d_len == -1) if (d_len == -1)
d_len = strlen(data); d_len = strlen(data);
@ -636,10 +562,9 @@ aisl_write(aisl_stream_t stream, const char *data, int32_t d_len)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
int int
aisl_puts(const char *str, aisl_stream_t stream) aisl_puts(const char *str, AislStream stream)
{ {
if (stream->state < AISL_STREAM_STATE_SEND_BODY) if (stream->state < AISL_STREAM_STATE_SEND_BODY) {
{
if (aisl_stream_close_headers(stream) != AISL_SUCCESS) if (aisl_stream_close_headers(stream) != AISL_SUCCESS)
return -1; return -1;
} }
@ -648,8 +573,8 @@ aisl_puts(const char *str, aisl_stream_t stream)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
aisl_t AislInstance
aisl_stream_get_instance(aisl_stream_t stream) aisl_stream_get_instance(AislStream stream)
{ {
return stream->client->server->instance; return stream->client->server->instance;
} }
@ -657,7 +582,7 @@ aisl_stream_get_instance(aisl_stream_t stream)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
void void
aisl_set_output_event(aisl_stream_t stream, bool value) aisl_set_output_event(AislStream stream, bool value)
{ {
if (value) if (value)
stream->flags |= FLAG_OUTPUT_CHUNKED; stream->flags |= FLAG_OUTPUT_CHUNKED;
@ -668,10 +593,8 @@ aisl_set_output_event(aisl_stream_t stream, bool value)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
bool bool
aisl_get_output_event(aisl_stream_t stream) aisl_get_output_event(AislStream stream)
{ {
return (stream->flags & FLAG_OUTPUT_CHUNKED); return (stream->flags & FLAG_OUTPUT_CHUNKED);
} }

View File

@ -1,11 +1,24 @@
#ifndef AISL_STREAM_H_A9EC6601_34B2_4F3E_B631_EEDA8B6EF0D3 /******************************************************************************
*
* Copyright (c) 2017-2019 by Löwenware Ltd
* Please, refer LICENSE file for legal information
*
******************************************************************************/
/**
* @file dummy.h
* @author Ilja Kartašov <ik@lowenware.com>
* @brief
*
* @see https://lowenware.com/
*/#ifndef AISL_STREAM_H_A9EC6601_34B2_4F3E_B631_EEDA8B6EF0D3
#define AISL_STREAM_H_A9EC6601_34B2_4F3E_B631_EEDA8B6EF0D3 #define AISL_STREAM_H_A9EC6601_34B2_4F3E_B631_EEDA8B6EF0D3
#include <aisl/types.h> #include <aisl/types.h>
#include <aisl/stream.h> #include <aisl/stream.h>
#include "buffer.h" #include "buffer.h"
#define AISL_STREAM(x) ((aisl_stream_t) x) #define AISL_STREAM(x) ((AislStream) x)
typedef enum typedef enum
{ {
@ -21,15 +34,14 @@ typedef enum
, AISL_STREAM_STATE_DONE , AISL_STREAM_STATE_DONE
} aisl_stream_state_t; } AislStreamState;
struct aisl_stream struct aisl_stream {
{
struct buffer buffer; struct buffer buffer;
aisl_client_t client; AislClient client;
void * u_ptr; void *u_ptr;
uint64_t content_length; uint64_t content_length;
int32_t head_offset; int32_t head_offset;
@ -37,60 +49,58 @@ struct aisl_stream
int id; int id;
int flags; int flags;
aisl_http_response_t http_response; AislHttpResponse http_response;
aisl_stream_state_t state; AislStreamState state;
}; };
aisl_stream_t AislStream
aisl_stream_new(aisl_client_t client, int id ); aisl_stream_new(AislClient client, int id);
void void
aisl_stream_free(aisl_stream_t stream); aisl_stream_free(AislStream stream);
int32_t int32_t
aisl_stream_get_buffer_space(aisl_stream_t stream); aisl_stream_get_buffer_space(AislStream stream);
int32_t int32_t
aisl_stream_get_buffer_size(aisl_stream_t stream); aisl_stream_get_buffer_size(AislStream stream);
char * char *
aisl_stream_get_data(aisl_stream_t stream, int32_t * p_length); aisl_stream_get_data(AislStream stream, int32_t *p_length);
void void
aisl_stream_shift(aisl_stream_t stream, int32_t offset); aisl_stream_shift(AislStream stream, int32_t offset);
bool bool
aisl_stream_is_done(aisl_stream_t stream); aisl_stream_is_done(AislStream stream);
aisl_stream_state_t AislStreamState
aisl_stream_get_state(aisl_stream_t stream); aisl_stream_get_state(AislStream stream);
void void
aisl_stream_set_request( aisl_stream_t stream, aisl_stream_set_request(AislStream stream,
aisl_http_method_t http_method, AislHttpMethod http_method,
const char * path, const char *path,
const char * query ); const char *query );
void void
aisl_stream_set_header( aisl_stream_t stream, aisl_stream_set_header(AislStream stream, const char *key, const char *value);
const char * key,
const char * value );
int int
aisl_stream_set_end_of_headers( aisl_stream_t stream ); aisl_stream_set_end_of_headers(AislStream stream);
int int
aisl_stream_set_body( aisl_stream_t stream, const char * data, int32_t size ); aisl_stream_set_body(AislStream stream, const char *data, int32_t size);
#endif /* !AISL_STREAM_H */ #endif /* !AISL_STREAM_H */

View File

@ -20,10 +20,9 @@
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
const char * const char *
aisl_status_to_string(aisl_status_t status) aisl_status_to_string(AislStatus status)
{ {
switch(status) switch(status) {
{
case AISL_INPUT_ERROR: return "AISL_INPUT_ERROR"; case AISL_INPUT_ERROR: return "AISL_INPUT_ERROR";
case AISL_EXTCALL_ERROR: return "AISL_EXTCALL_ERROR"; case AISL_EXTCALL_ERROR: return "AISL_EXTCALL_ERROR";
case AISL_SYSCALL_ERROR: return "AISL_SYSCALL_ERROR"; case AISL_SYSCALL_ERROR: return "AISL_SYSCALL_ERROR";
@ -37,11 +36,10 @@ aisl_status_to_string(aisl_status_t status)
__attribute__ ((visibility ("default") )) __attribute__ ((visibility ("default") ))
const char * const char *
aisl_evt_code_to_string( aisl_evt_code_t evt_code ) aisl_event_to_string(AislEvent evt_code)
{ {
switch(evt_code) switch(evt_code) {
{ case AISL_EVENT_SERVER_READY: return "SERVER READY";
case AISL_EVENT_SERVER_OPEN: return "SERVER OPEN";
case AISL_EVENT_SERVER_ERROR: return "SERVER ERROR"; case AISL_EVENT_SERVER_ERROR: return "SERVER ERROR";
case AISL_EVENT_CLIENT_CONNECT: return "CLIENT CONNECT"; case AISL_EVENT_CLIENT_CONNECT: return "CLIENT CONNECT";
case AISL_EVENT_CLIENT_DISCONNECT: return "CLIENT DISCONNECT"; case AISL_EVENT_CLIENT_DISCONNECT: return "CLIENT DISCONNECT";