mirror of
https://github.com/SuperBFG7/ympd
synced 2024-12-28 11:50:26 +00:00
t
This commit is contained in:
parent
49694c78df
commit
1ec430513f
@ -47,7 +47,7 @@ file(GLOB RESOURCES
|
|||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/mympd.c
|
src/mympd.c
|
||||||
src/mpd_client.c
|
src/mpd_client.c
|
||||||
src/mongoose.c
|
src/mongoose/mongoose.c
|
||||||
src/json_encode.c
|
src/json_encode.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
/* myMPD
|
|
||||||
(c) 2018 Juergen Mang <mail@jcgames.de>
|
|
||||||
This project's homepage is: https://github.com/jcorporation/ympd
|
|
||||||
|
|
||||||
myMPD ist fork of:
|
|
||||||
|
|
||||||
ympd
|
|
||||||
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
|
|
||||||
This project's homepage is: http://www.ympd.org
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation; version 2 of the License.
|
|
||||||
|
|
||||||
This program 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 General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License along
|
|
||||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "http_server.h"
|
|
||||||
|
|
||||||
int callback_http(struct mg_connection *c)
|
|
||||||
{
|
|
||||||
const struct embedded_file *req_file;
|
|
||||||
|
|
||||||
if(!strcmp(c->uri, "/"))
|
|
||||||
req_file = find_embedded_file("/index.html");
|
|
||||||
else
|
|
||||||
req_file = find_embedded_file(c->uri);
|
|
||||||
|
|
||||||
if(req_file)
|
|
||||||
{
|
|
||||||
mg_send_header(c, "Content-Type", req_file->mimetype);
|
|
||||||
mg_send_data(c, req_file->data, req_file->size);
|
|
||||||
|
|
||||||
return MG_TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
mg_send_status(c, 404);
|
|
||||||
mg_printf_data(c, "Not Found");
|
|
||||||
return MG_TRUE;
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/* myMPD
|
|
||||||
(c) 2018 Juergen Mang <mail@jcgames.de>
|
|
||||||
This project's homepage is: https://github.com/jcorporation/ympd
|
|
||||||
|
|
||||||
myMPD ist fork of:
|
|
||||||
|
|
||||||
ympd
|
|
||||||
(c) 2013-2014 Andrew Karpow <andy@ndyk.de>
|
|
||||||
This project's homepage is: http://www.ympd.org
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation; version 2 of the License.
|
|
||||||
|
|
||||||
This program 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 General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License along
|
|
||||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __HTTP_SERVER_H__
|
|
||||||
#define __HTTP_SERVER_H__
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
struct embedded_file {
|
|
||||||
const char *name;
|
|
||||||
const unsigned char *data;
|
|
||||||
const char *mimetype;
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
const struct embedded_file *find_embedded_file(const char *name);
|
|
||||||
int callback_http(struct mg_connection *c);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
5406
src/mongoose.c
5406
src/mongoose.c
File diff suppressed because it is too large
Load Diff
152
src/mongoose.h
152
src/mongoose.h
@ -1,152 +0,0 @@
|
|||||||
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
|
|
||||||
// Copyright (c) 2013-2014 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
//
|
|
||||||
// This software is dual-licensed: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License version 2 as
|
|
||||||
// published by the Free Software Foundation. For the terms of this
|
|
||||||
// license, see <http://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
// You are free to use this software under the terms of the GNU General
|
|
||||||
// Public License, but WITHOUT ANY WARRANTY; without even the implied
|
|
||||||
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// Alternatively, you can license this software under a commercial
|
|
||||||
// license, as set out in <http://cesanta.com/>.
|
|
||||||
|
|
||||||
#ifndef MONGOOSE_HEADER_INCLUDED
|
|
||||||
#define MONGOOSE_HEADER_INCLUDED
|
|
||||||
|
|
||||||
#define MONGOOSE_VERSION "5.6"
|
|
||||||
|
|
||||||
#include <stdio.h> // required for FILE
|
|
||||||
#include <stddef.h> // required for size_t
|
|
||||||
#include <sys/types.h> // required for time_t
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif // __cplusplus
|
|
||||||
|
|
||||||
// This structure contains information about HTTP request.
|
|
||||||
struct mg_connection {
|
|
||||||
const char *request_method; // "GET", "POST", etc
|
|
||||||
const char *uri; // URL-decoded URI
|
|
||||||
const char *http_version; // E.g. "1.0", "1.1"
|
|
||||||
const char *query_string; // URL part after '?', not including '?', or NULL
|
|
||||||
|
|
||||||
char remote_ip[48]; // Max IPv6 string length is 45 characters
|
|
||||||
char local_ip[48]; // Local IP address
|
|
||||||
unsigned short remote_port; // Client's port
|
|
||||||
unsigned short local_port; // Local port number
|
|
||||||
|
|
||||||
int num_headers; // Number of HTTP headers
|
|
||||||
struct mg_header {
|
|
||||||
const char *name; // HTTP header name
|
|
||||||
const char *value; // HTTP header value
|
|
||||||
} http_headers[30];
|
|
||||||
|
|
||||||
char *content; // POST (or websocket message) data, or NULL
|
|
||||||
size_t content_len; // Data length
|
|
||||||
|
|
||||||
int is_websocket; // Connection is a websocket connection
|
|
||||||
int status_code; // HTTP status code for HTTP error handler
|
|
||||||
int wsbits; // First byte of the websocket frame
|
|
||||||
void *server_param; // Parameter passed to mg_create_server()
|
|
||||||
void *connection_param; // Placeholder for connection-specific data
|
|
||||||
void *callback_param;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct mg_server; // Opaque structure describing server instance
|
|
||||||
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_RECV, // Mongoose has received POST data chunk.
|
|
||||||
// Callback should return a number of bytes to discard from
|
|
||||||
// the receive buffer, or -1 to close the 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
|
|
||||||
struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
|
|
||||||
void mg_destroy_server(struct mg_server **);
|
|
||||||
const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
|
|
||||||
time_t mg_poll_server(struct mg_server *, int milliseconds);
|
|
||||||
const char **mg_get_valid_option_names(void);
|
|
||||||
const char *mg_get_option(const struct mg_server *server, const char *name);
|
|
||||||
void mg_copy_listeners(struct mg_server *from, struct mg_server *to);
|
|
||||||
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 *);
|
|
||||||
|
|
||||||
// Connection management functions
|
|
||||||
void mg_send_status(struct mg_connection *, int status_code);
|
|
||||||
void mg_send_header(struct mg_connection *, const char *name, const char *val);
|
|
||||||
size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
|
|
||||||
size_t mg_printf_data(struct mg_connection *, const char *format, ...);
|
|
||||||
size_t mg_write(struct mg_connection *, const void *buf, size_t len);
|
|
||||||
size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
|
|
||||||
|
|
||||||
size_t mg_websocket_write(struct mg_connection *, int opcode,
|
|
||||||
const char *data, size_t data_len);
|
|
||||||
size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
|
|
||||||
const char *fmt, ...);
|
|
||||||
|
|
||||||
void mg_send_file(struct mg_connection *, const char *path, const char *);
|
|
||||||
void mg_send_file_data(struct mg_connection *, int fd);
|
|
||||||
|
|
||||||
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_get_var(const struct mg_connection *conn, const char *var_name,
|
|
||||||
char *buf, size_t buf_len);
|
|
||||||
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
|
|
||||||
int mg_parse_multipart(const char *buf, int buf_len,
|
|
||||||
char *var_name, int var_name_len,
|
|
||||||
char *file_name, int file_name_len,
|
|
||||||
const char **data, int *data_len);
|
|
||||||
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
size_t mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
|
|
||||||
int mg_url_decode(const char *src, size_t src_len, char *dst, size_t dst_len, int);
|
|
||||||
int mg_terminate_ssl(struct mg_connection *c, const char *cert);
|
|
||||||
int mg_forward(struct mg_connection *c, const char *addr);
|
|
||||||
void *mg_mmap(FILE *fp, size_t size);
|
|
||||||
void mg_munmap(void *p, size_t size);
|
|
||||||
|
|
||||||
|
|
||||||
// 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
|
|
@ -25,7 +25,7 @@
|
|||||||
#ifndef __MPD_CLIENT_H__
|
#ifndef __MPD_CLIENT_H__
|
||||||
#define __MPD_CLIENT_H__
|
#define __MPD_CLIENT_H__
|
||||||
|
|
||||||
#include "mongoose.h"
|
#include "mongoose/mongoose.h"
|
||||||
|
|
||||||
#define RETURN_ERROR_AND_RECOVER(X) do { \
|
#define RETURN_ERROR_AND_RECOVER(X) do { \
|
||||||
fprintf(stderr, "MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \
|
fprintf(stderr, "MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \
|
||||||
|
39
src/mympd.c
39
src/mympd.c
@ -30,8 +30,8 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#include "mongoose.h"
|
#include "mongoose/mongoose.h"
|
||||||
#include "http_server.h"
|
//#include "http_server.h"
|
||||||
#include "mpd_client.h"
|
#include "mpd_client.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
@ -44,36 +44,39 @@ void bye()
|
|||||||
force_exit = 1;
|
force_exit = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int server_callback(struct mg_connection *c, enum mg_event ev) {
|
|
||||||
int result = MG_FALSE;
|
static int is_websocket(const struct mg_connection *nc) {
|
||||||
|
return nc->flags & MG_F_IS_WEBSOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void server_callback(struct mg_connection *c, int ev, void *p) {
|
||||||
|
// int result = MG_EV_FALSE;
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
|
|
||||||
switch(ev) {
|
switch(ev) {
|
||||||
case MG_CLOSE:
|
case MG_EV_CLOSE:
|
||||||
mpd_close_handler(c);
|
mpd_close_handler(c);
|
||||||
return MG_TRUE;
|
break;
|
||||||
case MG_REQUEST:
|
// return MG_TRUE;
|
||||||
if (c->is_websocket) {
|
case MG_EV_HTTP_REQUEST:
|
||||||
|
if (is_websocket(c)) {
|
||||||
c->content[c->content_len] = '\0';
|
c->content[c->content_len] = '\0';
|
||||||
if(c->content_len)
|
if(c->content_len)
|
||||||
return callback_mpd(c);
|
callback_mpd(c);
|
||||||
else
|
break;
|
||||||
return MG_TRUE;
|
|
||||||
} else
|
} else
|
||||||
return MG_FALSE;
|
break;
|
||||||
case MG_AUTH:
|
case MG_EV_AUTH:
|
||||||
// no auth for websockets since mobile safari does not support it
|
// no auth for websockets since mobile safari does not support it
|
||||||
if ( (mpd.gpass == NULL) || (c->is_websocket) || ((mpd.local_port > 0) && (c->local_port == mpd.local_port)) )
|
if ( (mpd.gpass == NULL) || (c->is_websocket) || ((mpd.local_port > 0) && (c->local_port == mpd.local_port)) )
|
||||||
return MG_TRUE;
|
break;
|
||||||
else {
|
else {
|
||||||
if ( (fp = fopen(mpd.gpass, "r")) != NULL ) {
|
if ( (fp = fopen(mpd.gpass, "r")) != NULL ) {
|
||||||
result = mg_authorize_digest(c, fp);
|
mg_authorize_digest(c, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
break;
|
||||||
default:
|
|
||||||
return MG_FALSE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user