Skip to content

Commit

Permalink
mod: .travis.yml Drop 0.5
Browse files Browse the repository at this point in the history
mod:   README.md                   Comment, issue JuliaWeb#43 URI field
mod:   REQUIRE                     Drop 0.5
mod:   src/HttpCommon.jl           AbstractString -> String. Constructors accepting AbstractString.
  • Loading branch information
hustf committed Nov 22, 2017
1 parent e546dc2 commit 3d68769
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- nightly
notifications:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ It has five fields:
* `resource`: the resource requested (e.g. "/hello/world")
* `headers`: see `Headers` above
* `data`: the data in the request as a vector of bytes
* `uri`: additional details, normally not used


#### `Cookie`
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.5
julia 0.6
URIParser
43 changes: 24 additions & 19 deletions src/HttpCommon.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__precompile__()

module HttpCommon

using URIParser: URI, unescape
Expand All @@ -15,14 +15,13 @@ include("status.jl")
"""
`Headers` represents the header fields for an HTTP request.
"""
const Headers = Dict{AbstractString, AbstractString}
const Headers = Dict{String, String}
headers() = Headers(
"Server" => "Julia/$VERSION",
"Content-Type" => "text/html; charset=utf-8",
"Content-Language" => "en",
"Date" => Dates.format(now(Dates.UTC), Dates.RFC1123Format) )


"""
A `Request` represents an HTTP request sent by a client to a server.
It has five fields:
Expand All @@ -32,15 +31,16 @@ It has five fields:
* `headers`: see `Headers` above
* `data`: the request data as a vector of bytes
"""
type Request
mutable struct Request
method::String # HTTP method string (e.g. "GET")
resource::String # Resource requested (e.g. "/hello/world")
headers::Headers
data::Vector{UInt8}
uri::URI
end
Request() = Request("", "", Headers(), UInt8[], URI(""))
Request(method, resource, headers, data) = Request(method, resource, headers, data, URI(""))
Request(method, resource, headers, data) =
Request(String(method), String(resource), Headers(headers), Vector{UInt8}(data), URI(""))

Base.show(io::IO, r::Request) = print(io, "Request(", r.uri, ", ",
length(r.headers), " headers, ",
Expand All @@ -52,12 +52,12 @@ A `Cookie` represents an HTTP cookie. It has three fields:
`name` and `value` are strings, and `attrs` is dictionary
of pairs of strings.
"""
type Cookie
mutable struct Cookie
name::String
value::String
attrs::Dict{String, String}
end
Cookie(name, value) = Cookie(name, value, Dict{String, String}())
Cookie(name, value) = Cookie(String(name), String(value), Dict{String, String}())
Base.show(io::IO, c::Cookie) = print(io, "Cookie(", c.name, ", ", c.value,
", ", length(c.attrs), " attributes)")

Expand All @@ -77,7 +77,7 @@ It has six fields:
Response has many constructors - use `methods(Response)` for full list.
"""
type Response
mutable struct Response
status::Int
headers::Headers
cookies::Dict{String, Cookie}
Expand All @@ -91,16 +91,21 @@ type Response
end
# If a Response is instantiated with all of fields except for `finished`,
# `finished` will default to `false`.
const HttpData = Union{Vector{UInt8}, AbstractString}
const HttpData = Union{Vector{UInt8}, String}
Response(s::Int, h::Headers, d::HttpData) =
Response(s, h, Dict{String, Cookie}(), d, Nullable(), Response[], false, Request[])
Response(s, h, Dict{String, Cookie}(), Vector{UInt8}(d), Nullable(), Response[], false, Request[])
Response(s::Int, h::Dict{AbstractString,AbstractString}, d::HttpData) =
Response(s, Headers(h), Dict{String, Cookie}(), Vector{UInt8}(d), Nullable(), Response[], false, Request[])
Response(s::Int, h::Headers) = Response(s, h, UInt8[])
Response(s::Int, d::HttpData) = Response(s, headers(), d)
Response(d::HttpData, h::Headers) = Response(200, h, d)
Response(d::HttpData) = Response(d, headers())
Response(s::Int, h::Dict{AbstractString,AbstractString}) = Response(s, Headers(h), UInt8[])
Response(s::Int, d::HttpData) = Response(s, headers(), Vector{UInt8}(d))
Response(d::HttpData, h::Headers) = Response(200, h, Vector{UInt8}(d))
Response(d::HttpData, h::Dict{AbstractString,AbstractString}) = Response(200, Headers(h), Vector{UInt8}(d))
Response(d::HttpData) = Response(Vector{UInt8}(d), headers())
Response(s::Int) = Response(s, headers(), UInt8[])
Response() = Response(200)


function Base.show(io::IO, r::Response)
print(io, "Response(", r.status, " ", get(STATUS_CODES, r.status, "Unknown Code"), ", ",
length(r.headers)," headers, ",
Expand All @@ -109,23 +114,23 @@ end


"""
escapeHTML(i::AbstractString)
escapeHTML(i::String)
Returns a string with special HTML characters escaped: &, <, >, ", '
"""
function escapeHTML(i::AbstractString)
function escapeHTML(i::String)
# Refer to http://stackoverflow.com/a/7382028/3822752 for spec. links
o = replace(i, "&", "&amp;")
o = replace(o, "\"", "&quot;")
o = replace(o, "'", "&#39;")
o = replace(o, "<", "&lt;")
o = replace(o, ">", "&gt;")
return o
return o
end


"""
parsequerystring(query::AbstractString)
parsequerystring(query::String)
Convert a valid querystring to a Dict:
Expand All @@ -135,8 +140,8 @@ Convert a valid querystring to a Dict:
# "baz" => "<a href='http://www.hackershool.com'>hello world!</a>"
# "foo" => "bar"
"""
function parsequerystring{T<:AbstractString}(query::T)
q = Dict{AbstractString,AbstractString}()
function parsequerystring(query::T) where T<:AbstractString
q = Dict{String,String}()
length(query) == 0 && return q
for field in split(query, "&")
keyval = split(field, "=")
Expand Down

0 comments on commit 3d68769

Please sign in to comment.