Packages
mist
6.0.3
6.0.3
6.0.2
6.0.1
6.0.0
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
5.0.0-rc1
4.0.7
4.0.6
4.0.5
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
3.0.0
2.0.0
1.2.0
1.1.0
1.0.0
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.17.0
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.3
a misty Gleam web server
Current section
Files
Jump to
Current section
Files
src/mist_ffi.erl
-module(mist_ffi).
-export([binary_match/2, decode_packet/3, decode_atom/1,file_open/1, string_to_int/2, hpack_decode/2,
hpack_encode/2, hpack_new_max_table_size/2, ets_lookup_element/3, get_path_and_query/1,
file_close/1, now/0]).
now() ->
Timestamp = os:system_time(microsecond),
{Date, Time} = calendar:system_time_to_universal_time(Timestamp, microsecond),
Weekday = calendar:day_of_the_week(Date),
{Weekday, Date, Time}.
decode_packet(Type, Packet, Opts) ->
case erlang:decode_packet(Type, Packet, Opts) of
{ok, http_eoh, Rest} ->
{ok, {end_of_headers, Rest}};
{ok, {http_request, <<"PRI">>, '*', {2, 0}}, Rest} ->
{ok, {http2_upgrade, Rest}};
{ok, Binary, Rest} ->
{ok, {binary_data, Binary, Rest}};
{more, undefined} ->
{ok, {more_data, none}};
{more, Length} ->
{ok, {more_data, {some, Length}}};
{error, Reason} ->
{error, Reason}
end.
binary_match(Source, Pattern) ->
case binary:match(Source, Pattern) of
{Before, After} ->
{ok, {Before, After}};
nomatch ->
{error, nil}
end.
string_to_int(String, Base) ->
try
{ok, erlang:list_to_integer(String, Base)}
catch
error:badarg ->
{error, nil}
end.
file_open(Path) ->
case file:open(Path, [raw, binary]) of
{ok, Fd} ->
{ok, Fd};
{error, enoent} ->
{error, no_entry};
{error, eacces} ->
{error, no_access};
{error, eisdir} ->
{error, is_dir};
_ ->
{error, unknown_file_error}
end.
file_close(File) ->
case file:close(File) of
ok ->
{ok, nil};
{error, enoent} ->
{error, no_entry};
{error, eacces} ->
{error, no_access};
{error, eisdir} ->
{error, is_dir};
_ ->
{error, unknown_file_error}
end.
hpack_decode(Context, Bin) ->
case hpack:decode(Bin, Context) of
{ok, {Headers, NewContext}} ->
{ok, {Headers, NewContext}};
{error, compression_error} ->
{error, {hpack_error, compression}};
{error, {compression_error, {bad_header_packet, Binary}}} ->
{error, {hpack_error, {bad_header_packet, Binary}}}
end.
hpack_encode(Context, Headers) ->
hpack:encode(Headers, Context).
hpack_new_max_table_size(Context, Size) ->
hpack:new_max_table_size(Size, Context).
ets_lookup_element(Table, Key, Position) ->
try
{ok, ets:lookup_element(Table, Key, Position)}
catch
error:badarg ->
{error, nil}
end.
get_path_and_query(String) ->
case uri_string:parse(String) of
{error, Value, Term} ->
{error, {Value, Term}};
UriMap ->
Query =
case maps:find(query, UriMap) of
error ->
{error, nil};
Ok -> Ok
end,
{ok, {maps:get(path, UriMap), Query}}
end.
decode_atom(Value) when is_atom(Value) -> {ok, Value};
decode_atom(_Value) -> {error, nil}.