|
5 | 5 |
|
6 | 6 | -module(mochiweb_util).
|
7 | 7 |
|
8 |
| --export([join/2, quote_plus/1, urlencode/1, parse_qs/1, unquote/1]). |
| 8 | +-export([join/2, quote_plus/1, urlencode/1, parse_qs/1, unquote/1, unquote_path/1]). |
9 | 9 | -export([path_split/1]).
|
10 | 10 | -export([urlsplit/1, urlsplit_path/1, urlunsplit/1, urlunsplit_path/1]).
|
11 | 11 | -export([guess_mime/1, parse_header/1]).
|
@@ -34,6 +34,8 @@ hexdigit(C) when C < 16 -> $A + (C - 10).
|
34 | 34 | unhexdigit(C) when C >= $0, C =< $9 -> C - $0;
|
35 | 35 | unhexdigit(C) when C >= $a, C =< $f -> C - $a + 10;
|
36 | 36 | unhexdigit(C) when C >= $A, C =< $F -> C - $A + 10.
|
| 37 | +unhexdigit(Hi, Lo) -> |
| 38 | + unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4). |
37 | 39 |
|
38 | 40 | %% @spec partition(String, Sep) -> {String, [], []} | {Prefix, Sep, Postfix}
|
39 | 41 | %% @doc Inspired by Python 2.5's str.partition:
|
@@ -256,10 +258,27 @@ qs_revdecode([], Acc) ->
|
256 | 258 | qs_revdecode([$+ | Rest], Acc) ->
|
257 | 259 | qs_revdecode(Rest, [$\s | Acc]);
|
258 | 260 | qs_revdecode([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) ->
|
259 |
| - qs_revdecode(Rest, [(unhexdigit(Lo) bor (unhexdigit(Hi) bsl 4)) | Acc]); |
| 261 | + qs_revdecode(Rest, [(unhexdigit(Hi, Lo)) | Acc]); |
260 | 262 | qs_revdecode([C | Rest], Acc) ->
|
261 | 263 | qs_revdecode(Rest, [C | Acc]).
|
262 | 264 |
|
| 265 | +%% @spec unquote_path(string() | binary()) -> string() |
| 266 | +%% @doc Unquote a URL encoded string, does not encode + into space. |
| 267 | +unquote_path(Binary) when is_binary(Binary) -> |
| 268 | + unquote_path(binary_to_list(Binary)); |
| 269 | +unquote_path(String) -> |
| 270 | + qs_revdecode_path(lists:reverse(String)). |
| 271 | + |
| 272 | +qs_revdecode_path(S) -> |
| 273 | + qs_revdecode_path(S, []). |
| 274 | + |
| 275 | +qs_revdecode_path([], Acc) -> |
| 276 | + Acc; |
| 277 | +qs_revdecode_path([Lo, Hi, ?PERCENT | Rest], Acc) when ?IS_HEX(Lo), ?IS_HEX(Hi) -> |
| 278 | + qs_revdecode_path(Rest, [(unhexdigit(Hi, Lo)) | Acc]); |
| 279 | +qs_revdecode_path([C | Rest], Acc) -> |
| 280 | + qs_revdecode_path(Rest, [C | Acc]). |
| 281 | + |
263 | 282 | %% @spec urlsplit(Url) -> {Scheme, Netloc, Path, Query, Fragment}
|
264 | 283 | %% @doc Return a 5-tuple, does not expand % escapes. Only supports HTTP style
|
265 | 284 | %% URLs.
|
|
0 commit comments