1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-11-26 23:07:17 +00:00

upgraded to mongoose 5.4, introducing ipv6 support

This commit is contained in:
Andrew Karpow 2014-10-19 19:52:23 +02:00
parent 87fdd34f87
commit aa60cb8991
6 changed files with 2417 additions and 1611 deletions

View File

@ -7,6 +7,7 @@ set(CPACK_PACKAGE_VERSION_MINOR "2")
set(CPACK_PACKAGE_VERSION_PATCH "2") set(CPACK_PACKAGE_VERSION_PATCH "2")
option(WITH_MPD_HOST_CHANGE "Let users of the web frontend change the MPD Host" ON) option(WITH_MPD_HOST_CHANGE "Let users of the web frontend change the MPD Host" ON)
option(WITH_IPV6 "enable IPv6 support" ON)
find_package(LibMPDClient REQUIRED) find_package(LibMPDClient REQUIRED)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
@ -18,6 +19,9 @@ include(CheckCSourceCompiles)
set(CMAKE_C_FLAGS "-std=gnu99 -Wall") set(CMAKE_C_FLAGS "-std=gnu99 -Wall")
set(CMAKE_C_FLAGS_DEBUG "-ggdb -pedantic") set(CMAKE_C_FLAGS_DEBUG "-ggdb -pedantic")
if(WITH_IPV6)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS NS_ENABLE_IPV6)
endif()
file(GLOB RESOURCES file(GLOB RESOURCES
RELATIVE ${PROJECT_SOURCE_DIR} RELATIVE ${PROJECT_SOURCE_DIR}

View File

@ -34,10 +34,10 @@ int callback_http(struct mg_connection *c)
mg_send_header(c, "Content-Type", req_file->mimetype); mg_send_header(c, "Content-Type", req_file->mimetype);
mg_send_data(c, req_file->data, req_file->size); mg_send_data(c, req_file->data, req_file->size);
return MG_REQUEST_PROCESSED; return MG_TRUE;
} }
mg_send_status(c, 404); mg_send_status(c, 404);
mg_printf_data(c, "Not Found"); mg_printf_data(c, "Not Found");
return MG_REQUEST_PROCESSED; return MG_TRUE;
} }

File diff suppressed because it is too large Load Diff

View File

