Skip to content

Commit

Permalink
Update to use latest cowboy API
Browse files Browse the repository at this point in the history
  • Loading branch information
yrashk committed Sep 26, 2012
1 parent 76fb799 commit 54f112b
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 36 deletions.
6 changes: 2 additions & 4 deletions lib/dynamo/cowboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule Dynamo.Cowboy do
"""
def run(app, options // []) do
:application.start(:ranch)
:application.start(:cowboy)

port = options[:port] || 4000
Expand All @@ -30,10 +31,7 @@ defmodule Dynamo.Cowboy do
IO.puts "Running #{inspect app} at localhost:#{port} with Cowboy on #{Dynamo.env}"
end

:cowboy.start_listener(app, acceptors,
:cowboy_tcp_transport, [port: port],
:cowboy_http_protocol, [dispatch: dispatch]
)
:cowboy.start_http(app, acceptors, [port: port], [dispatch: dispatch])
end

def shutdown(app) do
Expand Down
12 changes: 6 additions & 6 deletions lib/dynamo/cowboy/body_parser.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule Dynamo.Cowboy.BodyParser do
require :cowboy_http_req, as: R
require :cowboy_req, as: R

@moduledoc false

def parse(dict, req) do
{ type, req } = R.parse_header(:"Content-Type", req)
{ :ok, type, req } = R.parse_header("content-type", req)
parse_body(type, dict, req)
end

Expand All @@ -27,11 +27,11 @@ defmodule Dynamo.Cowboy.BodyParser do
{ acc, req }
end

defp parse_multipart({ { :headers, headers }, req }, nil, nil, acc) do
defp parse_multipart({ :headers, headers, req }, nil, nil, acc) do
parse_multipart(R.multipart_data(req), headers, "", acc)
end

defp parse_multipart({ { :body, tail }, req }, headers, body, acc) do
defp parse_multipart({ :body, tail, req }, headers, body, acc) do
parse_multipart(R.multipart_data(req), headers, body <> tail, acc)
end

Expand All @@ -41,7 +41,7 @@ defmodule Dynamo.Cowboy.BodyParser do
end

defp multipart_entry(headers, body, acc) do
case List.keyfind(headers, "Content-Disposition", 0) do
case List.keyfind(headers, "content-disposition", 0) do
{ _, value } ->
[_|parts] = String.split(value, ";", global: true)
parts = lc part inlist parts, do: to_multipart_kv(part)
Expand All @@ -51,7 +51,7 @@ defmodule Dynamo.Cowboy.BodyParser do
entry =
case List.keyfind(parts, "filename", 0) do
{ "filename", filename } ->
{ _, type } = List.keyfind(headers, :"Content-Type", 0) || { :"Content-Type", nil }
{ _, type } = List.keyfind(headers, "content-type", 0) || { "content-type", nil }
{ name, Dynamo.HTTP.File.new(name: name, filename: filename, content_type: type, body: body) }
_ ->
{ name, body }
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamo/cowboy/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Dynamo.Cowboy.Handler do
to respond to http and websockets requests.
"""

require :cowboy_http_req, as: R
require :cowboy_req, as: R
@behaviour :cowboy_http_handler

