| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(cowboy_bstr). |
| 16 |
|
| 17 |
%% Binary strings. |
| 18 |
-export([capitalize_token/1]). |
| 19 |
-export([to_lower/1]). |
| 20 |
-export([to_upper/1]). |
| 21 |
|
| 22 |
%% Characters. |
| 23 |
-export([char_to_lower/1]). |
| 24 |
-export([char_to_upper/1]). |
| 25 |
|
| 26 |
%% The first letter and all letters after a dash are capitalized. |
| 27 |
%% This is the form seen for header names in the HTTP/1.1 RFC and |
| 28 |
%% others. Note that using this form isn't required, as header names |
| 29 |
%% are case insensitive, and it is only provided for use with eventual |
| 30 |
%% badly implemented clients. |
| 31 |
-spec capitalize_token(B) -> B when B::binary(). |
| 32 |
capitalize_token(B) -> |
| 33 |
capitalize_token(B, true, <<>>). |
| 34 |
capitalize_token(<<>>, _, Acc) -> |
| 35 |
Acc; |
| 36 |
capitalize_token(<< $-, Rest/bits >>, _, Acc) -> |
| 37 |
capitalize_token(Rest, true, << Acc/binary, $- >>); |
| 38 |
capitalize_token(<< C, Rest/bits >>, true, Acc) -> |
| 39 |
capitalize_token(Rest, false, << Acc/binary, (char_to_upper(C)) >>); |
| 40 |
capitalize_token(<< C, Rest/bits >>, false, Acc) -> |
| 41 |
capitalize_token(Rest, false, << Acc/binary, (char_to_lower(C)) >>). |
| 42 |
|
| 43 |
-spec to_lower(B) -> B when B::binary(). |
| 44 |
to_lower(B) -> |
| 45 |
<< << (char_to_lower(C)) >> || << C >> <= B >>. |
| 46 |
|
| 47 |
-spec to_upper(B) -> B when B::binary(). |
| 48 |
to_upper(B) -> |
| 49 |
<< << (char_to_upper(C)) >> || << C >> <= B >>. |
| 50 |
|
| 51 |
-spec char_to_lower(char()) -> char(). |
| 52 |
char_to_lower($A) -> $a; |
| 53 |
char_to_lower($B) -> $b; |
| 54 |
char_to_lower($C) -> $c; |
| 55 |
char_to_lower($D) -> $d; |
| 56 |
char_to_lower($E) -> $e; |
| 57 |
char_to_lower($F) -> $f; |
| 58 |
char_to_lower($G) -> $g; |
| 59 |
char_to_lower($H) -> $h; |
| 60 |
char_to_lower($I) -> $i; |
| 61 |
char_to_lower($J) -> $j; |
| 62 |
char_to_lower($K) -> $k; |
| 63 |
char_to_lower($L) -> $l; |
| 64 |
char_to_lower($M) -> $m; |
| 65 |
char_to_lower($N) -> $n; |
| 66 |
char_to_lower($O) -> $o; |
| 67 |
char_to_lower($P) -> $p; |
| 68 |
char_to_lower($Q) -> $q; |
| 69 |
char_to_lower($R) -> $r; |
| 70 |
char_to_lower($S) -> $s; |
| 71 |
char_to_lower($T) -> $t; |
| 72 |
char_to_lower($U) -> $u; |
| 73 |
char_to_lower($V) -> $v; |
| 74 |
char_to_lower($W) -> $w; |
| 75 |
char_to_lower($X) -> $x; |
| 76 |
char_to_lower($Y) -> $y; |
| 77 |
char_to_lower($Z) -> $z; |
| 78 |
char_to_lower(Ch) -> Ch. |
| 79 |
|
| 80 |
-spec char_to_upper(char()) -> char(). |
| 81 |
char_to_upper($a) -> $A; |
| 82 |
char_to_upper($b) -> $B; |
| 83 |
char_to_upper($c) -> $C; |
| 84 |
char_to_upper($d) -> $D; |
| 85 |
char_to_upper($e) -> $E; |
| 86 |
char_to_upper($f) -> $F; |
| 87 |
char_to_upper($g) -> $G; |
| 88 |
char_to_upper($h) -> $H; |
| 89 |
char_to_upper($i) -> $I; |
| 90 |
char_to_upper($j) -> $J; |
| 91 |
char_to_upper($k) -> $K; |
| 92 |
char_to_upper($l) -> $L; |
| 93 |
char_to_upper($m) -> $M; |
| 94 |
char_to_upper($n) -> $N; |
| 95 |
char_to_upper($o) -> $O; |
| 96 |
char_to_upper($p) -> $P; |
| 97 |
char_to_upper($q) -> $Q; |
| 98 |
char_to_upper($r) -> $R; |
| 99 |
char_to_upper($s) -> $S; |
| 100 |
char_to_upper($t) -> $T; |
| 101 |
char_to_upper($u) -> $U; |
| 102 |
char_to_upper($v) -> $V; |
| 103 |
char_to_upper($w) -> $W; |
| 104 |
char_to_upper($x) -> $X; |
| 105 |
char_to_upper($y) -> $Y; |
| 106 |
char_to_upper($z) -> $Z; |
| 107 |
char_to_upper(Ch) -> Ch. |
| 108 |
|
| 109 |
%% Tests. |
| 110 |
|
| 111 |
-ifdef(TEST). |
| 112 |
capitalize_token_test_() -> |
| 113 |
Tests = [ |
| 114 |
{<<"heLLo-woRld">>, <<"Hello-World">>}, |
| 115 |
{<<"Sec-Websocket-Version">>, <<"Sec-Websocket-Version">>}, |
| 116 |
{<<"Sec-WebSocket-Version">>, <<"Sec-Websocket-Version">>}, |
| 117 |
{<<"sec-websocket-version">>, <<"Sec-Websocket-Version">>}, |
| 118 |
{<<"SEC-WEBSOCKET-VERSION">>, <<"Sec-Websocket-Version">>}, |
| 119 |
{<<"Sec-WebSocket--Version">>, <<"Sec-Websocket--Version">>}, |
| 120 |
{<<"Sec-WebSocket---Version">>, <<"Sec-Websocket---Version">>} |
| 121 |
], |
| 122 |
[{H, fun() -> R = capitalize_token(H) end} || {H, R} <- Tests]. |
| 123 |
-endif. |