Skip to content

Commit

Permalink
feat: handle QUIT command
Browse files Browse the repository at this point in the history
The client uses the QUIT command to terminate the session.  The
server MUST acknowledge the QUIT command and then close the
connection to the client.  This is the preferred method for a client
to indicate that it has finished all of its transactions with the
NNTP server.
  • Loading branch information
sntran committed Feb 28, 2021
1 parent 2a11db7 commit 295e545
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/gen_nntp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ handle_info(timeout, #client{module =Module, transport = Transport} = Client) ->
Transport:send(Socket, "200 Service available, posting allowed\r\n"),
{noreply, Client};

% The client uses the QUIT command to terminate the session. The server
% MUST acknowledge the QUIT command and then close the connection to
% the client.
handle_info({tcp, Socket, <<"QUIT\r\n">>}, #client{transport = Transport} = Client) ->
Transport:send(Socket, <<"205 Connection closing\r\n">>),
Transport:close(Socket),
{noreply, Client};

handle_info({tcp, Socket, Line}, Client) ->
#client{transport = Transport, module = Module, state = State} = Client,
ok = Transport:setopts(Socket, [{active, once}]),
Expand Down
14 changes: 10 additions & 4 deletions test/gen_nntp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ defmodule GenNNTPTest do

opts = [:binary, packet: :line, active: false]
{:ok, socket} = :gen_tcp.connect('localhost', 119, opts)
%{socket: socket}
{:ok, greeting} = :gen_tcp.recv(socket, 0, 1000)
%{socket: socket, greeting: greeting}
end

test "MUST send a 200 greeting", %{socket: socket} do
{:ok, data} = :gen_tcp.recv(socket, 0, 1000)
assert data =~ ~r/^200 /
test "MUST send a 200 greeting", %{greeting: greeting} do
assert greeting =~ ~r/^200 /
end

test "MUST send a 205 if the client sends QUIT", %{socket: socket} do
:ok = :gen_tcp.send(socket, "QUIT\r\n")
{:ok, data} = :gen_tcp.recv(socket, 0)
assert data =~ ~r/^205 /
end

end
Expand Down

0 comments on commit 295e545

Please sign in to comment.