Skip to content

Commit

Permalink
web sockets
Browse files Browse the repository at this point in the history
curl request doesn't work
  • Loading branch information
hexisdylan committed Jul 24, 2022
1 parent 6f8c398 commit afc7639
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Servy.Handler do
alias Servy.Conv
alias Servy.BearController
import Servy.Plugins, only: [rewrite_path: 1, log: 1, track: 1]
import Servy.Parser, only: [parse: 1]
import Servy.Parser, only: [parse: 1]
@moduledoc "Handles HTTP requests."
# @pages_path Path.expand("../pages", __DIR__)
@doc "Transforms the request into a response."
Expand Down
92 changes: 92 additions & 0 deletions lib/http_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
defmodule Servy.HttpServer do
@doc """
Starts the server on the given `port` of localhost.
"""
def start(port) when is_integer(port) and port > 1023 do
# Creates a socket to listen for client connections.
# `listen_socket` is bound to the listening socket.
{:ok, listen_socket} =
:gen_tcp.listen(port, [:binary, packet: :raw, active: false, reuseaddr: true])

# Socket options (don't worry about these details):
# `:binary` - open the socket in "binary" mode and deliver data as binaries
# `packet: :raw` - deliver the entire binary without doing any packet handling
# `active: false` - receive data when we're ready by calling `:gen_tcp.recv/2`
# `reuseaddr: true` - allows reusing the address if the listener crashes

IO.puts("\n🎧 Listening for connection requests on port #{port}...\n")

accept_loop(listen_socket)
end

@doc """
Accepts client connections on the `listen_socket`.
"""
def accept_loop(listen_socket) do
IO.puts("⌛️ Waiting to accept a client connection...\n")

# Suspends (blocks) and waits for a client connection. When a connection
# is accepted, `client_socket` is bound to a new client socket.
{:ok, client_socket} = :gen_tcp.accept(listen_socket)

IO.puts("⚡️ Connection accepted!\n")

# Receives the request and sends a response over the client socket.
serve(client_socket)

# Loop back to wait and accept the next connection.
accept_loop(listen_socket)
end

@doc """
Receives the request on the `client_socket` and
sends a response back over the same socket.
"""
def serve(client_socket) do
client_socket
|> read_request
# |> generate_response
|> Servy.Handler.handle()
|> write_response(client_socket)
end

@doc """
Receives a request on the `client_socket`.
"""
def read_request(client_socket) do
# all available bytes
{:ok, request} = :gen_tcp.recv(client_socket, 0)

IO.puts("➡️ Received request:\n")
IO.puts(request)

request
end

@doc """
Returns a generic HTTP response.
"""
def generate_response(_request) do
"""
HTTP/1.1 200 OK\r
Content-Type: text/plain\r
Content-Length: 6\r
\r
Hello!
"""
end

@doc """
Sends the `response` over the `client_socket`.
"""
def write_response(response, client_socket) do
:ok = :gen_tcp.send(client_socket, response)

IO.puts("⬅️ Sent response:\n")
IO.puts(response)

# Closes the client socket, ending the connection.
# Does not close the listen socket!
:gen_tcp.close(client_socket)
end
end

1 comment on commit afc7639

@dids-reyes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore commit description: curl requests work

Please sign in to comment.