0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 04:30:25 +00:00

Remove the onrequest hook

It was redundant with middlewares. Allows us to save a few operations
for every incoming requests.
This commit is contained in:
Loïc Hoguin 2014-09-24 14:39:17 +03:00
parent 25259671f5
commit aa4d86b81f
12 changed files with 86 additions and 260 deletions

View file

@ -45,9 +45,6 @@ the halt tuple. The error tuple will therefore be removed.
### Hooks ### Hooks
The `onrequest` hook will be removed. It can easily be replaced
by a middleware.
The interface of the `onresponse` hook will change. There has The interface of the `onresponse` hook will change. There has
been a number of issues and added complexity with the current been a number of issues and added complexity with the current
interface that warrant fixing. The main problem is that the interface that warrant fixing. The main problem is that the

View file

@ -1,45 +1,7 @@
::: Hooks ::: Hooks
Cowboy provides two hooks. `onrequest` is called once the request Hooks allow the user to customize Cowboy's behavior during specific
line and headers have been received. `onresponse` is called just operations.
before sending the response.
:: Onrequest
The `onrequest` hook is called as soon as Cowboy finishes fetching
the request headers. It occurs before any other processing, including
routing. It can be used to perform any modification needed on the
request object before continuing with the processing. If a reply is
sent inside this hook, then Cowboy will move on to the next request,
skipping any subsequent handling.
This hook is a function that takes a request object as argument,
and returns a request object. This function MUST NOT crash. Cowboy
will not send any reply if a crash occurs in this function.
You can specify the `onrequest` hook when creating the listener,
inside the request options.
``` erlang
cowboy:start_http(my_http_listener, 100,
[{port, 8080}],
[
{env, [{dispatch, Dispatch}]},
{onrequest, fun ?MODULE:debug_hook/1}
]
).
```
The following hook function prints the request object everytime a
request is received. This can be useful for debugging, for example.
``` erlang
debug_hook(Req) ->
erlang:display(Req),
Req.
```
Make sure to always return the last request object obtained.
:: Onresponse :: Onresponse
@ -48,9 +10,9 @@ to the socket. It can be used for the purposes of logging responses,
or for modifying the response headers or body. The best example is or for modifying the response headers or body. The best example is
providing custom error pages. providing custom error pages.
Note that like the `onrequest` hook, this function MUST NOT crash. Note that this function MUST NOT crash. Cowboy may or may not send a
Cowboy may or may not send a reply if this function crashes. If a reply reply if this function crashes. If a reply is sent, the hook MUST
is sent, the hook MUST explicitly provide all headers that are needed. explicitly provide all headers that are needed.
You can specify the `onresponse` hook when creating the listener. You can specify the `onresponse` hook when creating the listener.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Before After
Before After

View file

