cowboy_req modifiers
leonardb opened this issue · 3 comments
Forgive me if my understanding is incorrect, but the majority of the cowboy_req: methods return a cowboy_req as well as the data, for example, reading the body_qs or dealing with multi-part form data.
From cowboy_req.erl
%% Whenever <em>Req</em> is returned, it should always be kept in place of
%% the one given as argument in your function call, because it keeps
%% track of the request and response state. Doing so allows Cowboy to do
%% some lazy evaluation and cache results when possible.
With leptus we have no way of modifying the Req as it is not returned by any of the handlers. So any modifications/updates to the Req are discarded.
This means for example, that cookies cannot be set/updated (as they require modifying the headers 'in a special way') and multipart form data cannot be reliably read (as this is a stream which can only be read once.
Maybe it would be possible to provide an alternate return format to support modified Req?
This would most likely be needed at any point the Req might be modified. The ones that stand out are:
Handler:init/3
Handler:Method/3
Handler:is_authenticated/3
In each of those case the returned Req would have to be passed to the next call
Leptus' principle is simple (I have to clarify it in README though):
you get a request and you should give a response without manipulating the Req (which is why functions in leptus don't return Req), and the response can contain Status, Headers and Body.
I didn't keep that note from cowboy_req.erl
in mind, my bad.
We don't need to get/set cookies since it's a REST framework, but we've got Headers, we can parse them through the Req, or set cookies and returning them through the Headers.
Actually, I don't get the point of your speech:
...multipart form data cannot be reliably read...
Having an alternate return format might be cool, so I keep this issue open.
Thanks.
How about providing a bunch of functions to deal with Headers?
#12 (comment)
leptus_req provides get_req/1
and set_req/2
that basically allows modifying Req object.