About
An implementation of the HTTP protocol in the Mys programming language.
API
http — Various HTTP helpers
class HttpError(Error):
message: string
class Response:
status_code: i64
headers: {string: string}
content: bytes
func get(host: string,
port: i64 = 80,
path: string = "/",
secure: bool = False,
headers: {string: string} = {},
check: bool = True) -> Response:
Get given path from given address.
func post(host: string,
content: bytes?,
port: i64 = 80,
path: string = "/",
secure: bool = False,
headers: {string: string} = {},
check: bool = True) -> Response:
Post given path and content to given address.
http.header_parser — Header parser
class Request:
A HTTP request.
method: string
path: string
query: string?
params: {string: string}
fragment: string?
headers: {string: string}
func parse_request(header: bytes) -> Request:
Parse given request header.
func parse_request(header: bytes, size: i64) -> Request:
Parse given request header.
func parse_query(value: string) -> {string: string}:
Parse given parameters and return them as a dictionary.
http.server — A HTTP server
enum Status(i64):
HTTP response status codes.
Continue
Ok
Found
BadRequest
Unauthorized
NotFound
MethodNotAllowed
InternalServerError
trait Route:
A route.
func on_get(self, request: Request) -> Response:
Handle a GET request.
func on_post(self, request: Request) -> Response:
Handle a POST request.
func on_delete(self, request: Request) -> Response:
Handle a DELETE request.
trait Response:
A response.
func write(self, client: Client):
Write the response to the client.
class ResponseError(Error):
status: Status
class Request:
method: string
path: string
query: string
params: {string: string}
fragment: string
headers: {string: string}
matches: [string]?
class StringResponse(Response):
status: Status
data: string
func __init__(self, data: string, status: Status = Status.Ok):
func write(self, client: Client):
class FileResponse(Response):
status: Status
path: Path
begin: i64?
end: i64?
func __init__(self,
path: Path,
status: Status = Status.Ok,
begin: i64? = None,
end: i64? = None):
func write(self, client: Client):
class EmptyResponse(Response):
status: Status
func __init__(self, status: Status = Status.Ok):
func write(self, client: Client):
class Server:
A HTTP server.
func __init__(self):
func add_route(self, path: string, route: Route):
Add a route for given path.
func add_route_regex(self, path: regex, route: Route):
Add a route for given path.
func serve(self, port: i64):
Serve HTTP requests.