HttpServer
struct HttpServer
An HTTP Server
Methods
new
pub fn new(listener: Listener, handler: Handler, cookie_key: CookieKey? = nil): HttpServer
[src]
pub fn new(listener: Listener, handler: Handler, cookie_key: CookieKey? = nil): HttpServer {
return HttpServer { listener, handler, cookie_key };
}
Creates a new HttpServer
.
new
pub fn new(listener: Listener, handler: Handler, cookie_key: CookieKey? = nil): HttpServer
[src]
pub fn new(listener: Listener, handler: Handler, cookie_key: CookieKey? = nil): HttpServer {
return HttpServer { listener, handler, cookie_key };
}
Creates a new HttpServer
.
Creates a new HttpServer
.
Optionally, specify the CookieKey
used for the CookieJar.private
and
CookieJar.signed
methods.
Example
nv,no_run
use std.net.TcpListener;
use std.net.http.CookieKey;
use std.net.http.server.{
HttpServer,
Router,
};
fn main() throws {
let listener = try TcpListener.bind("127.0.0.1:8080");
let app = Router.new();
let cookie_key = CookieKey.generate();
let server = HttpServer.new(listener, app, cookie_key:);
try server.serve();
}
[src]
pub fn serve(self) throws IoError {
loop {
let io = try self.listener.accept();
spawn {
let io = io;
let handler = self.handler;
let cookie_key = self.cookie_key;
let conn = _HttpConnection.new(_create_native_readwriteclose(io));
for (let _request in conn) {
spawn {
let request = Request {
base: HttpRequest {
method: (_request.method as Method)!,
uri: _request.uri,
headers: _request.headers as Headers,
body: _request.body.map(|body| body.to_nop_read_close()),
},
remote_addr: io.remote_addr(),
path_params: nil,
on_upgrade: _request.on_upgrade,
cookie_key,
cookie_jar: nil,
};
do {
let resp = try handler.handle(request) as HttpResponse;
_request
.respond(
resp.status as int,
resp.headers as _Headers,
resp.body.map(_create_native_readclose)
);
} catch(e: HttpError) {
_request
.respond(
e.status as int,
_Headers.new(),
_create_native_readclose(Cursor.new(e.error()))
);
} catch(e) {
_request
.respond(
http.InternalServerError as int,
_Headers.new(),
_create_native_readclose(Cursor.new(e.error()))
);
}
}
}
}
}
}
Serve incoming HTTP connections
[src]
pub fn serve(self) throws IoError {
loop {
let io = try self.listener.accept();
spawn {
let io = io;
let handler = self.handler;
let cookie_key = self.cookie_key;
let conn = _HttpConnection.new(_create_native_readwriteclose(io));
for (let _request in conn) {
spawn {
let request = Request {
base: HttpRequest {
method: (_request.method as Method)!,
uri: _request.uri,
headers: _request.headers as Headers,
body: _request.body.map(|body| body.to_nop_read_close()),
},
remote_addr: io.remote_addr(),
path_params: nil,
on_upgrade: _request.on_upgrade,
cookie_key,
cookie_jar: nil,
};
do {
let resp = try handler.handle(request) as HttpResponse;
_request
.respond(
resp.status as int,
resp.headers as _Headers,
resp.body.map(_create_native_readclose)
);
} catch(e: HttpError) {
_request
.respond(
e.status as int,
_Headers.new(),
_create_native_readclose(Cursor.new(e.error()))
);
} catch(e) {
_request
.respond(
http.InternalServerError as int,
_Headers.new(),
_create_native_readclose(Cursor.new(e.error()))
);
}
}
}
}
}
}
Serve incoming HTTP connections
Serve incoming HTTP connections