Request
struct Request
An incoming HTTP request
[src]
pub fn method(self): Method {
return self.base.method;
}
Returns the method of an incoming HTTP request.
[src]
pub fn method(self): Method {
return self.base.method;
}
Returns the method of an incoming HTTP request.
Returns the method of an incoming HTTP request.
[src]
pub fn uri(self): Uri {
return self.base.uri;
}
Returns the Uri of request
[src]
pub fn uri(self): Uri {
return self.base.uri;
}
Returns the Uri of request
Returns the Uri of request
[src]
pub fn headers(self): Headers {
return self.base.headers;
}
Returns the headers of request URI.
[src]
pub fn headers(self): Headers {
return self.base.headers;
}
Returns the headers of request URI.
Returns the headers of request URI.
[src]
pub fn body(self): ReadClose? {
return self.base.body;
}
Returns the body of the request.
[src]
pub fn body(self): ReadClose? {
return self.base.body;
}
Returns the body of the request.
Returns the body of the request.
[src]
pub fn set_body(self, body: ReadClose?): Request {
self.base.body = body;
return self;
}
Changes the body of the request.
[src]
pub fn set_body(self, body: ReadClose?): Request {
self.base.body = body;
return self;
}
Changes the body of the request.
Changes the body of the request.
[src]
pub fn text(self): string throws HttpError {
do {
return try self.base.body?.read_to_string() || "";
} catch(e) {
throw HttpError { status: http.BadRequest, source: e };
}
}
Receives the body and parses it as utf8 string.
[src]
pub fn text(self): string throws HttpError {
do {
return try self.base.body?.read_to_string() || "";
} catch(e) {
throw HttpError { status: http.BadRequest, source: e };
}
}
Receives the body and parses it as utf8 string.
Receives the body and parses it as utf8 string.
[src]
pub fn bytes(self): Bytes throws HttpError {
do {
let buf = Bytes.new();
if (let body = self.base.body) {
try body.read_to_end(buf);
}
return buf;
} catch(e) {
throw HttpError { status: http.BadRequest, source: e };
}
}
Receives the body as binary.
[src]
pub fn bytes(self): Bytes throws HttpError {
do {
let buf = Bytes.new();
if (let body = self.base.body) {
try body.read_to_end(buf);
}
return buf;
} catch(e) {
throw HttpError { status: http.BadRequest, source: e };
}
}
Receives the body as binary.
Receives the body as binary.
[src]
pub fn json<T>(self): T throws HttpError {
do {
return try json.parse(self.bytes());
} catch(e) {
throw HttpError { status: http.BadRequest, source: e };
}
}
Parse the body as JSON.
[src]
pub fn json<T>(self): T throws HttpError {
do {
return try json.parse(self.bytes());
} catch(e) {
throw HttpError { status: http.BadRequest, source: e };
}
}
Parse the body as JSON.
Parse the body as JSON.
parse_query
pub fn parse_query<T>(self): T throws HttpError
[src]
pub fn parse_query<T>(self): T throws HttpError {
do {
let data = try querystring.parse::<T>(self.text());
return data;
} catch(e: querystring.QsError) {
throw HttpError {
status: http.BadRequest,
source: e,
};
} catch(e: HttpError) {
throw e;
}
}
Parse the query string into given type.
parse_query
pub fn parse_query<T>(self): T throws HttpError
[src]
pub fn parse_query<T>(self): T throws HttpError {
do {
let data = try querystring.parse::<T>(self.text());
return data;
} catch(e: querystring.QsError) {
throw HttpError {
status: http.BadRequest,
source: e,
};
} catch(e: HttpError) {
throw e;
}
}
Parse the query string into given type.
Parse the query string into given type.
path_param
pub fn path_param(self, name: string): string?
[src]
pub fn path_param(self, name: string): string? {
return self.path_params?.get(name);
}
Returns the path parameter with the given name.
path_param
pub fn path_param(self, name: string): string?
[src]
pub fn path_param(self, name: string): string? {
return self.path_params?.get(name);
}
Returns the path parameter with the given name.
Returns the path parameter with the given name.
parse_path_params
pub fn parse_path_params<T>(self): T throws HttpError
[src]
pub fn parse_path_params<T>(self): T throws HttpError {
do {
return try parse_path_params::<T>(self.path_params);
} catch(e: ParsePathParamError) {
throw HttpError {
status: http.BadRequest,
source: e,
};
}
}
Parses the path parameters into given type.
parse_path_params
pub fn parse_path_params<T>(self): T throws HttpError
[src]
pub fn parse_path_params<T>(self): T throws HttpError {
do {
return try parse_path_params::<T>(self.path_params);
} catch(e: ParsePathParamError) {
throw HttpError {
status: http.BadRequest,
source: e,
};
}
}
Parses the path parameters into given type.
Parses the path parameters into given type.
remote_addr
pub fn remote_addr(self): Addr?
[src]
pub fn remote_addr(self): Addr? {
return self.remote_addr;
}
Returns the remote address of the request.
remote_addr
pub fn remote_addr(self): Addr?
[src]
pub fn remote_addr(self): Addr? {
return self.remote_addr;
}
Returns the remote address of the request.
Returns the remote address of the request.
[src]
pub fn real_ip(self): Ip? {
if (let ip = self.base.headers.get("x-real-ip").and_then(Ip.parse)) {
return ip;
}
if (let list = self.base.headers.get("x-forwarded-for").and_then(|s| s.split(","))) {
for (let s in list) {
if (let ip = Ip.parse(s.trim())) {
return ip;
}
}
}
return self.remote_addr.and_then(|addr| addr.(TcpAddr))?.ip();
}
Returns the real IP address from the request.
[src]
pub fn real_ip(self): Ip? {
if (let ip = self.base.headers.get("x-real-ip").and_then(Ip.parse)) {
return ip;
}
if (let list = self.base.headers.get("x-forwarded-for").and_then(|s| s.split(","))) {
for (let s in list) {
if (let ip = Ip.parse(s.trim())) {
return ip;
}
}
}
return self.remote_addr.and_then(|addr| addr.(TcpAddr))?.ip();
}
Returns the real IP address from the request.
Returns the real IP address from the request.
content_type
pub fn content_type(self): string?
[src]
pub fn content_type(self): string? {
return self.base.headers.get("content-type");
}
Returns the content type of the request.
content_type
pub fn content_type(self): string?
[src]
pub fn content_type(self): string? {
return self.base.headers.get("content-type");
}
Returns the content type of the request.
Returns the content type of the request.
[src]
pub fn form<T>(self): T throws HttpError {
do {
if (self.base.method == Method.Get) {
return try querystring.parse(self.base.uri.query() || "");
}
let content_type = try self
.content_type()
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect content type `application/x-www-form-urlencoded`",
});
if (!content_type.starts_with("application/x-www-form-urlencoded")) {
throw HttpError {
status: http.BadRequest,
source: `invalid content type "${content_type}", expect: "application/x-www-form-urlencoded"`,
};
}
return try querystring.parse(self.text());
} catch(e: querystring.QsError) {
throw HttpError { status: http.BadRequest, source: e };
} catch(e: HttpError) {
throw e;
}
}
Parse the form data into given type.
[src]
pub fn form<T>(self): T throws HttpError {
do {
if (self.base.method == Method.Get) {
return try querystring.parse(self.base.uri.query() || "");
}
let content_type = try self
.content_type()
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect content type `application/x-www-form-urlencoded`",
});
if (!content_type.starts_with("application/x-www-form-urlencoded")) {
throw HttpError {
status: http.BadRequest,
source: `invalid content type "${content_type}", expect: "application/x-www-form-urlencoded"`,
};
}
return try querystring.parse(self.text());
} catch(e: querystring.QsError) {
throw HttpError { status: http.BadRequest, source: e };
} catch(e: HttpError) {
throw e;
}
}
Parse the form data into given type.
Parse the form data into given type.
[src]
pub fn multipart(self): Multipart throws HttpError {
let content_type = try self
.content_type()
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect content type `multipart/form-data`",
});
let boundary = try _Multipart
.parse_boundary(content_type)
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect boundary",
});
let body = try self
.base
.body
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect request body",
});
return _Multipart.new(_create_native_read(body), boundary) as Multipart;
}
Returns the multipart form data.
[src]
pub fn multipart(self): Multipart throws HttpError {
let content_type = try self
.content_type()
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect content type `multipart/form-data`",
});
let boundary = try _Multipart
.parse_boundary(content_type)
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect boundary",
});
let body = try self
.base
.body
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect request body",
});
return _Multipart.new(_create_native_read(body), boundary) as Multipart;
}
Returns the multipart form data.
Returns the multipart form data.
Example
use std.io.Bytes;
use std.net.http.server.{HttpServer, func_handler, Response};
use std.net.TcpListener;
fn main() throws {
let app = func_handler(|req| {
let multipart = try req.multipart();
while (let field = try multipart.next()) {
let name = field.name();
let content_type = field.content_type();
let buf = Bytes.new();
try field.read_to_end(buf);
// do something
// ..
}
return Response.text("hello world!");
});
let listener = try TcpListener.bind("127.0.0.1:8080");
let server = HttpServer.new(listener, app);
try server.serve();
}
websocket
pub fn websocket(self, handler: |(Websocket)|, sub_protocols: [string]? = nil): Response throws HttpError
[src]
pub fn websocket(
self, handler: |(Websocket) throws|, sub_protocols: [string]? = nil
): Response throws HttpError {
let on_upgrade = try self
.on_upgrade
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "cannot upgrade to websocket connection",
});
if (self.base.method != Method.Get) {
throw HttpError {
status: http.BadRequest,
source: "invalid method, expect GET",
};
}
if (self.base.headers.get("upgrade")?.equal_ignore_ascii_case("websocket") != true) {
throw HttpError {
status: http.BadRequest,
source: "expect `upgrade: websocket`",
};
}
if (self.base.headers.get("sec-websocket-version") != "13") {
throw HttpError {
status: http.BadRequest,
source: "expect `sec-websocket-version: 13`",
};
}
if (self.base.headers.get("connection")?.to_lowercase()?.contains("upgrade") != true) {
throw HttpError {
status: http.BadRequest,
source: "expect `connection: upgrade`",
};
}
let key = try self
.base
.headers
.get("sec-websocket-key")
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect `sec-websocket-key`",
});
let protocol: string? = nil;
if (let sub_protocols = sub_protocols) {
if (let request_protocols = self.base.headers.get("sec-websocket-protocol")) {
for (let request_protocol in request_protocols.split(",")) {
let request_protocol = request_protocol.trim();
if (sub_protocols.contains(request_protocol)) {
protocol = request_protocol;
break;
}
}
}
}
spawn {
do {
let io = try on_upgrade.upgrade();
let websocket = _Websocket.accept(_create_native_readwriteclose(io), protocol) as Websocket;
try handler(websocket);
}
}
let resp = Response.new(http.SwitchingProtocols);
let headers = resp.headers();
headers.set("connection", "upgrade");
headers.set("upgrade", "websocket");
headers.set("sec-websocket-accept", derive_accept_key(key));
if (let protocol = protocol) {
headers.set("sec-websocket-protocol", protocol);
}
return resp;
}
Upgrades the connection to a websocket connection.
websocket
pub fn websocket(self, handler: |(Websocket)|, sub_protocols: [string]? = nil): Response throws HttpError
[src]
pub fn websocket(
self, handler: |(Websocket) throws|, sub_protocols: [string]? = nil
): Response throws HttpError {
let on_upgrade = try self
.on_upgrade
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "cannot upgrade to websocket connection",
});
if (self.base.method != Method.Get) {
throw HttpError {
status: http.BadRequest,
source: "invalid method, expect GET",
};
}
if (self.base.headers.get("upgrade")?.equal_ignore_ascii_case("websocket") != true) {
throw HttpError {
status: http.BadRequest,
source: "expect `upgrade: websocket`",
};
}
if (self.base.headers.get("sec-websocket-version") != "13") {
throw HttpError {
status: http.BadRequest,
source: "expect `sec-websocket-version: 13`",
};
}
if (self.base.headers.get("connection")?.to_lowercase()?.contains("upgrade") != true) {
throw HttpError {
status: http.BadRequest,
source: "expect `connection: upgrade`",
};
}
let key = try self
.base
.headers
.get("sec-websocket-key")
.ok_or_else(|| HttpError {
status: http.BadRequest,
source: "expect `sec-websocket-key`",
});
let protocol: string? = nil;
if (let sub_protocols = sub_protocols) {
if (let request_protocols = self.base.headers.get("sec-websocket-protocol")) {
for (let request_protocol in request_protocols.split(",")) {
let request_protocol = request_protocol.trim();
if (sub_protocols.contains(request_protocol)) {
protocol = request_protocol;
break;
}
}
}
}
spawn {
do {
let io = try on_upgrade.upgrade();
let websocket = _Websocket.accept(_create_native_readwriteclose(io), protocol) as Websocket;
try handler(websocket);
}
}
let resp = Response.new(http.SwitchingProtocols);
let headers = resp.headers();
headers.set("connection", "upgrade");
headers.set("upgrade", "websocket");
headers.set("sec-websocket-accept", derive_accept_key(key));
if (let protocol = protocol) {
headers.set("sec-websocket-protocol", protocol);
}
return resp;
}
Upgrades the connection to a websocket connection.
Upgrades the connection to a websocket connection.
cookie_jar
pub fn cookie_jar(self): CookieJar
[src]
pub fn cookie_jar(self): CookieJar {
if (let cookie_jar = self.cookie_jar) {
return cookie_jar;
}
let cookie_jar = CookieJar.extract_from_headers(self.headers());
cookie_jar.set_key(self.cookie_key);
self.cookie_jar = cookie_jar;
return cookie_jar;
}
Returns the cookie jar of the request.
cookie_jar
pub fn cookie_jar(self): CookieJar
[src]
pub fn cookie_jar(self): CookieJar {
if (let cookie_jar = self.cookie_jar) {
return cookie_jar;
}
let cookie_jar = CookieJar.extract_from_headers(self.headers());
cookie_jar.set_key(self.cookie_key);
self.cookie_jar = cookie_jar;
return cookie_jar;
}
Returns the cookie jar of the request.
Returns the cookie jar of the request.