Files
imeai-aKode/codex-rs/feedback/src/upload.rs

154 lines
4.8 KiB
Rust

use std::io;
use std::io::Write;
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;
use anyhow::Result;
use bytes::Bytes;
use codex_http_client::RequestBuilder;
use codex_http_client::RouteAwareClientPool;
use flate2::Compression;
use flate2::write::GzEncoder;
use http::HeaderMap;
use http::StatusCode;
use sentry::ClientOptions;
use sentry::protocol::Attachment;
use sentry::protocol::Envelope;
use sentry::protocol::EnvelopeHeaders;
use sentry::protocol::EnvelopeItem;
use sentry::types::Dsn;
use crate::MAX_DECODED_UPLOAD_BYTES;
pub(super) const DEFAULT_RATE_LIMIT: Duration = Duration::from_secs(/*secs*/ 60);
pub(super) enum EnvelopeKind {
Event,
Attachment,
}
pub(super) fn envelope_request(
client_pool: &RouteAwareClientPool,
dsn: &Dsn,
body: Bytes,
timeout: Duration,
) -> RequestBuilder {
let sentry_options = ClientOptions::default();
let sentry_auth = dsn.to_auth(Some(sentry_options.user_agent.as_ref()));
let request = client_pool
.post(dsn.envelope_api_url().as_str())
.header("X-Sentry-Auth", sentry_auth.to_string());
// Persisted parts retain their encoding. Gzip's magic bytes cannot begin
// an uncompressed envelope, which starts with a JSON header.
let request = if body.starts_with(&[0x1f, 0x8b]) {
request.header("Content-Encoding", "gzip")
} else {
request
};
request.body(body).timeout(timeout)
}
/// Send an already-serialized envelope within the report's shared deadline.
///
/// aKode: feedback is never uploaded to a remote service (upstream sent it to
/// Sentry). The signature is kept so callers compile; it always fails.
pub(super) async fn send_envelope(
_client_pool: &RouteAwareClientPool,
_dsn: &Dsn,
_body: Vec<u8>,
_kind: EnvelopeKind,
_deadline: Instant,
_rate_limited: &mut bool,
) -> Result<StatusCode> {
anyhow::bail!("feedback upload is disabled in aKode; use the local save option instead")
}
pub(super) fn sentry_rate_limit_delay(headers: &HeaderMap) -> Option<Duration> {
let mut retry_after = None;
for value in headers.get_all("X-Sentry-Rate-Limits") {
for quota in value.to_str().unwrap_or_default().split(',') {
let mut fields = quota.trim().split(':');
let seconds = fields.next().unwrap_or_default();
let categories = fields.next().unwrap_or_default();
if !categories.is_empty()
&& !categories
.split(';')
.any(|category| matches!(category, "error" | "attachment"))
{
continue;
}
let delay = seconds
.parse::<f64>()
.ok()
.and_then(|seconds| Duration::try_from_secs_f64(seconds.ceil()).ok())
.unwrap_or(DEFAULT_RATE_LIMIT);
retry_after = retry_after.max(Some(delay));
}
}
retry_after
}
pub(super) fn parse_retry_after(value: &str) -> Option<Duration> {
value
.parse::<u64>()
.map(Duration::from_secs)
.or_else(|_| {
httpdate::parse_http_date(value)
.map(|date| date.duration_since(SystemTime::now()).unwrap_or_default())
})
.ok()
}
pub(super) fn encode_envelope(envelope: &Envelope) -> io::Result<(Vec<u8>, usize)> {
let mut writer = CountingWriter {
inner: GzEncoder::new(Vec::new(), Compression::fast()),
bytes: 0,
};
envelope.to_writer(&mut writer)?;
let decoded_bytes = writer.bytes;
let mut body = writer.inner.finish()?;
// Already-compressed attachments can grow beyond Sentry's 200 MiB wire limit.
// Reuse the buffer for the raw envelope when gzip does not make it smaller.
if body.len() >= decoded_bytes {
body.clear();
envelope.to_writer(&mut body)?;
}
Ok((body, decoded_bytes))
}
pub(super) fn encode_attachment_envelope(
headers: &EnvelopeHeaders,
attachment: Attachment,
) -> Result<Vec<u8>> {
anyhow::ensure!(
attachment.buffer.len() <= MAX_DECODED_UPLOAD_BYTES,
"feedback attachment exceeds the size limit"
);
let mut envelope = Envelope::new().with_headers(headers.clone());
envelope.add_item(EnvelopeItem::Attachment(attachment));
let (body, decoded_bytes) = encode_envelope(&envelope)?;
anyhow::ensure!(
decoded_bytes <= MAX_DECODED_UPLOAD_BYTES,
"feedback attachment envelope exceeds the size limit"
);
Ok(body)
}
struct CountingWriter<W> {
inner: W,
bytes: usize,
}
impl<W: Write> Write for CountingWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let written = self.inner.write(buf)?;
self.bytes += written;
Ok(written)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}