Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/detailyang/lua-resty-cors
It's the implement of CORS on OpenResty
https://github.com/detailyang/lua-resty-cors
cors resty
Last synced: 3 months ago
JSON representation
It's the implement of CORS on OpenResty
- Host: GitHub
- URL: https://github.com/detailyang/lua-resty-cors
- Owner: detailyang
- License: mit
- Created: 2016-10-10T12:04:04.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-05T12:29:26.000Z (about 6 years ago)
- Last Synced: 2024-02-13T21:48:35.609Z (9 months ago)
- Topics: cors, resty
- Language: Lua
- Homepage:
- Size: 20.5 KB
- Stars: 55
- Watchers: 6
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-resty - lua-resty-cors - Origin Resource Sharing (CORS) implementation for OpenResty (Libraries)
README
Name
====
lua-resty-cors# lua-resty-cors
It's the implement of [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) on OpenResty and
It backports the [nginx-http-cors](https://github.com/x-v8/ngx_http_cors_filter) to OpenRestyTable of Contents
-----------------
* [Name](#name)
* [Status](#status)
* [Usage](#usage)
* [API](#api)
* [Contributing](#contributing)
* [Author](#author)
* [License](#license)Status
====
[![Build Status](https://travis-ci.org/detailyang/lua-resty-cors.svg?branch=master)](https://travis-ci.org/detailyang/lua-resty-cors)Usage
====
It may be placed on the nginx http block for a global CORS config or in each server block to configure a different CORS for each virtual host as the following:```bash
http {
init_by_lua_block {
local cors = require('lib.resty.cors');cors.allow_host([==[.*\.google\.com]==])
cors.allow_host([==[.*\.facebook\.com]==])
cors.expose_header('x-custom-field1')
cors.expose_header('x-custom-field2')
cors.allow_method('GET')
cors.allow_method('POST')
cors.allow_method('PUT')
cors.allow_method('DELETE')
cors.allow_header('x-custom-field1')
cors.allow_header('x-custom-field2')
cors.max_age(7200)
cors.allow_credentials(false)
}
header_filter_by_lua_block {
local cors = require('lib.resty.cors');
cors.run()
}
}
```API
====allow_host
---
`syntax: cors.allow_host(host)`This will match the host from cors request then be added to the header Access-Control-Allow-Origin like as the following:
```bash
Request:
Origin: https://www.google.comResponse:
Access-Control-Allow-Origin: http://www.google.com
```expose_header
---
`syntax: cors.expose_header(header)`This will be added to the header Access-Control-Expose-Headers like as the following:
```bash
Request:
Origin: https://www.google.comResponse:
Access-Control-Expose-Headers: x-custom-field1,x-custom-field2
```allow_method
---
`syntax: cors.allow_method(method)`This will be added to the header Access-Control-Allow-Methods like as the following:
```bash
Request:
Origin: https://www.google.comResponse:
Access-Control-Allow-Methods:GET,POST,PUT
```allow_header
---
`syntax: cors.allow_header(header)`This will be added to the header Access-Control-Allow-Headers like as the following:
```bash
Request:
Origin: https://www.google.comResponse:
Access-Control-Allow-Headers:x-custom-field1,x-custom-field2
```max_age
---
`syntax: cors.max_age(age)`This will be added to the header Access-Control-Max-Age like as the following:
```bash
Request:
Origin: https://www.google.comResponse:
Access-Control-Max-Age: 7200
```Allow-Credentials
---
`syntax: cors.allow_credentials(true or false)`This will be added to the header Access-Control-Allow-Credentials like as the following:
```bash
Request:
Origin: https://www.google.comResponse:
Access-Control-Allow-Credentials: true
```run
---
`syntax: cors.run()`This is the entry for lua-resty-cors to run
Contributing
------------To contribute to lua-resty-cors, clone this repo locally and commit your code on a separate branch.
PS: PR Welcome :rocket: :rocket: :rocket: :rocket:
Author
------> GitHub [@detailyang](https://github.com/detailyang)
License
-------
lua-resty-cors is licensed under the [MIT] license.[MIT]: https://github.com/detailyang/ybw/blob/master/licenses/MIT