| 1 |
= cowboy_req:parse_header(3) |
| 2 |
|
| 3 |
== Name |
| 4 |
|
| 5 |
cowboy_req:parse_header - Parse the given HTTP header |
| 6 |
|
| 7 |
== Description |
| 8 |
|
| 9 |
[source,erlang] |
| 10 |
---- |
| 11 |
parse_header(Name, Req) -> ParsedValue | Default |
| 12 |
parse_header(Name, Req, Default) -> ParsedValue | Default |
| 13 |
|
| 14 |
Name :: binary() |
| 15 |
Req :: cowboy_req:req() |
| 16 |
ParsedValue :: any() |
| 17 |
Default :: any() |
| 18 |
---- |
| 19 |
|
| 20 |
Parse the given HTTP header. |
| 21 |
|
| 22 |
The header name must be given as a lowercase binary string. |
| 23 |
While header names are case insensitive, Cowboy requires them |
| 24 |
to be given as lowercase to function properly. |
| 25 |
|
| 26 |
The type of the parsed value varies depending on |
| 27 |
the header. Similarly, the default value when calling |
| 28 |
`cowboy_req:parse_header/2` differs depending on the |
| 29 |
header. |
| 30 |
|
| 31 |
== Arguments |
| 32 |
|
| 33 |
Name:: |
| 34 |
|
| 35 |
Desired HTTP header name as a lowercase binary string. |
| 36 |
|
| 37 |
Req:: |
| 38 |
|
| 39 |
The Req object. |
| 40 |
|
| 41 |
Default:: |
| 42 |
|
| 43 |
Default value returned when the header is missing. |
| 44 |
|
| 45 |
== Return value |
| 46 |
|
| 47 |
The parsed header value varies depending on the header. |
| 48 |
When the header is missing, the default argument is returned. |
| 49 |
|
| 50 |
== Headers |
| 51 |
|
| 52 |
The following snippets detail the types returned by the |
| 53 |
different headers. Unless mentioned otherwise, the |
| 54 |
default value when the header is missing will be `undefined`: |
| 55 |
|
| 56 |
.accept |
| 57 |
[source,erlang] |
| 58 |
---- |
| 59 |
parse_header(<<"accept">>, Req) |
| 60 |
-> [{{Type, SubType, Params}, Quality, AcceptExt}] |
| 61 |
|
| 62 |
Type :: binary() %% case insensitive |
| 63 |
SubType :: binary() %% case insensitive |
| 64 |
Params :: [{Key, Value}] |
| 65 |
Quality :: 0..1000 |
| 66 |
AcceptExt :: [Key | {Key, Value}] |
| 67 |
Key :: binary() %% case insensitive |
| 68 |
Value :: binary() %% case sensitive |
| 69 |
---- |
| 70 |
|
| 71 |
.accept-charset, accept-encoding and accept-language |
| 72 |
[source,erlang] |
| 73 |
---- |
| 74 |
parse_header(Name, Req) -> [{Value, Quality}] |
| 75 |
|
| 76 |
Name :: <<"accept-charset">> |
| 77 |
| <<"accept-encoding">> |
| 78 |
| <<"accept-language">> |
| 79 |
Value :: binary() %% case insensitive |
| 80 |
Quality :: 0..1000 |
| 81 |
---- |
| 82 |
|
| 83 |
.access-control-request-headers |
| 84 |
[source,erlang] |
| 85 |
---- |
| 86 |
parse_header(<<"access-control-request-headers">>, Req) |
| 87 |
-> [Header] |
| 88 |
|
| 89 |
Header :: binary() %% case insensitive |
| 90 |
---- |
| 91 |
|
| 92 |
.access-control-request-method |
| 93 |
[source,erlang] |
| 94 |
---- |
| 95 |
parse_header(<<"access-control-request-method">>) |
| 96 |
-> Method |
| 97 |
|
| 98 |
Method :: binary() %% case sensitive |
| 99 |
---- |
| 100 |
|
| 101 |
.authorization and proxy-authorization |
| 102 |
[source,erlang] |
| 103 |
---- |
| 104 |
parse_header(<<"authorization">>, Req) |
| 105 |
-> {basic, Username :: binary(), Password :: binary()} |
| 106 |
| {bearer, Token :: binary()} |
| 107 |
| {digest, [{Key :: binary(), Value :: binary()}]} |
| 108 |
---- |
| 109 |
|
| 110 |
// @todo Currently also parses connection. Do we want this? Should it be documented? Use case? |
| 111 |
|
| 112 |
.content-encoding and content-language |
| 113 |
[source,erlang] |
| 114 |
---- |
| 115 |
parse_header(Name, Req) -> [Value] |
| 116 |
|
| 117 |
Name :: <<"content-encoding">> |
| 118 |
| <<"content-language">> |
| 119 |
Value :: binary() %% case insensitive |
| 120 |
---- |
| 121 |
|
| 122 |
.content-length |
| 123 |
[source,erlang] |
| 124 |
---- |
| 125 |
parse_header(<<"content-length">>, Req) -> non_neg_integer() |
| 126 |
---- |
| 127 |
|
| 128 |
When the content-length header is missing, `0` is returned. |
| 129 |
|
| 130 |
.content-type |
| 131 |
[source,erlang] |
| 132 |
---- |
| 133 |
parse_header(<<"content-type">>, Req) |
| 134 |
-> {Type, SubType, Params} |
| 135 |
|
| 136 |
Type :: binary() %% case insensitive |
| 137 |
SubType :: binary() %% case insensitive |
| 138 |
Params :: [{Key, Value}] |
| 139 |
Key :: binary() %% case insensitive |
| 140 |
Value :: binary() %% case sensitive; |
| 141 |
---- |
| 142 |
|
| 143 |
Note that the value for the charset parameter is case insensitive |
| 144 |
and returned as a lowercase binary string. |
| 145 |
|
| 146 |
.cookie |
| 147 |
[source,erlang] |
| 148 |
---- |
| 149 |
parse_header(<<"cookie">>, Req) -> [{Name, Value}] |
| 150 |
|
| 151 |
Name :: binary() %% case sensitive |
| 152 |
Value :: binary() %% case sensitive |
| 153 |
---- |
| 154 |
|
| 155 |
When the cookie header is missing, `[]` is returned. |
| 156 |
|
| 157 |
While an empty cookie header is not valid, some clients do |
| 158 |
send it. Cowboy will in this case also return `[]`. |
| 159 |
|
| 160 |
.expect |
| 161 |
[source,erlang] |
| 162 |
---- |
| 163 |
parse_header(<<"expect">>, Req) -> continue |
| 164 |
---- |
| 165 |
|
| 166 |
.if-match and if-none-match |
| 167 |
[source,erlang] |
| 168 |
---- |
| 169 |
parse_header(Name, Req) |
| 170 |
-> '*' | [{weak | strong, OpaqueTag}] |
| 171 |
|
| 172 |
Name :: <<"if-match">> |
| 173 |
| <<"if-none-match">> |
| 174 |
OpaqueTag :: binary() %% case sensitive |
| 175 |
---- |
| 176 |
|
| 177 |
.if-modified-since and if-unmodified-since |
| 178 |
[source,erlang] |
| 179 |
---- |
| 180 |
parse_header(Name, Req) -> calendar:datetime() |
| 181 |
---- |
| 182 |
|
| 183 |
.max-forwards |
| 184 |
[source,erlang] |
| 185 |
---- |
| 186 |
parse_header(<<"max-forwards">>, Req) -> non_neg_integer() |
| 187 |
---- |
| 188 |
|
| 189 |
.origin |
| 190 |
[source,erlang] |
| 191 |
---- |
| 192 |
parse_header(<<"origin">>, Req) |
| 193 |
-> [{Scheme, Host, Port} | GUID] |
| 194 |
|
| 195 |
Scheme :: <<"http">> | <<"https">> |
| 196 |
Host :: binary() %% case insensitive |
| 197 |
Port :: 0..65535 |
| 198 |
GUID :: reference() |
| 199 |
---- |
| 200 |
|
| 201 |
Cowboy generates a reference in place of a GUID when the URI |
| 202 |
uses an unsupported uri-scheme or is not an absolute URI. |
| 203 |
|
| 204 |
[source,erlang] |
| 205 |
---- |
| 206 |
parse_header(<<"range">>, Req) -> {From, To} | Final |
| 207 |
|
| 208 |
From :: non_neg_integer() |
| 209 |
To :: non_neg_integer() | infinity |
| 210 |
Final :: neg_integer() |
| 211 |
---- |
| 212 |
|
| 213 |
.sec-websocket-extensions |
| 214 |
[source,erlang] |
| 215 |
---- |
| 216 |
parse_header(<<"sec-websocket-extensions">>, Req) |
| 217 |
-> [{Extension, Params}] |
| 218 |
|
| 219 |
Extension :: binary() %% case sensitive |
| 220 |
Params :: [Key | {Key, Value}] |
| 221 |
Key :: binary() %% case sensitive |
| 222 |
Value :: binary() %% case sensitive |
| 223 |
---- |
| 224 |
|
| 225 |
.sec-websocket-protocol and upgrade |
| 226 |
[source,erlang] |
| 227 |
---- |
| 228 |
parse_header(Name, Req) -> [Token] |
| 229 |
|
| 230 |
Name :: <<"sec-websocket-protocol">> |
| 231 |
| <<"upgrade">> |
| 232 |
Token :: binary() %% case insensitive |
| 233 |
---- |
| 234 |
|
| 235 |
.trailer |
| 236 |
[source,erlang] |
| 237 |
---- |
| 238 |
parse_header(Name, Req) -> [Header] |
| 239 |
|
| 240 |
Header :: binary() %% case insensitive |
| 241 |
---- |
| 242 |
|
| 243 |
.x-forwarded-for |
| 244 |
[source,erlang] |
| 245 |
---- |
| 246 |
parse_header(<<"x-forwarded-for">>, Req) -> [Token] |
| 247 |
|
| 248 |
Token :: binary() %% case sensitive |
| 249 |
---- |
| 250 |
|
| 251 |
This function will crash when attempting to parse a |
| 252 |
header Cowboy does not currently understand. |
| 253 |
|
| 254 |
== Changelog |
| 255 |
|
| 256 |
* *2.8*: The function now parses `access-control-request-headers`, |
| 257 |
`access-control-request-method`, `content-encoding`, |
| 258 |
`content-language`, `max-forwards`, `origin`, |
| 259 |
`proxy-authorization` and `trailer`. |
| 260 |
* *2.0*: Only the parsed header value is returned, it is no longer wrapped in a tuple. |
| 261 |
* *1.0*: Function introduced. |
| 262 |
|
| 263 |
== Examples |
| 264 |
|
| 265 |
.Parse the accept header with a custom default value |
| 266 |
[source,erlang] |
| 267 |
---- |
| 268 |
%% Accept everything when header is missing. |
| 269 |
Accept = cowboy_req:parse_header(<<"accept">>, Req, |
| 270 |
[{{ <<"*">>, <<"*">>, []}, 1000, []}]). |
| 271 |
---- |
| 272 |
|
| 273 |
.Parse the content-length header |
| 274 |
[source,erlang] |
| 275 |
---- |
| 276 |
%% Default content-length is 0. |
| 277 |
Length = cowboy_req:header(<<"content-length">>, Req). |
| 278 |
---- |
| 279 |
|
| 280 |
== See also |
| 281 |
|
| 282 |
link:man:cowboy_req(3)[cowboy_req(3)], |
| 283 |
link:man:cowboy_req:header(3)[cowboy_req:header(3)], |
| 284 |
link:man:cowboy_req:headers(3)[cowboy_req:headers(3)] |