NormalizePathMiddleware
struct NormalizePathMiddleware
Middleware for normalizing a request's path so that routes can be matched more flexibly.
Example
nv
use std.net.http.server.{
middlewares.{
NormalizePathMiddleware,
TrailingSlash
},
Router,
};
let app = Router.new().with(NormalizePathMiddleware.new(TrailingSlash.Trim));
Implementions
Methods
new
pub fn new(style: TrailingSlash): NormalizePathMiddleware
[src]
pub fn new(style: TrailingSlash): NormalizePathMiddleware {
return NormalizePathMiddleware {
style,
trim_end: try! Regex.new("//+$"),
merge_slash: try! Regex.new("//+"),
};
}
Creates a new NormalizePath
middleware with the specified trailing slash style.
new
pub fn new(style: TrailingSlash): NormalizePathMiddleware
[src]
pub fn new(style: TrailingSlash): NormalizePathMiddleware {
return NormalizePathMiddleware {
style,
trim_end: try! Regex.new("//+$"),
merge_slash: try! Regex.new("//+"),
};
}
Creates a new NormalizePath
middleware with the specified trailing slash style.
Creates a new NormalizePath
middleware with the specified trailing slash style.
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
let original_path = request.uri().path();
if (!original_path.is_empty()) {
let path = original_path;
switch (self.style) {
case .Always:
path = `${original_path}/`;
case .Trim:
path = self.trim_end.replace_all(path, "");
}
path = self.merge_slash.replace_all(path, "/");
if (path.is_empty()) {
path = "/";
}
if (path != original_path) {
try request.uri().set_path(path);
}
}
return try handler.handle(request);
});
}
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
let original_path = request.uri().path();
if (!original_path.is_empty()) {
let path = original_path;
switch (self.style) {
case .Always:
path = `${original_path}/`;
case .Trim:
path = self.trim_end.replace_all(path, "");
}
path = self.merge_slash.replace_all(path, "/");
if (path.is_empty()) {
path = "/";
}
if (path != original_path) {
try request.uri().set_path(path);
}
}
return try handler.handle(request);
});
}