@ -65,15 +65,15 @@
inkscape:pageopacity="1" inkscape:pageopacity="1"
inkscape:pageshadow="2" inkscape:pageshadow="2"
inkscape:zoom="1.4142136" inkscape:zoom="1.4142136"
inkscape:cx="229.71447" inkscape:cx="82.28271"
inkscape:cy="764.83183" inkscape:cy="764.83183"
inkscape:document-units="px" inkscape:document-units="px"
inkscape:current-layer="layer1" inkscape:current-layer="layer1"
showgrid="false" showgrid="false"
inkscape:window-width="1920" inkscape:window-width="2560"
inkscape:window-height="1014" inkscape:window-height="1402"
inkscape:window-x="0" inkscape:window-x="0"
inkscape:window-y="33" inkscape:window-y="38"
inkscape:window-maximized="1" inkscape:window-maximized="1"
inkscape:snap-global="true" inkscape:snap-global="true"
showguides="true"> showguides="true">
@ -93,7 +93,7 @@
<dc:format>image/svg+xml</dc:format> <dc:format>image/svg+xml</dc:format>
<dc:type <dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title> <dc:title />
</cc:Work> </cc:Work>
</rdf:RDF> </rdf:RDF>
</metadata> </metadata>
@ -101,42 +101,28 @@
inkscape:label="Layer 1" inkscape:label="Layer 1"
inkscape:groupmode="layer" inkscape:groupmode="layer"
id="layer1"> id="layer1">
<use
x="0"
y="0"
xlink:href="#path5757"
id="use5779"
transform="matrix(0.59961275,-0.80029029,0.80029029,0.59961275,-103.8895,437.48518)"
width="744.09448"
height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" />
<use
x="0"
y="0"
xlink:href="#path5757"
id="use5777"
transform="matrix(0.92125726,-0.38895379,0.38895379,0.92125726,-85.14742,176.0134)"
width="744.09448"
height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" />
<path <path
style="fill:none;stroke:#6d8e41;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:2, 4;stroke-dashoffset:0" inkscape:export-ydpi="89.926643"
d="m 188.5,231.36218 187,79"
id="path5757"
inkscape:connector-curvature="0"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643" inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" /> inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:connector-curvature="0"
id="use5779"
d="M 194.29441,340.67017 369.64493,238.3853"
style="fill:none;stroke:#6d8e41;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:2, 4;stroke-dashoffset:0" />
<path
inkscape:export-ydpi="89.926643"
inkscape:export-xdpi="89.926643"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:connector-curvature="0"
id="use5777"
d="m 178.49877,231.1517 203.00246,0.045"
style="fill:none;stroke:#6d8e41;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:1.99999999, 3.99999998;stroke-dashoffset:0" />
<use <use
x="0" x="0"
y="0" y="0"
xlink:href="#g5650" xlink:href="#g5650"
id="use5753" id="use5753"
transform="translate(475.11201,-33.017248)" transform="translate(475.11201,-117.70525)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -154,12 +140,12 @@
inkscape:export-xdpi="89.926643" inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" /> inkscape:export-ydpi="89.926643" />
<rect <rect
style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:#a9ca7d;stroke-width:2.44279908999999984;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0" style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:#a9ca7d;stroke-width:2.44279909;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
id="rect5367" id="rect5367"
width="207.05719" width="207.05719"
height="171.55719" height="171.55719"
x="43.721401" x="43.721401"
y="360.88528" y="276.1973"
rx="11.072577" rx="11.072577"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643" inkscape:export-xdpi="89.926643"
@ -169,34 +155,38 @@
y="0" y="0"
xlink:href="#g5650" xlink:href="#g5650"
id="use5654" id="use5654"
transform="translate(205.03261,53.351708)" transform="translate(205.03261,-31.336292)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643" inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" /> inkscape:export-ydpi="89.926643" />
<use <g
x="0"
y="0"
xlink:href="#use5656"
id="use5658"
transform="translate(0,-86.13396)"
width="744.09448"
height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" />
<use
x="0"
y="0"
xlink:href="#use5658"
id="use5660" id="use5660"
transform="translate(0,-87.519558)" transform="translate(205.03261,-207.5)">
width="744.09448" <path
height="1052.3622" style="opacity:0.8;fill:none;stroke:#6d8e41;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" d="m -57.78256,351.41962 0,52.3259"
inkscape:export-xdpi="89.926643" id="path3051"
inkscape:export-ydpi="89.926643" /> inkscape:connector-curvature="0" />
<path
sodipodi:type="star"
style="opacity:0.8;fill:#6d8e41;fill-opacity:1;fill-rule:nonzero;stroke:#6d8e41;stroke-width:0;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
id="path3053"
sodipodi:sides="3"
sodipodi:cx="-222.73865"
sodipodi:cy="415.25897"
sodipodi:r1="14.849242"
sodipodi:r2="7.4246211"
sodipodi:arg1="1.5707963"
sodipodi:arg2="2.6179939"
inkscape:flatsided="true"
inkscape:rounded="0"
inkscape:randomized="0"
d="m -222.73865,430.10821 -12.85982,-22.27386 25.71964,0 z"
inkscape:transform-center-y="2.1823437"
transform="matrix(0.58787746,0,0,0.58787746,73.160466,163.35774)" />
</g>
<use <use
x="0" x="0"
y="0" y="0"
@ -284,17 +274,6 @@
x="-224.02068" x="-224.02068"
y="274.53128" y="274.53128"
rx="15" /> rx="15" />
<use
x="0"
y="0"
xlink:href="#rect5273"
id="use5335"
transform="translate(318.97597,268.31614)"
width="744.09448"
height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" />
<use <use
x="0" x="0"
y="0" y="0"
@ -328,23 +307,20 @@
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643" inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" /> inkscape:export-ydpi="89.926643" />
<use <rect
x="0" rx="15"
y="0" y="386.37247"
xlink:href="#use5335" x="94.955292"
height="36.392323"
width="104.5895"
id="use5363" id="use5363"
transform="translate(0,173.33215)" style="fill:#d1f2a5;fill-opacity:1;fill-rule:nonzero;stroke:#a9ca7d;stroke-width:3;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0" />
width="744.09448"
height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643" />
<use <use
x="0" x="0"
y="0" y="0"
xlink:href="#use5361" xlink:href="#use5361"
id="use5365" id="use5365"
transform="translate(0,173.66424)" transform="translate(0,88.97624)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -393,7 +369,7 @@
xml:space="preserve" xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#77823c;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans" style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#77823c;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="146.53125" x="146.53125"
y="407.78009" y="323.0921"
id="text5371-5" id="text5371-5"
sodipodi:linespacing="125%" sodipodi:linespacing="125%"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -402,7 +378,7 @@
sodipodi:role="line" sodipodi:role="line"
id="tspan5373-0" id="tspan5373-0"
x="146.53125" x="146.53125"
y="407.78009">router</tspan></text> y="323.0921">router</tspan></text>
<text <text
xml:space="preserve" xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans" style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
@ -414,25 +390,11 @@
id="tspan5373-6" id="tspan5373-6"
x="-58.692513" x="-58.692513"
y="53.112247">some text</tspan></text> y="53.112247">some text</tspan></text>
<text
xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="147.00391"
y="321.39722"
id="text5371-2-3"
sodipodi:linespacing="125%"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
inkscape:export-xdpi="89.926643"
inkscape:export-ydpi="89.926643"><tspan
sodipodi:role="line"
id="tspan5373-6-7"
x="147.00391"
y="321.39722">onrequest</tspan></text>
<text <text
xml:space="preserve" xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans" style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="146.53125" x="146.53125"
y="495.07318" y="410.38519"
id="text5371-2-3-0" id="text5371-2-3-0"
sodipodi:linespacing="125%" sodipodi:linespacing="125%"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -441,11 +403,11 @@
sodipodi:role="line" sodipodi:role="line"
id="tspan5373-6-7-3" id="tspan5373-6-7-3"
x="146.53125" x="146.53125"
y="495.07318">handler</tspan></text> y="410.38519">handler</tspan></text>
<text <text
xml:space="preserve" xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans" style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="-446.99591" x="-362.30792"
y="63.078125" y="63.078125"
id="text5371-2-3-0-7" id="text5371-2-3-0-7"
sodipodi:linespacing="125%" sodipodi:linespacing="125%"
@ -455,7 +417,7 @@
inkscape:export-ydpi="89.926643"><tspan inkscape:export-ydpi="89.926643"><tspan
sodipodi:role="line" sodipodi:role="line"
id="tspan5373-6-7-3-9" id="tspan5373-6-7-3-9"
x="-446.99591" x="-362.30792"
y="63.078125">middlewares</tspan></text> y="63.078125">middlewares</tspan></text>
<text <text
xml:space="preserve" xml:space="preserve"
@ -487,7 +449,7 @@
y="0" y="0"
xlink:href="#rect5273-7" xlink:href="#rect5273-7"
id="use5668" id="use5668"
transform="translate(589.05532,207.03588)" transform="translate(589.05532,122.34788)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -498,7 +460,7 @@
y="0" y="0"
xlink:href="#rect5273" xlink:href="#rect5273"
id="use5670" id="use5670"
transform="translate(589.05538,355.27934)" transform="translate(589.05538,270.59134)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -509,7 +471,7 @@
y="0" y="0"
xlink:href="#use5355" xlink:href="#use5355"
id="use5672" id="use5672"
transform="translate(270.07946,434.91762)" transform="translate(270.07946,350.22962)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -520,7 +482,7 @@
y="0" y="0"
xlink:href="#text5371-4-0" xlink:href="#text5371-4-0"
id="use5674" id="use5674"
transform="translate(270.29655,434.16115)" transform="translate(270.29655,349.47315)"
width="744.09448" width="744.09448"
height="1052.3622" height="1052.3622"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -530,7 +492,7 @@
xml:space="preserve" xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#77823c;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans" style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#77823c;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="417.30829" x="417.30829"
y="321.42792" y="236.73991"
id="text5371-9" id="text5371-9"
sodipodi:linespacing="125%" sodipodi:linespacing="125%"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -539,12 +501,12 @@
sodipodi:role="line" sodipodi:role="line"
id="tspan5373-57" id="tspan5373-57"
x="417.30829" x="417.30829"
y="321.42792">reply</tspan></text> y="236.73991">reply</tspan></text>
<text <text
xml:space="preserve" xml:space="preserve"
style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans" style="font-size:16px;font-style:normal;font-weight:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;text-anchor:middle;fill:#6d8e41;fill-opacity:1;stroke:none;font-family:Sans;-inkscape-font-specification:Sans"
x="417.30829" x="417.30829"
y="407.77994" y="323.09195"
id="text5371-2-0" id="text5371-2-0"
sodipodi:linespacing="125%" sodipodi:linespacing="125%"
inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png" inkscape:export-filename="/home/essen/extend/cowboy/guide/http_req_resp.png"
@ -553,6 +515,6 @@
sodipodi:role="line" sodipodi:role="line"
id="tspan5373-6-8" id="tspan5373-6-8"
x="417.30829" x="417.30829"
y="407.77994">onresponse</tspan></text> y="323.09195">onresponse</tspan></text>
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 20 KiB

