davidmoreno / onion

C library to create simple HTTP servers and Web Applications.
http://www.coralbits.com/libonion/
Other
2.02k stars 250 forks source link

IS there any source of help for regex patterns? #243

Closed alexe100 closed 5 years ago

alexe100 commented 5 years ago

Hi! I am trying to implement a response for a GET action for https://www.xxx.yyy/storeddata/11/2018

I am using: onion_url_add(urls,"^(.*)$", HandleReq);

Then I have to get fullpath and do string tokenizer in order to verify if the month and year were passed. Using C# it is possible to define a pattern like:

.../{$month}/{$year} and get we just process month and year variables. Can we do something like that using onion?

By the way, another curiosity I have:

onion_url_add vs onion_url_add_with_data

Which data is that? HTTP body? I use onion_request_get_data(req);

Thanks a lot

Alex

davidmoreno commented 5 years ago

Not tested, but you should be able to use:

onion_url_add(urls, "^/([1-9]+)/([1-9]+)/$", handle);
...
month = onion_request_get_query(req, "1");
year = onion_request_get_query(req, "2");

Truth be told its long time I dont use this functionality, so there may be a better way.

About the difference is to add the data parameter to the handler, or not. Remember the handler has the signature:

onion_connection_status_t my_handler(void *userdata, onion_request *, onion_response *);

So if you use the _with_data, then the userdata is passed through. But sometimes you don't need any data to pass.