Git Product home page Git Product logo

cowmachine's Introduction

Test Hex.pm version Hex.pm Downloads Hex.pm Documentation Erlang Versions License

Cowmachine

Webmachine for Zotonic and Cowboy

This is an adaptation of https://github.com/webmachine/webmachine for the Cowboy web server.

Cowmachine is a request handler for Cowboy.

Main differences with Basho’s Webmachine are:

  • Use cowboy instead of mochiweb for the http server
  • Separate dispatching
  • Simplified callbacks
  • Single process/4 callback for all http methods
  • Caching of some callbacks (like modified)
  • More streaming options for returned data
  • Better support for range requests

Installation

Cowmachine is at Hex, in your rebar.config file use:

{deps, [
    cowmachine
]}.

You can also use the direct Git url and use the development version:

{deps, [
    {cowmachine, {git, "https://github.com/zotonic/cowmachine.git", {branch, "master"}}}
]}.

Calling Cowmachine

Cowmachine can be called from your Cowboy middleware:

-spec execute(Req, Env) -> {ok, Req, Env} | {stop, Req}
    when Req::cowboy_req:req(), Env::cowboy_middleware:env().
execute(Req, Env) ->
    % Replace below with your own controller module and optionally wrap
    % the request in your own request-context record or map.
    EnvController = Env#{
        cowmachine_controller => mycontroller
    },
    RequestContext = cowmachine_req:init_context(Req, EnvController, #{}),
    % Set options for the cowmachine
    Options = #{
        on_request =>
            fun(Ctx) ->
                % Perform anything after initialization of your request
                % Examples are checking special cookies, changing headers, etc.
                Ctx
            end,
        on_welformed =>
            fun(Ctx) ->
                % Perform anything after well-formedness check of your request
                % Examples are parsing the query args, or authentication
                Ctx
            end,
        on_handled =>
            fun(Ctx) ->
                % Perform anything after processing, before sending the result.
                Ctx
            end
    },
    % Handle the request, returns updated Req and Env for next Cowboy middleware
    cowmachine:request(RequestContext, Options).

Or just use the default Cowmachine middleware:

    #{
        middlewares => [
            % ... add your dispatcher middlware
            cowmachine
        ],
        request_timeout => 60000,
        env => #{
            % If no dispatcher, default to `mycontroller` as the cowmachine
            % controller.
            cowmachine_controller => mycontroller
        }
    }.

Dispatching

You can use the Zotonic Dispatch Compiler to match your controller paths against the request.

Controller

The controller provides the callbacks to handle the request.

The controller is an Erlang module implementing callback functions, only functions that return some non-default value need to be implemented.

For the controller callbacks and more documentation, check the wiki pages.

Documentation generation

Edoc

Generate public API

rebar3 edoc

Generate private API

rebar3 as edoc_private edoc

ExDoc

rebar3 ex_doc --logo doc/img/logo.png --output edoc -f html

cowmachine's People

Contributors

ddeboer avatar mmzeeman avatar mworrell avatar onnet avatar rustkas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cowmachine's Issues

Crash with search param ?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

In the logs I’m seeing a (silent) crash:

2022-01-04 00:26:07.698 [error] <0.26448.8> Cowboy stream 1 with ranch listener zotonic_https_listener_ipv4 
and connection process <0.26442.8> had its request process exit with reason: {nocatch,{stop_request,500,
{function_clause,[{cowmachine_util,parse_qs_name,[<<"=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000">>,[],<<>>],[
  {file,"/home/zotonic/zotonic/_build/default/lib/cowmachine/src/cowmachine_util.erl"},{line,284}]},
  {z_context,ensure_qs,1,[{file,"/home/zotonic/zotonic/apps/zotonic_core/src/support/z_context.erl"},{line,
  523}]},{z_cowmachine_middleware,'-execute/2-fun-0-',1,[{file,"/home/zotonic/zotonic/apps/zotonic_core/src/
  support/z_cowmachine_middleware.erl"},{line,54}]},{cowmachine_decision_core,...},...]}}} in 
  cowmachine_decision_core:handle_request/2 line 40

This is caused by search param ?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000 which is an easter egg in PHP applications - it is a common way to check if the server exposes sensitive data.

Make controller_http_error configurable

Also test if controller_http_error exists.

Maybe provide a default controller in cowmachine and add a configuration to set an application specific error controller?

Publish action is broken

The publish action shows a repeating error:

Proceed? ("Y")> I didn't get that. This boolean kind of question.

And won't finish.

Wiki documentation. Controller-Functions

  • There is strange -spec description of known_methods (Another name for the function). It is very likely that this is a typo.
  • content_types_provided. The selected content type can be found with: cowmachine_req:resp_content_type(Context) instead of cowmachine_req:resp_content_type(Request).
  • charsets_provided. The selected character set can be found with: cowmachine_req:resp_chosen_charset(Context) instead of cowmachine_req:resp_chosen_charset(Request).

Check why cowboy sometimes responds with a 204 instead of a 500

When an error happens during responding, cowboy sometimes returns with a 204 response instead of the expected 500 response we are requesting.

Steps to reproduce..

Make a bad match in cowmachine_response:server_header/0, and check the server response in cowmachine_test:cowmachine_start_test/0

Publish this package to Hex

Currently, publishing to Hex fails because

cowboy, cowlib, mochiweb, parse_trans, ranch, sendfile

are not published on Hex.

  • I switched parse_trans to the Hex dep.
  • Removed sendfile (#2).
  • Cowboy, cowlib and ranch will be fine with Cowboy 2 is released.
  • Perhaps we should try to move away from Mochiweb (we only rely on some small parts of it anyway).

Make sure custom websocket handlers are loaded

Cowboy loads custom websocket handlers via cowboy_handler:execute/2 it calls an extra init export. Cowmachine can use custom websocket handlers, but it doesn't call the init/2 export of the handler. Therefore it will not automatically load the handler module. This leads to an initialization problem. You will only notice this when you need special websocket handlers.

Add test cases

We need test cases for whole requests with different test controllers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.