Fix Brotli response body decoding.

This commit is contained in:
jcamiel 2022-05-11 17:13:16 +02:00 committed by Fabrice Reix
parent c112f759f9
commit cf240a3921
8 changed files with 43 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

View File

@ -15,5 +15,6 @@ curl 'http://localhost:8000/compressed/brotli' --compressed
curl 'http://localhost:8000/compressed/brotli' --compressed
curl 'http://localhost:8000/compressed/brotli' --compressed
curl 'http://localhost:8000/compressed/brotli' --compressed
curl 'http://localhost:8000/compressed/brotli_large' --compressed
curl 'http://localhost:8000/compressed/brotli_identity' --compressed

View File

@ -121,6 +121,15 @@
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line">file,<span class="filename">hello.txt</span>;</span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"></span><span class="comment"># Test a large brotli compressed body:</span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/compressed/brotli_large</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line"><span class="string">Content-Encoding</span><span>:</span> <span class="string">br</span></span>
<span class="line"><span class="string">Content-Type</span><span>:</span> <span class="string">image/jpeg</span></span>
<span class="line section-header">[Asserts]</span>
<span class="line"><span class="query-type">bytes</span> <span class="predicate-type">startsWith</span> hex,<span class="hex">ffd8ffe0</span>;</span> <span class="comment"># JPEG magic number</span>
<span class="line">file,<span class="filename">cat.jpg</span>;</span>
</span></span><span class="hurl-entry"><span class="request"><span class="line"></span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/compressed/brotli_identity</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="line"><span class="string">Content-Length</span><span>:</span> <span class="string">17</span></span>

View File

@ -121,6 +121,15 @@ GET http://localhost:8000/compressed/brotli
HTTP/1.0 200
file,hello.txt;
# Test a large brotli compressed body:
GET http://localhost:8000/compressed/brotli_large
HTTP/1.0 200
Content-Encoding: br
Content-Type: image/jpeg
[Asserts]
bytes startsWith hex,ffd8ffe0; # JPEG magic number
file,cat.jpg;
GET http://localhost:8000/compressed/brotli_identity
HTTP/1.0 200
Content-Length: 17

File diff suppressed because one or more lines are too long

View File

@ -42,6 +42,17 @@ def compressed_brotli():
return resp
@app.route("/compressed/brotli_large")
def compressed_brotli_large():
assert "br" in request.headers["Accept-Encoding"]
with open("tests_ok/cat.jpg.br", "rb") as f:
data = f.read()
resp = make_response(data)
resp.headers["Content-Encoding"] = "br"
resp.headers["Content-Type"] = "image/jpeg"
return resp
@app.route("/compressed/brotli_identity")
def compressed_brotli_identity():
assert "br" in request.headers["Accept-Encoding"]

View File

@ -98,17 +98,15 @@ impl http::Response {
///
/// * data - Compressed bytes.
fn uncompress_brotli(data: &[u8]) -> Result<Vec<u8>, RunnerError> {
let mut reader = brotli::Decompressor::new(data, 4096);
let mut buf = [0u8; 4096];
let n = match reader.read(&mut buf[..]) {
Err(_) => {
return Err(RunnerError::CouldNotUncompressResponse(
"brotli".to_string(),
));
}
Ok(size) => size,
};
Ok(buf[..n].to_vec())
let buffer_size = 4096;
let mut reader = brotli::Decompressor::new(data, buffer_size);
let mut buf = Vec::new();
match reader.read_to_end(&mut buf) {
Ok(_) => Ok(buf),
Err(_) => Err(RunnerError::CouldNotUncompressResponse(
"brotli".to_string(),
)),
}
}
/// Decompress GZip compressed data.
@ -226,7 +224,7 @@ pub mod tests {
}],
body: vec![
0x21, 0x2c, 0x00, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c,
0x64, 0x21,
0x64, 0x21, 0x03,
],
duration: Default::default(),
};
@ -241,7 +239,7 @@ pub mod tests {
}],
body: vec![
0x21, 0x2c, 0x00, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c,
0x64, 0x21,
0x64, 0x21, 0x03,
],
duration: Default::default(),
};
@ -261,7 +259,7 @@ pub mod tests {
fn test_uncompress_brotli() {
let data = vec![
0x21, 0x2c, 0x00, 0x04, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c,
0x64, 0x21,
0x64, 0x21, 0x03,
];
assert_eq!(uncompress_brotli(&data[..]).unwrap(), b"Hello World!");
}