1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-06 05:02:40 +03:00

Removed config option structure

This commit is contained in:
Lammert Bies
2016-12-29 00:16:50 +01:00
parent fbb26a2a70
commit ab54a82c2d
9 changed files with 118 additions and 166 deletions

View File

@@ -429,7 +429,6 @@ LIBHTTP_API void httplib_set_auth_handler(struct httplib_context *ctx, const cha
If given parameter name is not valid, NULL is returned. For valid
names, return value is guaranteed to be non-NULL. If parameter is not
set, zero-length string is returned. */
LIBHTTP_API const char *httplib_get_option(const struct httplib_context *ctx, const char *name);
/* Get context from connection. */
@@ -441,28 +440,6 @@ httplib_get_context(const struct httplib_connection *conn);
LIBHTTP_API void *httplib_get_user_data(const struct httplib_context *ctx);
struct httplib_option {
const char *name;
int type;
const char *default_value;
};
enum {
CONFIG_TYPE_UNKNOWN = 0x0,
};
/* Return array of struct httplib_option, representing all valid configuration
options of libhttp.c.
The array is terminated by a NULL name option. */
LIBHTTP_API const struct httplib_option *httplib_get_valid_options(void);
struct httplib_server_ports {
int protocol; /* 1 = IPv4, 2 = IPv6, 3 = both */
int port; /* port number */
@@ -941,6 +918,7 @@ LIBHTTP_API int httplib_closedir( DIR *dir );
LIBHTTP_API void httplib_cry( const struct httplib_context *ctx, const struct httplib_connection *conn, PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(3, 4);
LIBHTTP_API char * httplib_error_string( int error_code, char *buf, size_t buf_len );
LIBHTTP_API const char * httplib_get_builtin_mime_type( const char *file_name );
LIBHTTP_API const char * httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen );
LIBHTTP_API uint64_t httplib_get_random( void );
LIBHTTP_API void * httplib_get_user_connection_data( const struct httplib_connection *conn );
LIBHTTP_API int httplib_kill( pid_t pid, int sig_num );

View File

@@ -27,14 +27,6 @@
#include "httplib_main.h"
/*
* Config option name, config types, default value
*/
struct httplib_option XX_httplib_config_options[] = {
{ NULL, CONFIG_TYPE_UNKNOWN, NULL }
};
/*
* Check if the XX_httplib_config_options and the corresponding enum have
* compatible sizes

View File

@@ -39,7 +39,6 @@
void XX_httplib_free_context( struct httplib_context *ctx ) {
int i;
struct httplib_handler_info *tmp_rh;
if ( ctx == NULL ) return;
@@ -105,8 +104,6 @@ void XX_httplib_free_context( struct httplib_context *ctx ) {
ctx->url_rewrite_patterns = httplib_free( ctx->url_rewrite_patterns );
ctx->websocket_root = httplib_free( ctx->websocket_root );
for (i = 0; i < NUM_OPTIONS; i++) ctx->cfg[i] = httplib_free( ctx->cfg[i] );
/*
* Deallocate request handlers
*/

View File

@@ -1,7 +1,5 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2016 the Civetweb developers
* Copyright (c) 2004-2013 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,8 +22,12 @@
#include "httplib_main.h"
static const char * store_bool( char *buffer, size_t buflen, bool value );
static const char * store_int( char *buffer, size_t buflen, int value );
static const char * store_str( char *buffer, size_t buflen, const char *value );
/*
* const char *httplib_get_option( const struct httplib_context *ctx, const char *name );
* const char *httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen );
*
* The function httplib_get_option() returns the content of an option for a
* given context. If an error occurs, NULL is returned. If the option is valid
@@ -33,17 +35,102 @@
* string.
*/
const char *httplib_get_option( const struct httplib_context *ctx, const char *name ) {
const char *httplib_get_option( const struct httplib_context *ctx, const char *name, char *buffer, size_t buflen ) {
int i;
if ( name == NULL || buffer == NULL || buflen < 1 ) return NULL;
if ( name == NULL ) return NULL;
buffer[0] = '\0';
i = XX_httplib_get_option_index( name );
if ( i == -1 ) return NULL;
if ( ! httplib_strcasecmp( name, "access_control_allow_origin" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->access_control_allow_origin );
if ( ! httplib_strcasecmp( name, "access_control_list" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->access_control_list );
if ( ! httplib_strcasecmp( name, "access_log_file" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->access_log_file );
if ( ! httplib_strcasecmp( name, "allow_sendfile_call" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->allow_sendfile_call );
if ( ! httplib_strcasecmp( name, "authentication_domain" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->authentication_domain );
if ( ! httplib_strcasecmp( name, "cgi_environment" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->cgi_environment );
if ( ! httplib_strcasecmp( name, "cgi_interpreter" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->cgi_interpreter );
if ( ! httplib_strcasecmp( name, "cgi_pattern" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->cgi_pattern );
if ( ! httplib_strcasecmp( name, "decode_url" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->decode_url );
if ( ! httplib_strcasecmp( name, "document_root" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->document_root );
if ( ! httplib_strcasecmp( name, "enable_directory_listing" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->enable_directory_listing );
if ( ! httplib_strcasecmp( name, "enable_keep_alive" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->enable_keep_alive );
if ( ! httplib_strcasecmp( name, "error_log_file" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->error_log_file );
if ( ! httplib_strcasecmp( name, "error_pages" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->error_pages );
if ( ! httplib_strcasecmp( name, "extra_mime_types" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->extra_mime_types );
if ( ! httplib_strcasecmp( name, "global_auth_file" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->global_auth_file );
if ( ! httplib_strcasecmp( name, "hide_file_pattern" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->hide_file_pattern );
if ( ! httplib_strcasecmp( name, "index_files" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->index_files );
if ( ! httplib_strcasecmp( name, "listening_ports" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->listening_ports );
if ( ! httplib_strcasecmp( name, "num_threads" ) ) return (ctx == NULL) ? buffer : store_int( buffer, buflen, ctx->num_threads );
if ( ! httplib_strcasecmp( name, "protect_uri" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->protect_uri );
if ( ! httplib_strcasecmp( name, "put_delete_auth_file" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->put_delete_auth_file );
if ( ! httplib_strcasecmp( name, "request_timeout" ) ) return (ctx == NULL) ? buffer : store_int( buffer, buflen, ctx->request_timeout );
if ( ! httplib_strcasecmp( name, "run_as_user" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->run_as_user );
if ( ! httplib_strcasecmp( name, "ssi_pattern" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->ssi_pattern );
if ( ! httplib_strcasecmp( name, "ssl_ca_file" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->ssl_ca_file );
if ( ! httplib_strcasecmp( name, "ssl_ca_path" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->ssl_ca_path );
if ( ! httplib_strcasecmp( name, "ssl_certificate" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->ssl_certificate );
if ( ! httplib_strcasecmp( name, "ssl_cipher_list" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->ssl_cipher_list );
if ( ! httplib_strcasecmp( name, "ssl_protocol_version" ) ) return (ctx == NULL) ? buffer : store_int( buffer, buflen, ctx->ssl_protocol_version );
if ( ! httplib_strcasecmp( name, "ssl_short_trust" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->ssl_short_trust );
if ( ! httplib_strcasecmp( name, "ssl_verify_depth" ) ) return (ctx == NULL) ? buffer : store_int( buffer, buflen, ctx->ssl_verify_depth );
if ( ! httplib_strcasecmp( name, "ssl_verify_paths" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->ssl_verify_paths );
if ( ! httplib_strcasecmp( name, "ssl_verify_peer" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->ssl_verify_peer );
if ( ! httplib_strcasecmp( name, "static_file_max_age" ) ) return (ctx == NULL) ? buffer : store_int( buffer, buflen, ctx->static_file_max_age );
if ( ! httplib_strcasecmp( name, "throttle" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->throttle );
if ( ! httplib_strcasecmp( name, "tcp_nodelay" ) ) return (ctx == NULL) ? buffer : store_bool( buffer, buflen, ctx->tcp_nodelay );
if ( ! httplib_strcasecmp( name, "url_rewrite_patterns" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->url_rewrite_patterns );
if ( ! httplib_strcasecmp( name, "websocket_root" ) ) return (ctx == NULL) ? buffer : store_str( buffer, buflen, ctx->websocket_root );
if ( ! httplib_strcasecmp( name, "websocket_timeout" ) ) return (ctx == NULL) ? buffer : store_int( buffer, buflen, ctx->websocket_timeout );
if ( ctx == NULL || ctx->cfg[i] == NULL ) return "";
return ctx->cfg[i];
return NULL;
} /* httplib_get_option */
/*
* static const char *store_str( char *buffer, size_t buflen, const char *value );
*
* The function store_str() returns a pointer to a filled string containing the
* value of a configuration option. If an error occurs or the option is NULL,
* the function returns NULL.
*/
static const char *store_str( char *buffer, size_t buflen, const char *value ) {
if ( value == NULL ) return NULL;
if ( buflen < strlen(value)+1 ) return NULL;
httplib_strlcpy( buffer, value, buflen );
return buffer;
} /* store_str */
/*
* static const char *store_bool( char *buffer, size_t buflen, bool value );
*
* The function store_bool() returns a pointer to a filled string containing
* the value of a boolean configuration option. If an error occurs the function
* returns NULL.
*/
static const char *store_bool( char *buffer, size_t buflen, bool value ) {
return store_str( buffer, buflen, (value) ? "yes" : "no" );
} /* store_bool */
/*
* static const char *store_int( char *buffer, size_t buflen, int value );
*
* The function store_int() returns a pointer to a filled string containing the
* value of an integer configuration option. If an error occurs the function
* returns NULL.
*/
static const char *store_int( char *buffer, size_t buflen, int value ) {
char storage[32];
snprintf( storage, 32, "%d", value );
return store_str( buffer, buflen, storage );
} /* store_int */

View File

@@ -26,18 +26,3 @@
*/
#include "httplib_main.h"
int XX_httplib_get_option_index( const char *name ) {
int i;
if ( name == NULL ) return -1;
for (i=0; XX_httplib_config_options[i].name != NULL; i++) {
if ( strcmp( XX_httplib_config_options[i].name, name ) == 0 ) return i;
}
return -1;
} /* XX_httplib_get_option_index */

View File

@@ -26,9 +26,3 @@
*/
#include "httplib_main.h"
const struct httplib_option *httplib_get_valid_options( void ) {
return XX_httplib_config_options;
} /* httplib_get_valid_options */

View File

@@ -389,11 +389,6 @@ union usa {
struct sockaddr_in6 sin6;
};
/* NOTE(lsm): this enum shoulds be in sync with the config_options below. */
enum {
NUM_OPTIONS
};
/*
* enum CTX_STATUS_...
*
@@ -531,7 +526,6 @@ struct httplib_context {
volatile enum ctx_status_t status; /* Should we stop event loop */
SSL_CTX *ssl_ctx; /* SSL context */
char *cfg[NUM_OPTIONS]; /* LibHTTP configuration parameters */
struct httplib_callbacks callbacks; /* User-defined callback function */
void *user_data; /* User-defined data */
enum ctx_type_t ctx_type; /* CTX_TYPE_SERVER or CTX_TYPE_CLIENT */
@@ -815,7 +809,6 @@ bool XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp,
void XX_httplib_free_context( struct httplib_context *ctx );
const char * XX_httplib_get_header( const struct httplib_request_info *ri, const char *name );
void XX_httplib_get_mime_type( struct httplib_context *ctx, const char *path, struct vec *vec );
int XX_httplib_get_option_index( const char *name );
const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const struct httplib_connection *conn );
uint32_t XX_httplib_get_remote_ip( const struct httplib_connection *conn );
int XX_httplib_get_request_handler( struct httplib_connection *conn, int handler_type, httplib_request_handler *handler, httplib_websocket_connect_handler *connect_handler, httplib_websocket_ready_handler *ready_handler, httplib_websocket_data_handler *data_handler, httplib_websocket_close_handler *close_handler, httplib_authorization_handler *auth_handler, void **cbdata );
@@ -934,4 +927,3 @@ extern pthread_mutexattr_t XX_httplib_pthread_mutex_attr;
#endif /* _WIN32 */
extern const struct uriprot_tp XX_httplib_abs_uri_protocols[];
extern struct httplib_option XX_httplib_config_options[];

View File

@@ -280,10 +280,6 @@ struct httplib_context *httplib_start( const struct httplib_callbacks *callbacks
static bool process_options( struct httplib_context *ctx, const struct httplib_option_t *options ) {
int i;
int idx;
const char *default_value;
if ( ctx == NULL ) return false;
ctx->access_control_allow_origin = NULL;
@@ -406,33 +402,11 @@ static bool process_options( struct httplib_context *ctx, const struct httplib_o
if ( check_dir( ctx, options, "websocket_root", & ctx->websocket_root ) ) return true;
if ( check_int( ctx, options, "websocket_timeout", & ctx->websocket_timeout, 0, INT_MAX ) ) return true;
else {
idx = XX_httplib_get_option_index( options->name );
if ( idx == -1 ) { cleanup( ctx, "Invalid option: %s", options->name ); return true; }
if ( options->value == NULL ) { cleanup( ctx, "%s: option value cannot be NULL", options->name ); return true; }
if ( ctx->cfg[idx] != NULL ) {
httplib_cry( ctx, NULL, "warning: %s: duplicate option", options->name );
ctx->cfg[idx] = httplib_free( ctx->cfg[idx] );
}
ctx->cfg[idx] = httplib_strdup( options->value );
}
options++;
}
/*
* Set default value if needed
* TODO: Currently silently ignoring unrecognized options
*/
for (i=0; XX_httplib_config_options[i].name != NULL; i++) {
default_value = XX_httplib_config_options[i].default_value;
if ( ctx->cfg[i] == NULL && default_value != NULL ) ctx->cfg[i] = httplib_strdup( default_value );
options++;
}
return false;

View File

@@ -136,6 +136,11 @@ struct tuser_data {
char *first_message;
};
struct httplib_option {
const char * name;
const char * value;
};
static int g_exit_flag = 0; /* Main loop should exit */
static char g_server_base_name[40]; /* Set by init_server_name() */
static const char *g_server_name; /* Set by init_server_name() */
@@ -162,12 +167,11 @@ static struct tuser_data
enum { OPTION_TITLE, OPTION_ICON, NUM_MAIN_OPTIONS };
static struct httplib_option main_config_options[] = {
{ "title", 0x02, NULL },
{ "icon", 0x02, NULL },
{ NULL, CONFIG_TYPE_UNKNOWN, NULL }
{ "title", NULL },
{ "icon", NULL },
{ NULL, NULL }
};
static void WINCDECL signal_handler(int sig_num) {
g_exit_flag = sig_num;
@@ -212,9 +216,6 @@ show_server_name(void)
static NO_RETURN void show_usage_and_exit( const char *exeName ) {
const struct httplib_option *options;
int i;
if ( exeName == NULL || *exeName == '\0' ) exeName = "libhttp";
show_server_name();
@@ -233,16 +234,6 @@ static NO_RETURN void show_usage_and_exit( const char *exeName ) {
fprintf(stderr, " %s -R <htpasswd_file> <realm> <user>\n", exeName);
fprintf(stderr, "\nOPTIONS:\n");
options = httplib_get_valid_options();
for (i = 0; options[i].name != NULL; i++) {
fprintf(stderr, " -%s %s\n", options[i].name, ((options[i].default_value == NULL) ? "<empty>" : options[i].default_value));
}
options = main_config_options;
for (i = 0; options[i].name != NULL; i++) {
fprintf(stderr, " -%s %s\n", options[i].name, ((options[i].default_value == NULL) ? "<empty>" : options[i].default_value));
}
exit(EXIT_FAILURE);
}
@@ -335,8 +326,6 @@ static const char * get_option( struct httplib_option_t *options, const char *op
static int set_option( struct httplib_option_t *options, const char *name, const char *value ) {
int i;
int type;
const struct httplib_option *default_options = httplib_get_valid_options();
for (i = 0; main_config_options[i].name != NULL; i++) {
if (0 == strcmp(name, main_config_options[i].name)) {
@@ -346,44 +335,6 @@ static int set_option( struct httplib_option_t *options, const char *name, const
}
}
type = CONFIG_TYPE_UNKNOWN;
for (i = 0; default_options[i].name != NULL; i++) {
if (!strcmp( default_options[i].name, name ) ) type = default_options[i].type;
}
switch (type) {
case CONFIG_TYPE_UNKNOWN:
/* unknown option */
return 0;
case 0x1 : /* CONFIG_TYPE_NUMBER: */
/* integer number > 0, e.g. number of threads */
if (atol(value) < 0) {
/* invalid number */
return 0;
}
break;
case 0x02 : /* CONFIG_TYPE_STRING: */
/* any text */
break;
case 0x5 : /* CONFIG_TYPE_BOOLEAN: */
/* boolean value, yes or no */
if ((0 != strcmp(value, "yes")) && (0 != strcmp(value, "no"))) {
/* invalid boolean */
return 0;
}
break;
case 0x03 : /* CONFIG_TYPE_FILE: */
case 0x04 : /* CONFIG_TYPE_DIRECTORY: */
/* TODO (low): check this option when it is set, instead of calling
* verify_existence later */
break;
case 0x06 : /* CONFIG_TYPE_EXT_PATTERN: */
/* list of file extentions */
break;
default:
die("Unknown option type - option %s", name);
}
for (i = 0; i < MAX_OPTIONS; i++) {
if ( options[i].name == NULL ) {
@@ -2072,16 +2023,18 @@ main(int argc, char *argv[])
#else /* GUI */
int
main(int argc, char *argv[])
{
int main( int argc, char *argv[] ) {
char buf1[1024];
char buf2[1024];
init_server_name( argc, (const char **)argv );
start_libhttp(argc, argv);
fprintf(stdout,
"%s started on port(s) %s with web root [%s]\n",
g_server_name,
httplib_get_option(g_ctx, "listening_ports"),
httplib_get_option(g_ctx, "document_root"));
httplib_get_option( g_ctx, "listening_ports", buf1, sizeof(buf1) ),
httplib_get_option( g_ctx, "document_root", buf2, sizeof(buf2) ) );
while (g_exit_flag == 0) {
sleep(1);
}