Before After
Before After

View file

@ -25,15 +25,6 @@ A binary status can be used to set a custom message.
HTTP version. HTTP version.
: onrequest_fun() = fun((cowboy_req:req()) -> cowboy_req:req())
Fun called immediately after receiving a request.
It can perform any operation on the Req object, including
reading the request body or replying. If a reply is sent,
the processing of the request ends here, before any middleware
is executed.
: onresponse_fun() = fun((http_status(), http_headers(), : onresponse_fun() = fun((http_status(), http_headers(),
iodata(), cowboy_req:req()) -> cowboy_req:req()) iodata(), cowboy_req:req()) -> cowboy_req:req())

View file

@ -14,7 +14,6 @@ as a Ranch protocol.
| {max_keepalive, non_neg_integer()} | {max_keepalive, non_neg_integer()}
| {max_request_line_length, non_neg_integer()} | {max_request_line_length, non_neg_integer()}
| {middlewares, [module()]} | {middlewares, [module()]}
| {onrequest, cowboy:onrequest_fun()}
| {onresponse, cowboy:onresponse_fun()} | {onresponse, cowboy:onresponse_fun()}
| {timeout, timeout()}] | {timeout, timeout()}]
@ -67,10 +66,6 @@ Maximum length of the request line.
List of middlewares to execute for every requests. List of middlewares to execute for every requests.
: onrequest (undefined)
Fun called every time a request is received.
: onresponse (undefined) : onresponse (undefined)
Fun called every time a response is sent. Fun called every time a response is sent.

