mirror of
https://github.com/cuberite/libevent.git
synced 2025-08-04 01:36:23 -04:00
sample/http-server: fix parameter parsing
argv[1] is currently unconditionally passed as the docroot to send_document_cb(). This is broken if any optional parameters are provided, such as -p <port>. Signed-off-by: David Disseldorp <ddiss@samba.org> (cherry picked from commit 7d71214e02646a31eede66ad5badf695a2ab2058)
This commit is contained in:
parent
f9d7ac0de8
commit
4a6f1ccfae
@ -99,14 +99,14 @@ static const struct table_entry {
|
|||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
struct options
|
struct options {
|
||||||
{
|
|
||||||
int port;
|
int port;
|
||||||
int iocp;
|
int iocp;
|
||||||
int verbose;
|
int verbose;
|
||||||
|
|
||||||
int unlink;
|
int unlink;
|
||||||
const char *unixsock;
|
const char *unixsock;
|
||||||
|
const char *docroot;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Try to guess a good content-type for 'path' */
|
/* Try to guess a good content-type for 'path' */
|
||||||
@ -182,7 +182,7 @@ static void
|
|||||||
send_document_cb(struct evhttp_request *req, void *arg)
|
send_document_cb(struct evhttp_request *req, void *arg)
|
||||||
{
|
{
|
||||||
struct evbuffer *evb = NULL;
|
struct evbuffer *evb = NULL;
|
||||||
const char *docroot = arg;
|
struct options *o = arg;
|
||||||
const char *uri = evhttp_request_get_uri(req);
|
const char *uri = evhttp_request_get_uri(req);
|
||||||
struct evhttp_uri *decoded = NULL;
|
struct evhttp_uri *decoded = NULL;
|
||||||
const char *path;
|
const char *path;
|
||||||
@ -222,12 +222,12 @@ send_document_cb(struct evhttp_request *req, void *arg)
|
|||||||
if (strstr(decoded_path, ".."))
|
if (strstr(decoded_path, ".."))
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
len = strlen(decoded_path)+strlen(docroot)+2;
|
len = strlen(decoded_path)+strlen(o->docroot)+2;
|
||||||
if (!(whole_path = malloc(len))) {
|
if (!(whole_path = malloc(len))) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
evutil_snprintf(whole_path, len, "%s/%s", docroot, decoded_path);
|
evutil_snprintf(whole_path, len, "%s/%s", o->docroot, decoded_path);
|
||||||
|
|
||||||
if (stat(whole_path, &st)<0) {
|
if (stat(whole_path, &st)<0) {
|
||||||
goto err;
|
goto err;
|
||||||
@ -346,12 +346,13 @@ done:
|
|||||||
static void
|
static void
|
||||||
print_usage(FILE *out, const char *prog, int exit_code)
|
print_usage(FILE *out, const char *prog, int exit_code)
|
||||||
{
|
{
|
||||||
fprintf(out, "Syntax: [ OPTS ] %s <docroot>\n", prog);
|
fprintf(out,
|
||||||
fprintf(out, " -p - port\n");
|
"Syntax: %s [ OPTS ] <docroot>\n"
|
||||||
fprintf(out, " -U - bind to unix socket\n");
|
" -p - port\n"
|
||||||
fprintf(out, " -u - unlink unix socket before bind\n");
|
" -U - bind to unix socket\n"
|
||||||
fprintf(out, " -I - IOCP\n");
|
" -u - unlink unix socket before bind\n"
|
||||||
fprintf(out, " -v - verbosity, enables libevent debug logging too\n");
|
" -I - IOCP\n"
|
||||||
|
" -v - verbosity, enables libevent debug logging too\n", prog);
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
static struct options
|
static struct options
|
||||||
@ -377,6 +378,7 @@ parse_opts(int argc, char **argv)
|
|||||||
if (optind >= argc || (argc - optind) > 1) {
|
if (optind >= argc || (argc - optind) > 1) {
|
||||||
print_usage(stdout, argv[0], 1);
|
print_usage(stdout, argv[0], 1);
|
||||||
}
|
}
|
||||||
|
o.docroot = argv[optind];
|
||||||
|
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
@ -504,7 +506,7 @@ main(int argc, char **argv)
|
|||||||
|
|
||||||
/* We want to accept arbitrary requests, so we need to set a "generic"
|
/* We want to accept arbitrary requests, so we need to set a "generic"
|
||||||
* cb. We can also add callbacks for specific paths. */
|
* cb. We can also add callbacks for specific paths. */
|
||||||
evhttp_set_gencb(http, send_document_cb, argv[1]);
|
evhttp_set_gencb(http, send_document_cb, &o);
|
||||||
|
|
||||||
if (o.unixsock) {
|
if (o.unixsock) {
|
||||||
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
|
#ifdef EVENT__HAVE_STRUCT_SOCKADDR_UN
|
||||||
|
Loading…
x
Reference in New Issue
Block a user