-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement wildcard URI matching for http_server (IDFGH-10574) #2581
Implement wildcard URI matching for http_server (IDFGH-10574) #2581
Conversation
here is a tester I wrote to verify it's working correctly, for reference: void main(void)
{
struct uritest {
const char *template;
const char *uri;
bool matches;
};
struct uritest uris[] = {
{"/", "/", true},
{"", "", true},
{"/", "", false},
{"/wrong", "/", false},
{"/", "/wrong", false},
{"/asdfghjkl/qwertrtyyuiuioo", "/asdfghjkl/qwertrtyyuiuioo", true},
{"/path", "/path", true},
{"/path", "/path/", false},
{"/path/", "/path", false},
{"?", "", false}, // this is not valid, but should not crash
{"?", "sfsdf", false},
{"/path/?", "/pa", false},
{"/path/?", "/path", true},
{"/path/?", "/path/", true},
{"/path/?", "/path/alalal", false},
{"/path/*", "/path", false},
{"/path/*", "/", false},
{"/path/*", "/path/", true},
{"/path/*", "/path/blabla", true},
{"*", "", true},
{"*", "/", true},
{"*", "/aaa", true},
{"/path/?*", "/pat", false},
{"/path/?*", "/pathb", false},
{"/path/?*", "/pathxx", false},
{"/path/?*", "/pathblabla", false},
{"/path/?*", "/path", true},
{"/path/?*", "/path/", true},
{"/path/?*", "/path/blabla", true},
{"/path/*?", "/pat", false},
{"/path/*?", "/pathb", false},
{"/path/*?", "/pathxx", false},
{"/path/*?", "/path", true},
{"/path/*?", "/path/", true},
{"/path/*?", "/path/blabla", true},
{"/path/*/xxx", "/path/", false},
{"/path/*/xxx", "/path/*/xxx", true},
{}
};
struct uritest *ut = &uris[0];
while(ut->template != 0) {
bool match = uri_matches(ut->template, ut->uri, strlen(ut->uri));
if (match != ut->matches) {
printf("\x1b[31mERR\x1b[m should match? %d, matched? %d\x1b[m\r\n",
ut->matches, match);
} else {
printf("\x1b[32mOK\x1b[m matched? %d\x1b[m\r\n",
ut->matches);
}
ut++;
}
printf("END.\r\n");
} |
@MightyPork Thanks for this new feature. Please fix the conflict so that we can move forward. |
sure i'll fix it. the change is small, so you could also just copy the new function and update the places where it's called from. but didn't you want to merge #2578 before adding more stuff to the HTTP server? |
@MightyPork #2578 is almost through. It will get merged before this pull request is processed |
913bc38
to
bb84828
Compare
the rebase resolved without any changes needed, it should be mergeable now |
Any ideas when this will be merged? |
I have information they're a bit overloaded but working on it. So maybe
we'll get it for Christmas
For now you can copy the http server component to your project and apply
this patch manually, that's what I've been doing for the last month or so.
IDF gives it precedence over the library one if the name matches
…On Tue, Nov 20, 2018, 4:47 PM Oleg Antonyan ***@***.*** wrote:
Any ideas when this will be merged?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2581 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB8lHpPaO-mdKJcWIOXlLnR0WnlmUzouks5uxCQhgaJpZM4XnW_8>
.
|
Nice. Thanks for the great work |
@MightyPork Thank you for this extremely useful feature. Just to keep everyone updated about the status, we are in the process of reviewing this and hope to get this merged soon. |
@anurag-kar what is the progress? I took a break to work on a different project, now came back to the one I developed this for, and sadly finding I still have copy and patch http_server myself :/ |
@MightyPork The PR is still under review. We are considering the possibility of supporting the URI template RFC, in which case there may be additions on top of your feature. So expect some delay. |
ok, just so you know why I added this, the main use case is static files. If you don't like my implementation, adding a simple "*" or NULL route acting as a catch-all would be sufficient for most use cases. Below is, for illustration, my server config that loads CSS, JS, favicon etc using the * route. I have a script that generates a look-up table, and Without the wild-card, you would need to list every single file in the array here, and that's just annoying (plus there's more metadata to include, like mime type and file length, which just isn't where to put with one field I never heard of the "URI template RFC" before, so you get an idea how well known it is. Consider if it's in scope of the SDK to implement something like that - it can be easily done in user code after the route is matched by a wildcard. static const httpd_uri_t routes[] = {
{
.uri = "/",
.method = HTTP_GET,
.handler = rt_static_file,
.user_ctx = "A|index.html", // A| indicates login is needed
},
{
.uri = "/login",
.method = HTTP_GET,
.handler = rt_static_file,
.user_ctx = "login.html",
},
{
.uri = "/login",
.method = HTTP_POST, // post = form was submitted
.handler = rt_login,
.user_ctx = NULL,
},
{
.uri = "/logout",
.method = HTTP_GET,
.handler = rt_logout,
.user_ctx = NULL,
},
{
.uri = "*", // needs wildcard routes
.method = HTTP_GET,
.handler = rt_static_file,
.user_ctx = NULL,
},
}; |
+1 @MightyPork |
@MightyPork Merged to ESP-IDF 91d6b3b. Please help to check once (there is also file serving example at https://github.com/espressif/esp-idf/tree/master/examples/protocols/http_server/file_serving). |
Doesn't work for me |
In 416c55e, a new member was added to |
@igrr thank you! now it works |
tested, works great! Also really like the way you made it extensible |
Can you show an example of your implementation of the rt_static_file() function? |
This PR makes URIs in the HTTP server much more flexible, allowing to e.g. have optional trailing slashes, or to match all files and paths in a folder.
What has been done is best explained in the doc comment: