mirror of
https://github.com/libevent/libevent.git
synced 2025-01-31 09:12:55 +08:00
s/http-server: add options (for persistent port)
This commit is contained in:
parent
8c1838beec
commit
ed705ba704
@ -20,6 +20,7 @@
|
|||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
#include <getopt.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#ifndef S_ISDIR
|
#ifndef S_ISDIR
|
||||||
@ -86,6 +87,11 @@ static const struct table_entry {
|
|||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct options
|
||||||
|
{
|
||||||
|
int port;
|
||||||
|
};
|
||||||
|
|
||||||
/* Try to guess a good content-type for 'path' */
|
/* Try to guess a good content-type for 'path' */
|
||||||
static const char *
|
static const char *
|
||||||
guess_content_type(const char *path)
|
guess_content_type(const char *path)
|
||||||
@ -320,10 +326,27 @@ done:
|
|||||||
evbuffer_free(evb);
|
evbuffer_free(evb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static struct options
|
||||||
syntax(void)
|
parse_opts(int argc, char **argv)
|
||||||
{
|
{
|
||||||
fprintf(stdout, "Syntax: http-server <docroot>\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 <docroot>\n", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -332,8 +355,8 @@ main(int argc, char **argv)
|
|||||||
struct event_base *base;
|
struct event_base *base;
|
||||||
struct evhttp *http;
|
struct evhttp *http;
|
||||||
struct evhttp_bound_socket *handle;
|
struct evhttp_bound_socket *handle;
|
||||||
|
struct options o = parse_opts(argc, argv);
|
||||||
|
|
||||||
ev_uint16_t port = 0;
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
WSADATA WSAData;
|
WSADATA WSAData;
|
||||||
WSAStartup(0x101, &WSAData);
|
WSAStartup(0x101, &WSAData);
|
||||||
@ -341,10 +364,6 @@ main(int argc, char **argv)
|
|||||||
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
|
||||||
return (1);
|
return (1);
|
||||||
#endif
|
#endif
|
||||||
if (argc < 2) {
|
|
||||||
syntax();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
base = event_base_new();
|
base = event_base_new();
|
||||||
if (!base) {
|
if (!base) {
|
||||||
@ -367,10 +386,9 @@ main(int argc, char **argv)
|
|||||||
evhttp_set_gencb(http, send_document_cb, argv[1]);
|
evhttp_set_gencb(http, send_document_cb, argv[1]);
|
||||||
|
|
||||||
/* Now we tell the evhttp what port to listen on */
|
/* 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) {
|
if (!handle) {
|
||||||
fprintf(stderr, "couldn't bind to port %d. Exiting.\n",
|
fprintf(stderr, "couldn't bind to port %d. Exiting.\n", o.port);
|
||||||
(int)port);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user