Skip to content
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

Path rewriting utility function #462

Open
JosiahParry opened this issue Nov 10, 2024 · 0 comments
Open

Path rewriting utility function #462

JosiahParry opened this issue Nov 10, 2024 · 0 comments
Labels
enhancement New feature or request ergonomics Ease of use, developer friendliness help wanted Extra attention is needed

Comments

@JosiahParry
Copy link

What is the problem your feature solves, or the need it fulfills?

I am using pingora to proxy to other locally hosted web applications. These applications are hosted at a random port on localhost.

In order to forward the request to the appropriately the path needs to be rewritten.

Describe the solution you'd like

It would be nice if there was a method, associated function, function, etc, that simplified the process of path rewriting. For example when using caddy, the path is rewritten automagically. I suspect a method in pingora would ensure that rewriting is done correctly whereas I wouldn't put it past myself to do it incorrectly.

Describe alternatives you've considered

The following is the approach I am taking:

// helper function to get the first path then the rest of the request path
pub fn split_uri(uri: &Uri) -> (String, String) {
    let full_uri = uri.to_string();
    let splits = full_uri.split("/").skip(1).collect::<Vec<&str>>();
    let slug = format!("{}", splits[0..1].join("/"));
    let path = format!("/{}", splits[1..].join("/"));
    (slug, path)
}

// method implementation in the ProxyHttp trait
    // Optional but recommended method to modify requests before forwarding
    async fn upstream_request_filter(
        &self,
        session: &mut Session,
        request: &mut RequestHeader,
        _ctx: &mut Self::CTX,
    ) -> Result<()> {
        let path = request.uri.path();

        let (slug, path) = split_uri(&session.req_header().uri);

        // this check is only an example, in reality I am fetching from a DashMap
        if slug == "app" {
            request.set_uri(
                Uri::builder()
                    .path_and_query(PathAndQuery::from_str(&path).unwrap())
                    .build()
                    .unwrap(),
            );
        }
        Ok(())
    }

Additional context

It would be really great if pingora handled these headers in an automatic way so that some of the simplicity of other reverse proxies—e.g. caddy—could be found in pingora. That would give us (less web savvy users) an easier time focusing on the other fun stuff that pingora enables

@eaufavor eaufavor added enhancement New feature or request ergonomics Ease of use, developer friendliness labels Nov 12, 2024
@johnhurt johnhurt added the help wanted Extra attention is needed label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ergonomics Ease of use, developer friendliness help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants