Warp server handling fragments


I’m struggling to understand how to use the path::param filter in warp. This may in fact be an XY problem, where the problem is not with path::param but in the approach to the task overall. Right now I have two static pages. I’d like to serve them both at the same path depending upon parameter presence. When no param is present it should serve the login page. This page uses oauth2 to return a redirect url with token parameters.

The simplest task is to check if a param is present and serve the logged-in user page. Of course ideally I’d like to figure out how to parse the various params and pass that info to the logged-in user page correctly, but for now I’m just trying to get two different files at the same path.

If I just define my route as warp::get2().and(warp::path("mypath").and(warp::fs::file("my/file/path/myfile.html") then warp serves myfile at /mypath and at any /mypath#param1=foo&param2=bar.... I clearly don’t understand what warp::path::param is for because I expect to be able to do something like

                   .map(| param | if param.is_empty() 
                            { warp::fs::file("my/file/path/myfile.html") 
                            } else 
                           { warp::fs::file("my/file/path/myOTHERfile.html")

But this does not work. How would one accomplish what I’m trying to do with warp?



So my question was based on not understanding the terminology of URI elements correctly. I’m handling URIs that contain information of interest in a fragment delineated by #. Is there any way to handle this in warp?



I don’t think this is possible. My understanding is that the fragment (part of an URI after the hash sign) points to an anchor on the page. That is, /page and /page#login still are the same page and I think they are indistinguishable for the server.

Afaik, acting differently depending on the fragment is something only front-end code (ie JavaScript) can do.



Thanks. I found a stackoverflow question that said the same. Turns out I’ve been sending a request for response_type=token which returns a url with a fragment, but I need to send the request with response_type=code which returns a url with a query string that warp CAN parse. I didn’t have luck parsing it with the query() filter though. Had to use filters::query::raw().

1 Like