update for libevent 2.2 handling of custom HTTP methods

see 8dcb94a4ca
This commit is contained in:
Thomas Bernard 2019-04-16 01:02:37 +02:00
parent 5ae326a95e
commit 41f5475080
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
3 changed files with 38 additions and 9 deletions

View File

@ -17,7 +17,9 @@ endif
#CFLAGS += -I/usr/local/include
CFLAGS += $(shell $(PKG_CONFIG) --cflags libevent)
#CFLAGS += -DENABLE_UPNP_EVENTS
ifeq ($(shell if $(PKG_CONFIG) --atleast-version 2.2 libevent ; then echo "true" ; fi),true)
CFLAGS += -DENABLE_UPNP_EVENTS
endif
#LDLIBS = -levent
LDLIBS = $(shell $(PKG_CONFIG) --libs-only-l libevent)

View File

@ -6,8 +6,8 @@ http://libevent.org
https://github.com/libevent/libevent
The UPnP Event code needs SUBSCRIBE / UNSUBSCRIBE / NOTIFY HTTP methods.
Implementation is in progress in libevent HTTP code :
https://github.com/libevent/libevent/pull/327
Implementation is added to libevent 2.2.x :
https://github.com/libevent/libevent/commit/8dcb94a4ca0999bdada6baa2a986f4c00a922060
TODO : improve error handling / reporting

View File

@ -658,10 +658,37 @@ static int upnpc_send_soap_request(upnpc_device_t * p, const char * url,
#ifdef ENABLE_UPNP_EVENTS
#define EVHTTP_REQ_NOTIFY ((EVHTTP_REQ_MAX) << 1)
#define EVHTTP_REQ_SUBSCRIBE ((EVHTTP_REQ_NOTIFY) << 1)
static const struct evhttp_extended_method ext_methods[] = {
{"NOTIFY", EVHTTP_REQ_NOTIFY, EVHTTP_METHOD_HAS_BODY},
{"SUBSCRIBE", EVHTTP_REQ_SUBSCRIBE, EVHTTP_METHOD_HAS_BODY},
{NULL, 0, 0}
#define EVHTTP_REQ_UNSUBSCRIBE ((EVHTTP_REQ_SUBSCRIBE) << 1)
static int ext_methods_cb(struct evhttp_ext_method *p)
{
if(p == NULL)
return -1;
if(p->method != NULL) {
if(strcmp(p->method, "NOTIFY") == 0) {
p->type = EVHTTP_REQ_NOTIFY;
p->flags = EVHTTP_METHOD_HAS_BODY;
} else if(strcmp(p->method, "SUBSCRIBE") == 0) {
p->type = EVHTTP_REQ_SUBSCRIBE;
} else if(strcmp(p->method, "UNSUBSCRIBE") == 0) {
p->type = EVHTTP_REQ_UNSUBSCRIBE;
} else {
return -1;
}
} else switch(p->type) {
case EVHTTP_REQ_NOTIFY:
p->method = "NOTIFY";
p->flags = EVHTTP_METHOD_HAS_BODY;
break;
case EVHTTP_REQ_SUBSCRIBE:
p->method = "SUBSCRIBE";
break;
case EVHTTP_REQ_UNSUBSCRIBE:
p->method = "UNSUBSCRIBE";
break;
default:
return -1;
}
return 0;
};
void upnpc_event_conn_req(struct evhttp_request * req, void * data)
@ -907,7 +934,7 @@ int upnpc_event_subscribe(upnpc_device_t * p)
debug_printf("evhttp_new() FAILED\n");
return -1;
}
evhttp_set_extended_methods(p->parent->http_server, ext_methods);
evhttp_set_ext_method_cmp(p->parent->http_server, ext_methods_cb);
evhttp_set_allowed_methods(p->parent->http_server, EVHTTP_REQ_NOTIFY);
evhttp_set_cb(p->parent->http_server, "/evt_conn", upnpc_event_conn_req, p);
if(evhttp_bind_socket(p->parent->http_server, p->parent->local_address, p->parent->local_port) < 0) {
@ -926,7 +953,7 @@ int upnpc_event_subscribe(upnpc_device_t * p)
if(p->soap_conn == NULL) {
p->soap_conn = evhttp_connection_base_new(p->parent->base, NULL, hostname, port);
}
evhttp_connection_set_extended_methods(p->soap_conn, ext_methods);
evhttp_connection_set_ext_method_cmp(p->soap_conn, ext_methods_cb);
req = evhttp_request_new(upnpc_subscribe_response, p);
headers = evhttp_request_get_output_headers(req);
/*buffer = evhttp_request_get_output_buffer(req);*/