|
| 1 | +// Copyright (C) 2024 Quickwit, Inc. |
| 2 | +// |
| 3 | +// Quickwit is offered under the AGPL v3.0 and as commercial software. |
| 4 | +// For commercial licensing, contact us at hello@quickwit.io. |
| 5 | +// |
| 6 | +// AGPL: |
| 7 | +// This program is free software: you can redistribute it and/or modify |
| 8 | +// it under the terms of the GNU Affero General Public License as |
| 9 | +// published by the Free Software Foundation, either version 3 of the |
| 10 | +// License, or (at your option) any later version. |
| 11 | +// |
| 12 | +// This program is distributed in the hope that it will be useful, |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +// GNU Affero General Public License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU Affero General Public License |
| 18 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +use core::fmt; |
| 21 | +use std::io::Read; |
| 22 | + |
| 23 | +use bytes::Bytes; |
| 24 | +use flate2::read::GzDecoder; |
| 25 | +use tokio::task; |
| 26 | +use warp::reject::Reject; |
| 27 | +use warp::Filter; |
| 28 | + |
| 29 | +/// There are two ways to decompress the body: |
| 30 | +/// - Stream the body through an async decompressor |
| 31 | +/// - Fetch the body and then decompress the bytes |
| 32 | +/// |
| 33 | +/// The first approach lowers the latency, while the second approach is more CPU efficient. |
| 34 | +/// Ingesting data is usually CPU bound and there is considerable latency until the data is |
| 35 | +/// searchable, so the second approach is more suitable for this use case. |
| 36 | +async fn decompress_body(encoding: Option<String>, body: Bytes) -> Result<Bytes, warp::Rejection> { |
| 37 | + match encoding.as_deref() { |
| 38 | + Some("gzip" | "x-gzip") => { |
| 39 | + let decompressed = task::spawn_blocking(move || { |
| 40 | + let mut decompressed = Vec::new(); |
| 41 | + let mut decoder = GzDecoder::new(body.as_ref()); |
| 42 | + decoder |
| 43 | + .read_to_end(&mut decompressed) |
| 44 | + .map_err(|_| warp::reject())?; |
| 45 | + Result::<_, warp::Rejection>::Ok(Bytes::from(decompressed)) |
| 46 | + }) |
| 47 | + .await |
| 48 | + .map_err(|_| warp::reject())??; |
| 49 | + Ok(decompressed) |
| 50 | + } |
| 51 | + Some("zstd") => { |
| 52 | + let decompressed = task::spawn_blocking(move || { |
| 53 | + zstd::decode_all(body.as_ref()) |
| 54 | + .map(Bytes::from) |
| 55 | + .map_err(|_| warp::reject()) |
| 56 | + }) |
| 57 | + .await |
| 58 | + .map_err(|_| warp::reject())??; |
| 59 | + Ok(decompressed) |
| 60 | + } |
| 61 | + Some(encoding) => Err(warp::reject::custom(UnsupportedEncoding( |
| 62 | + encoding.to_string(), |
| 63 | + ))), |
| 64 | + _ => Ok(body), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug)] |
| 69 | +struct UnsupportedEncoding(String); |
| 70 | + |
| 71 | +impl fmt::Display for UnsupportedEncoding { |
| 72 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 73 | + write!( |
| 74 | + f, |
| 75 | + "Unsupported Content-Encoding {}. Supported encodings are 'gzip' and 'zstd'.", |
| 76 | + self.0 |
| 77 | + ) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl Reject for UnsupportedEncoding {} |
| 82 | + |
| 83 | +/// Custom filter for optional decompression |
| 84 | +pub(crate) fn get_body_bytes() -> impl Filter<Extract = (Bytes,), Error = warp::Rejection> + Clone { |
| 85 | + warp::header::optional("content-encoding") |
| 86 | + .and(warp::body::bytes()) |
| 87 | + .and_then(|encoding: Option<String>, body: Bytes| async move { |
| 88 | + decompress_body(encoding, body).await |
| 89 | + }) |
| 90 | +} |
0 commit comments