View file

@ -126,8 +126,7 @@ Types:
Return the requested URL excluding the path component. Return the requested URL excluding the path component.
This function will always return `undefined` until the This function will always return `undefined` until the
`cowboy_router` middleware has been executed. This includes `cowboy_router` middleware has been executed.
the `onrequest` hook.
: match_cookies(Req, Fields) -> Map : match_cookies(Req, Fields) -> Map
@ -371,8 +370,7 @@ Types:
Return the requested URL. Return the requested URL.
This function will always return `undefined` until the This function will always return `undefined` until the
`cowboy_router` middleware has been executed. This includes `cowboy_router` middleware has been executed.
the `onrequest` hook.
: version(Req) -> Version : version(Req) -> Version

View file

@ -6,7 +6,6 @@ The `cowboy_spdy` module implements SPDY/3 as a Ranch protocol.
: opts() = [{env, cowboy_middleware:env()} : opts() = [{env, cowboy_middleware:env()}
| {middlewares, [module()]} | {middlewares, [module()]}
| {onrequest, cowboy:onrequest_fun()}
| {onresponse, cowboy:onresponse_fun()}] | {onresponse, cowboy:onresponse_fun()}]
Configuration for the SPDY protocol handler. Configuration for the SPDY protocol handler.
@ -30,10 +29,6 @@ Initial middleware environment.
List of middlewares to execute for every requests. List of middlewares to execute for every requests.
: onrequest (undefined)
Fun called every time a request is received.
: onresponse (undefined) : onresponse (undefined)
Fun called every time a response is sent. Fun called every time a response is sent.

