How to get path from URL in warp?

1.2k Views Asked by At

I am using warp to build a Proxy. The proxy doesn't care about parameters or path, it just delegate requests from clients. So the client may query like:

https://proxy.com/p1?a=1&b=2 or https://proxy.com/p2?c=1 or many other different paths

I want to write something like this:

    /*
    client query: https://proxy.com/test_path1?a=1
    client query: https://proxy.com/test_path2?b=2
     */
    let filter = warp::any()
        .and(warp::query::<HashMap<String, String>>())
        .map(|p: HashMap<String, String>| {
            let path = xxx.get_path();  // path = "/test_path1" or "/test_path2"
            println!("{:?}",p); // p = {"a":1} or {"b":2} respectively
        });

instead of this:

    let path1 = warp::path("test_path1").and(warp::query::<HashMap<String, String>>()).map(|p: HashMap<String,String>|{
        let path = "test_path1".to_string();
        println!("{:?}",p); // p = {"a":1} or {"b":2} respectively
    });

    let path2 = warp::path("test_path2").and(warp::query::<HashMap<String, String>>()).map(|p: HashMap<String,String>|{
        let path = "test_path2".to_string();
        println!("{:?}",p); // p = {"a":1} or {"b":2} respectively
    });

How can I do it?

2

There are 2 best solutions below

2
Masklinn On

https://docs.rs/warp/latest/warp/filters/path/fn.tail.html

But are you even using warp::any? That's useful as a base to BYO (e.g. to add external data to the request) but warp::query is a filter, if you're using only that it doesn't need to be chained to anything.

And HTTP allows repeating query parameters so technically if you reify to a hashmap there is no guarantee the "proxy" will fully replicate the original request.

2
isaactfa On

Without any further detail, this does what you want by utilizing the full filter:

use warp::{self, path::FullPath, Filter};

let filter = warp::any()
    .and(warp::query::<HashMap<String, String>>())
    .and(warp::path::full())
    .map(|q: HashMap<_, _>, p: FullPath| {
        let path = p.as_str();
        format!("path: {}\nquery: {:?}", path, q)
    });