@ -20,7 +20,7 @@
#ifndef MONGOOSE_HEADER_INCLUDED #ifndef MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED #define MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_VERSION "5.3" #define MONGOOSE_VERSION "5.4"
#include <stdio.h> // required for FILE #include <stdio.h> // required for FILE
#include <stddef.h> // required for size_t #include <stddef.h> // required for size_t
@ -37,7 +37,7 @@ struct mg_connection {
const char *query_string; // URL part after '?', not including '?', or NULL const char *query_string; // URL part after '?', not including '?', or NULL
char remote_ip[48]; // Max IPv6 string length is 45 characters char remote_ip[48]; // Max IPv6 string length is 45 characters
const char *local_ip; // Local IP address char local_ip[48]; // Local IP address
unsigned short remote_port; // Client's port unsigned short remote_port; // Client's port
unsigned short local_port; // Local port number unsigned short local_port; // Local port number
@ -48,46 +48,70 @@ struct mg_connection {
} http_headers[30]; } http_headers[30];
char *content; // POST (or websocket message) data, or NULL char *content; // POST (or websocket message) data, or NULL
size_t content_len; // content length size_t content_len; // Data length
int is_websocket; // Connection is a websocket connection int is_websocket; // Connection is a websocket connection
int status_code; // HTTP status code for HTTP error handler int status_code; // HTTP status code for HTTP error handler
int wsbits; // First byte of the websocket frame int wsbits; // First byte of the websocket frame
void *server_param; // Parameter passed to mg_add_uri_handler() void *server_param; // Parameter passed to mg_add_uri_handler()
void *connection_param; // Placeholder for connection-specific data void *connection_param; // Placeholder for connection-specific data
void *callback_param; // Used by mg_iterate_over_connections() void *callback_param; // Needed by mg_iterate_over_connections()
}; };
struct mg_server; // Opaque structure describing server instance struct mg_server; // Opaque structure describing server instance
typedef int (*mg_handler_t)(struct mg_connection *); enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
enum mg_event {
MG_POLL = 100, // Callback return value is ignored
MG_CONNECT, // If callback returns MG_FALSE, connect fails
MG_AUTH, // If callback returns MG_FALSE, authentication fails
MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
MG_CLOSE, // Connection is closed, callback return value is ignored
MG_WS_HANDSHAKE, // New websocket connection, handshake request
MG_WS_CONNECT, // New websocket connection established
MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
};
typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
// Websocket opcodes, from http://tools.ietf.org/html/rfc6455
enum {
WEBSOCKET_OPCODE_CONTINUATION = 0x0,
WEBSOCKET_OPCODE_TEXT = 0x1,
WEBSOCKET_OPCODE_BINARY = 0x2,
WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
WEBSOCKET_OPCODE_PING = 0x9,
WEBSOCKET_OPCODE_PONG = 0xa
};
// Server management functions // Server management functions
struct mg_server *mg_create_server(void *server_param); struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
void mg_destroy_server(struct mg_server **); void mg_destroy_server(struct mg_server **);
const char *mg_set_option(struct mg_server *, const char *opt, const char *val); const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
unsigned int mg_poll_server(struct mg_server *, int milliseconds); int mg_poll_server(struct mg_server *, int milliseconds);
void mg_set_request_handler(struct mg_server *, mg_handler_t);
void mg_set_http_close_handler(struct mg_server *, mg_handler_t);
void mg_set_http_error_handler(struct mg_server *, mg_handler_t);
void mg_set_auth_handler(struct mg_server *, mg_handler_t);
const char **mg_get_valid_option_names(void); const char **mg_get_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name); const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_set_listening_socket(struct mg_server *, int sock); void mg_set_listening_socket(struct mg_server *, int sock);
int mg_get_listening_socket(struct mg_server *); int mg_get_listening_socket(struct mg_server *);
void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *); void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *);
struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
void mg_wakeup_server(struct mg_server *);
void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
struct mg_connection *mg_connect(struct mg_server *, const char *, int, int);
// Connection management functions // Connection management functions
void mg_send_status(struct mg_connection *, int status_code); void mg_send_status(struct mg_connection *, int status_code);
void mg_send_header(struct mg_connection *, const char *name, const char *val); void mg_send_header(struct mg_connection *, const char *name, const char *val);
void mg_send_data(struct mg_connection *, const void *data, int data_len); size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
void mg_printf_data(struct mg_connection *, const char *format, ...); size_t mg_printf_data(struct mg_connection *, const char *format, ...);
size_t mg_write(struct mg_connection *, const void *buf, int len);
size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
int mg_websocket_write(struct mg_connection *, int opcode, size_t mg_websocket_write(struct mg_connection *, int opcode,
const char *data, size_t data_len); const char *data, size_t data_len);
size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
const char *fmt, ...);
// Deprecated in favor of mg_send_* interface void mg_send_file(struct mg_connection *, const char *path);
int mg_write(struct mg_connection *, const void *buf, int len);
int mg_printf(struct mg_connection *conn, const char *fmt, ...);
const char *mg_get_header(const struct mg_connection *, const char *name); const char *mg_get_header(const struct mg_connection *, const char *name);
const char *mg_get_mime_type(const char *name, const char *default_mime_type); const char *mg_get_mime_type(const char *name, const char *default_mime_type);
@ -103,20 +127,18 @@ int mg_parse_multipart(const char *buf, int buf_len,
void *mg_start_thread(void *(*func)(void *), void *param); void *mg_start_thread(void *(*func)(void *), void *param);
char *mg_md5(char buf[33], ...); char *mg_md5(char buf[33], ...);
int mg_authorize_digest(struct mg_connection *c, FILE *fp); int mg_authorize_digest(struct mg_connection *c, FILE *fp);
int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int);
int mg_terminate_ssl(struct mg_connection *c, const char *cert);
// Callback function return codes // Templates support
enum { MG_REQUEST_NOT_PROCESSED, MG_REQUEST_PROCESSED, MG_REQUEST_CALL_AGAIN }; struct mg_expansion {
enum { MG_AUTH_FAIL, MG_AUTH_OK }; const char *keyword;
enum { MG_ERROR_NOT_PROCESSED, MG_ERROR_PROCESSED }; void (*handler)(struct mg_connection *);
enum { MG_CLIENT_CONTINUE, MG_CLIENT_CLOSE };
// HTTP client events
enum {
MG_CONNECT_SUCCESS, MG_CONNECT_FAILURE,
MG_DOWNLOAD_SUCCESS, MG_DOWNLOAD_FAILURE
}; };
int mg_connect(struct mg_server *, const char *host, int port, int use_ssl, void mg_template(struct mg_connection *, const char *text,
mg_handler_t handler, void *param); struct mg_expansion *expansions);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -49,11 +49,11 @@ int callback_mpd(struct mg_connection *c)
char *p_charbuf = NULL; char *p_charbuf = NULL;
if(cmd_id == -1) if(cmd_id == -1)
return MG_CLIENT_CONTINUE; return MG_TRUE;
if(mpd.conn_state != MPD_CONNECTED && cmd_id != MPD_API_SET_MPDHOST && if(mpd.conn_state != MPD_CONNECTED && cmd_id != MPD_API_SET_MPDHOST &&
cmd_id != MPD_API_GET_MPDHOST && cmd_id != MPD_API_SET_MPDPASS) cmd_id != MPD_API_GET_MPDHOST && cmd_id != MPD_API_SET_MPDPASS)
return MG_CLIENT_CONTINUE; return MG_TRUE;
mpd_connection_set_timeout(mpd.conn, 10000); mpd_connection_set_timeout(mpd.conn, 10000);
switch(cmd_id) switch(cmd_id)
@ -163,7 +163,7 @@ int callback_mpd(struct mg_connection *c)
free(p_charbuf); free(p_charbuf);
mpd.port = int_buf; mpd.port = int_buf;
mpd.conn_state = MPD_RECONNECT; mpd.conn_state = MPD_RECONNECT;
return MG_CLIENT_CONTINUE; return MG_TRUE;
} }
break; break;
case MPD_API_GET_MPDHOST: case MPD_API_GET_MPDHOST:
@ -179,7 +179,7 @@ int callback_mpd(struct mg_connection *c)
mpd.password = p_charbuf; mpd.password = p_charbuf;
mpd.conn_state = MPD_RECONNECT; mpd.conn_state = MPD_RECONNECT;
return MG_CLIENT_CONTINUE; return MG_TRUE;
} }
break; break;
#endif #endif
@ -198,7 +198,7 @@ int callback_mpd(struct mg_connection *c)
if(n > 0) if(n > 0)
mg_websocket_write(c, 1, mpd.buf, n); mg_websocket_write(c, 1, mpd.buf, n);
return MG_CLIENT_CONTINUE; return MG_TRUE;
} }
int mpd_close_handler(struct mg_connection *c) int mpd_close_handler(struct mg_connection *c)
@ -209,11 +209,11 @@ int mpd_close_handler(struct mg_connection *c)
return 0; return 0;
} }
static int mpd_notify_callback(struct mg_connection *c) { static int mpd_notify_callback(struct mg_connection *c, enum mg_event ev) {
size_t n; size_t n;
if(!c->is_websocket) if(!c->is_websocket)
return MG_REQUEST_PROCESSED; return MG_TRUE;
if(c->callback_param) if(c->callback_param)
{ {
@ -222,7 +222,7 @@ static int mpd_notify_callback(struct mg_connection *c) {
(const char *)c->callback_param); (const char *)c->callback_param);
mg_websocket_write(c, 1, mpd.buf, n); mg_websocket_write(c, 1, mpd.buf, n);
return MG_REQUEST_PROCESSED; return MG_TRUE;
} }
if(!c->connection_param) if(!c->connection_param)
@ -253,7 +253,7 @@ static int mpd_notify_callback(struct mg_connection *c) {
} }
} }
return MG_REQUEST_PROCESSED; return MG_TRUE;
} }
void mpd_poll(struct mg_server *s) void mpd_poll(struct mg_server *s)

