| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "sd-dlopen.h" |
| 5 |
|
| 6 |
#include "shared-forward.h" |
| 7 |
|
| 8 |
int dlopen_microhttpd(int log_level); |
| 9 |
|
| 10 |
#if HAVE_MICROHTTPD |
| 11 |
#define MICROHTTPD_NOTE(priority) \ |
| 12 |
SD_ELF_NOTE_DLOPEN("microhttpd", \ |
| 13 |
"Support for embedded HTTP server via libmicrohttpd", \ |
| 14 |
priority, \ |
| 15 |
"libmicrohttpd.so.12") |
| 16 |
|
| 17 |
#define DLOPEN_MICROHTTPD(log_level, priority) \ |
| 18 |
({ \ |
| 19 |
MICROHTTPD_NOTE(priority); \ |
| 20 |
dlopen_microhttpd(log_level); \ |
| 21 |
}) |
| 22 |
|
| 23 |
#include <microhttpd.h> |
| 24 |
|
| 25 |
#include "dlfcn-util.h" |
| 26 |
|
| 27 |
/* Those defines are added when options are renamed. If the old names |
| 28 |
* are not '#define'd, then they are not deprecated yet and there are |
| 29 |
* enum elements with the same name. Hence let's check for the *old* name, |
| 30 |
* and define the new name by the value of the old name. */ |
| 31 |
|
| 32 |
/* Renamed in μhttpd 0.9.51 */ |
| 33 |
#ifndef MHD_USE_PIPE_FOR_SHUTDOWN |
| 34 |
# define MHD_USE_ITC MHD_USE_PIPE_FOR_SHUTDOWN |
| 35 |
#endif |
| 36 |
|
| 37 |
/* Renamed in μhttpd 0.9.52 */ |
| 38 |
#ifndef MHD_USE_EPOLL_LINUX_ONLY |
| 39 |
# define MHD_USE_EPOLL MHD_USE_EPOLL_LINUX_ONLY |
| 40 |
#endif |
| 41 |
|
| 42 |
/* Renamed in μhttpd 0.9.52 */ |
| 43 |
#ifndef MHD_USE_SSL |
| 44 |
# define MHD_USE_TLS MHD_USE_SSL |
| 45 |
#endif |
| 46 |
|
| 47 |
/* Renamed in μhttpd 0.9.53 */ |
| 48 |
#ifndef MHD_USE_POLL_INTERNALLY |
| 49 |
# define MHD_USE_POLL_INTERNAL_THREAD MHD_USE_POLL_INTERNALLY |
| 50 |
#endif |
| 51 |
|
| 52 |
/* Both the old and new names are defines, check for the new one. */ |
| 53 |
|
| 54 |
/* Compatibility with libmicrohttpd < 0.9.38 */ |
| 55 |
#ifndef MHD_HTTP_NOT_ACCEPTABLE |
| 56 |
# define MHD_HTTP_NOT_ACCEPTABLE MHD_HTTP_METHOD_NOT_ACCEPTABLE |
| 57 |
#endif |
| 58 |
|
| 59 |
/* Renamed in μhttpd 0.9.74 (8c644fc1f4d498ea489add8d40a68f5d3e5899fa) */ |
| 60 |
#ifndef MHD_HTTP_CONTENT_TOO_LARGE |
| 61 |
# ifdef MHD_HTTP_PAYLOAD_TOO_LARGE |
| 62 |
# define MHD_HTTP_CONTENT_TOO_LARGE MHD_HTTP_PAYLOAD_TOO_LARGE /* 0.9.53 or newer */ |
| 63 |
# else |
| 64 |
# define MHD_HTTP_CONTENT_TOO_LARGE MHD_HTTP_REQUEST_ENTITY_TOO_LARGE |
| 65 |
# endif |
| 66 |
#endif |
| 67 |
|
| 68 |
#if MHD_VERSION < 0x00094203 |
| 69 |
# define MHD_create_response_from_fd_at_offset64 MHD_create_response_from_fd_at_offset |
| 70 |
#endif |
| 71 |
|
| 72 |
#if MHD_VERSION >= 0x00097002 |
| 73 |
# define mhd_result enum MHD_Result |
| 74 |
#else |
| 75 |
# define mhd_result int |
| 76 |
#endif |
| 77 |
|
| 78 |
extern DLSYM_PROTOTYPE(MHD_add_response_header); |
| 79 |
extern DLSYM_PROTOTYPE(MHD_create_response_from_buffer); |
| 80 |
extern DLSYM_PROTOTYPE(MHD_create_response_from_callback); |
| 81 |
#if MHD_VERSION < 0x00094203 |
| 82 |
extern DLSYM_PROTOTYPE(MHD_create_response_from_fd_at_offset); |
| 83 |
# define sym_MHD_create_response_from_fd_at_offset64 sym_MHD_create_response_from_fd_at_offset |
| 84 |
#else |
| 85 |
extern DLSYM_PROTOTYPE(MHD_create_response_from_fd_at_offset64); |
| 86 |
#endif |
| 87 |
extern DLSYM_PROTOTYPE(MHD_destroy_response); |
| 88 |
extern DLSYM_PROTOTYPE(MHD_get_connection_info); |
| 89 |
extern DLSYM_PROTOTYPE(MHD_get_connection_values); |
| 90 |
extern DLSYM_PROTOTYPE(MHD_get_daemon_info); |
| 91 |
extern DLSYM_PROTOTYPE(MHD_get_timeout); |
| 92 |
extern DLSYM_PROTOTYPE(MHD_lookup_connection_value); |
| 93 |
extern DLSYM_PROTOTYPE(MHD_queue_response); |
| 94 |
extern DLSYM_PROTOTYPE(MHD_run); |
| 95 |
extern DLSYM_PROTOTYPE(MHD_start_daemon); |
| 96 |
extern DLSYM_PROTOTYPE(MHD_stop_daemon); |
| 97 |
|
| 98 |
void microhttpd_logger(void *arg, const char *fmt, va_list ap) _printf_(2, 0); |
| 99 |
|
| 100 |
/* respond_oom() must be usable with return, hence this form. */ |
| 101 |
#define respond_oom(connection) log_oom(), mhd_respond_oom(connection) |
| 102 |
|
| 103 |
int mhd_respond_internal( |
| 104 |
struct MHD_Connection *connection, |
| 105 |
enum MHD_RequestTerminationCode code, |
| 106 |
const char *encoding, |
| 107 |
const char *buffer, |
| 108 |
size_t size, |
| 109 |
enum MHD_ResponseMemoryMode mode); |
| 110 |
|
| 111 |
#define mhd_respond_with_encoding(connection, code, encoding, message) \ |
| 112 |
mhd_respond_internal( \ |
| 113 |
(connection), (code), (encoding), \ |
| 114 |
message "\n", \ |
| 115 |
strlen(message) + 1, \ |
| 116 |
MHD_RESPMEM_PERSISTENT) |
| 117 |
|
| 118 |
#define mhd_respond(connection, code, message) \ |
| 119 |
mhd_respond_with_encoding(connection, code, NULL, message) \ |
| 120 |
|
| 121 |
int mhd_respond_oom(struct MHD_Connection *connection); |
| 122 |
|
| 123 |
int mhd_respondf_internal( |
| 124 |
struct MHD_Connection *connection, |
| 125 |
int error, |
| 126 |
enum MHD_RequestTerminationCode code, |
| 127 |
const char *encoding, |
| 128 |
const char *format, ...) _printf_(5,6); |
| 129 |
|
| 130 |
#define mhd_respondf(connection, error, code, format, ...) \ |
| 131 |
mhd_respondf_internal( \ |
| 132 |
connection, error, code, NULL, \ |
| 133 |
format "\n", \ |
| 134 |
##__VA_ARGS__) |
| 135 |
|
| 136 |
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname); |
| 137 |
|
| 138 |
/* Set gnutls internal logging function to a callback which uses our |
| 139 |
* own logging framework. |
| 140 |
* |
| 141 |
* gnutls categories are additionally filtered by our internal log |
| 142 |
* level, so it should be set fairly high to capture all potentially |
| 143 |
* interesting events without overwhelming detail. |
| 144 |
*/ |
| 145 |
int setup_gnutls_logger(char **categories); |
| 146 |
|
| 147 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(struct MHD_Daemon*, sym_MHD_stop_daemon, MHD_stop_daemonp, NULL); |
| 148 |
DEFINE_TRIVIAL_CLEANUP_FUNC_FULL_RENAME(struct MHD_Response*, sym_MHD_destroy_response, MHD_destroy_responsep, NULL); |
| 149 |
|
| 150 |
#else |
| 151 |
#define DLOPEN_MICROHTTPD(log_level, priority) dlopen_microhttpd(log_level) |
| 152 |
#endif |