Response
type Response = HttpResponse
An outgoing HTTP response
new
pub fn new(status: StatusCode): Response
[src]
pub fn new(status: StatusCode): Response {
return HttpResponse {
status: status,
headers: Headers.new(),
body: nil,
} as Response;
}
Creates a new HTTP response.
new
pub fn new(status: StatusCode): Response
[src]
pub fn new(status: StatusCode): Response {
return HttpResponse {
status: status,
headers: Headers.new(),
body: nil,
} as Response;
}
Creates a new HTTP response.
Creates a new HTTP response.
[src]
pub fn text(value: string): Response {
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.TEXT_PLAIN.to_string()}),
body: Cursor.new(value),
} as Response).set_content_length(value.len());
}
Creates a new HTTP response with text body, and also sets the Content-Type: text/plain
header.
[src]
pub fn text(value: string): Response {
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.TEXT_PLAIN.to_string()}),
body: Cursor.new(value),
} as Response).set_content_length(value.len());
}
Creates a new HTTP response with text body, and also sets the Content-Type: text/plain
header.
Creates a new HTTP response with text body, and also sets the Content-Type: text/plain
header.
[src]
pub fn html(value: string): Response {
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.TEXT_HTML_UTF_8.to_string()}),
body: Cursor.new(value),
} as Response).set_content_length(value.len());
}
Creates a new HTTP response with html body, and also sets the Content-Type: text/html; charset=utf-8
header.
[src]
pub fn html(value: string): Response {
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.TEXT_HTML_UTF_8.to_string()}),
body: Cursor.new(value),
} as Response).set_content_length(value.len());
}
Creates a new HTTP response with html body, and also sets the Content-Type: text/html; charset=utf-8
header.
Creates a new HTTP response with html body, and also sets the Content-Type: text/html; charset=utf-8
header.
[src]
pub fn bytes(value: Bytes): Response {
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.APPLICATION_OCTET_STREAM.to_string()}),
body: Cursor.new(value),
} as Response).set_content_length(value.len());
}
Creates a new HTTP response with bytes body, and also sets the Content-Type: application/octet-stream
header.
[src]
pub fn bytes(value: Bytes): Response {
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.APPLICATION_OCTET_STREAM.to_string()}),
body: Cursor.new(value),
} as Response).set_content_length(value.len());
}
Creates a new HTTP response with bytes body, and also sets the Content-Type: application/octet-stream
header.
Creates a new HTTP response with bytes body, and also sets the Content-Type: application/octet-stream
header.
[src]
pub fn json<T>(value: T): Response throws json.JsonError {
let body = try json.to_string(value);
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.APPLICATION_JSON.to_string()}),
body: Cursor.new(body),
} as Response).set_content_length(body.len());
}
Creates a new HTTP response with JSON body, and also sets the Content-Type: application/json
header.
[src]
pub fn json<T>(value: T): Response throws json.JsonError {
let body = try json.to_string(value);
return (HttpResponse {
status: http.OK,
headers: Headers.from_map({"content-type": mime.APPLICATION_JSON.to_string()}),
body: Cursor.new(body),
} as Response).set_content_length(body.len());
}
Creates a new HTTP response with JSON body, and also sets the Content-Type: application/json
header.
Creates a new HTTP response with JSON body, and also sets the Content-Type: application/json
header.
[src]
pub fn body(self): ReadClose? {
return (self as HttpResponse).body;
}
Returns the body of the response.
[src]
pub fn body(self): ReadClose? {
return (self as HttpResponse).body;
}
Returns the body of the response.
Returns the body of the response.
[src]
pub fn set_body(self, body: ReadClose?): Response {
(self as HttpResponse).body = body;
return self;
}
Sets the body of the response.
[src]
pub fn set_body(self, body: ReadClose?): Response {
(self as HttpResponse).body = body;
return self;
}
Sets the body of the response.
Sets the body of the response.
status
pub fn status(self): StatusCode
[src]
pub fn status(self): StatusCode {
return (self as HttpResponse).status;
}
Returns the status code of the response.
status
pub fn status(self): StatusCode
[src]
pub fn status(self): StatusCode {
return (self as HttpResponse).status;
}
Returns the status code of the response.
Returns the status code of the response.
set_status
pub fn set_status(self, status: StatusCode): Response
[src]
pub fn set_status(self, status: StatusCode): Response {
(self as HttpResponse).status = status;
return self;
}
Sets the status code of the response.
set_status
pub fn set_status(self, status: StatusCode): Response
[src]
pub fn set_status(self, status: StatusCode): Response {
(self as HttpResponse).status = status;
return self;
}
Sets the status code of the response.
Sets the status code of the response.
[src]
pub fn headers(self): Headers {
return (self as HttpResponse).headers;
}
Returns the headers of the response.
[src]
pub fn headers(self): Headers {
return (self as HttpResponse).headers;
}
Returns the headers of the response.
Returns the headers of the response.
set_headers
pub fn set_headers(self, headers: Headers | <string, string>): Response
[src]
pub fn set_headers(self, headers: Headers | <string, string>): Response {
switch (let headers = headers.(type)) {
case Headers:
(self as HttpResponse).headers = headers;
case <string, string>:
(self as HttpResponse).headers = Headers.from_map(headers);
}
return self;
}
Sets the headers for the response.
set_headers
pub fn set_headers(self, headers: Headers | <string, string>): Response
[src]
pub fn set_headers(self, headers: Headers | <string, string>): Response {
switch (let headers = headers.(type)) {
case Headers:
(self as HttpResponse).headers = headers;
case <string, string>:
(self as HttpResponse).headers = Headers.from_map(headers);
}
return self;
}
Sets the headers for the response.
Sets the headers for the response.
set_header
pub fn set_header(self, key: string, value: string): Response
[src]
pub fn set_header(self, key: string, value: string): Response {
(self as HttpResponse).headers.set(key, value);
return self;
}
Sets a header for the response.
set_header
pub fn set_header(self, key: string, value: string): Response
[src]
pub fn set_header(self, key: string, value: string): Response {
(self as HttpResponse).headers.set(key, value);
return self;
}
Sets a header for the response.
Sets a header for the response.
append_header
pub fn append_header(self, key: string, value: string): Response
[src]
pub fn append_header(self, key: string, value: string): Response {
(self as HttpResponse).headers.append(key, value);
return self;
}
Appends a header for the response.
append_header
pub fn append_header(self, key: string, value: string): Response
[src]
pub fn append_header(self, key: string, value: string): Response {
(self as HttpResponse).headers.append(key, value);
return self;
}
Appends a header for the response.
Appends a header for the response.
set_content_type
pub fn set_content_type(self, value: string): Response
[src]
pub fn set_content_type(self, value: string): Response {
(self as HttpResponse).headers.set("content-type", value);
return self;
}
Sets the content type of the response.
set_content_type
pub fn set_content_type(self, value: string): Response
[src]
pub fn set_content_type(self, value: string): Response {
(self as HttpResponse).headers.set("content-type", value);
return self;
}
Sets the content type of the response.
Sets the content type of the response.
set_content_length
pub fn set_content_length(self, value: int): Response
[src]
pub fn set_content_length(self, value: int): Response {
(self as HttpResponse).headers.set("content-length", `${value}`);
return self;
}
Sets the content length of the response.
set_content_length
pub fn set_content_length(self, value: int): Response
[src]
pub fn set_content_length(self, value: int): Response {
(self as HttpResponse).headers.set("content-length", `${value}`);
return self;
}
Sets the content length of the response.
Sets the content length of the response.
Usually does not need to be specifiy it directly.
sse
pub fn sse(handler: |(EventWriter)|, keep_alive: Duration? = nil): Response
[src]
pub fn sse(handler: |(EventWriter) throws|, keep_alive: Duration? = nil): Response {
let p = pipe();
let event_writer = EventWriter.new(p);
let resp = Response.new(http.OK);
let headers = resp.headers();
headers.set("content-type", mime.TEXT_EVENT_STREAM.to_string());
headers.set("x-accel-buffering", "no");
headers.set("cache-control", "no-cache");
resp.set_body(p);
if (let keep_alive = keep_alive) {
spawn {
do {
let ticker = Ticker.new(keep_alive);
defer ticker.stop();
for (let _ in ticker.channel()) {
try event_writer.comment("");
}
}
}
}
spawn {
do {
try handler(event_writer);
}
}
return resp;
}
Creates a SSE response.
sse
pub fn sse(handler: |(EventWriter)|, keep_alive: Duration? = nil): Response
[src]
pub fn sse(handler: |(EventWriter) throws|, keep_alive: Duration? = nil): Response {
let p = pipe();
let event_writer = EventWriter.new(p);
let resp = Response.new(http.OK);
let headers = resp.headers();
headers.set("content-type", mime.TEXT_EVENT_STREAM.to_string());
headers.set("x-accel-buffering", "no");
headers.set("cache-control", "no-cache");
resp.set_body(p);
if (let keep_alive = keep_alive) {
spawn {
do {
let ticker = Ticker.new(keep_alive);
defer ticker.stop();
for (let _ in ticker.channel()) {
try event_writer.comment("");
}
}
}
}
spawn {
do {
try handler(event_writer);
}
}
return resp;
}
Creates a SSE response.
Creates a SSE response.
Example
use std.net.http.server.{Response, HttpServer, func_handler};
use std.net.TcpListener;
fn main() throws {
let app = func_handler(|req| {
return Response.sse(|writer| {
do {
try writer.message("message1");
try writer.message("message2");
try writer.close();
}
});
});
let listener = try TcpListener.bind("127.0.0.1:8000");
let server = HttpServer.new(listener, app);
try server.serve();
}