Installation: julia> Pkg.add("HttpCommon")
This package provides types and helper functions for dealing with the HTTP protocol in Julia:
- Types to represent
Headers
,Request
s,Cookie
s, andResponse
s - A dictionary of
STATUS_CODES
that maps HTTP codes to descriptions - Utility functions
escapeHTML
andparsequerystring
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) )
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
: seeHeaders
abovedata
: the data in the request as a vector of bytesuri
: additional details, normally not used
A Cookie
represents an HTTP cookie. It has three fields:
name
and value
are strings, and attrs
is dictionary
of pairs of strings.
A Response
represents an HTTP response sent to a client by a server.
It has six fields:
status
: HTTP status code (seeSTATUS_CODES
) [default:200
]headers
:Headers
[default:HttpCommmon.headers()
]cookies
: Dictionary of strings =>Cookie
sdata
: the request data as a vector of bytes [default:UInt8[]
]finished
:true
if theReponse
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.
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"
Returns a string with special HTML characters escaped: &, <, >, ", '
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
:::::::::::::