noss / iserve

A small http server for erlang.

This URL has Read+Write access

iserve / src / iserve_util.erl
100644 42 lines (33 sloc) 1.197 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
%%% File : iserve_util.erl
%%% Author : Christian S <chsu79@gmail.com>
%%% Created : 12 Jul 2008 by Christian S <chsu79@gmail.com>
 
-module(iserve_util).
 
-export([urldecode/1, urlencode/1]).
 
urldecode(String) ->
    httpd_util:decode_hex(String).
 
-ifdef(XTEST).
urlencode_test_() ->
    [
     ?_assertMatch("ABCXYZ", urlencode("ABCXYZ")),
     ?_assertMatch("abcxyz", urlencode("abcxyz")),
     ?_assertMatch("0189", urlencode("0189")),
     ?_assertMatch("hello%20world", urlencode("hello world")),
     ?_assertMatch("~hello.world", urlencode("~hello.world"))
    ].
-endif.
 
urlencode(Bin) when is_binary(Bin) ->
    urlencode(binary_to_list(Bin));
urlencode(Integer) when is_integer(Integer) ->
    urlencode(erlang:integer_to_list(Integer));
urlencode(Bytes) ->
    lists:flatten([maybe_quote(Byte) || Byte <- Bytes]).
 
 
maybe_quote(Byte) when Byte >= $A, Byte =< $Z;
                       Byte >= $a, Byte =< $z;
                       Byte >= $0, Byte =< $9;
                        Byte =:= $-;
                        Byte =:= $_;
                        Byte =:= $.;
                        Byte =:= $~ ->
    Byte;
maybe_quote(Byte) ->
    io_lib:format("%~2.16.0b", [Byte]).