Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

UrlMap not interpreting REQUEST_URI correctly #9

Open
nettles-jarrod opened this issue Aug 12, 2013 · 13 comments
Open

UrlMap not interpreting REQUEST_URI correctly #9

nettles-jarrod opened this issue Aug 12, 2013 · 13 comments
Assignees

Comments

@nettles-jarrod
Copy link

In situations where there is a file prefix in the REQUEST_URI, UrlMap incorrectly includes it as part of the intended route.

For example, in Symfony during development, you usually visit the app through local.dev/app_dev.php/my/action and Symfony is smart enough to compute the /my/action route automatically, chopping off the app_dev.php.

StackPHP does not chop off this file prefix. It seems to me that this should be the expected behavior.

You can easily reproduce this issue by using two middlewares and UrlMap through a file in the URL.

@ghost ghost assigned CHH Aug 13, 2013
@CHH
Copy link
Member

CHH commented Aug 13, 2013

Tested it on the dev server, works for me.

app_dev.php:

<?php

require "vendor/autoload.php";

use Stack\CallableHttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$app = new CallableHttpKernel(function ($req) {
    return new Response("app");
});

$foo = new CallableHttpKernel(function ($req) {
    return new Response("foo");
});

$map = new Stack\UrlMap($app, [
    '/foo' => $foo
]);

$resp = $map->handle(Request::createFromGlobals());
$resp->send();

On the shell:

$ php -S localhost:4000 -t ./

Request in the browser: http://localhost:4000/app_dev.php/foo and you should get "foo"

Make sure that you request this code really through app_dev.php. It doesn't work if app_dev.php is not a real script and the "app_dev.php" is not the basename of the script filename. We can't do anything about this, it's all handled internally by the request object.

@CHH CHH closed this as completed Aug 13, 2013
@nettles-jarrod
Copy link
Author

Your example works correctly, but I invite you to try it with Stack\Builder and a fresh Symfony install.

There's either a problem, or the documentation isn't clear enough on how to handle this.

@CHH
Copy link
Member

CHH commented Aug 13, 2013

Ok, looking into it.

Am Dienstag, 13. August 2013 schrieb Jarrod Nettles :

Your example works, but I invite you to try it with Stack\Builder and a
fresh Symfony install.

There's either a problem, or the documentation isn't clear enough on how
to handle this.


Reply to this email directly or view it on GitHubhttps://github.com//issues/9#issuecomment-22580791
.

Christoph Hochstrasser
http://twitter.com/yuri41 | http://christophh.net | https://github.com/CHH

@igorw
Copy link
Contributor

igorw commented Aug 13, 2013

Re-opening, just so this does not get lost.

@igorw igorw reopened this Aug 13, 2013
@defrag
Copy link

defrag commented Aug 20, 2013

I had to use it today and I confirm that the issue exists. In my app, when i access site via app_dev.php,
i had to change server variables in handle to make it work with app_dev.php to:

   $server['SCRIPT_FILENAME'] = $server['SCRIPT_NAME'] = $server['PHP_SELF'] = $server['SCRIPT_NAME'] . $path;

cc @Blackshawk

@CHH
Copy link
Member

CHH commented Aug 20, 2013

@defrag Could you please post the complete file with your code?

@CHH
Copy link
Member

CHH commented Aug 20, 2013

@Blackshawk:
I've now tried it with Symfony 2.3 Components and Stack Builder and it works without problems on the builtin dev server.

What web server are you using?

Here is my code:

<?php

require "vendor/autoload.php";

use Stack\CallableHttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Stack\UrlMap;
use Stack\Builder;

$app = new CallableHttpKernel(function ($req) {
    return new Response("app");
});

$foo = new CallableHttpKernel(function ($req) {
    echo "<pre>";
    print_r($_SERVER);
    echo "</pre>";
    return new Response("foo");
});

$app = (new Builder)
    ->push(UrlMap::class, [
        '/foo' => $foo
    ])
    ->resolve($app);

$resp = $app->handle(Request::createFromGlobals());
$resp->send();

@CHH
Copy link
Member

CHH commented Aug 20, 2013

@Blackshawk: Will try it with a fresh Symfony install too.

@CHH
Copy link
Member

CHH commented Aug 20, 2013

@Blackshawk: Tried it with a completely fresh Symfony 2.3 install and it works with ./app/console server:run.

I added only this to my app_dev.php:

<?php

$kernel = (new Stack\Builder)
    ->push(Stack\UrlMap::class, [
        '/foo' => new Stack\CallableHttpKernel(function ($req) {
            return new Response("foo");
        })
    ])
    ->resolve($kernel);

@CHH
Copy link
Member

CHH commented Aug 20, 2013

@defrag @Blackshawk Which web server gives you this issues? Apache, NGINX or the builtin dev server?

@defrag
Copy link

defrag commented Aug 20, 2013

@CHH Well it will work with the CallableHttpKernel with simple response like you showed, but it will not work with symfony2 app kernel. In my case I have FeatureKernel that extends AppKernel, which have some associated webservices that needs to be mock per feature.

foreach ($fakeWs as $key => $value) {
    $map['/' . $key] = $featureKernel;
}

$stack = (new Stack\Builder)
    ->push('Stack\UrlMap', $map)
;

Then im accessing page like app_dev.php/FEATURE-123/ and it boots kernel with needed data.

If i will not change the server variables like I mentioned in sample before, all routings etc will totally mess. Try mapping new AppKernel instead of CallableHttpKernel and You will see errors.
Hope that explanation helps. I didnt found the best way to handle this yet, im currently using modified UrlMap.

@nettles-jarrod
Copy link
Author

@CHH I was using Apache, although I experienced the same problem using the built-in dev server also. I think @defrag is correct - I just tried bootstrapping a Symfony and a Silex kernel and was getting the same behavior. For some reason CallableHttpKernel doesn't exhibit this behavior.

@henrikbjorn
Copy link
Contributor

Have the same problem with lighttpd and fpm.

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

No branches or pull requests

5 participants