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

addr::remote() always returns None with externally-provided TcpListener #830

Open
mcginty opened this issue Apr 5, 2021 · 6 comments
Open
Labels
feature New feature or request

Comments

@mcginty
Copy link

mcginty commented Apr 5, 2021

there's no way currently to run a warp service that depends on the addr::remote() filter when served from a custom-initialized TCP socket, as neither Server::run_incoming2() nor FilteredService::call_with_addr() are accessible from outside the crate, so even trying the warp::service() route doesn't help.

#713 attempts to solve this one way, but it seems to have not gotten any activity.

In its simplest form, is there any reason not to simply make FilteredService::call_with_addr() public?

@wngr
Copy link
Contributor

wngr commented Apr 6, 2021

You can do it by jumping through some hoops using hyper:

use futures::{Future, TryFutureExt};
use hyper::{server::Server, service::make_service_fn};
use std::convert::Infallible;
use std::net::SocketAddr;
use warp::filters::BoxedFilter;
use warp::Reply;

/// Create a hyper server with the provided `filter`, binding to `addr`. This also sets the
/// `TCP_NODELAY` flag on incoming connections.
pub(crate) fn serve_it<T: Into<SocketAddr>>(
    addr: T,
    filter: BoxedFilter<(impl Reply + 'static,)>,
) -> anyhow::Result<(SocketAddr, impl Future<Output = anyhow::Result<()>>)> {
    let filtered_service = warp::service(filter);

    let make_svc = make_service_fn(move |_| {
        let filtered_service = filtered_service.clone();
        async move { Ok::<_, Infallible>(filtered_service) }
    });

    let listener: std::net::TcpListener = make_listener(addr)?;
    let bound_to = listener.local_addr()?;
    let builder = Server::from_tcp(listener)?;
    let fut = builder.tcp_nodelay(true).serve(make_svc).map_err(|e| e.into());
    Ok((bound_to, fut))
}

@mcginty
Copy link
Author

mcginty commented Apr 6, 2021

@wngr I had code similar to this - unless I'm mistaken, you can't then access the remote_addr in warp with this setup - it always returns None, which is the problem.

@wngr
Copy link
Contributor

wngr commented Apr 6, 2021

You're right, you can't. The original issue description is misleading then though, maybe you could clarify the missing behaviour a bit more.

@mcginty mcginty changed the title No way to get remote addr with an externally-provided TcpListener addr::remote() always returns None externally-provided TcpListener Apr 6, 2021
@mcginty mcginty changed the title addr::remote() always returns None externally-provided TcpListener addr::remote() always returns None with externally-provided TcpListener Apr 6, 2021
@mcginty
Copy link
Author

mcginty commented Apr 6, 2021

Oh wow, yeah that description was unreadable looking back - sorry about that. Hopefully the updated one is a bit more readable, thanks for looking into it.

@valbendan
Copy link
Contributor

Hope we can merge #713 before v0.4 release.

alixinne added a commit to alixinne/hyperion.rs that referenced this issue Jul 9, 2021
alixinne added a commit to alixinne/hyperion.rs that referenced this issue Nov 8, 2021
alixinne added a commit to alixinne/hyperion.rs that referenced this issue Nov 10, 2021
@tokahuke
Copy link

tokahuke commented May 29, 2022

So, what is the status on this? When is v0.4 coming?

I see there are actually two concurrent implementations in PR #964 and #713. Which will be accepted? I see that #964 does not break any public interface (allegedly). Can it be shipped in a minor version?

Asking this because I need it for a current project of mine.

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

No branches or pull requests

4 participants