tuupola / cors-middleware

PSR-7 and PSR-15 CORS middleware
MIT License
132 stars 16 forks source link

Zend Expressive Don't work CORS #6

Closed lpj145 closed 7 years ago

lpj145 commented 7 years ago

On last version of zend expressive https://docs.zendframework.com/zend-expressive/

The cors not works right!

When i: image

image

i think it not pass to cors on next function.

tuupola commented 7 years ago

The above code does not seem to be from my cors middleware?

lpj145 commented 7 years ago

above is a simple cors middleware, i can't implement you middleware.

lpj145 commented 7 years ago

please see it: https://gist.github.com/lpj145/cd2d8760ee08fb5ab9db4e05ed68b329#file-tuupolacors-php-L53

tuupola commented 7 years ago

Ah yes now I understand. There is no PSR-15 support yet because PSR-15 is not finalized. However Expressive has a PSR-15 wrapper for PSR-7 style middlewares.

lpj145 commented 7 years ago

oh, its song good for me, i need make factory to do it ?

Xerkus commented 7 years ago

Zend Expressive 2 wraps automatically, actually it is Zend Stratigility that does the wrapping, so it just works, nothing else needs to be done. https://github.com/zendframework/zend-stratigility/blob/master/src/MiddlewarePipe.php#L135 and https://github.com/zendframework/zend-stratigility/blob/master/src/MiddlewarePipe.php#L224

tuupola commented 7 years ago

@Xerkus that is what I thought.

@lpj145 Can you show example code how you initialise the middleware and an example curl request which fails. For example:

$ curl "https://api.example.com/foo" \
    --request OPTIONS \
    --include \
    --header "Origin: http://www.example.com" \
    --header "Access-Control-Request-Method: PUT" \
    --header "Access-Control-Request-Headers: Authorization, If-Match"
lpj145 commented 7 years ago

image

lpj145 commented 7 years ago

image

tuupola commented 7 years ago

Copy pasting code is preferred over screenshots. When testing copy paste my curl command exactly except the url. For example you should use --header "Origin: http://www.example.com" not --header "Origin: http//:www.example.com"

lpj145 commented 7 years ago

curl lvh.me/ -v --request OPTIONS --include --header "Origin: http://www.example.com" --header "Access-Control-Request-Method: PUT"

tuupola commented 7 years ago

You can use three ` characters to format the code.

https://guides.github.com/features/mastering-markdown/

lpj145 commented 7 years ago

image

curl lvh.me/ -v --request OPTIONS --include --header "Origin: ht" --header "Access-Control-Request-Method: PUT"

lpj145 commented 7 years ago

I'm very grateful for that.

tuupola commented 7 years ago

I just tested with the latest Expressive and everything seems to be working as exptected. Code copied from examples.

<?php

use Interop\Http\ServerMiddleware\DelegateInterface;
use Zend\Diactoros\Response\TextResponse;
use Zend\Expressive\AppFactory;

chdir(dirname(__DIR__));
require "vendor/autoload.php";

$app = AppFactory::create();

$app->get("/", function ($request, DelegateInterface $delegate) {
    return new TextResponse("Hello, world!");
});

$app->pipe(new Tuupola\Middleware\Cors([
    "origin" => ["*"],
    "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"],
    "headers.allow" => [],
    "headers.expose" => [],
    "credentials" => false,
    "cache" => 0,
]));

$app->pipeRoutingMiddleware();
$app->pipeDispatchMiddleware();
$app->run();
$ curl http://localhost:8081 --include --header "Origin: http://example.com"
HTTP/1.1 200 OK
Host: localhost:8081
Date: Wed, 20 Sep 2017 13:09:03 +0000
Connection: close
X-Powered-By: PHP/7.1.6
Content-Type: text/plain; charset=utf-8
Access-Control-Allow-Origin: http://example.com
Vary: Origin

Hello, world!