HttpCommon.jl
Installation: julia> Pkg.add("HttpCommon")
This package provides types and helper functions for dealing with the HTTP protocol in Julia:
- Types to represent
Headers, Requests, Cookies, and Responses
- A dictionary of
STATUS_CODES that maps HTTP codes to descriptions
- Utility functions
escapeHTMLand parsequerystring
HTTP Types
Headers
Headers represents the header fields for an HTTP request, and is type alias for Dict{AbstractString,AbstractString}.
There is a default constructor, headers, that produces a reasonable default set of headers:
Dict( "Server" => "Julia/$VERSION",
"Content-Type" => "text/html; charset=utf-8",
"Content-Language" => "en",
"Date" => Dates.format(now(Dates.UTC), Dates.RFC1123Format) )
Request
A Request represents an HTTP request sent by a client to a server.
It has five fields:
method: an HTTP methods string (e.g. "GET")
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
A Cookie represents an HTTP cookie. It has three fields:
name and value are strings, and attrs is dictionary
of pairs of strings.
Response
A Response represents an HTTP response sent to a client by a server.
It has six fields:
status: HTTP status code (see STATUS_CODES) [default: 200]
headers: Headers [default: HttpCommmon.headers()]
cookies: Dictionary of strings => Cookies
data: the request data as a vector of bytes [default: UInt8[]]
finished: true if the Reponse is valid, meaning that it can be
converted to an actual HTTP response [default: false]
requests: the history of requests that generated the response.
Can be greater than one if a redirect was involved.
Response has many constructors - use methods(Response) for full list.
Constants
STATUS_CODES
STATUS_CODES is a dictionary (Int => AbstractString) that maps all the
status codes defined in RFC's to their descriptions, e.g.
STATUS_CODES[200] # => "OK"
STATUS_CODES[404] # => "Not Found"
STATUS_CODES[418] # => "I'm a teapot"
STATUS_CODES[500] # => "Internal Server Error"
Utility functions
escapeHTML(i::AbstractString)
Returns a string with special HTML characters escaped: &, <, >, ", '
parsequerystring(query::AbstractString)
Convert a valid querystring to a Dict:
q = "foo=bar&baz=%3Ca%20href%3D%27http%3A%2F%2Fwww.hackershool.com%27%3Ehello%20world%21%3C%2Fa%3E"
parsequerystring(q)
# Dict{String,String} with 2 entries:
# "baz" => "<a href='http://www.hackershool.com'>hello world!</a>"
# "foo" => "bar"
:::::::::::::
:: ::
:: Made at ::
:: ::
:::::::::::::
::
Hacker School
:::::::::::::