Add restd library.
This commit is contained in:
@@ -1,25 +0,0 @@
|
|||||||
# CMakeLists files in this project can
|
|
||||||
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
|
|
||||||
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
|
|
||||||
cmake_minimum_required (VERSION 3.0)
|
|
||||||
|
|
||||||
project (domo)
|
|
||||||
|
|
||||||
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/build/bin")
|
|
||||||
set(ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/lib")
|
|
||||||
set(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/build/lib")
|
|
||||||
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-modules/")
|
|
||||||
|
|
||||||
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libuv ${CMAKE_CURRENT_BINARY_DIR}/libuv)
|
|
||||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../nats.c ${CMAKE_CURRENT_BINARY_DIR}/nats.c)
|
|
||||||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libsourcey ${CMAKE_CURRENT_BINARY_DIR}/libsourcey)
|
|
||||||
add_subdirectory(src)
|
|
||||||
|
|
||||||
add_custom_target (static_analysis
|
|
||||||
COMMAND echo "Static Analysis ......"
|
|
||||||
COMMAND ${CMAKE_SOURCE_DIR}/tools/static_analysis.sh ${CMAKE_SOURCE_DIR}/build
|
|
||||||
)
|
|
||||||
|
|
||||||
add_custom_target (valgrind
|
|
||||||
COMMAND valgrind --leak-check=full --trace-children=yes --malloc-fill=AE --free-fill=BD --track-origins=yes ${CMAKE_SOURCE_DIR}/build/bin/domo-iot
|
|
||||||
)
|
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
https://auth0.com/docs/flows/guides/device-auth/call-api-device-auth
|
https://auth0.com/docs/flows/guides/device-auth/call-api-device-auth
|
||||||
|
|
||||||
|
https://github.com/kozross/awesome-c
|
||||||
|
|
||||||
nikhilm.github.io/uvbook/An%20Introduction%20to%20libuv.pdf
|
nikhilm.github.io/uvbook/An%20Introduction%20to%20libuv.pdf
|
||||||
|
|
||||||
https://github.com/lasote/async_http_request/
|
https://github.com/lasote/async_http_request/
|
||||||
@@ -11,3 +13,9 @@ https://github.com/liigo/tinyweb
|
|||||||
https://github.com/dropbox/json11
|
https://github.com/dropbox/json11
|
||||||
|
|
||||||
https://github.com/lzpong/TinyWeb
|
https://github.com/lzpong/TinyWeb
|
||||||
|
|
||||||
|
https://github.com/sinemetu1/twitc/
|
||||||
|
|
||||||
|
https://kore.io/
|
||||||
|
|
||||||
|
http://facil.io/
|
||||||
|
|||||||
15
lib/CMakeLists.txt
Normal file
15
lib/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
cmake_minimum_required (VERSION 3.0)
|
||||||
|
|
||||||
|
project (librestd)
|
||||||
|
|
||||||
|
add_definitions (-DBUILD_DEBUG)
|
||||||
|
|
||||||
|
include_directories (${CMAKE_SOURCE_DIR}/libevent/include)
|
||||||
|
include_directories (${CMAKE_SOURCE_DIR}/build/libevent/include)
|
||||||
|
include_directories (${CMAKE_SOURCE_DIR}/qlibc/include)
|
||||||
|
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
add_library(restd-static STATIC
|
||||||
|
src/restd_server.c
|
||||||
|
src/restd_http_handler.c
|
||||||
|
)
|
||||||
43
lib/include/restd.h
Normal file
43
lib/include/restd.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/*!
|
||||||
|
* restd.h
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* @Author: NADAL Jean-Baptiste
|
||||||
|
* @Date: 23/12/2019
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _RESTD_H
|
||||||
|
#define _RESTD_H
|
||||||
|
|
||||||
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
|
#include "restd_server.h"
|
||||||
|
#include "restd_http_handler.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /*_RESTD_H */
|
||||||
159
lib/include/restd_http_handler.h
Normal file
159
lib/include/restd_http_handler.h
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/*!
|
||||||
|
* restd_http_handler.h
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* @Author: NADAL Jean-Baptiste
|
||||||
|
* @Date: 23/12/2019
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _RESTD_HTTP_HANDLER_H
|
||||||
|
#define _RESTD_HTTP_HANDLER_H
|
||||||
|
|
||||||
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------*\
|
||||||
|
| HTTP PROTOCOL SPECIFICS |
|
||||||
|
\*----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* HTTP PROTOCOL CODES */
|
||||||
|
#define HTTP_PROTOCOL_09 "HTTP/0.9"
|
||||||
|
#define HTTP_PROTOCOL_10 "HTTP/1.0"
|
||||||
|
#define HTTP_PROTOCOL_11 "HTTP/1.1"
|
||||||
|
|
||||||
|
/* HTTP RESPONSE CODES */
|
||||||
|
#define HTTP_NO_RESPONSE (0)
|
||||||
|
#define HTTP_CODE_CONTINUE (100)
|
||||||
|
#define HTTP_CODE_OK (200)
|
||||||
|
#define HTTP_CODE_CREATED (201)
|
||||||
|
#define HTTP_CODE_NO_CONTENT (204)
|
||||||
|
#define HTTP_CODE_PARTIAL_CONTENT (206)
|
||||||
|
#define HTTP_CODE_MULTI_STATUS (207)
|
||||||
|
#define HTTP_CODE_MOVED_TEMPORARILY (302)
|
||||||
|
#define HTTP_CODE_NOT_MODIFIED (304)
|
||||||
|
#define HTTP_CODE_Brestd_REQUEST (400)
|
||||||
|
#define HTTP_CODE_UNAUTHORIZED (401)
|
||||||
|
#define HTTP_CODE_FORBIDDEN (403)
|
||||||
|
#define HTTP_CODE_NOT_FOUND (404)
|
||||||
|
#define HTTP_CODE_METHOD_NOT_ALLOWED (405)
|
||||||
|
#define HTTP_CODE_REQUEST_TIME_OUT (408)
|
||||||
|
#define HTTP_CODE_GONE (410)
|
||||||
|
#define HTTP_CODE_REQUEST_URI_TOO_LONG (414)
|
||||||
|
#define HTTP_CODE_LOCKED (423)
|
||||||
|
#define HTTP_CODE_INTERNAL_SERVER_ERROR (500)
|
||||||
|
#define HTTP_CODE_NOT_IMPLEMENTED (501)
|
||||||
|
#define HTTP_CODE_SERVICE_UNAVAILABLE (503)
|
||||||
|
|
||||||
|
/* DEFAULT BEHAVIORS */
|
||||||
|
#define HTTP_CRLF "\r\n"
|
||||||
|
#define HTTP_DEF_CONTENTTYPE "application/octet-stream"
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------*\
|
||||||
|
| TYPEDEFS |
|
||||||
|
\*----------------------------------------------------------------------------*/
|
||||||
|
typedef struct restd_http_s restd_http_t;
|
||||||
|
|
||||||
|
/*!< Hook type */
|
||||||
|
#define RESTD_HOOK_ALL (0) /*!< call on each and every phases */
|
||||||
|
#define RESTD_HOOK_ON_CONNECT (1) /*!< call right after the establishment of connection */
|
||||||
|
#define RESTD_HOOK_AFTER_REQUESTLINE (1 << 2) /*!< call after parsing request line */
|
||||||
|
#define RESTD_HOOK_AFTER_HEADER (1 << 3) /*!< call after parsing all headers */
|
||||||
|
#define RESTD_HOOK_ON_BODY (1 << 4) /*!< call on every time body data received */
|
||||||
|
#define RESTD_HOOK_ON_REQUEST (1 << 5) /*!< call with complete request */
|
||||||
|
#define RESTD_HOOK_ON_CLOSE (1 << 6) /*!< call right before closing or next request */
|
||||||
|
|
||||||
|
enum restd_http_request_status_e
|
||||||
|
{
|
||||||
|
RESTD_HTTP_REQ_INIT = 0, /*!< initial state */
|
||||||
|
RESTD_HTTP_REQ_REQUESTLINE_DONE, /*!< received 1st line */
|
||||||
|
RESTD_HTTP_REQ_HEADER_DONE, /*!< received headers completely */
|
||||||
|
RESTD_HTTP_REQ_DONE, /*!< received body completely. no more data expected */
|
||||||
|
|
||||||
|
RESTD_HTTP_ERROR, /*!< unrecoverable error found. */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------*\
|
||||||
|
| PUBLIC FUNCTIONS |
|
||||||
|
\*----------------------------------------------------------------------------*/
|
||||||
|
extern int restd_http_handler(short event, restd_conn_t *conn, void *userdata);
|
||||||
|
|
||||||
|
extern enum restd_http_request_status_e restd_http_get_status(restd_conn_t *conn);
|
||||||
|
extern struct evbuffer *restd_http_get_inbuf(restd_conn_t *conn);
|
||||||
|
extern struct evbuffer *restd_http_get_outbuf(restd_conn_t *conn);
|
||||||
|
|
||||||
|
extern const char *restd_http_get_request_header(restd_conn_t *conn, const char *name);
|
||||||
|
extern off_t restd_http_get_content_length(restd_conn_t *conn);
|
||||||
|
extern size_t restd_http_get_content_length_stored(restd_conn_t *conn);
|
||||||
|
extern void *restd_http_get_content(restd_conn_t *conn, size_t maxsize, size_t *storedsize);
|
||||||
|
extern int restd_http_is_keepalive_request(restd_conn_t *conn);
|
||||||
|
|
||||||
|
extern int restd_http_set_response_header(restd_conn_t *conn, const char *name, const char *value);
|
||||||
|
extern const char *restd_http_get_response_header(restd_conn_t *conn, const char *name);
|
||||||
|
extern int restd_http_set_response_code(restd_conn_t *conn, int code, const char *reason);
|
||||||
|
extern int restd_http_set_response_content(restd_conn_t *conn, const char *contenttype, off_t size);
|
||||||
|
|
||||||
|
extern size_t restd_http_response(restd_conn_t *conn, int code, const char *contenttype, const void *data, off_t size);
|
||||||
|
extern size_t restd_http_send_header(restd_conn_t *conn);
|
||||||
|
extern size_t restd_http_send_data(restd_conn_t *conn, const void *data, size_t size);
|
||||||
|
extern size_t restd_http_send_chunk(restd_conn_t *conn, const void *data, size_t size);
|
||||||
|
|
||||||
|
extern const char *restd_http_get_reason(int code);
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
| DATA STRUCTURES |
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
struct restd_http_s
|
||||||
|
{
|
||||||
|
// HTTP Request
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
enum restd_http_request_status_e status; /*!< request status. */
|
||||||
|
struct evbuffer *inbuf; /*!< input data buffer. */
|
||||||
|
|
||||||
|
// request line - available on REQ_REQUESTLINE_DONE.
|
||||||
|
char *method; /*!< request method ex) GET */
|
||||||
|
char *uri; /*!< url+query ex) /data%20path?query=the%20value */
|
||||||
|
char *httpver; /*!< version ex) HTTP/1.1 */
|
||||||
|
char *path; /*!< decoded path ex) /data path */
|
||||||
|
char *query; /*!< query string ex) query=the%20value */
|
||||||
|
|
||||||
|
// request header - available on REQ_HEADER_DONE.
|
||||||
|
qlisttbl_t *headers; /*!< parsed request header entries */
|
||||||
|
char *host; /*!< host ex) www.domain.com or www.domain.com:8080 */
|
||||||
|
char *domain; /*!< domain name ex) www.domain.com (no port number) */
|
||||||
|
off_t contentlength; /*!< value of Content-Length header.*/
|
||||||
|
size_t bodyin; /*!< bytes moved to in-buff */
|
||||||
|
} request;
|
||||||
|
|
||||||
|
// HTTP Response
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct evbuffer *outbuf; /*!< output data buffer. */
|
||||||
|
bool frozen_header; /*!< indicator whether we sent header out or not */
|
||||||
|
|
||||||
|
// response headers
|
||||||
|
int code; /*!< response status-code */
|
||||||
|
char *reason; /*!< reason-phrase */
|
||||||
|
qlisttbl_t *headers; /*!< response header entries */
|
||||||
|
off_t contentlength; /*!< content length in response */
|
||||||
|
size_t bodyout; /*!< bytes added to out-buffer */
|
||||||
|
} response;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* _RESTD_HTTP_HANDLER_H */
|
||||||
146
lib/include/restd_server.h
Normal file
146
lib/include/restd_server.h
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
/*!
|
||||||
|
* restd_server.h
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* @Author: NADAL Jean-Baptiste
|
||||||
|
* @Date: 23/12/2019
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _RESTD_SERVER_H
|
||||||
|
#define _RESTD_SERVER_H
|
||||||
|
|
||||||
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include <qlibc/qlibc.h>
|
||||||
|
|
||||||
|
/*------------------------------- TYPEDEFS ----------------------------------*/
|
||||||
|
|
||||||
|
typedef struct restd_server_s restd_server_t;
|
||||||
|
typedef struct restd_conn_s restd_conn_t;
|
||||||
|
|
||||||
|
/*------------------------------- DEFINE ----------------------------------*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return values of user callback.
|
||||||
|
*/
|
||||||
|
#define RESTD_OK (0) /*!< I'm done with this request. Escalate to other hooks. */
|
||||||
|
#define RESTD_TAKEOVER (1) /*!< I'll handle the buffer directly this time, skip next hook */
|
||||||
|
#define RESTD_DONE (2) /*!< We're done with this request but keep the connection open. */
|
||||||
|
#define RESTD_CLOSE (3) /*!< We're done with this request. Close as soon as we sent all data out. */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* These flags are used for ad_log_level();
|
||||||
|
*/
|
||||||
|
enum restd_log_e
|
||||||
|
{
|
||||||
|
RESTD_LOG_DISABLE = 0,
|
||||||
|
RESTD_LOG_ERROR,
|
||||||
|
RESTD_LOG_WARN,
|
||||||
|
RESTD_LOG_INFO,
|
||||||
|
RESTD_LOG_DEBUG,
|
||||||
|
RESTD_LOG_DEBUG2,
|
||||||
|
};
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
| USER-CALLBACK |
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User callback(hook) prototype.
|
||||||
|
*/
|
||||||
|
typedef int (*restd_callback)(short event, restd_conn_t *conn, void *userdata);
|
||||||
|
typedef void (*restd_userdata_free_cb)(restd_conn_t *conn, void *userdata);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event types
|
||||||
|
*/
|
||||||
|
#define RESTD_EVENT_INIT (1) /*!< Call once upon new connection. */
|
||||||
|
#define RESTD_EVENT_READ (1 << 1) /*!< Call on read */
|
||||||
|
#define RESTD_EVENT_WRITE (1 << 2) /*!< Call on write. */
|
||||||
|
#define RESTD_EVENT_CLOSE (1 << 3) /*!< Call before closing. */
|
||||||
|
#define RESTD_EVENT_TIMEOUT (1 << 4) /*!< Timeout indicator, this flag will be set with AD_EVENT_CLOSE. */
|
||||||
|
#define RESTD_EVENT_SHUTDOWN (1 << 5) /*!< Shutdown indicator, this flag will be set with AD_EVENT_CLOSE. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defaults
|
||||||
|
*/
|
||||||
|
#define RESTD_NUM_USERDATA (2) /*!< Number of userdata. Currently 0 is for userdata, 1 is for extra. */
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*\
|
||||||
|
| DATA STRUCTURES |
|
||||||
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server info container.
|
||||||
|
*/
|
||||||
|
struct restd_server_s
|
||||||
|
{
|
||||||
|
int errcode; /*!< exit status. 0 for normal exit, non zero for error. */
|
||||||
|
pthread_t *thread; /*!< thread object. not null if server runs as a thread */
|
||||||
|
|
||||||
|
qhashtbl_t *options; /*!< server options */
|
||||||
|
qhashtbl_t *stats; /*!< internal statistics */
|
||||||
|
qlist_t *hooks; /*!< list of registered hooks */
|
||||||
|
struct evconnlistener *listener; /*!< listener */
|
||||||
|
struct event_base *evbase; /*!< event base */
|
||||||
|
|
||||||
|
struct bufferevent *notify_buffer; /*!< internal notification channel */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection structure.
|
||||||
|
*/
|
||||||
|
struct restd_conn_s
|
||||||
|
{
|
||||||
|
restd_server_t *server; /*!< reference pointer to server */
|
||||||
|
struct bufferevent *buffer; /*!< reference pointer to buffer */
|
||||||
|
struct evbuffer *in; /*!< in buffer */
|
||||||
|
struct evbuffer *out; /*!< out buffer */
|
||||||
|
int status; /*!< hook status such as AD_OK */
|
||||||
|
|
||||||
|
void *userdata[2]; /*!< userdata[0] for end user, userdata[1] for extra */
|
||||||
|
restd_userdata_free_cb userdata_free_cb[2]; /*!< callback to release user data */
|
||||||
|
char *method; /*!< request method. set by protocol handler */
|
||||||
|
};
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------*\
|
||||||
|
| PUBLIC FUNCTIONS |
|
||||||
|
\*----------------------------------------------------------------------------*/
|
||||||
|
enum restd_log_e restd_log_level(enum restd_log_e log_level);
|
||||||
|
|
||||||
|
extern restd_server_t *restd_server_new(void);
|
||||||
|
extern void restd_server_free(restd_server_t *server);
|
||||||
|
extern int restd_server_start(restd_server_t *server);
|
||||||
|
|
||||||
|
extern void restd_server_set_option(restd_server_t *server, const char *key, const char *value);
|
||||||
|
extern char *restd_server_get_option(restd_server_t *server, const char *key);
|
||||||
|
extern int restd_server_get_option_int(restd_server_t *server, const char *key);
|
||||||
|
|
||||||
|
extern void restd_server_register_hook(restd_server_t *server, restd_callback cb, void *userdata);
|
||||||
|
extern void restd_server_register_hook_on_method(restd_server_t *server, const char *method,
|
||||||
|
restd_callback cb, void *userdata);
|
||||||
|
|
||||||
|
extern void *restd_conn_set_extra(restd_conn_t *conn, const void *extra, restd_userdata_free_cb free_cb);
|
||||||
|
extern void *restd_conn_get_extra(restd_conn_t *conn);
|
||||||
|
extern void restd_conn_set_method(restd_conn_t *conn, char *method);
|
||||||
|
|
||||||
|
#endif /* _RESTD_SERVER_H */
|
||||||
187
lib/src/macro.h
Normal file
187
lib/src/macro.h
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
/******************************************************************************
|
||||||
|
* libasyncd
|
||||||
|
*
|
||||||
|
* Copyright (c) 2014 Seungyoung Kim.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef _MACRO_H
|
||||||
|
#define _MACRO_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macro Functions
|
||||||
|
*/
|
||||||
|
#define BUG_EXIT() ASSERT(false)
|
||||||
|
#define ASSERT(c) assert(c)
|
||||||
|
#define CONST_STRLEN(s) (sizeof(s) - 1)
|
||||||
|
#define IS_EMPTY_STR(s) ((*s == '\0') ? true : false)
|
||||||
|
#define IS_EQUAL_STR(s1,s2) (!strcmp(s1,s2))
|
||||||
|
#define ENDING_CHAR(s) (*(s + strlen(s) - 1))
|
||||||
|
#define NEW_OBJECT(t) ((t *)calloc(1, sizeof(t)))
|
||||||
|
|
||||||
|
#define DYNAMIC_VSPRINTF(s, f) do { \
|
||||||
|
size_t _strsize; \
|
||||||
|
for(_strsize = 1024; ; _strsize *= 2) { \
|
||||||
|
s = (char*)malloc(_strsize); \
|
||||||
|
if(s == NULL) { \
|
||||||
|
DEBUG("DYNAMIC_VSPRINTF(): can't allocate memory."); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
va_list _arglist; \
|
||||||
|
va_start(_arglist, f); \
|
||||||
|
int _n = vsnprintf(s, _strsize, f, _arglist); \
|
||||||
|
va_end(_arglist); \
|
||||||
|
if(_n >= 0 && _n < _strsize) break; \
|
||||||
|
free(s); \
|
||||||
|
} \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Error message
|
||||||
|
*/
|
||||||
|
extern int _restd_log_level;
|
||||||
|
#define ERROROUT (stderr)
|
||||||
|
#define ERROR(fmt, args...) if (_restd_log_level > 0) { \
|
||||||
|
fprintf(ERROROUT, "[ERROR] " fmt "\n", ##args); \
|
||||||
|
}
|
||||||
|
#define WARN(fmt, args...) if (_restd_log_level >= 2) { \
|
||||||
|
fprintf(ERROROUT, "[WARN] " fmt "\n", ##args); \
|
||||||
|
}
|
||||||
|
#define INFO(fmt, args...) if (_restd_log_level >= 3) { \
|
||||||
|
fprintf(ERROROUT, "[INFO] " fmt "\n", ##args); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Debug Macros
|
||||||
|
*/
|
||||||
|
#ifdef BUILD_DEBUG
|
||||||
|
#define DEBUGOUT (stdout)
|
||||||
|
#define DEBUG(fmt, args...) if (_restd_log_level >= 4) { \
|
||||||
|
fprintf(DEBUGOUT, "[DEBUG] " fmt " [%s(),%s:%d]\n", \
|
||||||
|
##args, __func__, __FILE__, __LINE__); \
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define DEBUG(fms, args...)
|
||||||
|
#endif /* BUILD_DEBUG */
|
||||||
|
|
||||||
|
// debug timer
|
||||||
|
#include <sys/time.h>
|
||||||
|
#define TIMER_START() \
|
||||||
|
int _swno = 0; \
|
||||||
|
struct timeval _tv1, _tv2; \
|
||||||
|
gettimeofday(&_tv1, NULL)
|
||||||
|
|
||||||
|
#define TIMER_STOP(prefix) { \
|
||||||
|
gettimeofday(&_tv2, NULL); \
|
||||||
|
_swno++; \
|
||||||
|
struct timeval _diff; \
|
||||||
|
_diff.tv_sec = _tv2.tv_sec - _tv1.tv_sec; \
|
||||||
|
if(_tv2.tv_usec >= _tv1.tv_usec) { \
|
||||||
|
_diff.tv_usec = _tv2.tv_usec - _tv1.tv_usec; \
|
||||||
|
} else { \
|
||||||
|
_diff.tv_sec -= 1; \
|
||||||
|
_diff.tv_usec = 1000000 - _tv1.tv_usec + _tv2.tv_usec; \
|
||||||
|
} \
|
||||||
|
printf("TIMER(%d,%s,%d): %zus %dus (%s:%d)\n", \
|
||||||
|
getpid(), prefix, _swno, _diff.tv_sec, (int)(_diff.tv_usec), \
|
||||||
|
__FILE__, __LINE__); \
|
||||||
|
gettimeofday(&_tv1, NULL); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Q_MUTEX Macros
|
||||||
|
*/
|
||||||
|
#ifndef _MULTI_THREADED
|
||||||
|
#define _MULTI_THREADED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#define Q_MUTEX_NEW(x,r) do { \
|
||||||
|
x = (qmutex_t *)calloc(1, sizeof(qmutex_t)); \
|
||||||
|
if(x == NULL) break; \
|
||||||
|
memset((void*)x, 0, sizeof(qmutex_t)); \
|
||||||
|
pthread_mutexattr_t _mutexattr; \
|
||||||
|
pthread_mutexattr_init(&_mutexattr); \
|
||||||
|
if(r == true) { \
|
||||||
|
pthread_mutexattr_settype(&_mutexattr, PTHREAD_MUTEX_RECURSIVE); \
|
||||||
|
} \
|
||||||
|
int _ret = pthread_mutex_init(&(x->mutex), &_mutexattr); \
|
||||||
|
pthread_mutexattr_destroy(&_mutexattr); \
|
||||||
|
if(_ret != 0) { \
|
||||||
|
char _errmsg[64]; \
|
||||||
|
strerror_r(_ret, _errmsg, sizeof(_errmsg)); \
|
||||||
|
DEBUG("Q_MUTEX: can't initialize mutex. [%d:%s]", _ret, _errmsg); \
|
||||||
|
free(x); \
|
||||||
|
x = NULL; \
|
||||||
|
} \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#define Q_MUTEX_LEAVE(x) do { \
|
||||||
|
if(x == NULL) break; \
|
||||||
|
if(!pthread_equal(x->owner, pthread_self())) { \
|
||||||
|
DEBUG("Q_MUTEX: unlock - owner mismatch."); \
|
||||||
|
} \
|
||||||
|
if((x->count--) < 0) x->count = 0; \
|
||||||
|
pthread_mutex_unlock(&(x->mutex)); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#define MAX_MUTEX_LOCK_WAIT (5000)
|
||||||
|
#define Q_MUTEX_ENTER(x) do { \
|
||||||
|
if(x == NULL) break; \
|
||||||
|
while(true) { \
|
||||||
|
int _ret, i; \
|
||||||
|
for(i = 0; (_ret = pthread_mutex_trylock(&(x->mutex))) != 0 \
|
||||||
|
&& i < MAX_MUTEX_LOCK_WAIT; i++) { \
|
||||||
|
if(i == 0) { \
|
||||||
|
DEBUG("Q_MUTEX: mutex is already locked - retrying"); \
|
||||||
|
} \
|
||||||
|
usleep(1); \
|
||||||
|
} \
|
||||||
|
if(_ret == 0) break; \
|
||||||
|
char _errmsg[64]; \
|
||||||
|
strerror_r(_ret, _errmsg, sizeof(_errmsg)); \
|
||||||
|
DEBUG("Q_MUTEX: can't get lock - force to unlock. [%d:%s]", \
|
||||||
|
_ret, _errmsg); \
|
||||||
|
Q_MUTEX_LEAVE(x); \
|
||||||
|
} \
|
||||||
|
x->count++; \
|
||||||
|
x->owner = pthread_self(); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#define Q_MUTEX_DESTROY(x) do { \
|
||||||
|
if(x == NULL) break; \
|
||||||
|
if(x->count != 0) DEBUG("Q_MUTEX: mutex counter is not 0."); \
|
||||||
|
int _ret; \
|
||||||
|
while((_ret = pthread_mutex_destroy(&(x->mutex))) != 0) { \
|
||||||
|
char _errmsg[64]; \
|
||||||
|
strerror_r(_ret, _errmsg, sizeof(_errmsg)); \
|
||||||
|
DEBUG("Q_MUTEX: force to unlock mutex. [%d:%s]", _ret, _errmsg); \
|
||||||
|
Q_MUTEX_LEAVE(x); \
|
||||||
|
} \
|
||||||
|
free(x); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
#endif /* _MACRO_H */
|
||||||
998
lib/src/restd_http_handler.c
Normal file
998
lib/src/restd_http_handler.c
Normal file
@@ -0,0 +1,998 @@
|
|||||||
|
/*!
|
||||||
|
* restd_http_handler.c
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* @Author: NADAL Jean-Baptiste
|
||||||
|
* @Date: 23/12/2019
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||||
|
|
||||||
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <event2/buffer.h>
|
||||||
|
#include <qlibc/qlibc.h>
|
||||||
|
|
||||||
|
#include "restd_server.h"
|
||||||
|
|
||||||
|
#include "restd_http_handler.h"
|
||||||
|
|
||||||
|
#include "macro.h"
|
||||||
|
|
||||||
|
#ifndef _DOXYGEN_SKIP
|
||||||
|
static restd_http_t *http_new(struct evbuffer *out);
|
||||||
|
static void http_free(restd_http_t *http);
|
||||||
|
static void http_free_cb(restd_conn_t *conn, void *userdata);
|
||||||
|
static size_t http_add_inbuf(struct evbuffer *buffer, restd_http_t *http,
|
||||||
|
size_t maxsize);
|
||||||
|
|
||||||
|
static int http_parser(restd_http_t *http, struct evbuffer *in);
|
||||||
|
static int parse_requestline(restd_http_t *http, char *line);
|
||||||
|
static int parse_headers(restd_http_t *http, struct evbuffer *in);
|
||||||
|
static int parse_body(restd_http_t *http, struct evbuffer *in);
|
||||||
|
static ssize_t parse_chunked_body(restd_http_t *http, struct evbuffer *in);
|
||||||
|
|
||||||
|
static bool isValidPathname(const char *path);
|
||||||
|
static void correctPathname(char *path);
|
||||||
|
static char *evbuffer_peekln(struct evbuffer *buffer, size_t *n_rerestd_out,
|
||||||
|
enum evbuffer_eol_style eol_style);
|
||||||
|
static ssize_t evbuffer_drainln(struct evbuffer *buffer, size_t *n_rerestd_out,
|
||||||
|
enum evbuffer_eol_style eol_style);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP protocol handler hook.
|
||||||
|
*
|
||||||
|
* This hook provides an easy way to handle HTTP request/response.
|
||||||
|
*
|
||||||
|
* @note
|
||||||
|
* This hook must be registered at the top of hook chain.
|
||||||
|
*
|
||||||
|
* @code
|
||||||
|
* restd_server_t *server = restd_server_new();
|
||||||
|
* restd_server_register_hook(server, restd_http_handler, NULL);
|
||||||
|
* @endcode
|
||||||
|
*/
|
||||||
|
int restd_http_handler(short event, restd_conn_t *conn, void *userdata)
|
||||||
|
{
|
||||||
|
if (event & RESTD_EVENT_INIT)
|
||||||
|
{
|
||||||
|
DEBUG("==> HTTP INIT");
|
||||||
|
restd_http_t *http = http_new(conn->out);
|
||||||
|
if (http == NULL)
|
||||||
|
return RESTD_CLOSE;
|
||||||
|
restd_conn_set_extra(conn, http, http_free_cb);
|
||||||
|
return RESTD_OK;
|
||||||
|
}
|
||||||
|
else if (event & RESTD_EVENT_READ)
|
||||||
|
{
|
||||||
|
DEBUG("==> HTTP READ");
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
int status = http_parser(http, conn->in);
|
||||||
|
if (conn->method == NULL && http->request.method != NULL)
|
||||||
|
{
|
||||||
|
restd_conn_set_method(conn, http->request.method);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
else if (event & RESTD_EVENT_WRITE)
|
||||||
|
{
|
||||||
|
DEBUG("==> HTTP WRITE");
|
||||||
|
return RESTD_OK;
|
||||||
|
}
|
||||||
|
else if (event & RESTD_EVENT_CLOSE)
|
||||||
|
{
|
||||||
|
DEBUG("==> HTTP CLOSE=%x (TIMEOUT=%d, SHUTDOWN=%d)",
|
||||||
|
event, event & RESTD_EVENT_TIMEOUT, event & RESTD_EVENT_SHUTDOWN);
|
||||||
|
return RESTD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
BUG_EXIT();
|
||||||
|
return RESTD_CLOSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the request status.
|
||||||
|
*/
|
||||||
|
enum restd_http_request_status_e restd_http_get_status(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http == NULL)
|
||||||
|
return RESTD_HTTP_ERROR;
|
||||||
|
return http->request.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
struct evbuffer *restd_http_get_inbuf(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
return http->request.inbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
struct evbuffer *restd_http_get_outbuf(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
return http->response.outbuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get request header.
|
||||||
|
*
|
||||||
|
* @param name name of header.
|
||||||
|
*
|
||||||
|
* @return value of string if found, otherwise NULL.
|
||||||
|
*/
|
||||||
|
const char *restd_http_get_request_header(restd_conn_t *conn, const char *name)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
return http->request.headers->getstr(http->request.headers, name, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the size of content from the request.
|
||||||
|
*/
|
||||||
|
off_t restd_http_get_content_length(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
return http->request.contentlength;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the actual size of data stored in in-buffer
|
||||||
|
*/
|
||||||
|
size_t restd_http_get_content_length_stored(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
return evbuffer_get_length(http->request.inbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove content from the in-buffer.
|
||||||
|
*
|
||||||
|
* The return data gets null terminated for convenience. For an example,
|
||||||
|
* if it reads 3 bytes, it will allocate 4 bytes and the 4th byte will
|
||||||
|
* be set to null terminator. `storedsized` will still return 3.
|
||||||
|
*
|
||||||
|
* @param maxsize maximum length of data to read. 0 to read everything.
|
||||||
|
* @param storedsize the size of data read and stored in the return.
|
||||||
|
*/
|
||||||
|
void *restd_http_get_content(restd_conn_t *conn, size_t maxsize, size_t *storedsize)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
|
||||||
|
size_t inbuflen = evbuffer_get_length(http->request.inbuf);
|
||||||
|
size_t readlen =
|
||||||
|
(maxsize == 0) ? inbuflen : ((inbuflen < maxsize) ? inbuflen : maxsize);
|
||||||
|
if (readlen == 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
void *data = malloc(readlen + 1);
|
||||||
|
if (data == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
size_t removedlen = evbuffer_remove(http->request.inbuf, data, readlen);
|
||||||
|
((char *)data)[removedlen] = '\0';
|
||||||
|
if (storedsize)
|
||||||
|
*storedsize = removedlen;
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the request is keep-alive request or not.
|
||||||
|
*
|
||||||
|
* @return 1 if keep-alive request, otherwise 0.
|
||||||
|
*/
|
||||||
|
int restd_http_is_keepalive_request(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http->request.httpver == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *connection = restd_http_get_request_header(conn, "Connection");
|
||||||
|
if (!strcmp(http->request.httpver, HTTP_PROTOCOL_11))
|
||||||
|
{
|
||||||
|
// In HTTP/1.1, Keep-Alive is on by default unless explicitly specified.
|
||||||
|
if (connection != NULL && !strcmp(connection, "close"))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// In older version, Keep-Alive is off by default unless requested.
|
||||||
|
if (connection != NULL && (!strcmp(connection, "Keep-Alive") || !strcmp(connection, "TE")))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set response header.
|
||||||
|
*
|
||||||
|
* @param name name of header.
|
||||||
|
* @param value value string to set. NULL to remove the header.
|
||||||
|
*
|
||||||
|
* @return 0 on success, -1 if we already sent it out.
|
||||||
|
*/
|
||||||
|
int restd_http_set_response_header(restd_conn_t *conn, const char *name,
|
||||||
|
const char *value)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http->response.frozen_header)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value != NULL)
|
||||||
|
{
|
||||||
|
http->response.headers->putstr(http->response.headers, name, value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
http->response.headers->remove(http->response.headers, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get response header.
|
||||||
|
*
|
||||||
|
* @param name name of header.
|
||||||
|
*
|
||||||
|
* @return value of string if found, otherwise NULL.
|
||||||
|
*/
|
||||||
|
const char *restd_http_get_response_header(restd_conn_t *conn, const char *name)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
return http->response.headers->getstr(http->response.headers, name, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return 0 on success, -1 if we already sent it out.
|
||||||
|
*/
|
||||||
|
int restd_http_set_response_code(restd_conn_t *conn, int code, const char *reason)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http->response.frozen_header)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
http->response.code = code;
|
||||||
|
if (reason)
|
||||||
|
http->response.reason = strdup(reason);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param size content size. -1 for chunked transfer encoding.
|
||||||
|
* @return 0 on success, -1 if we already sent it out.
|
||||||
|
*/
|
||||||
|
int restd_http_set_response_content(restd_conn_t *conn, const char *contenttype,
|
||||||
|
off_t size)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http->response.frozen_header)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Content-Type header.
|
||||||
|
restd_http_set_response_header(
|
||||||
|
conn, "Content-Type",
|
||||||
|
(contenttype) ? contenttype : HTTP_DEF_CONTENTTYPE);
|
||||||
|
if (size >= 0)
|
||||||
|
{
|
||||||
|
char clenval[20 + 1];
|
||||||
|
sprintf(clenval, "%jd", size);
|
||||||
|
restd_http_set_response_header(conn, "Content-Length", clenval);
|
||||||
|
http->response.contentlength = size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
restd_http_set_response_header(conn, "Transfer-Encoding", "chunked");
|
||||||
|
http->response.contentlength = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return total bytes sent, 0 on error.
|
||||||
|
*/
|
||||||
|
size_t restd_http_response(restd_conn_t *conn, int code, const char *contenttype,
|
||||||
|
const void *data, off_t size)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http->response.frozen_header)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set response headers.
|
||||||
|
if (restd_http_get_response_header(conn, "Connection") == NULL)
|
||||||
|
{
|
||||||
|
restd_http_set_response_header(
|
||||||
|
conn, "Connection",
|
||||||
|
(restd_http_is_keepalive_request(conn)) ? "Keep-Alive" : "close");
|
||||||
|
}
|
||||||
|
|
||||||
|
restd_http_set_response_code(conn, code, restd_http_get_reason(code));
|
||||||
|
restd_http_set_response_content(conn, contenttype, size);
|
||||||
|
return restd_http_send_data(conn, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return 0 total bytes put in out buffer, -1 if we already sent it out.
|
||||||
|
*/
|
||||||
|
size_t restd_http_send_header(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
if (http->response.frozen_header)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
http->response.frozen_header = true;
|
||||||
|
|
||||||
|
// Send status line.
|
||||||
|
const char *reason =
|
||||||
|
(http->response.reason) ? http->response.reason : restd_http_get_reason(http->response.code);
|
||||||
|
evbuffer_add_printf(http->response.outbuf, "%s %d %s" HTTP_CRLF,
|
||||||
|
http->request.httpver, http->response.code, reason);
|
||||||
|
|
||||||
|
// Send headers.
|
||||||
|
qlisttbl_obj_t obj;
|
||||||
|
bzero((void *)&obj, sizeof(obj));
|
||||||
|
qlisttbl_t *tbl = http->response.headers;
|
||||||
|
tbl->lock(tbl);
|
||||||
|
while (tbl->getnext(tbl, &obj, NULL, false))
|
||||||
|
{
|
||||||
|
evbuffer_add_printf(http->response.outbuf, "%s: %s" HTTP_CRLF,
|
||||||
|
(char *)obj.name, (char *)obj.data);
|
||||||
|
}
|
||||||
|
tbl->unlock(tbl);
|
||||||
|
|
||||||
|
// Send empty line, indicator of end of header.
|
||||||
|
evbuffer_add(http->response.outbuf, HTTP_CRLF, CONST_STRLEN(HTTP_CRLF));
|
||||||
|
|
||||||
|
return evbuffer_get_length(http->response.outbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return 0 on success, -1 if we already sent it out.
|
||||||
|
*/
|
||||||
|
size_t restd_http_send_data(restd_conn_t *conn, const void *data, size_t size)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
|
||||||
|
if (http->response.contentlength < 0)
|
||||||
|
{
|
||||||
|
WARN("Content-Length is not set. Invalid usage.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((http->response.bodyout + size) > http->response.contentlength)
|
||||||
|
{
|
||||||
|
WARN("Trying to send more data than supposed to");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t beforesize = evbuffer_get_length(http->response.outbuf);
|
||||||
|
if (!http->response.frozen_header)
|
||||||
|
{
|
||||||
|
restd_http_send_header(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data != NULL && size > 0)
|
||||||
|
{
|
||||||
|
if (evbuffer_add(http->response.outbuf, data, size))
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
http->response.bodyout += size;
|
||||||
|
return (evbuffer_get_length(http->response.outbuf) - beforesize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
size_t restd_http_send_chunk(restd_conn_t *conn, const void *data, size_t size)
|
||||||
|
{
|
||||||
|
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||||
|
|
||||||
|
if (http->response.contentlength >= 0)
|
||||||
|
{
|
||||||
|
WARN("Content-Length is set. Invalid usage.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!http->response.frozen_header)
|
||||||
|
{
|
||||||
|
restd_http_send_header(conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t beforesize = evbuffer_get_length(http->response.outbuf);
|
||||||
|
int status = 0;
|
||||||
|
if (size > 0)
|
||||||
|
{
|
||||||
|
status += evbuffer_add_printf(http->response.outbuf, "%zu" HTTP_CRLF,
|
||||||
|
size);
|
||||||
|
status += evbuffer_add(http->response.outbuf, data, size);
|
||||||
|
status += evbuffer_add(http->response.outbuf, HTTP_CRLF,
|
||||||
|
CONST_STRLEN(HTTP_CRLF));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status += evbuffer_add_printf(http->response.outbuf,
|
||||||
|
"0" HTTP_CRLF HTTP_CRLF);
|
||||||
|
}
|
||||||
|
if (status != 0)
|
||||||
|
{
|
||||||
|
WARN("Failed to add data to out-buffer. (size:%jd)", size);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t bytesout = evbuffer_get_length(http->response.outbuf) - beforesize;
|
||||||
|
http->response.bodyout += bytesout;
|
||||||
|
return bytesout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
const char *restd_http_get_reason(int code)
|
||||||
|
{
|
||||||
|
switch (code)
|
||||||
|
{
|
||||||
|
case HTTP_CODE_CONTINUE:
|
||||||
|
return "Continue";
|
||||||
|
case HTTP_CODE_OK:
|
||||||
|
return "OK";
|
||||||
|
case HTTP_CODE_CREATED:
|
||||||
|
return "Created";
|
||||||
|
case HTTP_CODE_NO_CONTENT:
|
||||||
|
return "No content";
|
||||||
|
case HTTP_CODE_PARTIAL_CONTENT:
|
||||||
|
return "Partial Content";
|
||||||
|
case HTTP_CODE_MULTI_STATUS:
|
||||||
|
return "Multi Status";
|
||||||
|
case HTTP_CODE_MOVED_TEMPORARILY:
|
||||||
|
return "Moved Temporarily";
|
||||||
|
case HTTP_CODE_NOT_MODIFIED:
|
||||||
|
return "Not Modified";
|
||||||
|
case HTTP_CODE_Brestd_REQUEST:
|
||||||
|
return "Bad Request";
|
||||||
|
case HTTP_CODE_UNAUTHORIZED:
|
||||||
|
return "Authorization Required";
|
||||||
|
case HTTP_CODE_FORBIDDEN:
|
||||||
|
return "Forbidden";
|
||||||
|
case HTTP_CODE_NOT_FOUND:
|
||||||
|
return "Not Found";
|
||||||
|
case HTTP_CODE_METHOD_NOT_ALLOWED:
|
||||||
|
return "Method Not Allowed";
|
||||||
|
case HTTP_CODE_REQUEST_TIME_OUT:
|
||||||
|
return "Request Time Out";
|
||||||
|
case HTTP_CODE_GONE:
|
||||||
|
return "Gone";
|
||||||
|
case HTTP_CODE_REQUEST_URI_TOO_LONG:
|
||||||
|
return "Request URI Too Long";
|
||||||
|
case HTTP_CODE_LOCKED:
|
||||||
|
return "Locked";
|
||||||
|
case HTTP_CODE_INTERNAL_SERVER_ERROR:
|
||||||
|
return "Internal Server Error";
|
||||||
|
case HTTP_CODE_NOT_IMPLEMENTED:
|
||||||
|
return "Not Implemented";
|
||||||
|
case HTTP_CODE_SERVICE_UNAVAILABLE:
|
||||||
|
return "Service Unavailable";
|
||||||
|
}
|
||||||
|
|
||||||
|
WARN("Undefined code found. %d", code);
|
||||||
|
return "-";
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Private internal functions.
|
||||||
|
*****************************************************************************/
|
||||||
|
#ifndef _DOXYGEN_SKIP
|
||||||
|
|
||||||
|
static restd_http_t *http_new(struct evbuffer *out)
|
||||||
|
{
|
||||||
|
// Create a new connection container.
|
||||||
|
restd_http_t *http = NEW_OBJECT(restd_http_t);
|
||||||
|
if (http == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Allocate additional resources.
|
||||||
|
http->request.inbuf = evbuffer_new();
|
||||||
|
http->request.headers = qlisttbl(
|
||||||
|
QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||||
|
http->response.headers = qlisttbl(
|
||||||
|
QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||||
|
if (http->request.inbuf == NULL || http->request.headers == NULL || http->response.headers == NULL)
|
||||||
|
{
|
||||||
|
http_free(http);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize structure.
|
||||||
|
http->request.status = RESTD_HTTP_REQ_INIT;
|
||||||
|
http->request.contentlength = -1;
|
||||||
|
http->response.contentlength = -1;
|
||||||
|
http->response.outbuf = out;
|
||||||
|
|
||||||
|
return http;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void http_free(restd_http_t *http)
|
||||||
|
{
|
||||||
|
if (http)
|
||||||
|
{
|
||||||
|
if (http->request.inbuf)
|
||||||
|
evbuffer_free(http->request.inbuf);
|
||||||
|
if (http->request.method)
|
||||||
|
free(http->request.method);
|
||||||
|
if (http->request.uri)
|
||||||
|
free(http->request.uri);
|
||||||
|
if (http->request.httpver)
|
||||||
|
free(http->request.httpver);
|
||||||
|
if (http->request.path)
|
||||||
|
free(http->request.path);
|
||||||
|
if (http->request.query)
|
||||||
|
free(http->request.query);
|
||||||
|
|
||||||
|
if (http->request.headers)
|
||||||
|
http->request.headers->free(http->request.headers);
|
||||||
|
if (http->request.host)
|
||||||
|
free(http->request.host);
|
||||||
|
if (http->request.domain)
|
||||||
|
free(http->request.domain);
|
||||||
|
|
||||||
|
if (http->response.headers)
|
||||||
|
http->response.headers->free(http->response.headers);
|
||||||
|
if (http->response.reason)
|
||||||
|
free(http->response.reason);
|
||||||
|
|
||||||
|
free(http);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void http_free_cb(restd_conn_t *conn, void *userdata)
|
||||||
|
{
|
||||||
|
http_free((restd_http_t *)userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static size_t http_add_inbuf(struct evbuffer *buffer, restd_http_t *http,
|
||||||
|
size_t maxsize)
|
||||||
|
{
|
||||||
|
if (maxsize == 0 || evbuffer_get_length(buffer) == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return evbuffer_remove_buffer(buffer, http->request.inbuf, maxsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static int http_parser(restd_http_t *http, struct evbuffer *in)
|
||||||
|
{
|
||||||
|
ASSERT(http != NULL && in != NULL);
|
||||||
|
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_INIT)
|
||||||
|
{
|
||||||
|
char *line = evbuffer_readln(in, NULL, EVBUFFER_EOL_CRLF);
|
||||||
|
if (line == NULL)
|
||||||
|
return http->request.status;
|
||||||
|
http->request.status = parse_requestline(http, line);
|
||||||
|
free(line);
|
||||||
|
// Do not call user callbacks until I reach the next state.
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_INIT)
|
||||||
|
{
|
||||||
|
return RESTD_TAKEOVER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_REQUESTLINE_DONE)
|
||||||
|
{
|
||||||
|
http->request.status = parse_headers(http, in);
|
||||||
|
// Do not call user callbacks until I reach the next state.
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_REQUESTLINE_DONE)
|
||||||
|
{
|
||||||
|
return RESTD_TAKEOVER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_HEADER_DONE)
|
||||||
|
{
|
||||||
|
http->request.status = parse_body(http, in);
|
||||||
|
// Do not call user callbacks until I reach the next state.
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_HEADER_DONE)
|
||||||
|
{
|
||||||
|
return RESTD_TAKEOVER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (http->request.status == RESTD_HTTP_REQ_DONE)
|
||||||
|
{
|
||||||
|
return RESTD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (http->request.status == RESTD_HTTP_ERROR)
|
||||||
|
{
|
||||||
|
return RESTD_CLOSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BUG_EXIT();
|
||||||
|
return RESTD_CLOSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static int parse_requestline(restd_http_t *http, char *line)
|
||||||
|
{
|
||||||
|
// Parse request line.
|
||||||
|
char *saveptr;
|
||||||
|
char *method = strtok_r(line, " ", &saveptr);
|
||||||
|
char *uri = strtok_r(NULL, " ", &saveptr);
|
||||||
|
char *httpver = strtok_r(NULL, " ", &saveptr);
|
||||||
|
char *tmp = strtok_r(NULL, " ", &saveptr);
|
||||||
|
|
||||||
|
if (method == NULL || uri == NULL || httpver == NULL || tmp != NULL)
|
||||||
|
{
|
||||||
|
DEBUG("Invalid request line. %s", line);
|
||||||
|
return RESTD_HTTP_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set request method
|
||||||
|
http->request.method = qstrupper(strdup(method));
|
||||||
|
|
||||||
|
// Set HTTP version
|
||||||
|
http->request.httpver = qstrupper(strdup(httpver));
|
||||||
|
if (strcmp(http->request.httpver, HTTP_PROTOCOL_09) && strcmp(http->request.httpver, HTTP_PROTOCOL_10) && strcmp(http->request.httpver, HTTP_PROTOCOL_11))
|
||||||
|
{
|
||||||
|
DEBUG("Unknown protocol: %s", http->request.httpver);
|
||||||
|
return RESTD_HTTP_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set URI
|
||||||
|
if (uri[0] == '/')
|
||||||
|
{
|
||||||
|
http->request.uri = strdup(uri);
|
||||||
|
}
|
||||||
|
else if ((tmp = strstr(uri, "://")))
|
||||||
|
{
|
||||||
|
// divide URI into host and path
|
||||||
|
char *path = strstr(tmp + CONST_STRLEN("://"), "/");
|
||||||
|
if (path == NULL)
|
||||||
|
{ // URI has no path ex) http://domain.com:80
|
||||||
|
http->request.headers->putstr(http->request.headers, "Host",
|
||||||
|
tmp + CONST_STRLEN("://"));
|
||||||
|
http->request.uri = strdup("/");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // URI has path, ex) http://domain.com:80/path
|
||||||
|
*path = '\0';
|
||||||
|
http->request.headers->putstr(http->request.headers, "Host",
|
||||||
|
tmp + CONST_STRLEN("://"));
|
||||||
|
*path = '/';
|
||||||
|
http->request.uri = strdup(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DEBUG("Invalid URI format. %s", uri);
|
||||||
|
return RESTD_HTTP_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set request path. Only path part from URI.
|
||||||
|
http->request.path = strdup(http->request.uri);
|
||||||
|
tmp = strstr(http->request.path, "?");
|
||||||
|
if (tmp)
|
||||||
|
{
|
||||||
|
*tmp = '\0';
|
||||||
|
http->request.query = strdup(tmp + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
http->request.query = strdup("");
|
||||||
|
}
|
||||||
|
qurl_decode(http->request.path);
|
||||||
|
|
||||||
|
// check path
|
||||||
|
if (isValidPathname(http->request.path) == false)
|
||||||
|
{
|
||||||
|
DEBUG("Invalid URI format : %s", http->request.uri);
|
||||||
|
return RESTD_HTTP_ERROR;
|
||||||
|
}
|
||||||
|
correctPathname(http->request.path);
|
||||||
|
|
||||||
|
DEBUG("Method=%s, URI=%s, VER=%s", http->request.method, http->request.uri, http->request.httpver);
|
||||||
|
|
||||||
|
return RESTD_HTTP_REQ_REQUESTLINE_DONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static int parse_headers(restd_http_t *http, struct evbuffer *in)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
while ((line = evbuffer_readln(in, NULL, EVBUFFER_EOL_CRLF)))
|
||||||
|
{
|
||||||
|
if (IS_EMPTY_STR(line))
|
||||||
|
{
|
||||||
|
const char *clen = http->request.headers->getstr(
|
||||||
|
http->request.headers, "Content-Length", false);
|
||||||
|
http->request.contentlength = (clen) ? atol(clen) : -1;
|
||||||
|
free(line);
|
||||||
|
return RESTD_HTTP_REQ_HEADER_DONE;
|
||||||
|
}
|
||||||
|
// Parse
|
||||||
|
char *name, *value;
|
||||||
|
char *tmp = strstr(line, ":");
|
||||||
|
if (tmp)
|
||||||
|
{
|
||||||
|
*tmp = '\0';
|
||||||
|
name = qstrtrim(line);
|
||||||
|
value = qstrtrim(tmp + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
name = qstrtrim(line);
|
||||||
|
value = "";
|
||||||
|
}
|
||||||
|
// Add
|
||||||
|
http->request.headers->putstr(http->request.headers, name, value);
|
||||||
|
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
return http->request.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static int parse_body(restd_http_t *http, struct evbuffer *in)
|
||||||
|
{
|
||||||
|
// Handle static data case.
|
||||||
|
if (http->request.contentlength == 0)
|
||||||
|
{
|
||||||
|
return RESTD_HTTP_REQ_DONE;
|
||||||
|
}
|
||||||
|
else if (http->request.contentlength > 0)
|
||||||
|
{
|
||||||
|
if (http->request.contentlength > http->request.bodyin)
|
||||||
|
{
|
||||||
|
size_t maxread = http->request.contentlength - http->request.bodyin;
|
||||||
|
if (maxread > 0 && evbuffer_get_length(in) > 0)
|
||||||
|
{
|
||||||
|
http->request.bodyin += http_add_inbuf(in, http, maxread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (http->request.contentlength == http->request.bodyin)
|
||||||
|
{
|
||||||
|
return RESTD_HTTP_REQ_DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Check if Transfer-Encoding is chunked.
|
||||||
|
const char *tranenc = http->request.headers->getstr(
|
||||||
|
http->request.headers, "Transfer-Encoding", false);
|
||||||
|
if (tranenc != NULL && !strcmp(tranenc, "chunked"))
|
||||||
|
{
|
||||||
|
// TODO: handle chunked encoding
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
ssize_t chunksize = parse_chunked_body(http, in);
|
||||||
|
if (chunksize > 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (chunksize == 0)
|
||||||
|
{
|
||||||
|
return RESTD_HTTP_REQ_DONE;
|
||||||
|
}
|
||||||
|
else if (chunksize == -1)
|
||||||
|
{
|
||||||
|
return http->request.status;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return RESTD_HTTP_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return RESTD_HTTP_REQ_DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return http->request.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse chunked body and append it to inbuf.
|
||||||
|
*
|
||||||
|
* @return number of bytes in a chunk. so 0 for the ending chunk. -1 for not enough data, -2 format error.
|
||||||
|
*/
|
||||||
|
static ssize_t parse_chunked_body(restd_http_t *http, struct evbuffer *in)
|
||||||
|
{
|
||||||
|
// Peek chunk size.
|
||||||
|
size_t crlf_len = 0;
|
||||||
|
char *line = evbuffer_peekln(in, &crlf_len, EVBUFFER_EOL_CRLF);
|
||||||
|
if (line == NULL)
|
||||||
|
return -1; // not enough data.
|
||||||
|
size_t linelen = strlen(line);
|
||||||
|
|
||||||
|
// Parse chunk size
|
||||||
|
int chunksize = -1;
|
||||||
|
sscanf(line, "%x", &chunksize);
|
||||||
|
free(line);
|
||||||
|
if (chunksize < 0)
|
||||||
|
return -2; // format error
|
||||||
|
|
||||||
|
// Check if we've received whole data of this chunk.
|
||||||
|
size_t datalen = linelen + crlf_len + chunksize + crlf_len;
|
||||||
|
size_t inbuflen = evbuffer_get_length(in);
|
||||||
|
if (inbuflen < datalen)
|
||||||
|
{
|
||||||
|
return -1; // not enough data.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy chunk body
|
||||||
|
evbuffer_drainln(in, NULL, EVBUFFER_EOL_CRLF);
|
||||||
|
http_add_inbuf(in, http, chunksize);
|
||||||
|
evbuffer_drainln(in, NULL, EVBUFFER_EOL_CRLF);
|
||||||
|
|
||||||
|
return chunksize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validate file path
|
||||||
|
*/
|
||||||
|
static bool isValidPathname(const char *path)
|
||||||
|
{
|
||||||
|
if (path == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int len = strlen(path);
|
||||||
|
if (len == 0 || len >= PATH_MAX)
|
||||||
|
return false;
|
||||||
|
else if (path[0] != '/')
|
||||||
|
return false;
|
||||||
|
else if (strpbrk(path, "\\:*?\"<>|") != NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// check folder name length
|
||||||
|
int n;
|
||||||
|
char *t;
|
||||||
|
for (n = 0, t = (char *)path; *t != '\0'; t++)
|
||||||
|
{
|
||||||
|
if (*t == '/')
|
||||||
|
{
|
||||||
|
n = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (n >= FILENAME_MAX)
|
||||||
|
{
|
||||||
|
DEBUG("Filename too long.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Correct pathname.
|
||||||
|
*
|
||||||
|
* @note
|
||||||
|
* remove : heading & tailing white spaces, double slashes, tailing slash
|
||||||
|
*/
|
||||||
|
static void correctPathname(char *path)
|
||||||
|
{
|
||||||
|
// Take care of head & tail white spaces.
|
||||||
|
qstrtrim(path);
|
||||||
|
|
||||||
|
// Take care of double slashes.
|
||||||
|
while (strstr(path, "//") != NULL)
|
||||||
|
qstrreplace("sr", path, "//", "/");
|
||||||
|
|
||||||
|
// Take care of tailing slash.
|
||||||
|
int len = strlen(path);
|
||||||
|
if (len <= 1)
|
||||||
|
return;
|
||||||
|
if (path[len - 1] == '/')
|
||||||
|
path[len - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static char *evbuffer_peekln(struct evbuffer *buffer, size_t *n_rerestd_out,
|
||||||
|
enum evbuffer_eol_style eol_style)
|
||||||
|
{
|
||||||
|
// Check if first line has arrived.
|
||||||
|
struct evbuffer_ptr ptr = evbuffer_search_eol(buffer, NULL, n_rerestd_out,
|
||||||
|
eol_style);
|
||||||
|
if (ptr.pos == -1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
char *line = (char *)malloc(ptr.pos + 1);
|
||||||
|
if (line == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Linearizes buffer
|
||||||
|
if (ptr.pos > 0)
|
||||||
|
{
|
||||||
|
char *bufferptr = (char *)evbuffer_pullup(buffer, ptr.pos);
|
||||||
|
ASSERT(bufferptr != NULL);
|
||||||
|
strncpy(line, bufferptr, ptr.pos);
|
||||||
|
}
|
||||||
|
line[ptr.pos] = '\0';
|
||||||
|
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static ssize_t evbuffer_drainln(struct evbuffer *buffer, size_t *n_rerestd_out,
|
||||||
|
enum evbuffer_eol_style eol_style)
|
||||||
|
{
|
||||||
|
char *line = evbuffer_readln(buffer, n_rerestd_out, eol_style);
|
||||||
|
if (line == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
size_t linelen = strlen(line);
|
||||||
|
free(line);
|
||||||
|
return linelen;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // _DOXYGEN_SKIP
|
||||||
799
lib/src/restd_server.c
Normal file
799
lib/src/restd_server.c
Normal file
@@ -0,0 +1,799 @@
|
|||||||
|
/*!
|
||||||
|
* restd_server.c
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015-2019, NADAL Jean-Baptiste. All rights reserved.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* @Author: NADAL Jean-Baptiste
|
||||||
|
* @Date: 23/12/2019
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||||
|
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||||
|
|
||||||
|
/*------------------------------- INCLUDES ----------------------------------*/
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
|
#include <event2/buffer.h>
|
||||||
|
#include <event2/bufferevent.h>
|
||||||
|
#include <event2/event.h>
|
||||||
|
#include <event2/listener.h>
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <sys/eventfd.h>
|
||||||
|
#else
|
||||||
|
#include <sys/event.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "macro.h"
|
||||||
|
#include "restd_server.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Local variables.
|
||||||
|
*/
|
||||||
|
static bool initialized = false;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Global variables.
|
||||||
|
*/
|
||||||
|
int _restd_log_level = RESTD_LOG_WARN;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* User callback hook container.
|
||||||
|
*/
|
||||||
|
typedef struct restd_hook_s restd_hook_t;
|
||||||
|
struct restd_hook_s
|
||||||
|
{
|
||||||
|
char *method;
|
||||||
|
restd_callback cb;
|
||||||
|
void *userdata;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Local functions.
|
||||||
|
*/
|
||||||
|
static int notify_loopexit(restd_server_t *server);
|
||||||
|
static void notify_cb(struct bufferevent *buffer, void *userdata);
|
||||||
|
static void *server_loop(void *instance);
|
||||||
|
static void close_server(restd_server_t *server);
|
||||||
|
static void libevent_log_cb(int severity, const char *msg);
|
||||||
|
static int set_undefined_options(restd_server_t *server);
|
||||||
|
static void listener_cb(struct evconnlistener *listener,
|
||||||
|
evutil_socket_t evsocket, struct sockaddr *sockaddr,
|
||||||
|
int socklen, void *userdata);
|
||||||
|
static restd_conn_t *conn_new(restd_server_t *server, struct bufferevent *buffer);
|
||||||
|
static void conn_reset(restd_conn_t *conn);
|
||||||
|
static void conn_free(restd_conn_t *conn);
|
||||||
|
static void conn_read_cb(struct bufferevent *buffer, void *userdata);
|
||||||
|
static void conn_write_cb(struct bufferevent *buffer, void *userdata);
|
||||||
|
static void conn_event_cb(struct bufferevent *buffer, short what, void *userdata);
|
||||||
|
static void conn_cb(restd_conn_t *conn, int event);
|
||||||
|
static int call_hooks(short event, restd_conn_t *conn);
|
||||||
|
static void *set_userdata(restd_conn_t *conn, int index, const void *userdata, restd_userdata_free_cb free_cb);
|
||||||
|
static void *get_userdata(restd_conn_t *conn, int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server option names and default values.
|
||||||
|
*/
|
||||||
|
#define RESTD_SERVER_OPTIONS { \
|
||||||
|
{"server.port", "8888"}, \
|
||||||
|
\
|
||||||
|
/* Addr format IPv4="1.2.3.4", IPv6="1:2:3:4:5:6", Unix="/path" */ \
|
||||||
|
{"server.addr", "0.0.0.0"}, \
|
||||||
|
\
|
||||||
|
{"server.backlog", "128"}, \
|
||||||
|
\
|
||||||
|
/* Set read timeout seconds. 0 means no timeout. */ \
|
||||||
|
{"server.timeout", "0"}, \
|
||||||
|
\
|
||||||
|
/* Enable or disable request pipelining, this change AD_DONE's behavior */ \
|
||||||
|
{"server.request_pipelining", "1"}, \
|
||||||
|
\
|
||||||
|
/* Run server in a separate thread */ \
|
||||||
|
{"server.thread", "0"}, \
|
||||||
|
\
|
||||||
|
/* Collect resources after stop */ \
|
||||||
|
{"server.free_on_stop", "1"}, \
|
||||||
|
\
|
||||||
|
/* End of array marker. Do not remove */ \
|
||||||
|
{"", "_END_"}};
|
||||||
|
|
||||||
|
/*------------------------------- FUNCTIONS ----------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set debug output level.
|
||||||
|
*
|
||||||
|
* @param debug_level debug output level. 0 to disable.
|
||||||
|
*
|
||||||
|
* @return previous debug level.
|
||||||
|
*
|
||||||
|
* @note
|
||||||
|
* debug_level:
|
||||||
|
* REST_LOG_DISABLE
|
||||||
|
* REST_LOG_ERROR
|
||||||
|
* REST_LOG_WARN (default)
|
||||||
|
* REST_LOG_INFO
|
||||||
|
* REST_LOG_DEBUG
|
||||||
|
* REST_LOG_DEBUG2
|
||||||
|
*/
|
||||||
|
enum restd_log_e restd_log_level(enum restd_log_e log_level)
|
||||||
|
{
|
||||||
|
int prev = _restd_log_level;
|
||||||
|
_restd_log_level = log_level;
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a server object.
|
||||||
|
*/
|
||||||
|
restd_server_t *restd_server_new(void)
|
||||||
|
{
|
||||||
|
if (initialized)
|
||||||
|
{
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
restd_server_t *server = NEW_OBJECT(restd_server_t);
|
||||||
|
if (server == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize instance.
|
||||||
|
server->options = qhashtbl(0, 0);
|
||||||
|
server->stats = qhashtbl(100, QHASHTBL_THREADSAFE);
|
||||||
|
server->hooks = qlist(0);
|
||||||
|
if (server->options == NULL || server->stats == NULL || server->hooks == NULL)
|
||||||
|
{
|
||||||
|
restd_server_free(server);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG("Created a server object.");
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Release server object and all the resources.
|
||||||
|
*/
|
||||||
|
void restd_server_free(restd_server_t *server)
|
||||||
|
{
|
||||||
|
if (server == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int thread = restd_server_get_option_int(server, "server.thread");
|
||||||
|
if (thread && server->thread)
|
||||||
|
{
|
||||||
|
notify_loopexit(server);
|
||||||
|
sleep(1);
|
||||||
|
close_server(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server->evbase)
|
||||||
|
{
|
||||||
|
event_base_free(server->evbase);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server->options)
|
||||||
|
{
|
||||||
|
server->options->free(server->options);
|
||||||
|
}
|
||||||
|
if (server->stats)
|
||||||
|
{
|
||||||
|
server->stats->free(server->stats);
|
||||||
|
}
|
||||||
|
if (server->hooks)
|
||||||
|
{
|
||||||
|
qlist_t *tbl = server->hooks;
|
||||||
|
restd_hook_t *hook;
|
||||||
|
while ((hook = tbl->popfirst(tbl, NULL)))
|
||||||
|
{
|
||||||
|
if (hook->method)
|
||||||
|
free(hook->method);
|
||||||
|
free(hook);
|
||||||
|
}
|
||||||
|
server->hooks->free(server->hooks);
|
||||||
|
}
|
||||||
|
free(server);
|
||||||
|
DEBUG("Server terminated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start server.
|
||||||
|
*
|
||||||
|
* @return 0 if successful, otherwise -1.
|
||||||
|
*/
|
||||||
|
int restd_server_start(restd_server_t *server)
|
||||||
|
{
|
||||||
|
DEBUG("Starting a server.");
|
||||||
|
|
||||||
|
// Set default options that were not set by user..
|
||||||
|
set_undefined_options(server);
|
||||||
|
|
||||||
|
// Hookup libevent's log message.
|
||||||
|
if (_restd_log_level >= RESTD_LOG_DEBUG)
|
||||||
|
{
|
||||||
|
event_set_log_callback(libevent_log_cb);
|
||||||
|
if (_restd_log_level >= RESTD_LOG_DEBUG2)
|
||||||
|
{
|
||||||
|
event_enable_debug_mode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse addr
|
||||||
|
int port = restd_server_get_option_int(server, "server.port");
|
||||||
|
char *addr = restd_server_get_option(server, "server.addr");
|
||||||
|
struct sockaddr *sockaddr = NULL;
|
||||||
|
size_t sockaddr_len = 0;
|
||||||
|
if (addr[0] == '/')
|
||||||
|
{ // Unix socket.
|
||||||
|
struct sockaddr_un unixaddr;
|
||||||
|
bzero((void *)&unixaddr, sizeof(struct sockaddr_un));
|
||||||
|
if (strlen(addr) >= sizeof(unixaddr.sun_path))
|
||||||
|
{
|
||||||
|
errno = EINVAL;
|
||||||
|
DEBUG("Too long unix socket name. '%s'", addr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
unixaddr.sun_family = AF_UNIX;
|
||||||
|
strcpy(unixaddr.sun_path, addr); // no need of strncpy()
|
||||||
|
sockaddr = (struct sockaddr *)&unixaddr;
|
||||||
|
sockaddr_len = sizeof(unixaddr);
|
||||||
|
}
|
||||||
|
else if (strstr(addr, ":"))
|
||||||
|
{ // IPv6
|
||||||
|
struct sockaddr_in6 ipv6addr;
|
||||||
|
bzero((void *)&ipv6addr, sizeof(struct sockaddr_in6));
|
||||||
|
ipv6addr.sin6_family = AF_INET6;
|
||||||
|
ipv6addr.sin6_port = htons(port);
|
||||||
|
evutil_inet_pton(AF_INET6, addr, &ipv6addr.sin6_addr);
|
||||||
|
sockaddr = (struct sockaddr *)&ipv6addr;
|
||||||
|
sockaddr_len = sizeof(ipv6addr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // IPv4
|
||||||
|
struct sockaddr_in ipv4addr;
|
||||||
|
bzero((void *)&ipv4addr, sizeof(struct sockaddr_in));
|
||||||
|
ipv4addr.sin_family = AF_INET;
|
||||||
|
ipv4addr.sin_port = htons(port);
|
||||||
|
ipv4addr.sin_addr.s_addr =
|
||||||
|
(IS_EMPTY_STR(addr)) ? INADDR_ANY : inet_addr(addr);
|
||||||
|
sockaddr = (struct sockaddr *)&ipv4addr;
|
||||||
|
sockaddr_len = sizeof(ipv4addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind
|
||||||
|
if (!server->evbase)
|
||||||
|
{
|
||||||
|
server->evbase = event_base_new();
|
||||||
|
if (!server->evbase)
|
||||||
|
{
|
||||||
|
ERROR("Failed to create a new event base.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a eventfd for notification channel.
|
||||||
|
#ifdef __linux__
|
||||||
|
int notifyfd = eventfd(0, 0);
|
||||||
|
#else
|
||||||
|
int notifyfd = kqueue();
|
||||||
|
#endif
|
||||||
|
server->notify_buffer = bufferevent_socket_new(server->evbase, notifyfd, BEV_OPT_CLOSE_ON_FREE);
|
||||||
|
bufferevent_setcb(server->notify_buffer, NULL, notify_cb, NULL, server);
|
||||||
|
|
||||||
|
if (!server->listener)
|
||||||
|
{
|
||||||
|
server->listener = evconnlistener_new_bind(
|
||||||
|
server->evbase, listener_cb, (void *)server,
|
||||||
|
LEV_OPT_THREADSAFE | LEV_OPT_REUSEABLE | LEV_OPT_CLOSE_ON_FREE,
|
||||||
|
restd_server_get_option_int(server, "server.backlog"),
|
||||||
|
sockaddr, sockaddr_len);
|
||||||
|
if (!server->listener)
|
||||||
|
{
|
||||||
|
ERROR("Failed to bind on %s:%d", addr, port);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen
|
||||||
|
INFO("Listening on %s:%d", addr, port);
|
||||||
|
|
||||||
|
int exitstatus = 0;
|
||||||
|
if (restd_server_get_option_int(server, "server.thread"))
|
||||||
|
{
|
||||||
|
DEBUG("Launching server as a thread.")
|
||||||
|
server->thread = NEW_OBJECT(pthread_t);
|
||||||
|
pthread_create(server->thread, NULL, &server_loop, (void *)server);
|
||||||
|
//pthread_detach(server->thread);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int *retval = server_loop(server);
|
||||||
|
exitstatus = *retval;
|
||||||
|
free(retval);
|
||||||
|
|
||||||
|
close_server(server);
|
||||||
|
if (restd_server_get_option_int(server, "server.free_on_stop"))
|
||||||
|
{
|
||||||
|
restd_server_free(server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return exitstatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set server option.
|
||||||
|
*
|
||||||
|
* @see AD_SERVER_OPTIONS
|
||||||
|
*/
|
||||||
|
void restd_server_set_option(restd_server_t *server, const char *key, const char *value)
|
||||||
|
{
|
||||||
|
server->options->putstr(server->options, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve server option.
|
||||||
|
*/
|
||||||
|
char *restd_server_get_option(restd_server_t *server, const char *key)
|
||||||
|
{
|
||||||
|
return server->options->getstr(server->options, key, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve server option in integer format.
|
||||||
|
*/
|
||||||
|
int restd_server_get_option_int(restd_server_t *server, const char *key)
|
||||||
|
{
|
||||||
|
char *value = restd_server_get_option(server, key);
|
||||||
|
return (value) ? atoi(value) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If there's no event, loopbreak or loopexit call won't work until one more
|
||||||
|
* event arrived. So we use eventfd as a internal notification channel to let
|
||||||
|
* server get out of the loop without waiting for an event.
|
||||||
|
*/
|
||||||
|
static int notify_loopexit(restd_server_t *server)
|
||||||
|
{
|
||||||
|
uint64_t x = 0;
|
||||||
|
return bufferevent_write(server->notify_buffer, &x, sizeof(uint64_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void notify_cb(struct bufferevent *buffer, void *userdata)
|
||||||
|
{
|
||||||
|
restd_server_t *server = (restd_server_t *)userdata;
|
||||||
|
event_base_loopexit(server->evbase, NULL);
|
||||||
|
DEBUG("Existing loop.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void *server_loop(void *instance)
|
||||||
|
{
|
||||||
|
restd_server_t *server = (restd_server_t *)instance;
|
||||||
|
|
||||||
|
int *retval = NEW_OBJECT(int);
|
||||||
|
DEBUG("Loop start");
|
||||||
|
event_base_loop(server->evbase, 0);
|
||||||
|
DEBUG("Loop finished");
|
||||||
|
*retval = (event_base_got_break(server->evbase)) ? -1 : 0;
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void close_server(restd_server_t *server)
|
||||||
|
{
|
||||||
|
DEBUG("Closing server.");
|
||||||
|
|
||||||
|
if (server->notify_buffer)
|
||||||
|
{
|
||||||
|
bufferevent_free(server->notify_buffer);
|
||||||
|
server->notify_buffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server->listener)
|
||||||
|
{
|
||||||
|
evconnlistener_free(server->listener);
|
||||||
|
server->listener = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server->thread)
|
||||||
|
{
|
||||||
|
void *retval = NULL;
|
||||||
|
DEBUG("Waiting server's last loop to finish.");
|
||||||
|
pthread_join(*(server->thread), &retval);
|
||||||
|
free(retval);
|
||||||
|
free(server->thread);
|
||||||
|
server->thread = NULL;
|
||||||
|
}
|
||||||
|
INFO("Server closed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void libevent_log_cb(int severity, const char *msg)
|
||||||
|
{
|
||||||
|
switch (severity)
|
||||||
|
{
|
||||||
|
case _EVENT_LOG_MSG:
|
||||||
|
{
|
||||||
|
INFO("%s", msg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case _EVENT_LOG_WARN:
|
||||||
|
{
|
||||||
|
WARN("%s", msg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case _EVENT_LOG_ERR:
|
||||||
|
{
|
||||||
|
ERROR("%s", msg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
DEBUG("%s", msg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
// Set default options that were not set by user..
|
||||||
|
static int set_undefined_options(restd_server_t *server)
|
||||||
|
{
|
||||||
|
int newentries = 0;
|
||||||
|
char *default_options[][2] = RESTD_SERVER_OPTIONS;
|
||||||
|
for (int i = 0; !IS_EMPTY_STR(default_options[i][0]); i++)
|
||||||
|
{
|
||||||
|
if (!restd_server_get_option(server, default_options[i][0]))
|
||||||
|
{
|
||||||
|
restd_server_set_option(server, default_options[i][0], default_options[i][1]);
|
||||||
|
newentries++;
|
||||||
|
}
|
||||||
|
DEBUG("%s=%s", default_options[i][0], restd_server_get_option(server, default_options[i][0]));
|
||||||
|
}
|
||||||
|
return newentries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void listener_cb(struct evconnlistener *listener, evutil_socket_t socket,
|
||||||
|
struct sockaddr *sockaddr, int socklen, void *userdata)
|
||||||
|
{
|
||||||
|
DEBUG("New connection.");
|
||||||
|
restd_server_t *server = (restd_server_t *)userdata;
|
||||||
|
|
||||||
|
// Create a new buffer.
|
||||||
|
struct bufferevent *buffer = NULL;
|
||||||
|
|
||||||
|
buffer = bufferevent_socket_new(server->evbase, socket, BEV_OPT_CLOSE_ON_FREE);
|
||||||
|
|
||||||
|
if (buffer == NULL)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
// Set read timeout.
|
||||||
|
int timeout = restd_server_get_option_int(server, "server.timeout");
|
||||||
|
if (timeout > 0)
|
||||||
|
{
|
||||||
|
struct timeval tm;
|
||||||
|
bzero((void *)&tm, sizeof(struct timeval));
|
||||||
|
tm.tv_sec = timeout;
|
||||||
|
bufferevent_set_timeouts(buffer, &tm, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a connection.
|
||||||
|
void *conn = conn_new(server, buffer);
|
||||||
|
if (!conn)
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
error:
|
||||||
|
if (buffer)
|
||||||
|
bufferevent_free(buffer);
|
||||||
|
ERROR("Failed to create a connection handler.");
|
||||||
|
event_base_loopbreak(server->evbase);
|
||||||
|
server->errcode = ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static restd_conn_t *conn_new(restd_server_t *server, struct bufferevent *buffer)
|
||||||
|
{
|
||||||
|
if (server == NULL || buffer == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new connection container.
|
||||||
|
restd_conn_t *conn = NEW_OBJECT(restd_conn_t);
|
||||||
|
if (conn == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Initialize with default values.
|
||||||
|
conn->server = server;
|
||||||
|
conn->buffer = buffer;
|
||||||
|
conn->in = bufferevent_get_input(buffer);
|
||||||
|
conn->out = bufferevent_get_output(buffer);
|
||||||
|
conn_reset(conn);
|
||||||
|
|
||||||
|
// Bind callback
|
||||||
|
bufferevent_setcb(buffer, conn_read_cb, conn_write_cb, conn_event_cb, (void *)conn);
|
||||||
|
bufferevent_setwatermark(buffer, EV_WRITE, 0, 0);
|
||||||
|
bufferevent_enable(buffer, EV_WRITE);
|
||||||
|
bufferevent_enable(buffer, EV_READ);
|
||||||
|
|
||||||
|
// Run callbacks with AD_EVENT_INIT event.
|
||||||
|
conn->status = call_hooks(RESTD_EVENT_INIT | RESTD_EVENT_WRITE, conn);
|
||||||
|
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void conn_reset(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
conn->status = RESTD_OK;
|
||||||
|
|
||||||
|
for (int i = 0; i < RESTD_NUM_USERDATA; i++)
|
||||||
|
{
|
||||||
|
if (conn->userdata[i])
|
||||||
|
{
|
||||||
|
if (conn->userdata_free_cb[i] != NULL)
|
||||||
|
{
|
||||||
|
conn->userdata_free_cb[i](conn, conn->userdata[i]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WARN("Found unreleased userdata.");
|
||||||
|
}
|
||||||
|
conn->userdata[i] = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conn->method)
|
||||||
|
{
|
||||||
|
free(conn->method);
|
||||||
|
conn->method = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void conn_free(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
if (conn)
|
||||||
|
{
|
||||||
|
if (conn->status != RESTD_CLOSE)
|
||||||
|
{
|
||||||
|
call_hooks(RESTD_EVENT_CLOSE | RESTD_EVENT_SHUTDOWN, conn);
|
||||||
|
}
|
||||||
|
conn_reset(conn);
|
||||||
|
if (conn->buffer)
|
||||||
|
{
|
||||||
|
|
||||||
|
bufferevent_free(conn->buffer);
|
||||||
|
}
|
||||||
|
free(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#define DRAIN_EVBUFFER(b) evbuffer_drain(b, evbuffer_get_length(b))
|
||||||
|
static void conn_read_cb(struct bufferevent *buffer, void *userdata)
|
||||||
|
{
|
||||||
|
DEBUG("read_cb");
|
||||||
|
restd_conn_t *conn = userdata;
|
||||||
|
conn_cb(conn, RESTD_EVENT_READ);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void conn_write_cb(struct bufferevent *buffer, void *userdata)
|
||||||
|
{
|
||||||
|
DEBUG("write_cb");
|
||||||
|
restd_conn_t *conn = userdata;
|
||||||
|
conn_cb(conn, RESTD_EVENT_WRITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void conn_event_cb(struct bufferevent *buffer, short what, void *userdata)
|
||||||
|
{
|
||||||
|
DEBUG("event_cb 0x%x", what);
|
||||||
|
restd_conn_t *conn = userdata;
|
||||||
|
|
||||||
|
if (what & BEV_EVENT_EOF || what & BEV_EVENT_ERROR || what & BEV_EVENT_TIMEOUT)
|
||||||
|
{
|
||||||
|
conn->status = RESTD_CLOSE;
|
||||||
|
conn_cb(conn, RESTD_EVENT_CLOSE | ((what & BEV_EVENT_TIMEOUT) ? RESTD_EVENT_TIMEOUT : 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void conn_cb(restd_conn_t *conn, int event)
|
||||||
|
{
|
||||||
|
DEBUG("conn_cb: status:0x%x, event:0x%x", conn->status, event)
|
||||||
|
if (conn->status == RESTD_OK || conn->status == RESTD_TAKEOVER)
|
||||||
|
{
|
||||||
|
int status = call_hooks(event, conn);
|
||||||
|
// Update status only when it's higher then before.
|
||||||
|
if (!(conn->status == RESTD_CLOSE || (conn->status == RESTD_DONE && conn->status >= status)))
|
||||||
|
{
|
||||||
|
conn->status = status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (conn->status == RESTD_DONE)
|
||||||
|
{
|
||||||
|
if (restd_server_get_option_int(conn->server, "server.request_pipelining"))
|
||||||
|
{
|
||||||
|
call_hooks(RESTD_EVENT_CLOSE, conn);
|
||||||
|
conn_reset(conn);
|
||||||
|
call_hooks(RESTD_EVENT_INIT, conn);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Do nothing but drain input buffer.
|
||||||
|
if (event == RESTD_EVENT_READ)
|
||||||
|
{
|
||||||
|
DEBUG("Draining in-buffer. %d", conn->status);
|
||||||
|
DRAIN_EVBUFFER(conn->in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (conn->status == RESTD_CLOSE)
|
||||||
|
{
|
||||||
|
if (evbuffer_get_length(conn->out) <= 0)
|
||||||
|
{
|
||||||
|
int newevent = (event & RESTD_EVENT_CLOSE) ? event : RESTD_EVENT_CLOSE;
|
||||||
|
call_hooks(newevent, conn);
|
||||||
|
conn_free(conn);
|
||||||
|
DEBUG("Connection closed.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register user hook.
|
||||||
|
*/
|
||||||
|
void restd_server_register_hook(restd_server_t *server, restd_callback cb, void *userdata)
|
||||||
|
{
|
||||||
|
restd_server_register_hook_on_method(server, NULL, cb, userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register user hook on method name.
|
||||||
|
*/
|
||||||
|
void restd_server_register_hook_on_method(restd_server_t *server, const char *method, restd_callback cb, void *userdata)
|
||||||
|
{
|
||||||
|
restd_hook_t hook;
|
||||||
|
bzero((void *)&hook, sizeof(restd_hook_t));
|
||||||
|
hook.method = (method) ? strdup(method) : NULL;
|
||||||
|
hook.cb = cb;
|
||||||
|
hook.userdata = userdata;
|
||||||
|
|
||||||
|
server->hooks->addlast(server->hooks, (void *)&hook, sizeof(restd_hook_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static int call_hooks(short event, restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
DEBUG("call_hooks: event 0x%x", event);
|
||||||
|
qlist_t *hooks = conn->server->hooks;
|
||||||
|
|
||||||
|
qlist_obj_t obj;
|
||||||
|
bzero((void *)&obj, sizeof(qlist_obj_t));
|
||||||
|
while (hooks->getnext(hooks, &obj, false) == true)
|
||||||
|
{
|
||||||
|
restd_hook_t *hook = (restd_hook_t *)obj.data;
|
||||||
|
if (hook->cb)
|
||||||
|
{
|
||||||
|
if (hook->method && conn->method && strcmp(hook->method, conn->method))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int status = hook->cb(event, conn, hook->userdata);
|
||||||
|
if (status != RESTD_OK)
|
||||||
|
{
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return RESTD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void *set_userdata(restd_conn_t *conn, int index, const void *userdata, restd_userdata_free_cb free_cb)
|
||||||
|
{
|
||||||
|
void *prev = conn->userdata;
|
||||||
|
conn->userdata[index] = (void *)userdata;
|
||||||
|
conn->userdata_free_cb[index] = free_cb;
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static void *get_userdata(restd_conn_t *conn, int index)
|
||||||
|
{
|
||||||
|
return conn->userdata[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set extra userdata into the connection.
|
||||||
|
*
|
||||||
|
* @return previous userdata;
|
||||||
|
*
|
||||||
|
* @note
|
||||||
|
* Extra userdata is for default protocol handler such as ad_http_handler to
|
||||||
|
* provide higher abstraction. End users should always use only ad_conn_set_userdata()
|
||||||
|
* to avoid any conflict with default handlers.
|
||||||
|
*/
|
||||||
|
void *restd_conn_set_extra(restd_conn_t *conn, const void *extra, restd_userdata_free_cb free_cb)
|
||||||
|
{
|
||||||
|
return set_userdata(conn, 1, extra, free_cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get extra userdata attached in this connection.
|
||||||
|
*/
|
||||||
|
void *restd_conn_get_extra(restd_conn_t *conn)
|
||||||
|
{
|
||||||
|
return get_userdata(conn, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set method name on this connection.
|
||||||
|
*
|
||||||
|
* Once the method name is set, hooks registered by ad_server_register_hook_on_method()
|
||||||
|
* will be called if method name matches with the registered name.
|
||||||
|
*
|
||||||
|
* @see ad_server_register_hook_on_method()
|
||||||
|
*/
|
||||||
|
void restd_conn_set_method(restd_conn_t *conn, char *method)
|
||||||
|
{
|
||||||
|
char *prev = conn->method;
|
||||||
|
conn->method = (method != NULL) ? strdup(method) : NULL;
|
||||||
|
if (prev)
|
||||||
|
{
|
||||||
|
free(prev);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ set (CMAKE_MODULE_PATH "${MODULE_PATH}")
|
|||||||
|
|
||||||
link_directories(${CMAKE_SOURCE_DIR}/build/lib)
|
link_directories(${CMAKE_SOURCE_DIR}/build/lib)
|
||||||
|
|
||||||
include_directories (${CMAKE_SOURCE_DIR}/libasyncd/include)
|
include_directories (${CMAKE_SOURCE_DIR}/domo-iot/lib/include)
|
||||||
include_directories (${CMAKE_SOURCE_DIR}/libevent/include)
|
include_directories (${CMAKE_SOURCE_DIR}/libevent/include)
|
||||||
include_directories (${CMAKE_SOURCE_DIR}/build/libevent/include)
|
include_directories (${CMAKE_SOURCE_DIR}/build/libevent/include)
|
||||||
include_directories (${CMAKE_SOURCE_DIR}/qlibc/include)
|
include_directories (${CMAKE_SOURCE_DIR}/qlibc/include)
|
||||||
@@ -26,7 +26,7 @@ add_executable (domo-iot ${source_files})
|
|||||||
target_link_libraries (domo-iot
|
target_link_libraries (domo-iot
|
||||||
LINK_PUBLIC
|
LINK_PUBLIC
|
||||||
nats_static
|
nats_static
|
||||||
asyncd-static
|
restd-static
|
||||||
qlibc-static
|
qlibc-static
|
||||||
qlibcext-static
|
qlibcext-static
|
||||||
event
|
event
|
||||||
|
|||||||
44
src/main.c
44
src/main.c
@@ -28,45 +28,41 @@
|
|||||||
|
|
||||||
/*-------------------------------- INCLUDES ---------------------------------*/
|
/*-------------------------------- INCLUDES ---------------------------------*/
|
||||||
|
|
||||||
#include <asyncd/asyncd.h>
|
#include <restd.h>
|
||||||
|
|
||||||
int my_http_get_handler(short event, ad_conn_t *conn, void *userdata)
|
int my_http_get_handler(short event, restd_conn_t *conn, void *userdata)
|
||||||
{
|
{
|
||||||
if (event & AD_EVENT_READ)
|
if (event & RESTD_EVENT_READ)
|
||||||
{
|
{
|
||||||
if (ad_http_get_status(conn) == AD_HTTP_REQ_DONE)
|
if (restd_http_get_status(conn) == RESTD_HTTP_REQ_DONE)
|
||||||
{
|
{
|
||||||
ad_http_response(conn, 200, "text/html", "Hello World", 11);
|
restd_http_response(conn, 200, "text/html", "Hello World", 11);
|
||||||
return ad_http_is_keepalive_request(conn) ? AD_DONE : AD_CLOSE;
|
return restd_http_is_keepalive_request(conn) ? RESTD_DONE : RESTD_CLOSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return AD_OK;
|
return RESTD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int my_http_default_handler(short event, ad_conn_t *conn, void *userdata)
|
int my_http_default_handler(short event, restd_conn_t *conn, void *userdata)
|
||||||
{
|
{
|
||||||
if (event & AD_EVENT_READ)
|
if (event & RESTD_EVENT_READ)
|
||||||
{
|
{
|
||||||
if (ad_http_get_status(conn) == AD_HTTP_REQ_DONE)
|
if (restd_http_get_status(conn) == RESTD_HTTP_REQ_DONE)
|
||||||
{
|
{
|
||||||
ad_http_response(conn, 501, "text/html", "Not implemented", 15);
|
restd_http_response(conn, 501, "text/html", "Not implemented", 15);
|
||||||
return AD_CLOSE; // Close connection.
|
return RESTD_CLOSE; // Close connection.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return AD_OK;
|
return RESTD_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
//SSL_load_error_strings();
|
restd_log_level(RESTD_LOG_DEBUG);
|
||||||
//SSL_library_init();
|
restd_server_t *server = restd_server_new();
|
||||||
ad_log_level(AD_LOG_DEBUG);
|
restd_server_set_option(server, "server.port", "8888");
|
||||||
ad_server_t *server = ad_server_new();
|
restd_server_register_hook(server, restd_http_handler, NULL); // HTTP Parser is also a hook.
|
||||||
ad_server_set_option(server, "server.port", "8888");
|
restd_server_register_hook_on_method(server, "GET", my_http_get_handler, NULL);
|
||||||
//ad_server_set_ssl_ctx(server,
|
restd_server_register_hook(server, my_http_default_handler, NULL);
|
||||||
// ad_server_ssl_ctx_create_simple("ssl.cert", "ssl.pkey"));
|
return restd_server_start(server);
|
||||||
ad_server_register_hook(server, ad_http_handler, NULL); // HTTP Parser is also a hook.
|
|
||||||
ad_server_register_hook_on_method(server, "GET", my_http_get_handler, NULL);
|
|
||||||
ad_server_register_hook(server, my_http_default_handler, NULL);
|
|
||||||
return ad_server_start(server);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user