mirror of
				https://github.com/SuperBFG7/ympd
				synced 2025-10-30 13:23:00 +00:00 
			
		
		
		
	
							
								
								
									
										1521
									
								
								src/mongoose.c
									
									
									
									
									
								
							
							
						
						
									
										1521
									
								
								src/mongoose.c
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										299
									
								
								src/mongoose.h
									
									
									
									
									
								
							
							
						
						
									
										299
									
								
								src/mongoose.h
									
									
									
									
									
								
							| @@ -1,147 +1,152 @@ | |||||||
| // 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 software 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 software 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 software 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 | #ifndef MONGOOSE_HEADER_INCLUDED | ||||||
|  | #define  MONGOOSE_HEADER_INCLUDED | ||||||
| #ifndef MONGOOSE_HEADER_INCLUDED |  | ||||||
| #define  MONGOOSE_HEADER_INCLUDED | #define MONGOOSE_VERSION "5.6" | ||||||
|  |  | ||||||
| #define MONGOOSE_VERSION "5.4" | #include <stdio.h>      // required for FILE | ||||||
|  | #include <stddef.h>     // required for size_t | ||||||
| #include <stdio.h>      // required for FILE | #include <sys/types.h>  // required for time_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 |   char local_ip[48];          // 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;         // Data 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_create_server() | ||||||
|   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; | ||||||
|   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 | enum mg_result { MG_FALSE, MG_TRUE, MG_MORE }; | ||||||
| enum mg_result { MG_FALSE, MG_TRUE, MG_MORE }; | enum mg_event { | ||||||
| enum mg_event { |   MG_POLL = 100,  // Callback return value is ignored | ||||||
|   MG_POLL = 100,  // Callback return value is ignored |   MG_CONNECT,     // If callback returns MG_FALSE, connect fails | ||||||
|   MG_CONNECT,     // If callback returns MG_FALSE, connect fails |   MG_AUTH,        // If callback returns MG_FALSE, authentication fails | ||||||
|   MG_AUTH,        // If callback returns MG_FALSE, authentication fails |   MG_REQUEST,     // If callback returns MG_FALSE, Mongoose continues with req | ||||||
|   MG_REQUEST,     // If callback returns MG_FALSE, Mongoose continues with req |   MG_REPLY,       // If callback returns MG_FALSE, Mongoose closes connection | ||||||
|   MG_REPLY,       // If callback returns MG_FALSE, Mongoose closes connection |   MG_RECV,        // Mongoose has received POST data chunk. | ||||||
|   MG_CLOSE,       // Connection is closed, callback return value is ignored |                   // Callback should return a number of bytes to discard from | ||||||
|   MG_WS_HANDSHAKE,  // New websocket connection, handshake request |                   // the receive buffer, or -1 to close the connection. | ||||||
|   MG_WS_CONNECT,  // New websocket connection established |   MG_CLOSE,       // Connection is closed, callback return value is ignored | ||||||
|   MG_HTTP_ERROR   // If callback returns MG_FALSE, Mongoose continues with err |   MG_WS_HANDSHAKE,  // New websocket connection, handshake request | ||||||
| }; |   MG_WS_CONNECT,  // New websocket connection established | ||||||
| typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event); |   MG_HTTP_ERROR   // If callback returns MG_FALSE, Mongoose continues with err | ||||||
|  | }; | ||||||
| // Websocket opcodes, from http://tools.ietf.org/html/rfc6455 | typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event); | ||||||
| enum { |  | ||||||
|   WEBSOCKET_OPCODE_CONTINUATION = 0x0, | // Websocket opcodes, from http://tools.ietf.org/html/rfc6455 | ||||||
|   WEBSOCKET_OPCODE_TEXT = 0x1, | enum { | ||||||
|   WEBSOCKET_OPCODE_BINARY = 0x2, |   WEBSOCKET_OPCODE_CONTINUATION = 0x0, | ||||||
|   WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8, |   WEBSOCKET_OPCODE_TEXT = 0x1, | ||||||
|   WEBSOCKET_OPCODE_PING = 0x9, |   WEBSOCKET_OPCODE_BINARY = 0x2, | ||||||
|   WEBSOCKET_OPCODE_PONG = 0xa |   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 **); | // Server management functions | ||||||
| const char *mg_set_option(struct mg_server *, const char *opt, const char *val); | struct mg_server *mg_create_server(void *server_param, mg_handler_t handler); | ||||||
| int mg_poll_server(struct mg_server *, int milliseconds); | void mg_destroy_server(struct mg_server **); | ||||||
| const char **mg_get_valid_option_names(void); | const char *mg_set_option(struct mg_server *, const char *opt, const char *val); | ||||||
| const char *mg_get_option(const struct mg_server *server, const char *name); | time_t mg_poll_server(struct mg_server *, int milliseconds); | ||||||
| void mg_set_listening_socket(struct mg_server *, int sock); | const char **mg_get_valid_option_names(void); | ||||||
| int mg_get_listening_socket(struct mg_server *); | const char *mg_get_option(const struct mg_server *server, const char *name); | ||||||
| void mg_iterate_over_connections(struct mg_server *, mg_handler_t, void *); | void mg_copy_listeners(struct mg_server *from, struct mg_server *to); | ||||||
| struct mg_connection *mg_next(struct mg_server *, struct mg_connection *); | struct mg_connection *mg_next(struct mg_server *, struct mg_connection *); | ||||||
| void mg_wakeup_server(struct mg_server *); | void mg_wakeup_server(struct mg_server *); | ||||||
| void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...); | 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); | struct mg_connection *mg_connect(struct mg_server *, const char *); | ||||||
|  |  | ||||||
| // 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); | ||||||
| size_t 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); | ||||||
| size_t 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_write(struct mg_connection *, const void *buf, size_t len); | ||||||
| size_t mg_printf(struct mg_connection *conn, const char *fmt, ...); | size_t mg_printf(struct mg_connection *conn, const char *fmt, ...); | ||||||
|  |  | ||||||
| size_t 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, | size_t mg_websocket_printf(struct mg_connection* conn, int opcode, | ||||||
|                            const char *fmt, ...); |                            const char *fmt, ...); | ||||||
|  |  | ||||||
| void mg_send_file(struct mg_connection *, const char *path); | 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); | const char *mg_get_header(const struct mg_connection *, const char *name); | ||||||
| int mg_get_var(const struct mg_connection *conn, const char *var_name, | const char *mg_get_mime_type(const char *name, const char *default_mime_type); | ||||||
|                char *buf, size_t buf_len); | int mg_get_var(const struct mg_connection *conn, const char *var_name, | ||||||
| int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); |                char *buf, size_t buf_len); | ||||||
| int mg_parse_multipart(const char *buf, int buf_len, | int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t); | ||||||
|                        char *var_name, int var_name_len, | int mg_parse_multipart(const char *buf, int buf_len, | ||||||
|                        char *file_name, int file_name_len, |                        char *var_name, int var_name_len, | ||||||
|                        const char **data, int *data_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], ...); | // Utility functions | ||||||
| int mg_authorize_digest(struct mg_connection *c, FILE *fp); | void *mg_start_thread(void *(*func)(void *), void *param); | ||||||
| int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len); | char *mg_md5(char buf[33], ...); | ||||||
| int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int); | int mg_authorize_digest(struct mg_connection *c, FILE *fp); | ||||||
| int mg_terminate_ssl(struct mg_connection *c, const char *cert); | 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); | ||||||
| // Templates support | int mg_terminate_ssl(struct mg_connection *c, const char *cert); | ||||||
| struct mg_expansion { | int mg_forward(struct mg_connection *c, const char *addr); | ||||||
|   const char *keyword; | void *mg_mmap(FILE *fp, size_t size); | ||||||
|   void (*handler)(struct mg_connection *); | void mg_munmap(void *p, size_t size); | ||||||
| }; |  | ||||||
| void mg_template(struct mg_connection *, const char *text, |  | ||||||
|                  struct mg_expansion *expansions); | // Templates support | ||||||
|  | struct mg_expansion { | ||||||
|  |   const char *keyword; | ||||||
| #ifdef __cplusplus |   void (*handler)(struct mg_connection *); | ||||||
| } | }; | ||||||
| #endif // __cplusplus | void mg_template(struct mg_connection *, const char *text, | ||||||
|  |                  struct mg_expansion *expansions); | ||||||
| #endif // MONGOOSE_HEADER_INCLUDED |  | ||||||
|  | #ifdef __cplusplus | ||||||
|  | } | ||||||
|  | #endif // __cplusplus | ||||||
|  |  | ||||||
|  | #endif // MONGOOSE_HEADER_INCLUDED | ||||||
|   | |||||||
| @@ -274,8 +274,11 @@ void mpd_poll(struct mg_server *s) | |||||||
|  |  | ||||||
|             if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) { |             if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) { | ||||||
|                 fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn)); |                 fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn)); | ||||||
|                 mg_iterate_over_connections(s, mpd_notify_callback,  |                 for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) | ||||||
|                     (void *)mpd_connection_get_error_message(mpd.conn)); |                 { | ||||||
|  |                     c->callback_param = (void *)mpd_connection_get_error_message(mpd.conn); | ||||||
|  |                     mpd_notify_callback(c, MG_POLL); | ||||||
|  |                 } | ||||||
|                 mpd.conn_state = MPD_FAILURE; |                 mpd.conn_state = MPD_FAILURE; | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
| @@ -283,8 +286,11 @@ void mpd_poll(struct mg_server *s) | |||||||
|             if(mpd.password && !mpd_run_password(mpd.conn, mpd.password)) |             if(mpd.password && !mpd_run_password(mpd.conn, mpd.password)) | ||||||
|             { |             { | ||||||
|                 fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn)); |                 fprintf(stderr, "MPD connection: %s\n", mpd_connection_get_error_message(mpd.conn)); | ||||||
|                 mg_iterate_over_connections(s, mpd_notify_callback,  |                 for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) | ||||||
|                     (void *)mpd_connection_get_error_message(mpd.conn)); |                 { | ||||||
|  |                     c->callback_param = (void *)mpd_connection_get_error_message(mpd.conn); | ||||||
|  |                     mpd_notify_callback(c, MG_POLL); | ||||||
|  |                 } | ||||||
|                 mpd.conn_state = MPD_FAILURE; |                 mpd.conn_state = MPD_FAILURE; | ||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
| @@ -307,7 +313,11 @@ void mpd_poll(struct mg_server *s) | |||||||
|  |  | ||||||
|         case MPD_CONNECTED: |         case MPD_CONNECTED: | ||||||
|             mpd.buf_size = mpd_put_state(mpd.buf, &mpd.song_id, &mpd.queue_version); |             mpd.buf_size = mpd_put_state(mpd.buf, &mpd.song_id, &mpd.queue_version); | ||||||
|             mg_iterate_over_connections(s, mpd_notify_callback, NULL); |             for (struct mg_connection *c = mg_next(s, NULL); c != NULL; c = mg_next(s, c)) | ||||||
|  |             { | ||||||
|  |                 c->callback_param = NULL; | ||||||
|  |                 mpd_notify_callback(c, MG_POLL); | ||||||
|  |             } | ||||||
|             break; |             break; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Andy
					Andy