1
votes

I work with erlang and I use modem which is related to my pc

and I want to send message from the modem to my phone number

I developed this function and it works perfectly

send(To) ->
Message =http_urii:encode("Vous n'avez pas d'écheance à payer."),
    Url = io_lib:format("http://192.168.1.5:9998/sendsms?tel=~s&text=~s", [To, Message]),
    http:request(lists:flatten(Url), sms).

my problem is related to the symbols with diacritic, e.g. é , à

because in my phone I receive the sms but the spécial characters are not displayed in the correct format the é became another character , the à became another character

and I want to receive in my phone this message :Vous n'avez pas d'écheance à payer.

I modify the source code of send

 send(To) ->
    Message =http_urii:encode("Vous n'avez pas d'%E9cheance %EA payer."),
        Url = io_lib:format("http://192.168.1.5:9998/sendsms?tel=~s&text=~s", [To, Message]),
        http:request(lists:flatten(Url), sms).

but in my phone I receive this message Vous n'avez pas d'%E9cheance %EA payer. without encoding of special characters

the source code of http_urii.erl is :

-module(http_urii).

-export([parse/1, encode/1, decode/1]).

%%%=========================================================================
%%%  API
%%%=========================================================================
parse(AbsURI) ->
    case parse_scheme(AbsURI) of
    {error, Reason} ->
        {error, Reason};
    {Scheme, Rest} ->
        case (catch parse_uri_rest(Scheme, Rest)) of
        {UserInfo, Host, Port, Path, Query} ->
            {Scheme, UserInfo, Host, Port, Path, Query};
        _  ->
            {error, {malformed_url, AbsURI}}
        end
    end.

encode(URI) ->
    Reserved = sets:from_list([$;, $:, $@, $&, $=, $+, $,, $/, $?,
                   $#, $[, $], $<, $>, $\", ${, $}, $|,
                   $\\, $', $^, $%, $ ]),
    lists:append(lists:map(fun(Char) ->
                   uri_encode(Char, Reserved)
               end, URI)).

decode([$%,Hex1,Hex2|Rest]) ->
    [hex2dec(Hex1)*16+hex2dec(Hex2)|decode(Rest)];
decode([First|Rest]) ->
    [First|decode(Rest)];
decode([]) ->
    [].

%%%========================================================================
%%% Internal functions
%%%========================================================================
parse_scheme(AbsURI) ->
    case split_uri(AbsURI, ":", {error, no_scheme}, 1, 1) of
    {error, no_scheme} ->
        {error, no_scheme};
    {StrScheme, Rest} ->
        case list_to_atom(http_util:to_lower(StrScheme)) of
        Scheme when Scheme == http; Scheme == https ->
            {Scheme, Rest};
        Scheme ->
            {error, {not_supported_scheme, Scheme}}
        end
    end.

parse_uri_rest(Scheme, "//" ++ URIPart) ->

    {Authority, PathQuery} =
    case split_uri(URIPart, "/", URIPart, 1, 0) of
        Split = {_, _} ->
        Split;
        URIPart ->
        case split_uri(URIPart, "\\?", URIPart, 1, 0) of
            Split = {_, _} ->
            Split;
            URIPart ->
            {URIPart,""}
        end
    end,

    {UserInfo, HostPort} = split_uri(Authority, "@", {"", Authority}, 1, 1),
    {Host, Port} = parse_host_port(Scheme, HostPort),
    {Path, Query} = parse_path_query(PathQuery),
    {UserInfo, Host, Port, Path, Query}.


parse_path_query(PathQuery) ->
    {Path, Query} =  split_uri(PathQuery, "\\?", {PathQuery, ""}, 1, 0),
    {path(Path), Query}.

parse_host_port(Scheme,"[" ++ HostPort) -> %ipv6
    DefaultPort = default_port(Scheme),
    {Host, ColonPort} = split_uri(HostPort, "\\]", {HostPort, ""}, 1, 1),
    {_, Port} = split_uri(ColonPort, ":", {"", DefaultPort}, 0, 1),
    {Host, int_port(Port)};

parse_host_port(Scheme, HostPort) ->
    DefaultPort = default_port(Scheme),
    {Host, Port} = split_uri(HostPort, ":", {HostPort, DefaultPort}, 1, 1),
    {Host, int_port(Port)}.

split_uri(UriPart, SplitChar, NoMatchResult, SkipLeft, SkipRight) ->
    case inets_regexp:first_match(UriPart, SplitChar) of
    {match, Match, _} ->
        {string:substr(UriPart, 1, Match - SkipLeft),
         string:substr(UriPart, Match + SkipRight, length(UriPart))};
    nomatch ->
        NoMatchResult
    end.

default_port(http) ->
    80;
default_port(https) ->
    443.

int_port(Port) when is_integer(Port) ->
    Port;
int_port(Port) when is_list(Port) ->
    list_to_integer(Port).

path("") ->
    "/";
path(Path) ->
    Path.

uri_encode(Char, Reserved) ->
    case sets:is_element(Char, Reserved) of
    true ->
        [ $% | http_util:integer_to_hexlist(Char)];
    false ->
        [Char]
    end.

hex2dec(X) when (X>=$0) andalso (X=<$9) -> X-$0;
hex2dec(X) when (X>=$A) andalso (X=<$F) -> X-$A+10;
hex2dec(X) when (X>=$a) andalso (X=<$f) -> X-$a+10.

now I change http_urii by the standart http_uri

but when I execute my project erlang

I have this error : Erlang R13B03 (erts-5.7.4) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.4  (abort with ^G)
1> exception: [{http_uri,encode,
                      ["Vous n'avez pas d'écheance à payer."]},
            {sms_alert,send,1},
            {webrpc_api,build_response,2},
            {webrpc_api,format_response,2},
            {webrpc_api,process_command,2},
            {httpd_response,traverse_modules,2},
            {httpd_response,generate_and_send_response,1},
            {httpd_request_handler,handle_response,1}]

I know that I work with erlang version 13, but I make the module http_uri.erl in the src of my project , so normaly it should be worked

1
Can you include the relevant code from your sendsms program? What character encoding are you using in each part of the system?Emil Vikström
I agree that sendsms seems like a likely source for the problem. I would recommend pointing this request at a web server somewhere where you can see the request log to make sure the diacritic is being represented correctly. Unfamiliar with the http_urii library, or is that a typo?macintux

1 Answers

0
votes

If you look at what http_urii:encode/1 returns you will find it is not what you explicitly used. On my machine I get:

4> http_urii:encode("Vous n'avez, pas d'énheance à payer.").
"Vous%20n%27avez%2C%20pas%20d%27énheance%20à%20payer."

There is a module http_uri in the standard Erlang release which returns the same thing in this case as your http_urii.