Skip to content

Commit 6f6bdf3

Browse files
committed
Add simple http client
Add simple http client that can be used in different situations, such as when performing updates, inspired to Mint Elixir http client. Signed-off-by: Davide Bettio <[email protected]>
1 parent 2f70efb commit 6f6bdf3

File tree

5 files changed

+394
-0
lines changed

5 files changed

+394
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Support for DragonFly BSD (generic_unix platform).
1212
- Added guards `is_even` and `is_odd` to the `Integer` module
1313
- Add a number of functions to proplists module, such as `delete/2`, `from/to_map/1`, etc...
14+
- Simple http client, that can be used for different use case such as downloading OTA updates
1415

1516
### Fixed
1617

examples/erlang/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ pack_runnable(code_lock code_lock estdlib eavmlib)
4040
pack_runnable(mqtt_client mqtt_client estdlib eavmlib)
4141
pack_runnable(network_console network_console estdlib eavmlib alisp)
4242
pack_runnable(logging_example logging_example estdlib eavmlib)
43+
pack_runnable(http_client http_client estdlib eavmlib)

examples/erlang/http_client.erl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2024 Davide Bettio <[email protected]>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(http_client).
22+
-export([start/0]).
23+
24+
start() ->
25+
ssl:start(),
26+
{ok, Conn} = ahttp_client:connect(https, "atomvm.net", 443, [
27+
{active, false}, {verify, verify_none}, {parse_headers, [<<"Location">>]}
28+
]),
29+
{ok, Conn2, _Ref} = ahttp_client:request(Conn, <<"GET">>, <<"/">>, [], undefined),
30+
loop(Conn2, {active, false}).
31+
32+
loop(Conn, {active, false}) ->
33+
case ahttp_client:recv(Conn, 0) of
34+
{ok, _Conn, closed} ->
35+
io:format("Connection closed.~n");
36+
{ok, UpdatedConn, Responses} ->
37+
io:format("Got: ~p~n", [Responses]),
38+
case maybe_terminate(Responses, UpdatedConn) of
39+
ok -> loop(UpdatedConn, {active, false});
40+
closed -> ok
41+
end;
42+
Other ->
43+
io:format("Unexpected reply: ~p~n", [Other])
44+
end;
45+
loop(Conn, {active, true}) ->
46+
receive
47+
Message ->
48+
case ahttp_client:stream(Conn, Message) of
49+
{ok, _Conn, closed} ->
50+
io:format("Connection closed.~n");
51+
{ok, UpdatedConn, Responses} ->
52+
io:format("Got: ~p~n", [Responses]),
53+
loop(UpdatedConn, {active, true});
54+
unknown ->
55+
io:format("Unexpected message: ~p~n", [Message])
56+
end
57+
end.
58+
59+
maybe_terminate([], _Conn) ->
60+
ok;
61+
maybe_terminate([{done, _Ref}], Conn) ->
62+
ahttp_client:close(Conn),
63+
closed;
64+
maybe_terminate([_H | T], Conn) ->
65+
maybe_terminate(T, Conn).

libs/eavmlib/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ project(eavmlib)
2323
include(BuildErlang)
2424

2525
set(ERLANG_MODULES
26+
ahttp_client
2627
atomvm
2728
avm_pubsub
2829
console

0 commit comments

Comments
 (0)