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

response from local file #36

Open
futurist opened this issue Jul 11, 2024 · 10 comments
Open

response from local file #36

futurist opened this issue Jul 11, 2024 · 10 comments

Comments

@futurist
Copy link
Contributor

I have a proxy rule like below:

https://example.com/x.json -> file:///test/x.json

Want to response from local file /test/x.json directly without any actual network request when the uri is https://example.com/x.json.

Is it supported and how to do that?

@hatoo
Copy link
Owner

hatoo commented Jul 11, 2024

It's not supported but looks good.
I'll implement it.

@cismous
Copy link

cismous commented Jul 11, 2024

I also require this functionality. I need to read response content and alter specific sections of it using regular expressions

@futurist
Copy link
Contributor Author

Just a discussion, does this feature related to some API changes?
For example the API design of rama, it exposed the whole service layer and upstream, but that is more complex and not strait forward to use, but the Matcher System can be reused?

@hatoo
Copy link
Owner

hatoo commented Jul 13, 2024

Yes, actually I'm going to change API fundamentally.
I don't know about rama but I'll let users create a custom service that receives Requests and returns Responses.
I think it will not be very complex.

@hatoo hatoo mentioned this issue Jul 13, 2024
@futurist
Copy link
Contributor Author

futurist commented Jul 25, 2024

I've tested #40, the speed is good, but I cannot find a clue how to response from local file without actually making request out.
Can you give an example to do this?

@hatoo
Copy link
Owner

hatoo commented Jul 25, 2024

Could you take a look #45?

@cismous
Copy link

cismous commented Aug 8, 2024

Can you provide an example of how to convert the res variable to text for rendering, given the following code:

let (mut res, _upgrade) = client.send_request(req).await?;

I want to replace the base with the content of the return to make modifications
@hatoo

@hatoo
Copy link
Owner

hatoo commented Aug 8, 2024

You can receive bodies in proxy and send back full body.

    let server = proxy
        .bind(("127.0.0.1", 3003), move |_client_addr, req| {
            let client = client.clone();
            async move {
                let uri = req.uri().clone();

                // You can modify request here
                // or You can just return response anyware

                let (res, _upgrade) = client.send_request(req).await?;

                println!("{} -> {}", uri, res.status());

                let (parts, body) = res.into_parts();
                let body = body.collect().await?.to_bytes();

                println!("{}", String::from_utf8_lossy(&body));

                // You can modify response here

                Ok::<_, http_mitm_proxy::default_client::Error>(Response::from_parts(
                    parts,
                    http_body_util::Full::new(body),
                ))
            }
        })
        .await
        .unwrap();

@cismous
Copy link

cismous commented Aug 8, 2024

@hatoo
Hello,
I found that when I access it through a browser, the printed content is garbled, but page render is ok.
If I use curl -x http://127.0.0.1:7888 https://www.google.com.hk the printed content is correct.
The cause has not been identified yet.

error image
image

right image
image

@cismous
Copy link

cismous commented Aug 9, 2024

Solved!
When browser send request,the request headers includes content-encoding: gzip,the response body is gzip file, cause garble。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants