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

@ -1,125 +1,147 @@
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com> // Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
// Copyright (c) 2013-2014 Cesanta Software Limited // Copyright (c) 2013-2014 Cesanta Software Limited
// All rights reserved // All rights reserved
// //
// This library is dual-licensed: you can redistribute it and/or modify // This library is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as // it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this // published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>. // license, see <http://www.gnu.org/licenses/>.
// //
// You are free to use this library under the terms of the GNU General // You are free to use this library under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied // Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details. // See the GNU General Public License for more details.
// //
// Alternatively, you can license this library under a commercial // Alternatively, you can license this library under a commercial
// license, as set out in <http://cesanta.com/>. // license, as set out in <http://cesanta.com/>.
// //
// NOTE: Detailed API documentation is at http://cesanta.com/#docs // NOTE: Detailed API documentation is at http://cesanta.com/#docs
#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
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif // __cplusplus #endif // __cplusplus
// This structure contains information about HTTP request. // This structure contains information about HTTP request.
struct mg_connection { struct mg_connection {
const char *request_method; // "GET", "POST", etc const char *request_method; // "GET", "POST", etc
const char *uri; // URL-decoded URI const char *uri; // URL-decoded URI
const char *http_version; // E.g. "1.0", "1.1" const char *http_version; // E.g. "1.0", "1.1"
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
int num_headers; // Number of HTTP headers int num_headers; // Number of HTTP headers
struct mg_header { struct mg_header {
const char *name; // HTTP header name const char *name; // HTTP header name
const char *value; // HTTP header value const char *value; // HTTP header value
} 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 {
// Server management functions MG_POLL = 100, // Callback return value is ignored
struct mg_server *mg_create_server(void *server_param); MG_CONNECT, // If callback returns MG_FALSE, connect fails
void mg_destroy_server(struct mg_server **); MG_AUTH, // If callback returns MG_FALSE, authentication fails
const char *mg_set_option(struct mg_server *, const char *opt, const char *val); MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
unsigned int mg_poll_server(struct mg_server *, int milliseconds); MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
void mg_set_request_handler(struct mg_server *, mg_handler_t); MG_CLOSE, // Connection is closed, callback return value is ignored
void mg_set_http_close_handler(struct mg_server *, mg_handler_t); MG_WS_HANDSHAKE, // New websocket connection, handshake request
void mg_set_http_error_handler(struct mg_server *, mg_handler_t); MG_WS_CONNECT, // New websocket connection established
void mg_set_auth_handler(struct mg_server *, mg_handler_t); MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
const char **mg_get_valid_option_names(void); };
const char *mg_get_option(const struct mg_server *server, const char *name); typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
void mg_set_listening_socket(struct mg_server *, int sock);
int mg_get_listening_socket(struct mg_server *); // Websocket opcodes, from http://tools.ietf.org/html/rfc6455
void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *); enum {
WEBSOCKET_OPCODE_CONTINUATION = 0x0,
// Connection management functions WEBSOCKET_OPCODE_TEXT = 0x1,
void mg_send_status(struct mg_connection *, int status_code); WEBSOCKET_OPCODE_BINARY = 0x2,
void mg_send_header(struct mg_connection *, const char *name, const char *val); WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
void mg_send_data(struct mg_connection *, const void *data, int data_len); WEBSOCKET_OPCODE_PING = 0x9,
void mg_printf_data(struct mg_connection *, const char *format, ...); WEBSOCKET_OPCODE_PONG = 0xa
};
int mg_websocket_write(struct mg_connection *, int opcode,
const char *data, size_t data_len); // Server management functions
struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
// Deprecated in favor of mg_send_* interface void mg_destroy_server(struct mg_server **);
int mg_write(struct mg_connection *, const void *buf, int len); const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
int mg_printf(struct mg_connection *conn, const char *fmt, ...); int mg_poll_server(struct mg_server *, int milliseconds);
const char **mg_get_valid_option_names(void);
const char *mg_get_header(const struct mg_connection *, const char *name); const char *mg_get_option(const struct mg_server *server, const char *name);
const char *mg_get_mime_type(const char *name, const char *default_mime_type); void mg_set_listening_socket(struct mg_server *, int sock);
int mg_get_var(const struct mg_connection *conn, const char *var_name, int mg_get_listening_socket(struct mg_server *);
char *buf, size_t buf_len); void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
int mg_parse_multipart(const char *buf, int buf_len, void mg_wakeup_server(struct mg_server *);
char *var_name, int var_name_len, void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
char *file_name, int file_name_len, struct mg_connection *mg_connect(struct mg_server *, const char *, int, int);
const char **data, int *data_len);
// Connection management functions
// Utility functions void mg_send_status(struct mg_connection *, int status_code);
void *mg_start_thread(void *(*func)(void *), void *param); void mg_send_header(struct mg_connection *, const char *name, const char *val);
char *mg_md5(char buf[33], ...); size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
int mg_authorize_digest(struct mg_connection *c, FILE *fp); size_t mg_printf_data(struct mg_connection *, const char *format, ...);
size_t mg_write(struct mg_connection *, const void *buf, int len);
// Callback function return codes size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
enum { MG_REQUEST_NOT_PROCESSED, MG_REQUEST_PROCESSED, MG_REQUEST_CALL_AGAIN };
enum { MG_AUTH_FAIL, MG_AUTH_OK }; size_t mg_websocket_write(struct mg_connection *, int opcode,
enum { MG_ERROR_NOT_PROCESSED, MG_ERROR_PROCESSED }; const char *data, size_t data_len);
enum { MG_CLIENT_CONTINUE, MG_CLIENT_CLOSE }; size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
const char *fmt, ...);
// HTTP client events
enum { void mg_send_file(struct mg_connection *, const char *path);
MG_CONNECT_SUCCESS, MG_CONNECT_FAILURE,
MG_DOWNLOAD_SUCCESS, MG_DOWNLOAD_FAILURE 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);
int mg_connect(struct mg_server *, const char *host, int port, int use_ssl, int mg_get_var(const struct mg_connection *conn, const char *var_name,
mg_handler_t handler, void *param); char *buf, size_t buf_len);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
#ifdef __cplusplus int mg_parse_multipart(const char *buf, int buf_len,
} char *var_name, int var_name_len,
#endif // __cplusplus char *file_name, int file_name_len,
const char **data, int *data_len);
#endif // MONGOOSE_HEADER_INCLUDED
// Utility functions
void *mg_start_thread(void *(*func)(void *), void *param);
char *mg_md5(char buf[33], ...);
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);
// Templates support
struct mg_expansion {
const char *keyword;
void (*handler)(struct mg_connection *);
};
void mg_template(struct mg_connection *, const char *text,
struct mg_expansion *expansions);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // MONGOOSE_HEADER_INCLUDED

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:
c->content[c->content_len] = '\0'; mpd_close_handler(c);
if(c->content_len) return MG_TRUE;
return callback_mpd(c); case MG_REQUEST:
else if (c->is_websocket) {
return MG_CLIENT_CONTINUE; c->content[c->content_len] = '\0';
if(c->content_len)
return callback_mpd(c);
else
return MG_TRUE;
} else
return callback_http(c);
case MG_AUTH:
return MG_TRUE;
default:
return MG_FALSE;
} }
else
return callback_http(c);
} }
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;