Skip to content

Commit

Permalink
Merge pull request #61 from vdayanand/v/http_fix
Browse files Browse the repository at this point in the history
Fix Mux tests for 0.7
  • Loading branch information
MikeInnes authored Jul 16, 2018
2 parents 26eccd9 + db7b9c2 commit edd4c67
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- nightly
matrix:
allow_failures:
Expand Down
6 changes: 2 additions & 4 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
julia 0.6
julia 0.7-
Lazy
Hiccup
HttpCommon
HttpServer
URIParser
AssetRegistry
WebSockets
HTTP
Compat 0.7.9
26 changes: 16 additions & 10 deletions src/basics.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Lazy, HttpServer, HttpCommon, URIParser
using Lazy, HTTP

import HTTP.Request

export respond, mux

Expand All @@ -13,17 +15,19 @@ function todict(req::Request)
req′ = Dict()
req′[:method] = req.method
req′[:headers] = req.headers
req′[:resource] = req.resource
req.data != "" && (req′[:data] = req.data)
req′[:resource] = req.target
req′[:data] = req.body
return req′
end

todict(app, req) = app(todict(req))

using HTTP.URIs: URI

function splitquery(app, req)
uri = URI(req[:resource])
delete!(req, :resource)
req[:path] = splitpath(uri.path)
req[:path] = splitpath(uri.path)
req[:query] = uri.query
app(req)
end
Expand All @@ -32,12 +36,14 @@ params!(req) = get!(req, :params, d())

# Response

import HttpCommon: Response
import HTTP: Response


Response(d::AbstractDict) =
Response(get(d, :status, 200),
convert(HTTP.Headers, get(d, :headers, HTTP.Headers()));
body = get(d, :body, ""))

Response(d::Associative) =
Response(get(d, :status, 200),
convert(Headers, get(d, :headers, HttpCommon.headers())),
get(d, :body, ""))

Response(o) = Response(stringmime(MIME"text/html"(), o))

Expand Down Expand Up @@ -84,6 +90,6 @@ function basiccatch(app, req)
println(io, "<pre class=\"box\">")
showerror(io, e, catch_backtrace())
println(io, "</pre>")
return d(:status => 500, :body => takebuf_string(io))
return d(:status => 500, :body => String(take!(io)))
end
end
15 changes: 8 additions & 7 deletions src/examples/files.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Hiccup
using Hiccup, Compat.Pkg
import Hiccup.div
import HttpServer.mimetypes

export files

Expand All @@ -13,11 +12,9 @@ function validpath(root, path; dirs = true)
end

ormatch(r::RegexMatch, x) = r.match
ormatch(r::Void, x) = x
ormatch(r::Nothing, x) = x

extension(f) = ormatch(match(r"(?<=\.)[^\.\\/]*$", f), "")

fileheaders(f) = d("Content-Type" => get(mimetypes, extension(f), "application/octet-stream"))
fileheaders(f) = d("Content-Type" => "application/octet-stream") # TODO: switch to using HTTP.sniff

fileresponse(f) = d(:file => f,
:body => read(f),
Expand Down Expand Up @@ -55,7 +52,11 @@ dirresponse(f) =

const ASSETS_DIR = "assets"
function packagefiles(dirs=true)
loadpaths = unique(vcat(Pkg.dir(), LOAD_PATH))
@static if VERSION < v"0.7.0-DEV"
loadpaths = unique(vcat(Pkg.dir(), LOAD_PATH))
else
loadpaths = LOAD_PATH
end
function absdir(req)
pkg = req[:params][:pkg]
for p in loadpaths
Expand Down
2 changes: 1 addition & 1 deletion src/routing.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HttpCommon
using HTTP

export method, GET, route, page, probabilty

Expand Down
29 changes: 20 additions & 9 deletions src/server.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HttpServer, Lazy
using HTTP.Servers, Lazy, Compat.Sockets

import HTTP.HandlerFunction
import Base.Meta.isexpr

export @app, serve
Expand All @@ -9,7 +10,7 @@ export @app, serve
# In general these methods provide a simple way to
# get up and running, but aren't meant to be comprehensive.

type App
mutable struct App
warez
end

Expand All @@ -27,27 +28,37 @@ macro app(def)
end
end

# conversion functions for known http_handler return objects
mk_response(d) = d
function mk_response(d::Dict)
r = HTTP.Response(get(d, :status, 200))
haskey(d, :body) && (r.body = d[:body])
haskey(d, :headers) && (r.headers = d[:headers])
return r
end

function http_handler(app::App)
handler = HttpHandler((req, res) -> app.warez(req))
handler.events["error"] = (client, error) -> println(error)
handler.events["listen"] = (port) -> println("Listening on $port...")
handler = HandlerFunction((req) -> mk_response(app.warez(req)))
# handler.events["error"] = (client, error) -> println(error)
# handler.events["listen"] = (port) -> println("Listening on $port...")
return handler
end

function ws_handler(app::App)
handler = WebSocketHandler((req, client) -> app.warez((req, client)))
handler = HandlerFunction((req, client) -> mk_response(app.warez((req, client))))
return handler
end

const default_port = 8000
const localhost = ip"127.0.0.1"

function serve(s::Server, port = default_port; kws...)
@async @errs run(s; port = port, kws...)
@async @errs HTTP.serve(s, localhost, port; kws...)
return
end

serve(h::App, port = default_port; kws...) =
serve(Server(http_handler(h)), port; kws...)
serve(Server(http_handler(h)), port; kws...)

serve(h::App, w::App, port = default_port) =
serve(Server(http_handler(h), ws_handler(w)), port)
serve(Server(http_handler(h), ws_handler(w)), port)
16 changes: 8 additions & 8 deletions src/websockets_integration.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using WebSockets

function todict(rc::Tuple{Request, WebSocket})
req, client = rc
req′ = todict(req)
req′[:socket] = client
return req′
end
#using WebSockets
#
#function todict(rc::Tuple{Request, WebSocket})
# req, client = rc
# req′ = todict(req)
# req′[:socket] = client
# return req′
#end

function wcatch(app, req)
try
Expand Down
2 changes: 1 addition & 1 deletion test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Requests
HTTP
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using Mux
using Base.Test
using Lazy
import Requests
using HTTP

@test Mux.notfound()(d())[:status] == 404

# Test basic server
@app test = (
Mux.defaults,
page(respond("<h1>Hello World!</h1>")),
page("/",respond("<h1>Hello World!</h1>")),
page("/about", respond("<h1>Boo!</h1>")),
page("/user/:user", req -> "<h1>Hello, $(req[:params][:user])!</h1>"),
Mux.notfound())
serve(test)
@test Requests.text(Requests.get("http://localhost:8000")) ==
@test String(HTTP.body(HTTP.get("http://localhost:8000"))) ==
"<h1>Hello World!</h1>"
@test Requests.text(Requests.get("http://localhost:8000/about")) ==
@test String(HTTP.body(HTTP.get("http://localhost:8000/about"))) ==
"<h1>Boo!</h1>"
@test Requests.text(Requests.get("http://localhost:8000/user/julia")) ==
@test String(HTTP.body(HTTP.get("http://localhost:8000/user/julia"))) ==
"<h1>Hello, julia!</h1>"

0 comments on commit edd4c67

Please sign in to comment.