View file

@ -34,9 +34,6 @@
-type http_version() :: 'HTTP/1.1' | 'HTTP/1.0'. -type http_version() :: 'HTTP/1.1' | 'HTTP/1.0'.
-export_type([http_version/0]). -export_type([http_version/0]).
-type onrequest_fun() :: fun((Req) -> Req).
-export_type([onrequest_fun/0]).
-type onresponse_fun() :: -type onresponse_fun() ::
fun((http_status(), http_headers(), iodata(), Req) -> Req). fun((http_status(), http_headers(), iodata(), Req) -> Req).
-export_type([onresponse_fun/0]). -export_type([onresponse_fun/0]).

View file

@ -32,7 +32,6 @@
| {max_keepalive, non_neg_integer()} | {max_keepalive, non_neg_integer()}
| {max_request_line_length, non_neg_integer()} | {max_request_line_length, non_neg_integer()}
| {middlewares, [module()]} | {middlewares, [module()]}
| {onrequest, cowboy:onrequest_fun()}
| {onresponse, cowboy:onresponse_fun()} | {onresponse, cowboy:onresponse_fun()}
| {timeout, timeout()}]. | {timeout, timeout()}].
-export_type([opts/0]). -export_type([opts/0]).
@ -43,7 +42,6 @@
middlewares :: [module()], middlewares :: [module()],
compress :: boolean(), compress :: boolean(),
env :: cowboy_middleware:env(), env :: cowboy_middleware:env(),
onrequest :: undefined | cowboy:onrequest_fun(),
onresponse = undefined :: undefined | cowboy:onresponse_fun(), onresponse = undefined :: undefined | cowboy:onresponse_fun(),
max_empty_lines :: non_neg_integer(), max_empty_lines :: non_neg_integer(),
req_keepalive = 1 :: non_neg_integer(), req_keepalive = 1 :: non_neg_integer(),
@ -85,7 +83,6 @@ init(Ref, Socket, Transport, Opts) ->
MaxRequestLineLength = get_value(max_request_line_length, Opts, 4096), MaxRequestLineLength = get_value(max_request_line_length, Opts, 4096),
Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]), Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]),
Env = [{listener, Ref}|get_value(env, Opts, [])], Env = [{listener, Ref}|get_value(env, Opts, [])],
OnRequest = get_value(onrequest, Opts, undefined),
OnResponse = get_value(onresponse, Opts, undefined), OnResponse = get_value(onresponse, Opts, undefined),
Timeout = get_value(timeout, Opts, 5000), Timeout = get_value(timeout, Opts, 5000),
ok = ranch:accept_ack(Ref), ok = ranch:accept_ack(Ref),
@ -95,8 +92,7 @@ init(Ref, Socket, Transport, Opts) ->
max_request_line_length=MaxRequestLineLength, max_request_line_length=MaxRequestLineLength,
max_header_name_length=MaxHeaderNameLength, max_header_name_length=MaxHeaderNameLength,
max_header_value_length=MaxHeaderValueLength, max_headers=MaxHeaders, max_header_value_length=MaxHeaderValueLength, max_headers=MaxHeaders,
onrequest=OnRequest, onresponse=OnResponse, onresponse=OnResponse, timeout=Timeout, until=until(Timeout)}, 0).
timeout=Timeout, until=until(Timeout)}, 0).
-spec until(timeout()) -> non_neg_integer() | infinity. -spec until(timeout()) -> non_neg_integer() | infinity.
until(infinity) -> until(infinity) ->
@ -410,26 +406,12 @@ request(Buffer, State=#state{socket=Socket, transport=Transport,
Req = cowboy_req:new(Socket, Transport, Peer, Method, Path, Req = cowboy_req:new(Socket, Transport, Peer, Method, Path,
Query, Version, Headers, Host, Port, Buffer, Query, Version, Headers, Host, Port, Buffer,
ReqKeepalive < MaxKeepalive, Compress, OnResponse), ReqKeepalive < MaxKeepalive, Compress, OnResponse),
onrequest(Req, State); execute(Req, State);
{error, _} -> {error, _} ->
%% Couldn't read the peer address; connection is gone. %% Couldn't read the peer address; connection is gone.
terminate(State) terminate(State)
end. end.
%% Call the global onrequest callback. The callback can send a reply,
%% in which case we consider the request handled and move on to the next
%% one. Note that since we haven't dispatched yet, we don't know the
%% handler, host_info, path_info or bindings yet.
-spec onrequest(cowboy_req:req(), #state{}) -> ok.
onrequest(Req, State=#state{onrequest=undefined}) ->
execute(Req, State);
onrequest(Req, State=#state{onrequest=OnRequest}) ->
Req2 = OnRequest(Req),
case cowboy_req:get(resp_state, Req2) of
waiting -> execute(Req2, State);
_ -> next_request(Req2, State, ok)
end.
-spec execute(cowboy_req:req(), #state{}) -> ok. -spec execute(cowboy_req:req(), #state{}) -> ok.
execute(Req, State=#state{middlewares=Middlewares, env=Env}) -> execute(Req, State=#state{middlewares=Middlewares, env=Env}) ->
execute(Req, State, Env, Middlewares). execute(Req, State, Env, Middlewares).

View file

@ -24,7 +24,7 @@
-export([system_code_change/4]). -export([system_code_change/4]).
%% Internal request process. %% Internal request process.
-export([request_init/11]). -export([request_init/10]).
-export([resume/5]). -export([resume/5]).
-export([reply/4]). -export([reply/4]).
-export([stream_reply/3]). -export([stream_reply/3]).
@ -59,7 +59,6 @@
buffer = <<>> :: binary(), buffer = <<>> :: binary(),
middlewares, middlewares,
env, env,
onrequest,
onresponse, onresponse,
peer, peer,
zdef, zdef,
@ -70,7 +69,6 @@
-type opts() :: [{env, cowboy_middleware:env()} -type opts() :: [{env, cowboy_middleware:env()}
| {middlewares, [module()]} | {middlewares, [module()]}
| {onrequest, cowboy:onrequest_fun()}
| {onresponse, cowboy:onresponse_fun()}]. | {onresponse, cowboy:onresponse_fun()}].
-export_type([opts/0]). -export_type([opts/0]).
@ -97,13 +95,12 @@ init(Parent, Ref, Socket, Transport, Opts) ->
{ok, Peer} = Transport:peername(Socket), {ok, Peer} = Transport:peername(Socket),
Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]), Middlewares = get_value(middlewares, Opts, [cowboy_router, cowboy_handler]),
Env = [{listener, Ref}|get_value(env, Opts, [])], Env = [{listener, Ref}|get_value(env, Opts, [])],
OnRequest = get_value(onrequest, Opts, undefined),
OnResponse = get_value(onresponse, Opts, undefined), OnResponse = get_value(onresponse, Opts, undefined),
Zdef = cow_spdy:deflate_init(), Zdef = cow_spdy:deflate_init(),
Zinf = cow_spdy:inflate_init(), Zinf = cow_spdy:inflate_init(),
ok = ranch:accept_ack(Ref), ok = ranch:accept_ack(Ref),
loop(#state{parent=Parent, socket=Socket, transport=Transport, loop(#state{parent=Parent, socket=Socket, transport=Transport,
middlewares=Middlewares, env=Env, onrequest=OnRequest, middlewares=Middlewares, env=Env,
onresponse=OnResponse, peer=Peer, zdef=Zdef, zinf=Zinf}). onresponse=OnResponse, peer=Peer, zdef=Zdef, zinf=Zinf}).
loop(State=#state{parent=Parent, socket=Socket, transport=Transport, loop(State=#state{parent=Parent, socket=Socket, transport=Transport,
@ -257,11 +254,11 @@ handle_frame(State, {syn_stream, StreamID, AssocToStreamID,
%% Erlang does not allow us to control the priority of processes %% Erlang does not allow us to control the priority of processes
%% so we ignore that value entirely. %% so we ignore that value entirely.
handle_frame(State=#state{middlewares=Middlewares, env=Env, handle_frame(State=#state{middlewares=Middlewares, env=Env,
onrequest=OnRequest, onresponse=OnResponse, peer=Peer}, onresponse=OnResponse, peer=Peer},
{syn_stream, StreamID, _, IsFin, _, _, {syn_stream, StreamID, _, IsFin, _, _,
Method, _, Host, Path, Version, Headers}) -> Method, _, Host, Path, Version, Headers}) ->
Pid = spawn_link(?MODULE, request_init, [ Pid = spawn_link(?MODULE, request_init, [
{self(), StreamID}, Peer, OnRequest, OnResponse, {self(), StreamID}, Peer, OnResponse,
Env, Middlewares, Method, Host, Path, Version, Headers Env, Middlewares, Method, Host, Path, Version, Headers
]), ]),
new_child(State, StreamID, Pid, IsFin); new_child(State, StreamID, Pid, IsFin);
@ -385,11 +382,10 @@ delete_child(Pid, State=#state{children=Children}) ->
%% Request process. %% Request process.
-spec request_init(socket(), {inet:ip_address(), inet:port_number()}, -spec request_init(socket(), {inet:ip_address(), inet:port_number()},
cowboy:onrequest_fun(), cowboy:onresponse_fun(), cowboy:onresponse_fun(), cowboy_middleware:env(), [module()],
cowboy_middleware:env(), [module()],
binary(), binary(), binary(), binary(), [{binary(), binary()}]) binary(), binary(), binary(), binary(), [{binary(), binary()}])
-> ok. -> ok.
request_init(FakeSocket, Peer, OnRequest, OnResponse, request_init(FakeSocket, Peer, OnResponse,
Env, Middlewares, Method, Host, Path, Version, Headers) -> Env, Middlewares, Method, Host, Path, Version, Headers) ->
{Host2, Port} = cow_http:parse_fullhost(Host), {Host2, Port} = cow_http:parse_fullhost(Host),
{Path2, Qs} = cow_http:parse_fullpath(Path), {Path2, Qs} = cow_http:parse_fullpath(Path),
@ -397,16 +393,7 @@ request_init(FakeSocket, Peer, OnRequest, OnResponse,
Req = cowboy_req:new(FakeSocket, ?MODULE, Peer, Req = cowboy_req:new(FakeSocket, ?MODULE, Peer,
Method, Path2, Qs, Version2, Headers, Method, Path2, Qs, Version2, Headers,
Host2, Port, <<>>, true, false, OnResponse), Host2, Port, <<>>, true, false, OnResponse),
case OnRequest of execute(Req, Env, Middlewares).
undefined ->
execute(Req, Env, Middlewares);
_ ->
Req2 = OnRequest(Req),
case cowboy_req:get(resp_state, Req2) of
waiting -> execute(Req2, Env, Middlewares);
_ -> ok
end
end.
-spec execute(cowboy_req:req(), cowboy_middleware:env(), [module()]) -spec execute(cowboy_req:req(), cowboy_middleware:env(), [module()])
-> ok. -> ok.

View file

@ -34,7 +34,6 @@ all() ->
{group, https}, {group, https},
{group, http_compress}, {group, http_compress},
{group, https_compress}, {group, https_compress},
{group, onrequest},
{group, onresponse}, {group, onresponse},
{group, onresponse_capitalize}, {group, onresponse_capitalize},
{group, parse_host}, {group, parse_host},
@ -43,7 +42,6 @@ all() ->
groups() -> groups() ->
Tests = cowboy_test:all(?MODULE) -- [ Tests = cowboy_test:all(?MODULE) -- [
onrequest, onrequest_reply, onrequest_hook,
onresponse_crash, onresponse_reply, onresponse_capitalize, onresponse_crash, onresponse_reply, onresponse_capitalize,
parse_host, set_env_dispatch parse_host, set_env_dispatch
], ],
@ -52,10 +50,6 @@ groups() ->
{https, [parallel], Tests}, {https, [parallel], Tests},
{http_compress, [parallel], Tests}, {http_compress, [parallel], Tests},
{https_compress, [parallel], Tests}, {https_compress, [parallel], Tests},
{onrequest, [parallel], [
onrequest,
onrequest_reply
]},
{onresponse, [parallel], [ {onresponse, [parallel], [
onresponse_crash, onresponse_crash,
onresponse_reply onresponse_reply
@ -98,15 +92,6 @@ init_per_group(Name = https_compress, Config) ->
{compress, true} {compress, true}
], Config); ], Config);
%% Most, if not all of these, should be in separate test suites. %% Most, if not all of these, should be in separate test suites.
init_per_group(onrequest, Config) ->
{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]},
{max_keepalive, 50},
{onrequest, fun do_onrequest_hook/1},
{timeout, 500}
]),
Port = ranch:get_port(onrequest),
[{type, tcp}, {port, Port}, {opts, []}|Config];
init_per_group(onresponse, Config) -> init_per_group(onresponse, Config) ->
{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [ {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch(Config)}]}, {env, [{dispatch, init_dispatch(Config)}]},
@ -574,31 +559,6 @@ nc_rand(Config) ->
nc_zero(Config) -> nc_zero(Config) ->
do_nc(Config, "/dev/zero"). do_nc(Config, "/dev/zero").
onrequest(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/"),
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{<<"server">>, <<"Serenity">>} = lists:keyfind(<<"server">>, 1, Headers),
{ok, <<"http_handler">>} = gun:await_body(ConnPid, Ref),
ok.
onrequest_reply(Config) ->
ConnPid = gun_open(Config),
Ref = gun:get(ConnPid, "/?reply=1"),
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
{<<"server">>, <<"Cowboy">>} = lists:keyfind(<<"server">>, 1, Headers),
{ok, <<"replied!">>} = gun:await_body(ConnPid, Ref),
ok.
%% Hook for the above onrequest tests.
do_onrequest_hook(Req) ->
case cowboy_req:match_qs(Req, [{reply, [], noreply}]) of
#{reply := noreply} ->
cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req);
_ ->
cowboy_req:reply(200, [], <<"replied!">>, Req)
end.
onresponse_capitalize(Config) -> onresponse_capitalize(Config) ->
Client = raw_open(Config), Client = raw_open(Config),
ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"), ok = raw_send(Client, "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"),