1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-07 16:02:55 +03:00

Add new interface to get listening ports (deprecate old interface)

This commit is contained in:
bel
2015-09-26 22:34:55 +02:00
parent b806e83762
commit ac3e0d8292
3 changed files with 74 additions and 9 deletions

View File

@@ -250,6 +250,8 @@ int main(int argc, char *argv[])
}; };
struct mg_callbacks callbacks; struct mg_callbacks callbacks;
struct mg_context *ctx; struct mg_context *ctx;
struct mg_server_ports ports[32];
size_t port_cnt, n;
memset(&callbacks, 0, sizeof(callbacks)); memset(&callbacks, 0, sizeof(callbacks));
ctx = mg_start(&callbacks, 0, options); ctx = mg_start(&callbacks, 0, options);
@@ -278,9 +280,17 @@ int main(int argc, char *argv[])
mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0); mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0);
#endif #endif
printf("Browse files at http://localhost:%s/\n", PORT); memset(ports, 0, sizeof(ports));
printf("Run example at http://localhost:%s%s\n", PORT, EXAMPLE_URI); port_cnt = mg_get_server_ports(ctx, 32, ports);
printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
for (n=0; n<port_cnt && n<32; n++) {
const char *proto = ports[n].is_ssl ? "https" : "http";
const char *host = ports[n].protocol==2 ? "[::1]" : "127.0.0.1";
printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port);
printf("Run example at %s://%s:%i%s\n", proto, host, ports[n].port, EXAMPLE_URI);
printf("Exit at %s://%s:%i%s\n", proto, host, ports[n].port, EXIT_URI);
printf("\n");
}
while (!exitNow) { while (!exitNow) {
#ifdef _WIN32 #ifdef _WIN32

View File

@@ -386,16 +386,33 @@ enum {
The array is terminated by a NULL name option. */ The array is terminated by a NULL name option. */
CIVETWEB_API const struct mg_option *mg_get_valid_options(void); CIVETWEB_API const struct mg_option *mg_get_valid_options(void);
struct mg_server_ports {
int protocol; /* 1 = IPv4, 2 = IPv6 */
int port; /* port number */
int is_ssl; /* https port: 0 = no, 1 = yes */
int is_redirect; /* redirect all requests: 0 = no, 1 = yes */
int _reserved1;
int _reserved2;
int _reserved3;
int _reserved4;
};
/* Get the list of ports that civetweb is listening on. /* Get the list of ports that civetweb is listening on.
size is the size of the ports int array and ssl int array to fill. The parameter size is the size of the ports array in elements.
It is the caller's responsibility to make sure ports and ssl each The caller is responsibility to allocate the required memory.
contain at least size int elements worth of memory to write into. This function returns the number of struct mg_server_ports elements
Return value is the number of ports and ssl information filled in. filled in, or <0 in case of an error. */
The value returned is read-only. Civetweb does not allow changing CIVETWEB_API int mg_get_server_ports(const struct mg_context *ctx,
configuration at run time. */ int size,
struct mg_server_ports *ports);
/* Deprecated. Use mg_get_server_ports instead. */
CIVETWEB_API size_t CIVETWEB_API size_t
mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl); mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl);
/* Add, edit or delete the entry in the passwords file. /* Add, edit or delete the entry in the passwords file.
This function allows an application to manipulate .htpasswd files on the This function allows an application to manipulate .htpasswd files on the

View File

@@ -1578,6 +1578,44 @@ mg_get_ports(const struct mg_context *ctx, size_t size, int *ports, int *ssl)
} }
int mg_get_server_ports(const struct mg_context *ctx,
int size,
struct mg_server_ports *ports)
{
int i, cnt = 0;
if (size <= 0) {
return -1;
}
memset(ports, 0, sizeof(*ports) * size);
if (!ctx) {
return -1;
}
if (!ctx->listening_sockets || !ctx->listening_ports) {
return -1;
}
for (i = 0; (i < size) && (i < (int)ctx->num_listening_sockets); i++) {
ports[cnt].port = ctx->listening_ports[i];
ports[cnt].is_ssl = ctx->listening_sockets[i].is_ssl;
ports[cnt].is_redirect = ctx->listening_sockets[i].ssl_redir;
if (ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET) {
/* IPv4 */
ports[cnt].protocol = 1;
cnt++;
} else if (ctx->listening_sockets[i].lsa.sa.sa_family == AF_INET6) {
/* IPv6 */
ports[cnt].protocol = 2;
cnt++;
}
}
return cnt;
}
static void sockaddr_to_string(char *buf, size_t len, const union usa *usa) static void sockaddr_to_string(char *buf, size_t len, const union usa *usa)
{ {
buf[0] = '\0'; buf[0] = '\0';