From ba3b3508467b7f86a99a5eeff2a5cc21c16d768a Mon Sep 17 00:00:00 2001 From: comonad Date: Mon, 15 Jun 2026 11:39:14 +0800 Subject: [PATCH] fix: response code for reverse_proxy --- src/artifacts.rs | 3 +++ src/error.rs | 15 +++++++++++++-- src/repos.rs | 28 +++++++++++++++++++--------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/artifacts.rs b/src/artifacts.rs index 5c2618f..aff4948 100644 --- a/src/artifacts.rs +++ b/src/artifacts.rs @@ -6,6 +6,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; +use actix_web::http::StatusCode; use bytes::Bytes; use futures_util::StreamExt; use reqwest::{Client, Response}; @@ -325,6 +326,8 @@ async fn download_payload(client: &Client, url: Url, config: &Config) -> Result< let response = client.get(url).send().await?; let status = response.status(); if !status.is_success() { + let status = + StatusCode::from_u16(status.as_u16()).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR); return Err(Error::Http(status)); } diff --git a/src/error.rs b/src/error.rs index f17da59..6d5c435 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ use std::result; use actix_web::ResponseError; +use actix_web::http::StatusCode; use thiserror::Error; type PutObjectSdkError = @@ -23,7 +24,7 @@ pub enum Error { #[error("Reqwest Error {0}")] Reqwest(#[from] reqwest::Error), #[error("HTTP Error {0}")] - Http(reqwest::StatusCode), + Http(StatusCode), #[error("{0}")] Custom(String), #[error("Too Large")] @@ -40,7 +41,17 @@ pub enum Error { Timeout, } -impl ResponseError for Error {} +impl ResponseError for Error { + fn status_code(&self) -> StatusCode { + match self { + Self::Reqwest(err) if err.is_connect() => StatusCode::BAD_GATEWAY, + Self::Reqwest(err) if err.is_timeout() => StatusCode::GATEWAY_TIMEOUT, + Self::Http(status) => *status, + Self::InvalidRequest => StatusCode::NOT_FOUND, + _ => StatusCode::INTERNAL_SERVER_ERROR, + } + } +} // Fix clippy "the `Err`-variant returned from this function is very large" impl From for Error { diff --git a/src/repos.rs b/src/repos.rs index 141c522..8336f6d 100644 --- a/src/repos.rs +++ b/src/repos.rs @@ -1,6 +1,6 @@ use std::sync::LazyLock; -use actix_web::http::{Method, Uri}; +use actix_web::http::{Method, StatusCode, Uri}; use actix_web::{HttpResponse, Route, guard, web}; use regex::Regex; @@ -464,18 +464,28 @@ pub fn nix_intel( } if task.path.starts_with("nar/") || task.path.ends_with(".narinfo") { - Ok(task + match task .resolve(&intel_mission, &config) .await? .reverse_proxy(&intel_mission) - .await? - .into()) + .await + { + Ok(resp) => Ok(resp.into()), + Err(Error::Http(status)) if status == StatusCode::NOT_FOUND => { + Ok(HttpResponse::NotFound().finish().into()) + } + Err(Error::Reqwest(_)) => Ok(HttpResponse::NotFound().finish().into()), + Err(e) => Err(e), + } } else if task.path == "nix-cache-info" { - Ok(task - .resolve_upstream() - .reverse_proxy(&intel_mission) - .await? - .into()) + match task.resolve_upstream().reverse_proxy(&intel_mission).await { + Ok(resp) => Ok(resp.into()), + Err(Error::Http(status)) if status == StatusCode::NOT_FOUND => { + Ok(HttpResponse::NotFound().finish().into()) + } + Err(Error::Reqwest(_)) => Ok(HttpResponse::NotFound().finish().into()), + Err(e) => Err(e), + } } else { Ok(Redirect::Permanent(task.upstream_url().to_string()).into()) }