View File

@ -38,28 +38,37 @@ void bye()
force_exit = 1; force_exit = 1;
} }
static int server_callback(struct mg_connection *c) { static int server_callback(struct mg_connection *c, enum mg_event ev) {
if (c->is_websocket) switch(ev) {
{ case MG_CLOSE:
mpd_close_handler(c);
return MG_TRUE;
case MG_REQUEST:
if (c->is_websocket) {
c->content[c->content_len] = '\0'; c->content[c->content_len] = '\0';
if(c->content_len) if(c->content_len)
return callback_mpd(c); return callback_mpd(c);
else else
return MG_CLIENT_CONTINUE; return MG_TRUE;
} } else
else
return callback_http(c); return callback_http(c);
case MG_AUTH:
return MG_TRUE;
default:
return MG_FALSE;
}
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int n, option_index = 0; int n, option_index = 0;
struct mg_server *server = mg_create_server(NULL); struct mg_server *server = mg_create_server(NULL, server_callback);
unsigned int current_timer = 0, last_timer = 0; unsigned int current_timer = 0, last_timer = 0;
char *run_as_user = NULL; char *run_as_user = NULL;
char const *error_msg = NULL;
atexit(bye); atexit(bye);
mg_set_option(server, "listening_port", "8080"); error_msg = mg_set_option(server, "listening_port", "8080");
mpd.port = 6600; mpd.port = 6600;
strcpy(mpd.host, "127.0.0.1"); strcpy(mpd.host, "127.0.0.1");
@ -82,7 +91,7 @@ int main(int argc, char **argv)
case 'p': case 'p':
mpd.port = atoi(optarg); mpd.port = atoi(optarg);
case 'w': case 'w':
mg_set_option(server, "listening_port", optarg); error_msg = mg_set_option(server, "listening_port", optarg);
break; break;
case 'u': case 'u':
run_as_user = strdup(optarg); run_as_user = strdup(optarg);
@ -105,19 +114,29 @@ int main(int argc, char **argv)
, argv[0]); , argv[0]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(error_msg)
{
fprintf(stderr, "Mongoose error: %s\n", error_msg);
return EXIT_FAILURE;
}
} }
/* drop privilges at last to ensure proper port binding */ /* drop privilges at last to ensure proper port binding */
if(run_as_user != NULL) if(run_as_user != NULL)
{ {
mg_set_option(server, "run_as_user", run_as_user); error_msg = mg_set_option(server, "run_as_user", run_as_user);
free(run_as_user); free(run_as_user);
if(error_msg)
{
fprintf(stderr, "Mongoose error: %s\n", error_msg);
return EXIT_FAILURE;
}
} }
mg_set_http_close_handler(server, mpd_close_handler);
mg_set_request_handler(server, server_callback);
while (!force_exit) { while (!force_exit) {
current_timer = mg_poll_server(server, 200); mg_poll_server(server, 200);
current_timer = time(NULL);
if(current_timer - last_timer) if(current_timer - last_timer)
{ {
last_timer = current_timer; last_timer = current_timer;