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
Provide Path, like old url crate #14
Comments
In what context would this be use? Should it include the query string? The fragment identifier? Note that HTTP’s What would be the semantics of What is In general, I prefer having simple features that can be composed and built on top of to achieved various things, rather than a bunch of elaborate but narrow-purpose features. |
My case:
Inside I have multiple operations (users, posts, tags, topics etc), each operation can have multiple sub-operations (create/update/read/update_all/delete_all), sub operations are not RESTful, so sub-paths are So inside my api instance I'll need to join 2 paths all the time,
2nd case: Once I have the correct path (i.e. /api/v1/users/delete_all), I will want to build a query string. How should I handle this with current design of rust-url? To generalise my use cases: Once I have a base URL (url with host,port and path), I will need to construct other URLs that start with base url, but append sub-paths and modify query string |
@Valve, you can do this by parsing with a base URL: let api_base = Url::parse("https://service.com/api/v1/").unwrap();
let delete = UrlParser::new().base_url(&api_base).parse("users/delete_all").unwrap();
assert!(delete.to_string() == "https://service.com/api/v1/users/delete_all".to_string());
let dallas = UrlParser::new().base_url(&api_base).parse("?q=city:Dallas").unwrap();
assert!(dallas.to_string() == "https://service.com/api/v1/users/delete_all?q=city:Dallas".to_string()); Alternatively, for the query string, you can set key/value pairs. This will deal with percent-encoding if your string contains let mut dallas2 = delete.clone();
dallas2.set_query_from_pairs([("q", "city:Dallas")].iter())
assert!(dallas2 == dallas); (I haven’t tried the code above, so maybe it doesn’t compile. But you get the idea.) But this is all with full URLs. This issues (if I understand correctly) is about manipulating paths without the rest of the URL. |
@SimonSapin thanks for the update, this is roughly what I've been doing. |
Yes, looking at some of the web frameworks (conduit, iron, nicket), I see this code a lot For an HTTP library, I'd rather the Request struct have a Path instead of a String for it's uri: struct Request {
uri: RequestUri,
// ...
}
enum RequestUri {
Star,
AbsoluteUri(Url),
AbsolutePath(Path), // <-- Path instead of String
Authority(Authority),
} |
It’s still not clear to me: what functionality do you think |
let route = Path::new("/app/foo/bar"); // pull off req.url?
let js = route.join("../foo.js");
let css = route.join("./bar.css"); |
Could you explain the semantics of |
In particular, for join, should "absolute" paths (starting with |
I'm just making up functionality that sounds like one might expect a
|
Likewise, to deal with multiple absolute Paths, perhaps |
It sounds like you don’t need any of this yourself. Do you? I’d rather wait until someone with a use case asks for something specific than speculate. |
True, the specific functionality I don't need myself. I'm working on an |
I’ve added Web browsers string the fragment identifier when sending an HTTP request, but a misbehaved client could still send it. After some quick testing with netcat against a few servers, it appears that servers typically parse the fragment identifier (rather than let it be part of the path or query string) but then ignore it. So I’d recommend something like this for an HTTP server: struct PathRequestTarget {
path: Vec<String>,
query: Option<String>
}
let (path, query, _fragment) = url::parse_path(input).unwrap();
PathRequestTarget { path: path, query: query } |
@seanmonstar By the way, is your HTTP library only for the server-side, or also clients? Is it available online yet? |
I'd like for it to do both. I'm aiming at it being a valid options for |
Defining a web server would likely be defining a bunch of paths, and it'd be nice to be able to parse them to ensure they are correct, as well as being able to modify the
Path
, such aspath.join(otherPath)
,path.add_query_param()
, etc.http://doc.rust-lang.org/url/struct.Path.html
The text was updated successfully, but these errors were encountered: