First of all happy new year to all ;-).

Second, sorry for the delay, but free time is more and more difficult to
find.

I propose here a patch for "evhttp_dispatch_callback" that allow him to
treat wild char in registered uri.
(patch is based on libevent-1.4.1-beta)


in attach you will find a test: "libevent-test.c" that will implement such
uri.
In the sample, I register "/test*" to the function "callback_http_index".
That means that this function will be triggered when the browser will
request the uri /test/12345/read (for example).

This give much more flexibility.

Thanks




> Hi William,
>
> I think the most straightforward approach is to extend the set
> callback mechanism to accept a wild card character such as *.  I will
> take a look at it.  For now, progress is tracked in the source forge
> tracker.
>
> Niels.
>
> On Nov 12, 2007 12:55 AM,  <[EMAIL PROTECTED]> wrote:
>> I'm no more able to use my wsgi python binding to libevent, because the
>> evhttp_dispatch_callback ( or evhttp_handle_request) function is now
>> static.
>>
>> Indeed, as I've mentioned in my mail of "Sun, October 14, 2007 10:22
>> am",
>> I've been forced to update this function.
>>
>> Because the function is not static, I'm able, with libevent-1.3 able to
>> over-write it in my own code:
>> Line 46 here
>> http://www.opensource4you.com/cgi-bin/gitweb.cgi?p=fapws2;a=blob;f=fapws2/_evhttp.c;h=eb0cd0aebcb31b48874842335575827b89091596;hb=05ec30b72f106120d1aa2c7913ba609a1a1fe4a4
>> This is no more possible with libevent-1.4.
>>
>>
>> Thanks to provide possibilities for extension like mine.
>> Or, if I'm not using libevent as it should be, thanks to provide me a
>> better way.
>>
>>
>> Thanks
>>
>>
>> PS:
>> I remind here that my goal is to call a callback based on the start of
>> the
>> url. For example, if I have a callback associated to the url:/static/,
>> all
>> the following urls will trigger this call back:
>> /static/img/logo.png
>> /static/css/main.css
>> /static/js/hello.js
>>
>> As specified in the wsgi specs ( www.wsgi.org), and url is composed of
>> different elements:
>> url = quote(environ.get('SCRIPT_NAME',''))
>> url += quote(environ.get('PATH_INFO',''))
>> if environ.get('QUERY_STRING'):
>>     url += '?' + environ['QUERY_STRING']
>>
>> In my first example associated variable will be:
>> environ['SCRIPT_NAME']="/static/",
>> environ["PATH_INFO"]="img/logo.png", and
>> environ["QUERY_STRING"]="".
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > Hi everyone,
>> >
>> > I am pleased to announce the release of libevent-1.4.0-beta.  This
>> > release contains major new features:
>> >
>> >  - RPC subsystem that makes it easy to write distributed servers and
>> > clients
>> >  - almost everything is documented via Doxygen now
>> >  - many fixes and improvements to evdns and evhttp
>> >  - libevent now builds two additional libraries: libevent_core
>> > (containing only the event core) and libevent_extras (contained evdns,
>> > evhttp and evrpc)
>> >  - performance improvements due to using a heap instead of red-black
>> > trees for timeouts
>> >  - Solaris' event ports are better supported
>> >
>> > Plus many other fixes - see the ChangeLog file for more details.
>> >
>> > You can download libevent from
>> >
>> >   http://www.monkey.org/~provos/libevent/
>> >
>> > I would like to thank Charles Kerr, Christopher Layne, Hannah
>> > Schroeter, Lubomir Marinov, Magne Mahre, Mark Heily, Maxim
>> > Yegorushkin, Nick Mathewson, Prakash Sangappa and Trond Norbye for
>> > their help.
>> >
>> > If you encounter any problems or would like to submit patches, let
>> > either Nick Mathewson or myself know or use the tracker at:
>> >
>> >   https://sourceforge.net/tracker/?group_id=50884
>> >
>> > Thanks,
>> >   Niels.
>> > _______________________________________________
>> > Libevent-users mailing list
>> > Libevent-users@monkey.org
>> > http://monkeymail.org/mailman/listinfo/libevent-users
>> >
>> >
>>
>>
>>
>
>
--- http.c	2007-12-22 04:03:54.000000000 +0100
+++ myhttp.c	2008-01-04 15:32:34.000000000 +0100
@@ -1913,13 +1913,18 @@ evhttp_dispatch_callback(struct httpcbq 
 
 	TAILQ_FOREACH(cb, callbacks, next) {
 		int res = 0;
-		if (p == NULL) {
-			res = strcmp(cb->what, req->uri) == 0;
-		} else {
-			res = ((strncmp(cb->what, req->uri, offset) == 0) &&
-					(cb->what[offset] == '\0'));
-		}
-
+        char *multi = strchr(cb->what, '*');
+        if (multi != NULL) {
+            int len = strlen(cb->what) -1;
+            res =  strncmp(cb->what, req->uri, len) == 0;
+        } else {
+            if (p == NULL) {
+                res = strcmp(cb->what, req->uri) == 0;
+            } else {
+                res = ((strncmp(cb->what, req->uri, offset) == 0) &&
+                        (cb->what[offset] == '\0'));
+            }
+        }
 		if (res)
 			return (cb);
 	}
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <stdlib.h>

#include "event.h"
#include "evhttp.h"

void callback_http_default(struct evhttp_request *, void *);
void callback_http_index(struct evhttp_request *, void *);

int main(int argc, char **argv)
{
	struct evhttp *httpd;

	event_init();
	
	/* bind to port 8081, server not yet started... */
	httpd = evhttp_start("0.0.0.0", 8081);

	/* assign callbacks */
	evhttp_set_cb(httpd, "/test*", callback_http_index, NULL);
	evhttp_set_gencb(httpd, callback_http_default, NULL);

	/* start the server */
	event_dispatch();

	evhttp_free(httpd);
        printf("done\n");
	return 0;
}

void callback_http_default(struct evhttp_request *req, void *arg)
{
	struct evbuffer *buf = evbuffer_new();
	char *escaped = evhttp_htmlescape(evhttp_request_uri(req));

	evbuffer_add_printf(buf, 
		"<html><head><title>Not Found</title></head>"
		"<body>Location %s not found</body></html>",
		escaped);
	evhttp_send_reply(req, HTTP_NOTFOUND, "Location not found", buf);
    evbuffer_free(buf);
}


void callback_http_index(struct evhttp_request *req, void *arg)
{
	struct evbuffer *buf = evbuffer_new();
	evbuffer_add_printf(buf,"<html><head><title>Testing</title></head><body>Testing</body></html>\n");
	evhttp_send_reply(req, HTTP_OK, "OK", buf);
    evbuffer_free(buf);
}
_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users

Reply via email to