Repository: influxdata/influxdb
File: influxdb3_server/src/http.rs (Rust)

Code before:
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use std::sync::Arc;
use std::task::Poll;
use std::{convert::Infallible, time::Duration};
use thiserror::Error;
use trace::ctx::SpanContext;
use unicode_segmentation::UnicodeSegmentation;
use uuid::Uuid;

pub(crate) const UNKNOWN_VAL: &str = "unknown";

/// Maximum length for untrusted input when logging to prevent log flooding
const MAX_PATH_LENGTH_FOR_LOGGING: usize = 256;
const MAX_CONTENT_LENGTH_HEADER_FOR_LOGGING: usize = 128;
const MAX_CLIENT_IP_FOR_LOGGING: usize = 128;

/// Truncate a string for logging untrusted input to prevent log flooding
fn truncate_for_logging(s: &str, max_len: usize) -> &str {
    if s.len() <= max_len {
        s
    } else {
        // Use floor_char_boundary to ensure we don't split a multi-byte UTF-8 character
        &s[..s.floor_char_boundary(max_len)]
    }
}

/// Error type for routing that can handle both standard errors and V2 write API errors
#[derive(Debug)]
enum RoutingError {
    Standard(Error),
    V2Write(V2WriteApiError),
    LegacyWrite(WriteParseError),
    Authentication(AuthenticationError),
    Authorization(ResourceAuthorizationError),
    NotFound,
    MethodNotAllowed(&'static str),
}

impl std::fmt::Display for RoutingError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Standard(e) => write!(f, "{}", e),
            Self::V2Write(e) => write!(f, "{}", e.0),
            Self::LegacyWrite(e) => write!(f, "{}", e),
            Self::Authentication(e) => write!(f, "{}", e),

Review comment (suggestion):
`floor_char_boundary` dopesn't a index >= self.len() check so only 
```
&s[..s.floor_char_boundary(max_len)]
```

is needed as the fn body. nice catch. 

Code after:
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use std::sync::Arc;
use std::task::Poll;
use std::{convert::Infallible, time::Duration};
use thiserror::Error;
use trace::ctx::SpanContext;
use unicode_segmentation::UnicodeSegmentation;
use uuid::Uuid;

pub(crate) const UNKNOWN_VAL: &str = "unknown";

/// Maximum length for untrusted input when logging to prevent log flooding
const MAX_PATH_LENGTH_FOR_LOGGING: usize = 256;
const MAX_CONTENT_LENGTH_HEADER_FOR_LOGGING: usize = 128;
const MAX_CLIENT_IP_FOR_LOGGING: usize = 128;

/// Truncate a string for logging untrusted input to prevent log flooding
fn truncate_for_logging(s: &str, max_len: usize) -> &str {
    &s[..s.floor_char_boundary(max_len)]
}

/// Error type for routing that can handle both standard errors and V2 write API errors
#[derive(Debug)]
enum RoutingError {
    Standard(Error),
    V2Write(V2WriteApiError),
    LegacyWrite(WriteParseError),
    Authentication(AuthenticationError),
    Authorization(ResourceAuthorizationError),
    NotFound,
    MethodNotAllowed(&'static str),
}

impl std::fmt::Display for RoutingError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Standard(e) => write!(f, "{}", e),
            Self::V2Write(e) => write!(f, "{}", e.0),
            Self::LegacyWrite(e) => write!(f, "{}", e),
            Self::Authentication(e) => write!(f, "{}", e),
            Self::Authorization(e) => write!(f, "{}", e),
            Self::NotFound => write!(f, "not found"),
            Self::MethodNotAllowed(msg) => write!(f, "{}", msg),
        }
    }