SetHeaderMiddleware
struct SetHeaderMiddleware
Middleware for overriding/appending headers to response.
Example
nv
use std.net.http.server.{
middlewares.SetHeaderMiddleware,
Router,
};
let app = Router.new().with(
SetHeaderMiddleware.new()
.appending("MyHeader1", "a")
.appending("MyHeader1", "b")
.overriding("MyHeader2", "a")
.overriding("MyHeader2", "b")
);
Implementions
Methods
new
pub fn new(): SetHeaderMiddleware
[src]
pub fn new(): SetHeaderMiddleware {
return SetHeaderMiddleware { actions: [] };
}
Creates a new SetHeader
middleware.
new
pub fn new(): SetHeaderMiddleware
[src]
pub fn new(): SetHeaderMiddleware {
return SetHeaderMiddleware { actions: [] };
}
Creates a new SetHeader
middleware.
Creates a new SetHeader
middleware.
overriding
pub fn overriding(self, key: string, value: string): SetHeaderMiddleware
[src]
pub fn overriding(self, key: string, value: string): SetHeaderMiddleware {
self.actions.push(SetHeaderMiddlewareAction { action: SetHeaderAction.Override, key, value });
return self;
}
Inserts a header to response.
overriding
pub fn overriding(self, key: string, value: string): SetHeaderMiddleware
[src]
pub fn overriding(self, key: string, value: string): SetHeaderMiddleware {
self.actions.push(SetHeaderMiddlewareAction { action: SetHeaderAction.Override, key, value });
return self;
}
Inserts a header to response.
Inserts a header to response.
If a previous value exists for the same header, it is removed and replaced with the new header value.
appending
pub fn appending(self, key: string, value: string): SetHeaderMiddleware
[src]
pub fn appending(self, key: string, value: string): SetHeaderMiddleware {
self.actions.push(SetHeaderMiddlewareAction { action: SetHeaderAction.Append, key, value });
return self;
}
Appends a header to response.
appending
pub fn appending(self, key: string, value: string): SetHeaderMiddleware
[src]
pub fn appending(self, key: string, value: string): SetHeaderMiddleware {
self.actions.push(SetHeaderMiddlewareAction { action: SetHeaderAction.Append, key, value });
return self;
}
Appends a header to response.
Appends a header to response.
If previous values exist, the header will have multiple values.
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
let response = try handler.handle(request);
for (let action in self.actions) {
if (action.action == SetHeaderAction.Override) {
response.headers().set(action.key, action.value);
} else if (action.action == SetHeaderAction.Append) {
response.headers().append(action.key, action.value);
}
}
return response;
});
}
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
let response = try handler.handle(request);
for (let action in self.actions) {
if (action.action == SetHeaderAction.Override) {
response.headers().set(action.key, action.value);
} else if (action.action == SetHeaderAction.Append) {
response.headers().append(action.key, action.value);
}
}
return response;
});
}