Skip to content

Commit e5b4097

Browse files
committedAug 1, 2017
Improved CORS support
1 parent 97c8ecc commit e5b4097

File tree

8 files changed

+258
-139
lines changed

8 files changed

+258
-139
lines changed
 

‎.gitlab-ci.yml

+30-31
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
before_script:
2-
- bash ci/docker_install.sh
3-
4-
test_5.4:
5-
image : phpplatform/php-apache-xdebug-composer:5.4
6-
script :
7-
- ./vendor/bin/phpunit -c ci/phpunit.xml
8-
9-
test_5.5:
10-
image : phpplatform/php-apache-xdebug-composer:5.5
11-
script :
12-
- ./vendor/bin/phpunit -c ci/phpunit.xml
13-
14-
test_5.6:
15-
image : phpplatform/php-apache-xdebug-composer:5.6
16-
script :
17-
- ./vendor/bin/phpunit -c ci/phpunit.xml
18-
19-
test_7.0:
20-
image : phpplatform/php-apache-xdebug-composer:7.0
21-
script :
22-
- ./vendor/bin/phpunit -c ci/phpunit.xml
23-
24-
test_7.1:
25-
image : phpplatform/php-apache-xdebug-composer:7.1
26-
script :
27-
- ./vendor/bin/phpunit -c ci/phpunit.xml
28-
29-
after_script:
30-
- bash <(curl -s https://codecov.io/bash) -t c09c03aa-44ab-4b07-934e-f20fd3a604c1
31-
- cat /var/log/apache2/error.log
1+
before_script:
2+
- bash ci/docker_install.sh
3+
4+
test_5.4:
5+
image : phpplatform/php-apache-xdebug-composer:5.4
6+
script :
7+
- ./vendor/bin/phpunit -c ci/phpunit.xml
8+
9+
test_5.5:
10+
image : phpplatform/php-apache-xdebug-composer:5.5
11+
script :
12+
- ./vendor/bin/phpunit -c ci/phpunit.xml
13+
14+
test_5.6:
15+
image : phpplatform/php-apache-xdebug-composer:5.6
16+
script :
17+
- ./vendor/bin/phpunit -c ci/phpunit.xml
18+
19+
test_7.0:
20+
image : phpplatform/php-apache-xdebug-composer:7.0
21+
script :
22+
- ./vendor/bin/phpunit -c ci/phpunit.xml
23+
24+
test_7.1:
25+
image : phpplatform/php-apache-xdebug-composer:7.1
26+
script :
27+
- ./vendor/bin/phpunit -c ci/phpunit.xml
28+
29+
after_script:
30+
- bash <(curl -s https://codecov.io/bash) -t c09c03aa-44ab-4b07-934e-f20fd3a604c1

‎README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,36 @@ CORS configurations enables CORS (Cross Origin Resource Sharing) authentication
168168

169169
``` JSON
170170
"CORS":{
171-
"AllowedOrigins":[
171+
"AllowOrigins":[
172172
],
173-
"AllowedMethods":[
173+
"AllowMethods":[
174174
],
175-
"AllowedHeaders":[
175+
"AllowHeaders":[
176176
],
177177
"AllowCredentials":false,
178178
"MaxAge":1000
179179
}
180180
```
181181

182-
Details of these can be found in [https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)
182+
Details of these can be found in [https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)
183+
184+
##### Access-Control-* Headers
185+
186+
* **Access-Control-Allow-Origin**
187+
This header is set to `origin` header in the request only if that `origin` is listed in `CORS.AllowOrigins` configuration
188+
189+
* **Access-Control-Allow-Methods**
190+
Comma seperated Methods configured in `CORS.AllowMethods`
191+
192+
* **Access-Control-Allow-Headers**
193+
Comma seperated Headers configured in `CORS.AllowHeaders`
194+
195+
* **Access-Control-Allow-Credentials**
196+
True/False configured in `CORS.AllowCredentials`
197+
198+
* **Access-Control-Max-Age**
199+
number of seconds configured in `CORS.MaxAge`
200+
201+
* **Access-Control-Exposed-Headers**
202+
Comma separated names of headers explicitely set in HTTPResponse object from the service
203+

‎config.json

+32-35
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
11
{
2-
"serializers":{
3-
"string":{
4-
"text/plain":"PhpPlatform\\RESTFul\\Serialization\\StringToPlainTextSerialization"
2+
"serializers": {
3+
"string": {
4+
"text/plain": "PhpPlatform\\RESTFul\\Serialization\\StringToPlainTextSerialization"
55
},
6-
"array":{
7-
"application/json":"PhpPlatform\\RESTFul\\Serialization\\ArrayToJsonSerialization"
6+
"array": {
7+
"application/json": "PhpPlatform\\RESTFul\\Serialization\\ArrayToJsonSerialization"
88
},
9-
"SimpleXMLElement":{
10-
"application/xml":"PhpPlatform\\RESTFul\\Serialization\\SimpleXMLElementToXmlSerialization"
9+
"SimpleXMLElement": {
10+
"application/xml": "PhpPlatform\\RESTFul\\Serialization\\SimpleXMLElementToXmlSerialization"
1111
}
1212
},
13-
"deserializers":{
14-
"application/json":{
15-
"array":"PhpPlatform\\RESTFul\\Serialization\\ArrayToJsonSerialization"
13+
"deserializers": {
14+
"application/json": {
15+
"array": "PhpPlatform\\RESTFul\\Serialization\\ArrayToJsonSerialization"
1616
},
17-
"application/xml":{
18-
"SimpleXMLElement":"PhpPlatform\\RESTFul\\Serialization\\SimpleXMLElementToXmlSerialization"
17+
"application/xml": {
18+
"SimpleXMLElement": "PhpPlatform\\RESTFul\\Serialization\\SimpleXMLElementToXmlSerialization"
1919
},
20-
"multipart/form-data":{
21-
"array":"PhpPlatform\\RESTFul\\Serialization\\MultiPartFormToArrayDeserialization"
20+
"multipart/form-data": {
21+
"array": "PhpPlatform\\RESTFul\\Serialization\\MultiPartFormToArrayDeserialization"
2222
},
23-
"application/x-www-form-urlencoded":{
24-
"array":"PhpPlatform\\RESTFul\\Serialization\\FormToArrayDeserialization"
23+
"application/x-www-form-urlencoded": {
24+
"array": "PhpPlatform\\RESTFul\\Serialization\\FormToArrayDeserialization"
2525
},
26-
"text/plain":{
27-
"string":"PhpPlatform\\RESTFul\\Serialization\\StringToPlainTextSerialization"
26+
"text/plain": {
27+
"string": "PhpPlatform\\RESTFul\\Serialization\\StringToPlainTextSerialization"
2828
}
2929
},
30-
"routes":{
31-
30+
"routes": {
31+
3232
},
33-
"recaptcha":{
34-
"enable":false,
35-
"secret":"",
36-
"url":"https://www.google.com/recaptcha/api/siteverify"
37-
},
38-
"CORS":{
39-
"AllowedOrigins":[
40-
],
41-
"AllowedMethods":[
42-
],
43-
"AllowedHeaders":[
44-
],
45-
"AllowCredentials":false,
46-
"MaxAge":1000
47-
}
33+
"recaptcha": {
34+
"enable": false,
35+
"secret": "",
36+
"url": "https://www.google.com/recaptcha/api/siteverify"
37+
},
38+
"CORS": {
39+
"AllowOrigins": [],
40+
"AllowMethods": [],
41+
"AllowHeaders": [],
42+
"AllowCredentials": false,
43+
"MaxAge": 1000
44+
}
4845
}

‎src/RESTFul/CORS/PreFlight.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace PhpPlatform\RESTFul\CORS;
4+
5+
use PhpPlatform\RESTFul\RESTService;
6+
use PhpPlatform\RESTFul\HTTPResponse;
7+
8+
/**
9+
* @Path "/"
10+
*/
11+
class PreFlight implements RESTService {
12+
13+
/**
14+
* @Path "/"
15+
* @OPTIONS
16+
*/
17+
function preFlight(){
18+
return new HTTPResponse();
19+
}
20+
}

‎src/RESTFul/HTTPResponse.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ function setData($data){
8383
*/
8484
function flush($httpAccept = null){
8585
try{
86-
// set reponse code and message
87-
header($_SERVER['SERVER_PROTOCOL']." ".$this->code." ".$this->message);
88-
86+
// set headers
8987
foreach ($this->headers as $name=>$value){
9088
header("$name:$value");
9189
}
9290

91+
// set reponse code and message
92+
header($_SERVER['SERVER_PROTOCOL']." ".$this->code." ".$this->message);
93+
9394
// clear buffer , if any
9495
if(ob_get_length() !== false){
9596
ob_clean();

0 commit comments

Comments
 (0)