Skip to content

Commit

Permalink
recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 23, 2022
1 parent b534ae7 commit 4bad5c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/conv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Servy.Conv do
defstruct method: "",
path: "",
params: %{},
headers: %{},
resp_body: "",
status: nil

Expand Down
26 changes: 23 additions & 3 deletions lib/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,36 @@ defmodule Servy.Parser do

[method, path, _] = String.split(request_line, " ")

params = parse_params(params_string)
headers = parse_headers(header_lines, %{})

params = parse_params(headers["Content-Type"], params_string)

%Conv{
method: method,
path: path,
params: params
params: params,
headers: headers
}
end

def parse_params(params_string) do
def parse_headers([head | tail], headers) do
# IO.puts("Head: #{inspect(head)} Tail: #{inspect(tail)}")

[key, value] = String.split(head, ": ")
headers = Map.put(headers, key, value)

# IO.puts("Key: #{inspect(key)} Value: #{inspect(value)}")

headers = Map.put(%{}, key, value)
# IO.inspect(headers)
parse_headers(tail, headers)
end

def parse_headers([], headers), do: headers

def parse_params("application/x-www-form-urlencoded", params_string) do
params_string |> String.trim() |> URI.decode_query()
end

def parse_params(_, _), do: %{}
end

0 comments on commit 4bad5c2

Please sign in to comment.