CompressionMiddleware
struct CompressionMiddleware
Implementions
Methods
new
pub fn new(level: Level = compress.Level.Default): CompressionMiddleware
[src]
pub fn new(level: compress.Level = compress.Level.Default): CompressionMiddleware {
return CompressionMiddleware { level };
}
new
pub fn new(level: Level = compress.Level.Default): CompressionMiddleware
[src]
pub fn new(level: compress.Level = compress.Level.Default): CompressionMiddleware {
return CompressionMiddleware { level };
}
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
if (let coding = request.headers().get("content-encoding")) {
if (let body = request.body()) {
switch (coding) {
case "br":
request
.set_body(compress
.BrotliDecoder
.new(body)
.to_read_close(body.close));
request.headers().remove("content-length");
case "deflate":
request
.set_body(compress
.DeflateDecoder
.new(body)
.to_read_close(body.close));
request.headers().remove("content-length");
case "gzip":
request
.set_body(compress.GzipDecoder.new(body).to_read_close(body.close));
request.headers().remove("content-length");
case "zstd":
request
.set_body(compress.ZstdDecoder.new(body).to_read_close(body.close));
request.headers().remove("content-length");
}
}
}
let resp = try handler.handle(request);
if (let body = resp.body()) {
if (let coding = parse_accept_encoding(request.headers())) {
let headers = resp.headers();
switch (coding) {
case ContentCoding.Deflate:
resp
.set_body(compress
.DeflateEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "deflate");
case ContentCoding.Gzip:
resp
.set_body(compress
.GzipEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "gzip");
case ContentCoding.Zstd:
resp
.set_body(compress
.GzipEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "zstd");
default:
resp
.set_body(compress
.BrotliEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "br");
}
}
}
return resp;
});
}
[src]
pub fn transform(self, handler: Handler): Handler {
return handler
.around(|handler, request| {
if (let coding = request.headers().get("content-encoding")) {
if (let body = request.body()) {
switch (coding) {
case "br":
request
.set_body(compress
.BrotliDecoder
.new(body)
.to_read_close(body.close));
request.headers().remove("content-length");
case "deflate":
request
.set_body(compress
.DeflateDecoder
.new(body)
.to_read_close(body.close));
request.headers().remove("content-length");
case "gzip":
request
.set_body(compress.GzipDecoder.new(body).to_read_close(body.close));
request.headers().remove("content-length");
case "zstd":
request
.set_body(compress.ZstdDecoder.new(body).to_read_close(body.close));
request.headers().remove("content-length");
}
}
}
let resp = try handler.handle(request);
if (let body = resp.body()) {
if (let coding = parse_accept_encoding(request.headers())) {
let headers = resp.headers();
switch (coding) {
case ContentCoding.Deflate:
resp
.set_body(compress
.DeflateEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "deflate");
case ContentCoding.Gzip:
resp
.set_body(compress
.GzipEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "gzip");
case ContentCoding.Zstd:
resp
.set_body(compress
.GzipEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "zstd");
default:
resp
.set_body(compress
.BrotliEncoder
.new(body, level: self.level)
.to_read_close(body.close));
headers.remove("content-length");
headers.set("content-encoding", "br");
}
}
}
return resp;
});
}