CorsMiddleware
struct CorsMiddleware
Implementions
Methods
new
pub fn new(): CorsMiddleware
[src]
pub fn new(): CorsMiddleware {
return CorsMiddleware {
allow_origins: [],
allow_methods: [],
};
}
Creates a new CorsMiddleware
middleware.
new
pub fn new(): CorsMiddleware
[src]
pub fn new(): CorsMiddleware {
return CorsMiddleware {
allow_origins: [],
allow_methods: [],
};
}
Creates a new CorsMiddleware
middleware.
Creates a new CorsMiddleware
middleware.
allow_origin
pub fn allow_origin(self, origin: string): CorsMiddleware
[src]
pub fn allow_origin(self, origin: string): CorsMiddleware {
self.allow_origins.push(origin);
return self;
}
Add an allow origin.
allow_origin
pub fn allow_origin(self, origin: string): CorsMiddleware
[src]
pub fn allow_origin(self, origin: string): CorsMiddleware {
self.allow_origins.push(origin);
return self;
}
Add an allow origin.
Add an allow origin.
allow_method
pub fn allow_method(self, method: string): CorsMiddleware
[src]
pub fn allow_method(self, method: string): CorsMiddleware {
self.allow_methods.push(method);
return self;
}
Add an allow method.
allow_method
pub fn allow_method(self, method: string): CorsMiddleware
[src]
pub fn allow_method(self, method: string): CorsMiddleware {
self.allow_methods.push(method);
return self;
}
Add an allow method.
Add an allow method.
is_valid_origin
pub fn is_valid_origin(self, origin: string): bool
[src]
fn is_valid_origin(self, origin: string): bool {
if (self.allow_origins.contains(origin)) {
return true;
}
return self.allow_origins.is_empty();
}
is_valid_origin
pub fn is_valid_origin(self, origin: string): bool
[src]
fn is_valid_origin(self, origin: string): bool {
if (self.allow_origins.contains(origin)) {
return true;
}
return self.allow_origins.is_empty();
}
[src]
pub fn transform(self, handler: Handler): Handler {
let that = self;
return handler
.around(|handler, request| {
if (let origin = request.headers().get("origin")) {
let origin_is_allow = that.is_valid_origin(origin);
if (!origin_is_allow) {
throw HttpError.new(http.Forbidden, "OriginNotAllowed");
}
let response = try handler.handle(request);
response.headers().append("Access-Control-Allow-Origin", origin);
return response;
} else {
return try handler.handle(request);
}
});
}
[src]
pub fn transform(self, handler: Handler): Handler {
let that = self;
return handler
.around(|handler, request| {
if (let origin = request.headers().get("origin")) {
let origin_is_allow = that.is_valid_origin(origin);
if (!origin_is_allow) {
throw HttpError.new(http.Forbidden, "OriginNotAllowed");
}
let response = try handler.handle(request);
response.headers().append("Access-Control-Allow-Origin", origin);
return response;
} else {
return try handler.handle(request);
}
});
}