cloudflare / pingora

A library for building fast, reliable and evolvable network services.
Apache License 2.0
20.25k stars 1.1k forks source link

proxy http echo #252

Closed Maydisease closed 3 weeks ago

Maydisease commented 1 month ago

I would like to implement the following functionality:

Act as a proxy server. When I match an endpoint that starts with /__api, do not proxy the request and return an error to the client. How can I achieve this?

vicanso commented 1 month ago

You can use session.respond_error like this:

https://github.com/cloudflare/pingora/blob/main/pingora-proxy/examples/gateway.rs#L42

Maydisease commented 1 month ago

You can use session.respond_error like this:

https://github.com/cloudflare/pingora/blob/main/pingora-proxy/examples/gateway.rs#L42

If I want to return a status code of 200 and customize the body, can this be achieved?

vicanso commented 1 month ago
 session.write_response_header(Box::new(header)).await?;
 session.write_response_body(body).await?;
Maydisease commented 1 month ago

` async fn request_filter(&self, session: &mut Session, ctx: &mut Self::CTX) -> Result { // ctx.beta_user = check_beta_user(session.req_header());

    let req_header = session.req_header();
    let uri = format!("{}", req_header.uri);

    let mut pathname = String::new();
    if uri.starts_with("https://") || uri.starts_with("http://") {
        let http_url = lib::utils::parse_url(&uri).unwrap();
        pathname = http_url.pathname;
    } else {
        pathname = req_header.uri.to_string();
    }

    if req_header.method == "POST" && pathname.starts_with("/__private_api") {
        let person = Person {
            name: "Alice".to_string(),
            age: 30,
            city: "Wonderland".to_string(),
        };

        let req_body = session.read_request_body().await?;
        println!("req_body: {}", String::from_utf8(Vec::from(req_body.unwrap())).unwrap());

        let body: Bytes = Bytes::from(serde_json::to_string(&person).unwrap());
        let mut header = ResponseHeader::build(200, None).unwrap();
        header.append_header("Content-Type", "application/json").unwrap();
        session.write_response_header(Box::new(header)).await?;
        session.write_response_body(body).await?;
        println!("echo result");
        return Ok(true);
    }

    Ok(false)
}

` I attempted to send content to the client, but it seems to have been intercepted directly, resulting in a client timeout. How can I resolve this issue?

vicanso commented 1 month ago
  1. content-length: body_size and session.finish_body().await?
  2. chunked response transfer-encoding: chunkedand session.finish_body().await?
Maydisease commented 1 month ago
  1. content-length: body_size and session.finish_body().await?
  2. chunked response transfer-encoding: chunkedand session.finish_body().await?

Thank you, it worked. Thanks again.

github-actions[bot] commented 3 weeks ago

This question has been stale for a week. It will be closed in an additional day if not updated.

github-actions[bot] commented 3 weeks ago

This issue has been closed because it has been stalled with no activity.