def init({ :tcp, :http }, req, app) do
Expand Down
24 changes: 14 additions & 10 deletions lib/dynamo/cowboy/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Dynamo.Cowboy.HTTP do
"""

@behaviour Dynamo.HTTP
require :cowboy_http_req, as: R
require :cowboy_req, as: R

Record.defmacros __ENV__, :connection,
[ :req, :path_info_segments, :script_name_segments, :req_headers,
Expand All @@ -23,9 +23,12 @@ defmodule Dynamo.Cowboy.HTTP do

@doc false
def new(req) do
{ segments, req } = R.path(req)
{ verb, req } = R.method(req)

{ binary, _ } = R.path req
{ segments, _, _} = :cowboy_dispatcher.split_path(binary,
fn(bin) -> :cowboy_http.urldecode(bin, :crash) end)

connection(
req: req,
original_method: verb,
Expand All @@ -51,17 +54,19 @@ defmodule Dynamo.Cowboy.HTTP do
## Request API

def query_string(connection(req: req)) do
{ query_string, _ } = R.raw_qs req
{ query_string, _ } = R.qs req
query_string
end

def path_segments(connection(req: req)) do
{ segments, _ } = R.path req
{ binary, _ } = R.path req
{ segments, _, _} = :cowboy_dispatcher.split_path(binary,
fn(bin) -> :cowboy_http.urldecode(bin, :crash) end)
segments
end

def path(connection(req: req)) do
{ binary, _ } = R.raw_path req
{ binary, _ } = R.path req
binary
end

Expand All @@ -73,7 +78,7 @@ defmodule Dynamo.Cowboy.HTTP do
## Response API

def send(_status, body,
connection(original_method: :HEAD)) when body != "" do
connection(original_method: "HEAD")) when body != "" do
raise Dynamo.HTTP.InvalidSendOnHeadError
end

Expand All @@ -92,7 +97,7 @@ defmodule Dynamo.Cowboy.HTTP do

def sendfile(path, connection(req: req) = conn) do
File.Stat[type: :regular, size: size] = File.stat!(path)
{ :ok, :cowboy_tcp_transport, socket } = :cowboy_http_req.transport(req)
{ :ok, :ranch_tcp, socket } = R.transport(req)
send(200, { size, fn -> :file.sendfile(path, socket) end }, conn)
end

Expand All @@ -106,7 +111,7 @@ defmodule Dynamo.Cowboy.HTTP do
## Misc

def fetch(:params, connection(req: req, params: nil) = conn) do
{ query_string, req } = R.raw_qs req
{ query_string, req } = R.qs req
params = Dynamo.HTTP.QueryParser.parse(query_string)
{ params, req } = Dynamo.Cowboy.BodyParser.parse(params, req)
connection(conn, req: req, params: params)
Expand All @@ -131,7 +136,6 @@ defmodule Dynamo.Cowboy.HTTP do

defp write_cookie({ key, value, opts }, req) do
opts = Keyword.update(opts, :http_only, true, fn(x) -> x end)
{ :ok, req } = R.set_resp_cookie(key, value, opts, req)
req
R.set_resp_cookie(key, value, opts, req)
end
end
4 changes: 2 additions & 2 deletions lib/dynamo/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Dynamo.HTTP do
## Examples
conn.method #=> :GET
conn.method #=> "GET"
"""
defcallback method(conn)
Expand All @@ -51,7 +51,7 @@ defmodule Dynamo.HTTP do
## Examples
conn.original_method #=> :GET
conn.original_method #=> "GET"
"""
defcallback original_method(conn)
Expand Down
1 change: 1 addition & 0 deletions lib/dynamo/router/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Dynamo.Router.Base do

@doc false
def service(conn) do
if is_binary(conn.method), do: conn = conn.method(binary_to_atom(conn.method))
dispatch(conn.method, conn.path_info_segments, conn)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/mix/tasks/dynamo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ defmodule Mix.Tasks.Dynamo do
end
defp deps do
[ { :cowboy, "0.6.1", github: "josevalim/cowboy" },
[ { :cowboy, %r(.*), github: "extend/cowboy" },
{ :ranch, %r(.*), github: "extend/ranch" },
{ :dynamo, "<%= @version %>", <%= @dynamo %> } ]
end
end
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ defmodule Dynamo.Mixfile do

def deps do
[ { :mimetypes, github: "spawngrid/mimetypes" },
{ :cowboy, github: "josevalim/cowboy" } ]
{ :ranch, github: "extend/ranch" },
{ :cowboy, github: "extend/cowboy" } ]
end

def test_deps do
Expand Down
7 changes: 4 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ "cowboy": "9ec5626171e0fb4b1e4bbb7a98f501866d004794",
[ "cowboy": "981ea359ba23564cb9bc3056a56dfcd1a98aa362",
"edown": "35f8296a25d14c5c1e3209399a2c1856700d9d5b",
"hackney": "041fdd3af5f2b9b5c2dbf687871def7e9f88cf0c",
"mimetypes": "e9dfab5aec98963589ecf13bdfcf0490667a730d" ]
"hackney": "77977858bf833e80abf16a276175320305108eb9",
"mimetypes": "e9dfab5aec98963589ecf13bdfcf0490667a730d",
"ranch": "cd099983b1b807b87fa050d1e4ff0a13aba25b49" ]
17 changes: 9 additions & 8 deletions test/dynamo/cowboy/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ defmodule Dynamo.Cowboy.HTTPTest do
end

def method(conn) do
assert conn.method == :GET
assert conn.original_method == :GET
assert conn.method == "GET"
assert conn.original_method == "GET"

conn = conn.method(:POST)
assert conn.method == :POST
assert conn.original_method == :GET
conn = conn.method("POST")
assert conn.method == "POST"
assert conn.original_method == "GET"

conn
end
Expand Down Expand Up @@ -181,6 +181,7 @@ defmodule Dynamo.Cowboy.HTTPTest do

headers = List.keydelete(headers, "Set-Cookie", 0)
assert List.keyfind(headers, "Set-Cookie", 0) == { "Set-Cookie","bar=baz; Version=1" }
# FIXME: Will fail until https://github.com/extend/cowboy/pull/247 is merged in
end

test :req_resp_cookies do
Expand Down Expand Up @@ -217,8 +218,8 @@ defmodule Dynamo.Cowboy.HTTPTest do
def req_headers(conn) do
conn = conn.fetch(:headers)
assert conn.req_headers["Host"] == "127.0.0.1:8011"
assert conn.req_headers["X-Special"] == "foo"
assert conn.req_headers["host"] == "127.0.0.1:8011"
assert conn.req_headers["x-special"] == "foo"
conn
end
Expand Down Expand Up @@ -291,7 +292,7 @@ defmodule Dynamo.Cowboy.HTTPTest do

test :sendfile do
{ 200, headers, "HELLO" } = request :get, "/sendfile"
assert List.keyfind(headers, "Content-Length", 0) == { "Content-Length", "5" }
assert List.keyfind(headers, "content-length", 0) == { "content-length", "5" }

assert { 500, _, _ } = request :get, "/invalid_sendfile"
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Mix.env(:dev)
Mix.shell(Mix.Shell.Process)
System.put_env("MIX_ENV", "dev")

:application.start(:ranch)
Dynamo.start(:dev, __FILE__)
ExUnit.start

Expand Down

0 comments on commit 54f112b

Please sign in to comment.