From ed705ba7042c7c2099d702476e29621cf8f168e2 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Tue, 13 Nov 2018 10:33:24 +0300 Subject: [PATCH] s/http-server: add options (for persistent port) --- sample/http-server.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/sample/http-server.c b/sample/http-server.c index 579feea6..0582b29b 100644 --- a/sample/http-server.c +++ b/sample/http-server.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #ifndef S_ISDIR @@ -86,6 +87,11 @@ static const struct table_entry { { NULL, NULL }, }; +struct options +{ + int port; +}; + /* Try to guess a good content-type for 'path' */ static const char * guess_content_type(const char *path) @@ -320,10 +326,27 @@ done: evbuffer_free(evb); } -static void -syntax(void) +static struct options +parse_opts(int argc, char **argv) { - fprintf(stdout, "Syntax: http-server \n"); + struct options o; + int opt; + + memset(&o, 0, sizeof(o)); + + while ((opt = getopt(argc, argv, "p:")) != -1) { + switch (opt) { + case 'p': o.port = atoi(optarg); break; + default : fprintf(stderr, "Unknown option %c\n", opt); break; + } + } + + if (optind >= argc || (argc-optind) > 1) { + fprintf(stdout, "Syntax: %s \n", argv[0]); + exit(1); + } + + return o; } int @@ -332,8 +355,8 @@ main(int argc, char **argv) struct event_base *base; struct evhttp *http; struct evhttp_bound_socket *handle; + struct options o = parse_opts(argc, argv); - ev_uint16_t port = 0; #ifdef _WIN32 WSADATA WSAData; WSAStartup(0x101, &WSAData); @@ -341,10 +364,6 @@ main(int argc, char **argv) if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return (1); #endif - if (argc < 2) { - syntax(); - return 1; - } base = event_base_new(); if (!base) { @@ -367,10 +386,9 @@ main(int argc, char **argv) evhttp_set_gencb(http, send_document_cb, argv[1]); /* Now we tell the evhttp what port to listen on */ - handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", port); + handle = evhttp_bind_socket_with_handle(http, "0.0.0.0", o.port); if (!handle) { - fprintf(stderr, "couldn't bind to port %d. Exiting.\n", - (int)port); + fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port); return 1; }