From eb822ac751dd2559b9ea3a04ec4978fe2733f6ce Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 27 Oct 2020 01:22:59 +0300 Subject: [PATCH] http-server: add -H option to change listen address --- sample/http-server.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sample/http-server.c b/sample/http-server.c index cf928437..b006891e 100644 --- a/sample/http-server.c +++ b/sample/http-server.c @@ -107,6 +107,7 @@ struct options { int unlink; const char *unixsock; + const char *bind; const char *docroot; }; @@ -351,6 +352,7 @@ print_usage(FILE *out, const char *prog, int exit_code) "Syntax: %s [ OPTS ] \n" " -p - port\n" " -U - bind to unix socket\n" + " -H - address to bind (default: 0.0.0.0)\n" " -u - unlink unix socket before bind\n" " -I - IOCP\n" " -m - max body size\n" @@ -365,7 +367,7 @@ parse_opts(int argc, char **argv) memset(&o, 0, sizeof(o)); - while ((opt = getopt(argc, argv, "hp:U:m:uIv")) != -1) { + while ((opt = getopt(argc, argv, "hp:U:m:uIvH:")) != -1) { switch (opt) { case 'p': o.port = atoi(optarg); break; case 'U': o.unixsock = optarg; break; @@ -373,6 +375,7 @@ parse_opts(int argc, char **argv) case 'I': o.iocp = 1; break; case 'm': o.max_body_size = atoi(optarg); break; case 'v': ++o.verbose; break; + case 'H': o.bind = optarg; break; case 'h': print_usage(stdout, argv[0], 0); break; default : fprintf(stderr, "Unknown option %c\n", opt); break; } @@ -548,9 +551,9 @@ main(int argc, char **argv) #endif /* EVENT__HAVE_STRUCT_SOCKADDR_UN */ } else { - handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port); + handle = evhttp_bind_socket_with_handle(http, o.bind, o.port); if (!handle) { - fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port); + fprintf(stderr, "couldn't bind to %s:%d. Exiting.\n", o.bind, o.port); ret = 1; goto err; } -- 2.40.0