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

Fixed cors origin port issue #59

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d8d24b1
.gitignore: add .eunit and .rebar
Ledest Mar 13, 2016
27e46b0
leptus: replace depracated erlang:now/0 to os:timestamp/0
Ledest Mar 13, 2016
5e06198
leptus_req: remove obsoleted cowboy_req's parameter 'infinity' in bod…
Ledest Apr 4, 2016
7704a34
leptus_req: add qs_vals/1
Ledest Aug 26, 2015
43f4247
leptus_pt: update check_clauses/1
Ledest Aug 26, 2015
0335692
.gitignore: add .rebar
Ledest Aug 26, 2015
5222a7e
leptus: add ssl and its deps to ensure_deps_started/0
Ledest Dec 12, 2016
9c0b2ff
remove reloader
Ledest Aug 3, 2016
370278c
remove support of jiffy JSON encode/decoder
Ledest Aug 3, 2016
a584988
leptus: add callbacks description for 'leptus' behaviour
Ledest Aug 3, 2016
59eb766
rebar.config: update deps
Ledest Aug 3, 2016
e489752
leptus_req: fix unpack/decode JSON and msgpack
Ledest Mar 2, 2017
a725e5f
leptus_pt: generate specs for routes/0 and allowed_methods/1
Ledest Oct 20, 2016
2c670c7
Fixed cors origin port issue
lukyanov Apr 10, 2017
ffc0111
Add new CORS-specific callback `allowed_headers`
lukyanov Apr 10, 2017
f77f23c
Treat "application/json; charset=utf-8" as json as well
lukyanov Apr 11, 2017
b870cd1
leptus_req: update body/1 to proper support "application/json", "appl…
Ledest Jun 29, 2017
0b1e5c4
leptus.app: clean up
Ledest Jun 29, 2017
6eb0495
update .travis.yml
Ledest Jul 4, 2017
9d9d0be
rebar.config: update msgpack deps
Ledest Jul 4, 2017
4e7f382
Revert "rebar.config: update msgpack deps"
Ledest Jul 4, 2017
e7d9405
rebar.config: update deps
Ledest Jul 4, 2017
bb36fc1
add rebar.config.script
Ledest Jul 4, 2017
89e8aed
rebar.config: update msgpack deps
Ledest Jul 4, 2017
eb1daac
update rebar.config.script
Ledest Jul 4, 2017
f90b658
.travis.yml: remove R15B03-1
Ledest Jul 9, 2017
d16c5e0
leptus_handler: replace application/x-msgpack to application/msgpack …
Ledest Jul 10, 2017
75e340e
README.org: update travis-ci icon
Ledest Jul 22, 2017
478d9eb
replace 'application/x-msgpack' to 'application/msgpack' in tests and…
Ledest Sep 20, 2017
0de0507
add support 'application/erlang' content type
Ledest Sep 20, 2017
627e021
leptus_handler: fix msgpack packer options (for msgpack-erlang >= 0.5)
Ledest Sep 20, 2017
92b5440
requre Erlang/OTP >= 18; remove workarounds for Erlang/OTP < 18
Ledest Oct 3, 2017
f8692b2
update .travis.yml
Ledest Oct 3, 2017
c5d9169
update README.org
Ledest Oct 3, 2017
9216851
README: update cowboy's URL
Ledest Oct 3, 2017
e107f08
Merge branch 'master' of https://github.com/Ledest/leptus into Ledest…
lukyanov Nov 8, 2017
aad4fa4
Merge branch 'Ledest-master'
lukyanov Nov 8, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.rebar
ebin
deps
*.beam
Expand All @@ -7,6 +8,8 @@ erl_crash.dump
docs/public_html
docs/tmp
docs/elisp
.eunit
.rebar
*~
\#*
.\#*
15 changes: 4 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
language: erlang
otp_release:
- R16B
- R15B03
- R15B02
- R15B01
- R15B
notifications:
email: false
irc: "chat.freenode.net#leptus"
on_success: change
on_failure: change
install: "make deps"
script: "make test"
otp_release:
- 20.1
- 19.3
- 18.3
19 changes: 4 additions & 15 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
* Leptus [[https://travis-ci.org/s1n4/leptus][https://travis-ci.org/s1n4/leptus.png?branch=master]]
* Leptus [[https://travis-ci.org/Ledest/leptus][https://travis-ci.org/Ledest/leptus.png]]

Leptus is an Erlang REST framework that runs on top of cowboy.

Leptus aims at simply creating RESTful APIs.

** Requirements

- Erlang/OTP R15B or newer
- [[https://github.com/extend/cowboy][cowboy]]
- [[https://github.com/davisp/jiffy][jiffy]] or [[https://github.com/talentdeficit/jsx][jsx]]
- Erlang/OTP 18 or newer
- [[https://github.com/ninenines/cowboy][cowboy]]
- [[https://github.com/talentdeficit/jsx][jsx]]
- [[https://github.com/msgpack/msgpack-erlang][msgpack]]

** Installation
Expand All @@ -27,17 +27,6 @@
]}.
#+END_SRC

NOTE: if you prefer [[https://github.com/talentdeficit/jsx][jsx]] rather than [[https://github.com/davisp/jiffy][jiffy]]
the environment variable ~USE_JSX~ must be set to ~true~ when getting
dependencies and/or compiling.

i.e.
#+BEGIN_SRC
USE_JSX=true make
# OR
USE_JSX=true rebar get-deps compile
#+END_SRC

** Quick example

#+BEGIN_SRC erlang
Expand Down
2 changes: 1 addition & 1 deletion docs/leptus_req.org
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
%% e.g.
body(Req) -> <<"foo=bar">>

%% when content-type is set to applicaation/json or application/x-msgpack
%% when content-type is set to applicaation/json or application/msgpack
body(Req) -> [{<<"function">>, <<"body/1">>}]
#+END_SRC

Expand Down
11 changes: 4 additions & 7 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
%%-*- mode: erlang -*-
{require_min_otp_vsn, "18"}.

{deps, [
{cowboy, ".*", {git, "git://github.com/extend/cowboy.git", {tag, "0.9.0"}}},
{jiffy, ".*", {git, "git://github.com/davisp/jiffy.git", {tag, "0.8.5"}}},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naive question.. what is the effect of removing jiffy here? I am using a combination of leptus and Jiffy in a few projects; is Jiffy completely interchangeable with msgpack, such that jiffy + leptus 1.0.0 (your proposal here) is fine?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I didn't meant this PR be such big. It is actually my mistake as I initially made a PR from my master branch. Now I added a lot of stuff to my master and it all appeared here. I'm going to close this PR.

As regards to your question, jiffy has been removed as an alternative to jsx (https://github.com/talentdeficit/jsx.git). leptus originally supported two different JSON libraries, in my changes it supports only one (jsx). msgpack is still supported as before.

{jsx, ".*", {git, "git://github.com/talentdeficit/jsx.git", {tag, "v1.4.5"}}},
{msgpack, ".*", {git, "git://github.com/msgpack/msgpack-erlang.git", {tag, "0.2.8"}}}
]}.
{deps, [{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", {branch, "1.0.x"}}},
{jsx, ".*", {git, "https://github.com/talentdeficit/jsx.git", {branch, "master"}}},
{msgpack, ".*", {git, "https://github.com/msgpack/msgpack-erlang.git", {branch, "master"}}}]}.

{clean_files, ["*.beam", "test/*.beam", "erl_crash.dump"]}.
57 changes: 0 additions & 57 deletions rebar.config.script

This file was deleted.

27 changes: 7 additions & 20 deletions src/leptus.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,10 @@
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.

{application, leptus,
[
{description, "REST framework that runs on top of cowboy"},
{vsn, "0.4.2"},
{registered, []},
{applications, [
kernel,
stdlib,
cowboy
]},
{mod, {leptus_app, []}},
{env, []},
{modules, [
leptus, leptus_app, leptus_sup,
leptus_handler, leptus_router, leptus_pt,
leptus_config, leptus_req_sup, leptus_req,
leptus_json, leptus_utils, leptus_logger,
leptus_access_log
]}
]}.
{application, leptus, [{description, "REST framework that runs on top of cowboy"},
{vsn, "1.0.0"},
{registered, []},
{applications, [kernel, stdlib, cowboy]},
{mod, {leptus_app, []}},
{env, []},
{modules, []}]}.
17 changes: 12 additions & 5 deletions src/leptus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@
-export_type([listener/0]).
-export_type([options/0]).

-callback init(Route::string(), Req::pid(), State::any()) -> {ok, any()} | {error, any()}.
-callback terminate(Reason::any(), Route::string(), Req::pid(), State::any()) -> ok.
-callback routes() -> [string()].
-callback allowed_methods(Route::string()) -> [binary()].
-callback get(Route::string(), Req::pid(), State::any()) -> {ok, any(), any()} | {error, any()}.
-callback put(Route::string(), Req::pid(), State::any()) -> {ok, any(), any()} | {error, any()}.
-callback post(Route::string(), Req::pid(), State::any()) -> {ok, any(), any()} | {error, any()}.
-callback delete(Route::string(), Req::pid(), State::any()) -> {ok, any(), any()} | {error, any()}.
-callback options(Route::string(), Req::pid(), State::any()) -> {ok, any(), any()} | {error, any()}.
-optional_callbacks([get/3, put/3, post/3, delete/3, options/3]).

%% -----------------------------------------------------------------------------
%% start a listener
Expand Down Expand Up @@ -251,10 +261,7 @@ ensure_started(App) ->
%% -----------------------------------------------------------------------------
-spec ensure_deps_started() -> ok.
ensure_deps_started() ->
ensure_started(crypto),
ensure_started(ranch),
ensure_started(cowlib),
ensure_started(cowboy).
lists:foreach(fun(A) -> ok = ensure_started(A) end, [crypto, asn1, public_key, ssl, ranch, cowlib, cowboy]).

-spec opt(atom(), options(), Default) -> any() | Default when Default :: any().
opt(Key, [{Key, Value}|_], _) ->
Expand Down Expand Up @@ -286,7 +293,7 @@ print_info(Listener, IP, Port) ->
update_listener_bucket({Listener, {Handlers, Opts}}) ->
%% [{Listener, Bucket}]
Bucket = #listener_bucket{handlers = Handlers, options = Opts,
started_timestamp = now()},
started_timestamp = os:timestamp()},
Listeners = leptus_config:lookup(listeners, []),
Listeners1 = lists:keystore(Listener, 1, Listeners, {Listener, Bucket}),
leptus_config:set(listeners, Listeners1).
36 changes: 25 additions & 11 deletions src/leptus_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
-type req() :: pid().
-type status() :: non_neg_integer() | binary() | atom().
-type headers() :: cowboy:http_headers().
-type body() :: binary() | string() | {json | msgpack, leptus_json:json_term()}
-type body() :: binary() | string() | {json | msgpack, jsx:json_term()} | {erlang, term()}
| {html, binary()}.
-type method() :: get | put | post | delete.
-type response() :: {body(), handler_state()}
| {status(), body(), handler_state()}
| {status(), headers(), body(), handler_state()}.
-type terminate_reason() :: normal | not_allowed | unauthenticated
| no_permission | {error, any()}.
-type data_format() :: text | json | msgpack | html.
-type data_format() :: text | erlang | json | msgpack | html.
-type status_code() :: 100..101 | 200..206 | 300..307 | 400..417 | 500..505.

-export_type([status/0]).
Expand Down Expand Up @@ -288,7 +288,7 @@ method_not_allowed(Handler, Route, HandlerState) ->

-spec allowed_methods(handler(), route()) -> binary().
allowed_methods(Handler, Route) ->
join_http_methods(Handler:allowed_methods(Route)).
join_with_comma(Handler:allowed_methods(Route)).

%% -----------------------------------------------------------------------------
%% Handler:cross_domains/3
Expand Down Expand Up @@ -344,10 +344,21 @@ is_preflight(Req) ->
-spec cors_headers(handler(), route(), binary(), req()) -> headers().
cors_headers(Handler, Route, Origin, Req) ->
AccessControlAllowOrigin = {<<"access-control-allow-origin">>, Origin},
AccessControlAllowHeaders =
case erlang:function_exported(Handler, allowed_headers, 2) of
true ->
Method = http_method(leptus_req:header(Req,
<<"access-control-request-method">>)),
AllowedHeaders = Handler:allowed_headers(Method, Route),
[{<<"access-control-allow-headers">>,
join_with_comma(AllowedHeaders)}];
false -> []
end,
case is_preflight(Req) of
true ->
[AccessControlAllowOrigin|[{<<"access-control-allow-methods">>,
allowed_methods(Handler, Route)}]];
allowed_methods(Handler, Route)}]]
++ AccessControlAllowHeaders;
false ->
[AccessControlAllowOrigin]
end.
Expand Down Expand Up @@ -407,10 +418,12 @@ reply(Status, Headers, Body, Req) ->
leptus_req:reply(Req, Status, Headers, Body).

-spec prepare_headers_body(headers(), body()) -> {headers(), body()}.
prepare_headers_body(Headers, {erlang, Body}) ->
{maybe_set_content_type(erlang, Headers), term_to_binary(Body)};
prepare_headers_body(Headers, {json, Body}) ->
{maybe_set_content_type(json, Headers), leptus_json:encode(Body)};
{maybe_set_content_type(json, Headers), jsx:encode(Body)};
prepare_headers_body(Headers, {msgpack, Body}) ->
{maybe_set_content_type(msgpack, Headers), msgpack:pack({Body}, [jiffy])};
{maybe_set_content_type(msgpack, Headers), msgpack:pack(Body, [{map_format, jsx}])};
prepare_headers_body(Headers, {html, Body}) ->
{maybe_set_content_type(html, Headers), Body};
prepare_headers_body(Headers, Body) ->
Expand All @@ -430,8 +443,9 @@ maybe_set_content_type(Type, Headers) ->
-spec content_type(data_format()) -> binary().
content_type(text) -> <<"text/plain">>;
content_type(html) -> <<"text/html">>;
content_type(erlang) -> <<"application/erlang">>;
content_type(json) -> <<"application/json">>;
content_type(msgpack) -> <<"application/x-msgpack">>.
content_type(msgpack) -> <<"application/msgpack">>.

%% -----------------------------------------------------------------------------
%% HTTP status code bindings
Expand Down Expand Up @@ -485,10 +499,10 @@ status(gateway_timeout) -> 504;
status(http_version_not_supported) -> 505;
status(A) -> A.

-spec join_http_methods([binary()]) -> binary().
join_http_methods(Methods) ->
<<", ", Allow/binary>> = << <<", ", M/binary>> || M <- Methods >>,
Allow.
-spec join_with_comma([binary()]) -> binary().
join_with_comma(List) ->
<<", ", Res/binary>> = << <<", ", E/binary>> || E <- List >>,
Res.

-spec compile_host(string() | binary()) -> [[binary() | atom()]] | [atom()].
compile_host(HostMatch) ->
Expand Down
82 changes: 0 additions & 82 deletions src/leptus_json.erl

This file was deleted.

Loading