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

Crate mount and trailing slash redirect #128

Open
sapsan4eg opened this issue Oct 31, 2016 · 1 comment
Open

Crate mount and trailing slash redirect #128

sapsan4eg opened this issue Oct 31, 2016 · 1 comment

Comments

@sapsan4eg
Copy link
Contributor

sapsan4eg commented Oct 31, 2016

Hi guys. I saw that already there are two issue with such behavior. However, they have not solution. I had some problem with crate mount and crate route.
Simple example

extern crate iron;
extern crate router;
extern crate mount;

use iron::{Iron, Request, Response, IronResult};
use iron::status;
use router::{Router};

fn main() {
    let mut router = Router::new();
    router.get("/get", query_handler, "query_handler");

    fn handler(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "OK")))
    }

    fn query_handler(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((status::Ok, "query")))
    }

    let mut mount = mount::Mount::new();
    mount
        .mount("/", handler)
        .mount("/api", router)
    ;

    Iron::new(mount).http("127.0.0.1:3000").unwrap();
}

When i create request like GET http://www.example.com/api/get/ i had redirect to http://www.example.com/get . This because the mount crate cuts url in there https://github.com/iron/mount/blob/master/src/mount.rs#L119

To use both the crates I had fork this project and add dependency route to mount and add this

let original = req.extensions.get::<mount::OriginalUrl>();

        if original.is_some() {
            url =  original.unwrap().clone();
        }

To https://github.com/iron/router/blob/master/src/router.rs#L178
Now all work fine. But the decision I do not like.

The only reasonable solution to move in iron crate this struct

pub struct OriginalUrl;
impl typemap::Key for OriginalUrl { type Value = Url; }

And use in mount and route crate

@untitaker
Copy link
Member

untitaker commented Oct 31, 2016

Ohh that's nasty.

The only reasonable solution to move in iron crate this struct

I agree, though I would like to have a "original request" field on the iron::Request struct (outside of the extension typemap, as new struct field) instead. E.g.:

struct Request {
    ...,
    parent: Option<Request>
}

And mount would insert a copy of the old request there before modifying the URL. This is more flexible (as modified headers are available pre-modification as well) though I'm concerned about unnecessary copies.

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

No branches or pull requests

2 participants