Skip to content

Commit

Permalink
add naive fix for cowboy #441
Browse files Browse the repository at this point in the history
  • Loading branch information
dvv committed Feb 19, 2013
1 parent dff9916 commit 4b7ee8d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cowboy_patch

Middleware for easing exposing RESTful services, helps dealing with unfriendly intermediary network environment which sometimes disallow certain critical HTTP methods and/or headers.
This one allows to tunnel information via URI.
Please, consult [comments in the code](https://github.com/dvv/stable/blob/master/src/cowboy_patch.erl#L9-L18) so far.
Please, consult [comments in the code](https://github.com/dvv/stable/blob/master/src/cowboy_patch.erl#L9-L18) so far.

Should you put `cowboy_patch` in middleware chain, request will be updated automatically.

Expand Down Expand Up @@ -68,6 +68,14 @@ Platform = cowboy_ua:platform(UserAgentHeader).

Should you put `cowboy_ua` in middleware chain, handler options will be augmented with `{useragent, {Agent, Platform}}` tuple.

cowboy_msie_fix_accept
--------------

By default MSIE [sends](https://github.com/extend/cowboy/issues/441) `Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*` header which is inappropriate to make hypermedia choice of the least surprise -- it anticipates `text/html` mime as the last fallback.
This makes creating REST services somewhat difficult.

Should you put `cowboy_msie` in middleware chain after `cowboy_router` and `cowboy_ua`, `Accept: ` will be substituted with `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8` which seems what other browsers send by default.

cowboy_common_handler
--------------

Expand Down
27 changes: 27 additions & 0 deletions src/cowboy_msie_fix_accept.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
%%
%% @doc Modify default Accept: header coming from MSIE.
%%
%% Should be fixing https://github.com/extend/cowboy/issues/441
%%
%% NB: Apply after cowboy_router and cowboy_ua middlewares.
%%

-module(cowboy_msie_fix_accept).
-author('Vladimir Dronnikov <dronnikov@gmail.com>').

-behaviour(cowboy_middleware).
-export([execute/2]).

execute(Req, Env) ->
{handler_opts, HandlerOpts} = lists:keyfind(handler_opts, 1, Env),
case lists:keyfind(useragent, 1, HandlerOpts) of
{useragent, {ie, windows}} ->
{Headers, Req2} = cowboy_req:headers(Req),
Req3 = cowboy_req:set([{headers, [
{<<"accept">>,
<<"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8">>}
| Headers]}], Req2),
{ok, Req3, Env};
_Else ->
{ok, Req, Env}
end.
2 changes: 2 additions & 0 deletions src/cowboy_ua.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%%
%% @doc Simple user-agent determination
%%
%% NB: Apply after cowboy_router middleware
%%

-module(cowboy_ua).
-author('Vladimir Dronnikov <dronnikov@gmail.com>').
Expand Down

0 comments on commit 4b7ee8d

Please sign in to comment.