LCOV - code coverage report
Current view: top level - proxy/src/serverless - sql_over_http.rs (source / functions) Coverage Total Hit
Test: ae4948feae6a1d420c855050eb8c189119446a71.info Lines: 5.4 % 845 46
Test Date: 2025-03-18 18:33:46 Functions: 3.9 % 103 4

            Line data    Source code
       1              : use std::pin::pin;
       2              : use std::sync::Arc;
       3              : 
       4              : use bytes::Bytes;
       5              : use futures::future::{Either, select, try_join};
       6              : use futures::{StreamExt, TryFutureExt};
       7              : use http::Method;
       8              : use http::header::AUTHORIZATION;
       9              : use http_body_util::combinators::BoxBody;
      10              : use http_body_util::{BodyExt, Full};
      11              : use http_utils::error::ApiError;
      12              : use hyper::body::Incoming;
      13              : use hyper::http::{HeaderName, HeaderValue};
      14              : use hyper::{HeaderMap, Request, Response, StatusCode, header};
      15              : use indexmap::IndexMap;
      16              : use postgres_client::error::{DbError, ErrorPosition, SqlState};
      17              : use postgres_client::{GenericClient, IsolationLevel, NoTls, ReadyForQueryStatus, Transaction};
      18              : use pq_proto::StartupMessageParamsBuilder;
      19              : use serde::Serialize;
      20              : use serde_json::Value;
      21              : use serde_json::value::RawValue;
      22              : use tokio::time::{self, Instant};
      23              : use tokio_util::sync::CancellationToken;
      24              : use tracing::{debug, error, info};
      25              : use typed_json::json;
      26              : use url::Url;
      27              : use uuid::Uuid;
      28              : 
      29              : use super::backend::{LocalProxyConnError, PoolingBackend};
      30              : use super::conn_pool::{AuthData, ConnInfoWithAuth};
      31              : use super::conn_pool_lib::{self, ConnInfo};
      32              : use super::error::HttpCodeError;
      33              : use super::http_util::json_response;
      34              : use super::json::{JsonConversionError, json_to_pg_text, pg_text_row_to_json};
      35              : use crate::auth::backend::{ComputeCredentialKeys, ComputeUserInfo};
      36              : use crate::auth::{ComputeUserInfoParseError, endpoint_sni};
      37              : use crate::config::{AuthenticationConfig, HttpConfig, ProxyConfig, TlsConfig};
      38              : use crate::context::RequestContext;
      39              : use crate::error::{ErrorKind, ReportableError, UserFacingError};
      40              : use crate::http::{ReadBodyError, read_body_with_limit};
      41              : use crate::metrics::{HttpDirection, Metrics};
      42              : use crate::proxy::{NeonOptions, run_until_cancelled};
      43              : use crate::serverless::backend::HttpConnError;
      44              : use crate::types::{DbName, RoleName};
      45              : use crate::usage_metrics::{MetricCounter, MetricCounterRecorder};
      46              : 
      47           11 : #[derive(serde::Deserialize)]
      48              : #[serde(rename_all = "camelCase")]
      49              : struct QueryData {
      50              :     query: String,
      51              :     #[serde(deserialize_with = "bytes_to_pg_text")]
      52              :     #[serde(default)]
      53              :     params: Vec<Option<String>>,
      54              :     #[serde(default)]
      55              :     array_mode: Option<bool>,
      56              : }
      57              : 
      58            1 : #[derive(serde::Deserialize)]
      59              : struct BatchQueryData {
      60              :     queries: Vec<QueryData>,
      61              : }
      62              : 
      63              : #[derive(serde::Deserialize)]
      64              : #[serde(untagged)]
      65              : enum Payload {
      66              :     Single(QueryData),
      67              :     Batch(BatchQueryData),
      68              : }
      69              : 
      70              : pub(super) static NEON_REQUEST_ID: HeaderName = HeaderName::from_static("neon-request-id");
      71              : 
      72              : static CONN_STRING: HeaderName = HeaderName::from_static("neon-connection-string");
      73              : static RAW_TEXT_OUTPUT: HeaderName = HeaderName::from_static("neon-raw-text-output");
      74              : static ARRAY_MODE: HeaderName = HeaderName::from_static("neon-array-mode");
      75              : static ALLOW_POOL: HeaderName = HeaderName::from_static("neon-pool-opt-in");
      76              : static TXN_ISOLATION_LEVEL: HeaderName = HeaderName::from_static("neon-batch-isolation-level");
      77              : static TXN_READ_ONLY: HeaderName = HeaderName::from_static("neon-batch-read-only");
      78              : static TXN_DEFERRABLE: HeaderName = HeaderName::from_static("neon-batch-deferrable");
      79              : 
      80              : static HEADER_VALUE_TRUE: HeaderValue = HeaderValue::from_static("true");
      81              : 
      82            3 : fn bytes_to_pg_text<'de, D>(deserializer: D) -> Result<Vec<Option<String>>, D::Error>
      83            3 : where
      84            3 :     D: serde::de::Deserializer<'de>,
      85            3 : {
      86              :     // TODO: consider avoiding the allocation here.
      87            3 :     let json: Vec<Value> = serde::de::Deserialize::deserialize(deserializer)?;
      88            3 :     Ok(json_to_pg_text(json))
      89            3 : }
      90              : 
      91              : #[derive(Debug, thiserror::Error)]
      92              : pub(crate) enum ConnInfoError {
      93              :     #[error("invalid header: {0}")]
      94              :     InvalidHeader(&'static HeaderName),
      95              :     #[error("invalid connection string: {0}")]
      96              :     UrlParseError(#[from] url::ParseError),
      97              :     #[error("incorrect scheme")]
      98              :     IncorrectScheme,
      99              :     #[error("missing database name")]
     100              :     MissingDbName,
     101              :     #[error("invalid database name")]
     102              :     InvalidDbName,
     103              :     #[error("missing username")]
     104              :     MissingUsername,
     105              :     #[error("invalid username: {0}")]
     106              :     InvalidUsername(#[from] std::string::FromUtf8Error),
     107              :     #[error("missing authentication credentials: {0}")]
     108              :     MissingCredentials(Credentials),
     109              :     #[error("missing hostname")]
     110              :     MissingHostname,
     111              :     #[error("invalid hostname: {0}")]
     112              :     InvalidEndpoint(#[from] ComputeUserInfoParseError),
     113              :     #[error("malformed endpoint")]
     114              :     MalformedEndpoint,
     115              : }
     116              : 
     117              : #[derive(Debug, thiserror::Error)]
     118              : pub(crate) enum Credentials {
     119              :     #[error("required password")]
     120              :     Password,
     121              :     #[error("required authorization bearer token in JWT format")]
     122              :     BearerJwt,
     123              : }
     124              : 
     125              : impl ReportableError for ConnInfoError {
     126            0 :     fn get_error_kind(&self) -> ErrorKind {
     127            0 :         ErrorKind::User
     128            0 :     }
     129              : }
     130              : 
     131              : impl UserFacingError for ConnInfoError {
     132            0 :     fn to_string_client(&self) -> String {
     133            0 :         self.to_string()
     134            0 :     }
     135              : }
     136              : 
     137            0 : fn get_conn_info(
     138            0 :     config: &'static AuthenticationConfig,
     139            0 :     ctx: &RequestContext,
     140            0 :     headers: &HeaderMap,
     141            0 :     tls: Option<&TlsConfig>,
     142            0 : ) -> Result<ConnInfoWithAuth, ConnInfoError> {
     143            0 :     let connection_string = headers
     144            0 :         .get(&CONN_STRING)
     145            0 :         .ok_or(ConnInfoError::InvalidHeader(&CONN_STRING))?
     146            0 :         .to_str()
     147            0 :         .map_err(|_| ConnInfoError::InvalidHeader(&CONN_STRING))?;
     148              : 
     149            0 :     let connection_url = Url::parse(connection_string)?;
     150              : 
     151            0 :     let protocol = connection_url.scheme();
     152            0 :     if protocol != "postgres" && protocol != "postgresql" {
     153            0 :         return Err(ConnInfoError::IncorrectScheme);
     154            0 :     }
     155              : 
     156            0 :     let mut url_path = connection_url
     157            0 :         .path_segments()
     158            0 :         .ok_or(ConnInfoError::MissingDbName)?;
     159              : 
     160            0 :     let dbname: DbName =
     161            0 :         urlencoding::decode(url_path.next().ok_or(ConnInfoError::InvalidDbName)?)?.into();
     162            0 :     ctx.set_dbname(dbname.clone());
     163              : 
     164            0 :     let username = RoleName::from(urlencoding::decode(connection_url.username())?);
     165            0 :     if username.is_empty() {
     166            0 :         return Err(ConnInfoError::MissingUsername);
     167            0 :     }
     168            0 :     ctx.set_user(username.clone());
     169              : 
     170            0 :     let auth = if let Some(auth) = headers.get(&AUTHORIZATION) {
     171            0 :         if !config.accept_jwts {
     172            0 :             return Err(ConnInfoError::MissingCredentials(Credentials::Password));
     173            0 :         }
     174              : 
     175            0 :         let auth = auth
     176            0 :             .to_str()
     177            0 :             .map_err(|_| ConnInfoError::InvalidHeader(&AUTHORIZATION))?;
     178              :         AuthData::Jwt(
     179            0 :             auth.strip_prefix("Bearer ")
     180            0 :                 .ok_or(ConnInfoError::MissingCredentials(Credentials::BearerJwt))?
     181            0 :                 .into(),
     182              :         )
     183            0 :     } else if let Some(pass) = connection_url.password() {
     184              :         // wrong credentials provided
     185            0 :         if config.accept_jwts {
     186            0 :             return Err(ConnInfoError::MissingCredentials(Credentials::BearerJwt));
     187            0 :         }
     188            0 : 
     189            0 :         AuthData::Password(match urlencoding::decode_binary(pass.as_bytes()) {
     190            0 :             std::borrow::Cow::Borrowed(b) => b.into(),
     191            0 :             std::borrow::Cow::Owned(b) => b.into(),
     192              :         })
     193            0 :     } else if config.accept_jwts {
     194            0 :         return Err(ConnInfoError::MissingCredentials(Credentials::BearerJwt));
     195              :     } else {
     196            0 :         return Err(ConnInfoError::MissingCredentials(Credentials::Password));
     197              :     };
     198              : 
     199            0 :     let endpoint = match connection_url.host() {
     200            0 :         Some(url::Host::Domain(hostname)) => {
     201            0 :             if let Some(tls) = tls {
     202            0 :                 endpoint_sni(hostname, &tls.common_names)?
     203            0 :                     .ok_or(ConnInfoError::MalformedEndpoint)?
     204              :             } else {
     205            0 :                 hostname
     206            0 :                     .split_once('.')
     207            0 :                     .map_or(hostname, |(prefix, _)| prefix)
     208            0 :                     .into()
     209              :             }
     210              :         }
     211              :         Some(url::Host::Ipv4(_) | url::Host::Ipv6(_)) | None => {
     212            0 :             return Err(ConnInfoError::MissingHostname);
     213              :         }
     214              :     };
     215            0 :     ctx.set_endpoint_id(endpoint.clone());
     216            0 : 
     217            0 :     let pairs = connection_url.query_pairs();
     218            0 : 
     219            0 :     let mut options = Option::None;
     220            0 : 
     221            0 :     let mut params = StartupMessageParamsBuilder::default();
     222            0 :     params.insert("user", &username);
     223            0 :     params.insert("database", &dbname);
     224            0 :     for (key, value) in pairs {
     225            0 :         params.insert(&key, &value);
     226            0 :         if key == "options" {
     227            0 :             options = Some(NeonOptions::parse_options_raw(&value));
     228            0 :         }
     229              :     }
     230              : 
     231            0 :     ctx.set_user_agent(
     232            0 :         headers
     233            0 :             .get(hyper::header::USER_AGENT)
     234            0 :             .and_then(|h| h.to_str().ok())
     235            0 :             .map(Into::into),
     236            0 :     );
     237            0 : 
     238            0 :     let user_info = ComputeUserInfo {
     239            0 :         endpoint,
     240            0 :         user: username,
     241            0 :         options: options.unwrap_or_default(),
     242            0 :     };
     243            0 : 
     244            0 :     let conn_info = ConnInfo { user_info, dbname };
     245            0 :     Ok(ConnInfoWithAuth { conn_info, auth })
     246            0 : }
     247              : 
     248            0 : pub(crate) async fn handle(
     249            0 :     config: &'static ProxyConfig,
     250            0 :     ctx: RequestContext,
     251            0 :     request: Request<Incoming>,
     252            0 :     backend: Arc<PoolingBackend>,
     253            0 :     cancel: CancellationToken,
     254            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, ApiError> {
     255            0 :     let result = handle_inner(cancel, config, &ctx, request, backend).await;
     256              : 
     257            0 :     let mut response = match result {
     258            0 :         Ok(r) => {
     259            0 :             ctx.set_success();
     260            0 : 
     261            0 :             // Handling the error response from local proxy here
     262            0 :             if config.authentication_config.is_auth_broker && r.status().is_server_error() {
     263            0 :                 let status = r.status();
     264              : 
     265            0 :                 let body_bytes = r
     266            0 :                     .collect()
     267            0 :                     .await
     268            0 :                     .map_err(|e| {
     269            0 :                         ApiError::InternalServerError(anyhow::Error::msg(format!(
     270            0 :                             "could not collect http body: {e}"
     271            0 :                         )))
     272            0 :                     })?
     273            0 :                     .to_bytes();
     274              : 
     275            0 :                 if let Ok(mut json_map) =
     276            0 :                     serde_json::from_slice::<IndexMap<&str, &RawValue>>(&body_bytes)
     277              :                 {
     278            0 :                     let message = json_map.get("message");
     279            0 :                     if let Some(message) = message {
     280            0 :                         let msg: String = match serde_json::from_str(message.get()) {
     281            0 :                             Ok(msg) => msg,
     282              :                             Err(_) => {
     283            0 :                                 "Unable to parse the response message from server".to_string()
     284              :                             }
     285              :                         };
     286              : 
     287            0 :                         error!("Error response from local_proxy: {status} {msg}");
     288              : 
     289            0 :                         json_map.retain(|key, _| !key.starts_with("neon:")); // remove all the neon-related keys
     290            0 : 
     291            0 :                         let resp_json = serde_json::to_string(&json_map)
     292            0 :                             .unwrap_or("failed to serialize the response message".to_string());
     293            0 : 
     294            0 :                         return json_response(status, resp_json);
     295            0 :                     }
     296            0 :                 }
     297              : 
     298            0 :                 error!("Unable to parse the response message from local_proxy");
     299            0 :                 return json_response(
     300            0 :                     status,
     301            0 :                     json!({ "message": "Unable to parse the response message from server".to_string() }),
     302            0 :                 );
     303            0 :             }
     304            0 :             r
     305              :         }
     306            0 :         Err(e @ SqlOverHttpError::Cancelled(_)) => {
     307            0 :             let error_kind = e.get_error_kind();
     308            0 :             ctx.set_error_kind(error_kind);
     309            0 : 
     310            0 :             let message = "Query cancelled, connection was terminated";
     311            0 : 
     312            0 :             tracing::info!(
     313            0 :                 kind=error_kind.to_metric_label(),
     314            0 :                 error=%e,
     315            0 :                 msg=message,
     316            0 :                 "forwarding error to user"
     317              :             );
     318              : 
     319            0 :             json_response(
     320            0 :                 StatusCode::BAD_REQUEST,
     321            0 :                 json!({ "message": message, "code": SqlState::PROTOCOL_VIOLATION.code() }),
     322            0 :             )?
     323              :         }
     324            0 :         Err(e) => {
     325            0 :             let error_kind = e.get_error_kind();
     326            0 :             ctx.set_error_kind(error_kind);
     327            0 : 
     328            0 :             let mut message = e.to_string_client();
     329            0 :             let db_error = match &e {
     330            0 :                 SqlOverHttpError::ConnectCompute(HttpConnError::PostgresConnectionError(e))
     331            0 :                 | SqlOverHttpError::Postgres(e) => e.as_db_error(),
     332            0 :                 _ => None,
     333              :             };
     334            0 :             fn get<'a, T: Default>(db: Option<&'a DbError>, x: impl FnOnce(&'a DbError) -> T) -> T {
     335            0 :                 db.map(x).unwrap_or_default()
     336            0 :             }
     337              : 
     338            0 :             if let Some(db_error) = db_error {
     339            0 :                 db_error.message().clone_into(&mut message);
     340            0 :             }
     341              : 
     342            0 :             let position = db_error.and_then(|db| db.position());
     343            0 :             let (position, internal_position, internal_query) = match position {
     344            0 :                 Some(ErrorPosition::Original(position)) => (Some(position.to_string()), None, None),
     345            0 :                 Some(ErrorPosition::Internal { position, query }) => {
     346            0 :                     (None, Some(position.to_string()), Some(query.clone()))
     347              :                 }
     348            0 :                 None => (None, None, None),
     349              :             };
     350              : 
     351            0 :             let code = get(db_error, |db| db.code().code());
     352            0 :             let severity = get(db_error, |db| db.severity());
     353            0 :             let detail = get(db_error, |db| db.detail());
     354            0 :             let hint = get(db_error, |db| db.hint());
     355            0 :             let where_ = get(db_error, |db| db.where_());
     356            0 :             let table = get(db_error, |db| db.table());
     357            0 :             let column = get(db_error, |db| db.column());
     358            0 :             let schema = get(db_error, |db| db.schema());
     359            0 :             let datatype = get(db_error, |db| db.datatype());
     360            0 :             let constraint = get(db_error, |db| db.constraint());
     361            0 :             let file = get(db_error, |db| db.file());
     362            0 :             let line = get(db_error, |db| db.line().map(|l| l.to_string()));
     363            0 :             let routine = get(db_error, |db| db.routine());
     364            0 : 
     365            0 :             tracing::info!(
     366            0 :                 kind=error_kind.to_metric_label(),
     367            0 :                 error=%e,
     368            0 :                 msg=message,
     369            0 :                 "forwarding error to user"
     370              :             );
     371              : 
     372            0 :             json_response(
     373            0 :                 e.get_http_status_code(),
     374            0 :                 json!({
     375            0 :                     "message": message,
     376            0 :                     "code": code,
     377            0 :                     "detail": detail,
     378            0 :                     "hint": hint,
     379            0 :                     "position": position,
     380            0 :                     "internalPosition": internal_position,
     381            0 :                     "internalQuery": internal_query,
     382            0 :                     "severity": severity,
     383            0 :                     "where": where_,
     384            0 :                     "table": table,
     385            0 :                     "column": column,
     386            0 :                     "schema": schema,
     387            0 :                     "dataType": datatype,
     388            0 :                     "constraint": constraint,
     389            0 :                     "file": file,
     390            0 :                     "line": line,
     391            0 :                     "routine": routine,
     392            0 :                 }),
     393            0 :             )?
     394              :         }
     395              :     };
     396              : 
     397            0 :     response
     398            0 :         .headers_mut()
     399            0 :         .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
     400            0 :     Ok(response)
     401            0 : }
     402              : 
     403              : #[derive(Debug, thiserror::Error)]
     404              : pub(crate) enum SqlOverHttpError {
     405              :     #[error("{0}")]
     406              :     ReadPayload(#[from] ReadPayloadError),
     407              :     #[error("{0}")]
     408              :     ConnectCompute(#[from] HttpConnError),
     409              :     #[error("{0}")]
     410              :     ConnInfo(#[from] ConnInfoError),
     411              :     #[error("response is too large (max is {0} bytes)")]
     412              :     ResponseTooLarge(usize),
     413              :     #[error("invalid isolation level")]
     414              :     InvalidIsolationLevel,
     415              :     /// for queries our customers choose to run
     416              :     #[error("{0}")]
     417              :     Postgres(#[source] postgres_client::Error),
     418              :     /// for queries we choose to run
     419              :     #[error("{0}")]
     420              :     InternalPostgres(#[source] postgres_client::Error),
     421              :     #[error("{0}")]
     422              :     JsonConversion(#[from] JsonConversionError),
     423              :     #[error("{0}")]
     424              :     Cancelled(SqlOverHttpCancel),
     425              : }
     426              : 
     427              : impl ReportableError for SqlOverHttpError {
     428            0 :     fn get_error_kind(&self) -> ErrorKind {
     429            0 :         match self {
     430            0 :             SqlOverHttpError::ReadPayload(e) => e.get_error_kind(),
     431            0 :             SqlOverHttpError::ConnectCompute(e) => e.get_error_kind(),
     432            0 :             SqlOverHttpError::ConnInfo(e) => e.get_error_kind(),
     433            0 :             SqlOverHttpError::ResponseTooLarge(_) => ErrorKind::User,
     434            0 :             SqlOverHttpError::InvalidIsolationLevel => ErrorKind::User,
     435            0 :             SqlOverHttpError::Postgres(p) => p.get_error_kind(),
     436            0 :             SqlOverHttpError::InternalPostgres(p) => {
     437            0 :                 if p.as_db_error().is_some() {
     438            0 :                     ErrorKind::Service
     439              :                 } else {
     440            0 :                     ErrorKind::Compute
     441              :                 }
     442              :             }
     443            0 :             SqlOverHttpError::JsonConversion(_) => ErrorKind::Postgres,
     444            0 :             SqlOverHttpError::Cancelled(c) => c.get_error_kind(),
     445              :         }
     446            0 :     }
     447              : }
     448              : 
     449              : impl UserFacingError for SqlOverHttpError {
     450            0 :     fn to_string_client(&self) -> String {
     451            0 :         match self {
     452            0 :             SqlOverHttpError::ReadPayload(p) => p.to_string(),
     453            0 :             SqlOverHttpError::ConnectCompute(c) => c.to_string_client(),
     454            0 :             SqlOverHttpError::ConnInfo(c) => c.to_string_client(),
     455            0 :             SqlOverHttpError::ResponseTooLarge(_) => self.to_string(),
     456            0 :             SqlOverHttpError::InvalidIsolationLevel => self.to_string(),
     457            0 :             SqlOverHttpError::Postgres(p) => p.to_string(),
     458            0 :             SqlOverHttpError::InternalPostgres(p) => p.to_string(),
     459            0 :             SqlOverHttpError::JsonConversion(_) => "could not parse postgres response".to_string(),
     460            0 :             SqlOverHttpError::Cancelled(_) => self.to_string(),
     461              :         }
     462            0 :     }
     463              : }
     464              : 
     465              : impl HttpCodeError for SqlOverHttpError {
     466            0 :     fn get_http_status_code(&self) -> StatusCode {
     467            0 :         match self {
     468            0 :             SqlOverHttpError::ReadPayload(e) => e.get_http_status_code(),
     469            0 :             SqlOverHttpError::ConnectCompute(h) => match h.get_error_kind() {
     470            0 :                 ErrorKind::User => StatusCode::BAD_REQUEST,
     471            0 :                 _ => StatusCode::INTERNAL_SERVER_ERROR,
     472              :             },
     473            0 :             SqlOverHttpError::ConnInfo(_) => StatusCode::BAD_REQUEST,
     474            0 :             SqlOverHttpError::ResponseTooLarge(_) => StatusCode::INSUFFICIENT_STORAGE,
     475            0 :             SqlOverHttpError::InvalidIsolationLevel => StatusCode::BAD_REQUEST,
     476            0 :             SqlOverHttpError::Postgres(_) => StatusCode::BAD_REQUEST,
     477            0 :             SqlOverHttpError::InternalPostgres(_) => StatusCode::INTERNAL_SERVER_ERROR,
     478            0 :             SqlOverHttpError::JsonConversion(_) => StatusCode::INTERNAL_SERVER_ERROR,
     479            0 :             SqlOverHttpError::Cancelled(_) => StatusCode::INTERNAL_SERVER_ERROR,
     480              :         }
     481            0 :     }
     482              : }
     483              : 
     484              : #[derive(Debug, thiserror::Error)]
     485              : pub(crate) enum ReadPayloadError {
     486              :     #[error("could not read the HTTP request body: {0}")]
     487              :     Read(#[from] hyper::Error),
     488              :     #[error("request is too large (max is {limit} bytes)")]
     489              :     BodyTooLarge { limit: usize },
     490              :     #[error("could not parse the HTTP request body: {0}")]
     491              :     Parse(#[from] serde_json::Error),
     492              : }
     493              : 
     494              : impl From<ReadBodyError<hyper::Error>> for ReadPayloadError {
     495            0 :     fn from(value: ReadBodyError<hyper::Error>) -> Self {
     496            0 :         match value {
     497            0 :             ReadBodyError::BodyTooLarge { limit } => Self::BodyTooLarge { limit },
     498            0 :             ReadBodyError::Read(e) => Self::Read(e),
     499              :         }
     500            0 :     }
     501              : }
     502              : 
     503              : impl ReportableError for ReadPayloadError {
     504            0 :     fn get_error_kind(&self) -> ErrorKind {
     505            0 :         match self {
     506            0 :             ReadPayloadError::Read(_) => ErrorKind::ClientDisconnect,
     507            0 :             ReadPayloadError::BodyTooLarge { .. } => ErrorKind::User,
     508            0 :             ReadPayloadError::Parse(_) => ErrorKind::User,
     509              :         }
     510            0 :     }
     511              : }
     512              : 
     513              : impl HttpCodeError for ReadPayloadError {
     514            0 :     fn get_http_status_code(&self) -> StatusCode {
     515            0 :         match self {
     516            0 :             ReadPayloadError::Read(_) => StatusCode::BAD_REQUEST,
     517            0 :             ReadPayloadError::BodyTooLarge { .. } => StatusCode::PAYLOAD_TOO_LARGE,
     518            0 :             ReadPayloadError::Parse(_) => StatusCode::BAD_REQUEST,
     519              :         }
     520            0 :     }
     521              : }
     522              : 
     523              : #[derive(Debug, thiserror::Error)]
     524              : pub(crate) enum SqlOverHttpCancel {
     525              :     #[error("query was cancelled")]
     526              :     Postgres,
     527              :     #[error("query was cancelled while stuck trying to connect to the database")]
     528              :     Connect,
     529              : }
     530              : 
     531              : impl ReportableError for SqlOverHttpCancel {
     532            0 :     fn get_error_kind(&self) -> ErrorKind {
     533            0 :         match self {
     534            0 :             SqlOverHttpCancel::Postgres => ErrorKind::ClientDisconnect,
     535            0 :             SqlOverHttpCancel::Connect => ErrorKind::ClientDisconnect,
     536              :         }
     537            0 :     }
     538              : }
     539              : 
     540              : #[derive(Clone, Copy, Debug)]
     541              : struct HttpHeaders {
     542              :     raw_output: bool,
     543              :     default_array_mode: bool,
     544              :     txn_isolation_level: Option<IsolationLevel>,
     545              :     txn_read_only: bool,
     546              :     txn_deferrable: bool,
     547              : }
     548              : 
     549              : impl HttpHeaders {
     550            0 :     fn try_parse(headers: &hyper::http::HeaderMap) -> Result<Self, SqlOverHttpError> {
     551            0 :         // Determine the output options. Default behaviour is 'false'. Anything that is not
     552            0 :         // strictly 'true' assumed to be false.
     553            0 :         let raw_output = headers.get(&RAW_TEXT_OUTPUT) == Some(&HEADER_VALUE_TRUE);
     554            0 :         let default_array_mode = headers.get(&ARRAY_MODE) == Some(&HEADER_VALUE_TRUE);
     555              : 
     556              :         // isolation level, read only and deferrable
     557            0 :         let txn_isolation_level = match headers.get(&TXN_ISOLATION_LEVEL) {
     558            0 :             Some(x) => Some(
     559            0 :                 map_header_to_isolation_level(x).ok_or(SqlOverHttpError::InvalidIsolationLevel)?,
     560              :             ),
     561            0 :             None => None,
     562              :         };
     563              : 
     564            0 :         let txn_read_only = headers.get(&TXN_READ_ONLY) == Some(&HEADER_VALUE_TRUE);
     565            0 :         let txn_deferrable = headers.get(&TXN_DEFERRABLE) == Some(&HEADER_VALUE_TRUE);
     566            0 : 
     567            0 :         Ok(Self {
     568            0 :             raw_output,
     569            0 :             default_array_mode,
     570            0 :             txn_isolation_level,
     571            0 :             txn_read_only,
     572            0 :             txn_deferrable,
     573            0 :         })
     574            0 :     }
     575              : }
     576              : 
     577            0 : fn map_header_to_isolation_level(level: &HeaderValue) -> Option<IsolationLevel> {
     578            0 :     match level.as_bytes() {
     579            0 :         b"Serializable" => Some(IsolationLevel::Serializable),
     580            0 :         b"ReadUncommitted" => Some(IsolationLevel::ReadUncommitted),
     581            0 :         b"ReadCommitted" => Some(IsolationLevel::ReadCommitted),
     582            0 :         b"RepeatableRead" => Some(IsolationLevel::RepeatableRead),
     583            0 :         _ => None,
     584              :     }
     585            0 : }
     586              : 
     587            0 : fn map_isolation_level_to_headers(level: IsolationLevel) -> Option<HeaderValue> {
     588            0 :     match level {
     589            0 :         IsolationLevel::ReadUncommitted => Some(HeaderValue::from_static("ReadUncommitted")),
     590            0 :         IsolationLevel::ReadCommitted => Some(HeaderValue::from_static("ReadCommitted")),
     591            0 :         IsolationLevel::RepeatableRead => Some(HeaderValue::from_static("RepeatableRead")),
     592            0 :         IsolationLevel::Serializable => Some(HeaderValue::from_static("Serializable")),
     593            0 :         _ => None,
     594              :     }
     595            0 : }
     596              : 
     597            0 : async fn handle_inner(
     598            0 :     cancel: CancellationToken,
     599            0 :     config: &'static ProxyConfig,
     600            0 :     ctx: &RequestContext,
     601            0 :     request: Request<Incoming>,
     602            0 :     backend: Arc<PoolingBackend>,
     603            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
     604            0 :     let _requeset_gauge = Metrics::get()
     605            0 :         .proxy
     606            0 :         .connection_requests
     607            0 :         .guard(ctx.protocol());
     608            0 :     info!(
     609            0 :         protocol = %ctx.protocol(),
     610            0 :         "handling interactive connection from client"
     611              :     );
     612              : 
     613            0 :     let conn_info = get_conn_info(
     614            0 :         &config.authentication_config,
     615            0 :         ctx,
     616            0 :         request.headers(),
     617            0 :         // todo: race condition?
     618            0 :         // we're unlikely to change the common names.
     619            0 :         config.tls_config.load().as_deref(),
     620            0 :     )?;
     621            0 :     info!(
     622            0 :         user = conn_info.conn_info.user_info.user.as_str(),
     623            0 :         "credentials"
     624              :     );
     625              : 
     626            0 :     match conn_info.auth {
     627            0 :         AuthData::Jwt(jwt) if config.authentication_config.is_auth_broker => {
     628            0 :             handle_auth_broker_inner(ctx, request, conn_info.conn_info, jwt, backend).await
     629              :         }
     630            0 :         auth => {
     631            0 :             handle_db_inner(
     632            0 :                 cancel,
     633            0 :                 config,
     634            0 :                 ctx,
     635            0 :                 request,
     636            0 :                 conn_info.conn_info,
     637            0 :                 auth,
     638            0 :                 backend,
     639            0 :             )
     640            0 :             .await
     641              :         }
     642              :     }
     643            0 : }
     644              : 
     645            0 : async fn handle_db_inner(
     646            0 :     cancel: CancellationToken,
     647            0 :     config: &'static ProxyConfig,
     648            0 :     ctx: &RequestContext,
     649            0 :     request: Request<Incoming>,
     650            0 :     conn_info: ConnInfo,
     651            0 :     auth: AuthData,
     652            0 :     backend: Arc<PoolingBackend>,
     653            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
     654            0 :     //
     655            0 :     // Determine the destination and connection params
     656            0 :     //
     657            0 :     let headers = request.headers();
     658              : 
     659              :     // Allow connection pooling only if explicitly requested
     660              :     // or if we have decided that http pool is no longer opt-in
     661            0 :     let allow_pool = !config.http_config.pool_options.opt_in
     662            0 :         || headers.get(&ALLOW_POOL) == Some(&HEADER_VALUE_TRUE);
     663              : 
     664            0 :     let parsed_headers = HttpHeaders::try_parse(headers)?;
     665              : 
     666            0 :     let mut request_len = 0;
     667            0 :     let fetch_and_process_request = Box::pin(
     668            0 :         async {
     669            0 :             let body = read_body_with_limit(
     670            0 :                 request.into_body(),
     671            0 :                 config.http_config.max_request_size_bytes,
     672            0 :             )
     673            0 :             .await?;
     674              : 
     675            0 :             request_len = body.len();
     676            0 : 
     677            0 :             Metrics::get()
     678            0 :                 .proxy
     679            0 :                 .http_conn_content_length_bytes
     680            0 :                 .observe(HttpDirection::Request, body.len() as f64);
     681            0 : 
     682            0 :             debug!(length = body.len(), "request payload read");
     683            0 :             let payload: Payload = serde_json::from_slice(&body)?;
     684            0 :             Ok::<Payload, ReadPayloadError>(payload) // Adjust error type accordingly
     685            0 :         }
     686            0 :         .map_err(SqlOverHttpError::from),
     687            0 :     );
     688            0 : 
     689            0 :     let authenticate_and_connect = Box::pin(
     690            0 :         async {
     691            0 :             let keys = match auth {
     692            0 :                 AuthData::Password(pw) => backend
     693            0 :                     .authenticate_with_password(ctx, &conn_info.user_info, &pw)
     694            0 :                     .await
     695            0 :                     .map_err(HttpConnError::AuthError)?,
     696            0 :                 AuthData::Jwt(jwt) => backend
     697            0 :                     .authenticate_with_jwt(ctx, &conn_info.user_info, jwt)
     698            0 :                     .await
     699            0 :                     .map_err(HttpConnError::AuthError)?,
     700              :             };
     701              : 
     702            0 :             let client = match keys.keys {
     703            0 :                 ComputeCredentialKeys::JwtPayload(payload)
     704            0 :                     if backend.auth_backend.is_local_proxy() =>
     705              :                 {
     706            0 :                     let mut client = backend.connect_to_local_postgres(ctx, conn_info).await?;
     707            0 :                     let (cli_inner, _dsc) = client.client_inner();
     708            0 :                     cli_inner.set_jwt_session(&payload).await?;
     709            0 :                     Client::Local(client)
     710              :                 }
     711              :                 _ => {
     712            0 :                     let client = backend
     713            0 :                         .connect_to_compute(ctx, conn_info, keys, !allow_pool)
     714            0 :                         .await?;
     715            0 :                     Client::Remote(client)
     716              :                 }
     717              :             };
     718              : 
     719              :             // not strictly necessary to mark success here,
     720              :             // but it's just insurance for if we forget it somewhere else
     721            0 :             ctx.success();
     722            0 :             Ok::<_, SqlOverHttpError>(client)
     723            0 :         }
     724            0 :         .map_err(SqlOverHttpError::from),
     725            0 :     );
     726              : 
     727            0 :     let (payload, mut client) = match run_until_cancelled(
     728            0 :         // Run both operations in parallel
     729            0 :         try_join(
     730            0 :             pin!(fetch_and_process_request),
     731            0 :             pin!(authenticate_and_connect),
     732            0 :         ),
     733            0 :         &cancel,
     734            0 :     )
     735            0 :     .await
     736              :     {
     737            0 :         Some(result) => result?,
     738            0 :         None => return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Connect)),
     739              :     };
     740              : 
     741            0 :     let mut response = Response::builder()
     742            0 :         .status(StatusCode::OK)
     743            0 :         .header(header::CONTENT_TYPE, "application/json");
     744              : 
     745              :     // Now execute the query and return the result.
     746            0 :     let json_output = match payload {
     747            0 :         Payload::Single(stmt) => {
     748            0 :             stmt.process(&config.http_config, cancel, &mut client, parsed_headers)
     749            0 :                 .await?
     750              :         }
     751            0 :         Payload::Batch(statements) => {
     752            0 :             if parsed_headers.txn_read_only {
     753            0 :                 response = response.header(TXN_READ_ONLY.clone(), &HEADER_VALUE_TRUE);
     754            0 :             }
     755            0 :             if parsed_headers.txn_deferrable {
     756            0 :                 response = response.header(TXN_DEFERRABLE.clone(), &HEADER_VALUE_TRUE);
     757            0 :             }
     758            0 :             if let Some(txn_isolation_level) = parsed_headers
     759            0 :                 .txn_isolation_level
     760            0 :                 .and_then(map_isolation_level_to_headers)
     761            0 :             {
     762            0 :                 response = response.header(TXN_ISOLATION_LEVEL.clone(), txn_isolation_level);
     763            0 :             }
     764              : 
     765            0 :             statements
     766            0 :                 .process(&config.http_config, cancel, &mut client, parsed_headers)
     767            0 :                 .await?
     768              :         }
     769              :     };
     770              : 
     771            0 :     let metrics = client.metrics(ctx);
     772            0 : 
     773            0 :     let len = json_output.len();
     774            0 :     let response = response
     775            0 :         .body(
     776            0 :             Full::new(Bytes::from(json_output))
     777            0 :                 .map_err(|x| match x {})
     778            0 :                 .boxed(),
     779            0 :         )
     780            0 :         // only fails if invalid status code or invalid header/values are given.
     781            0 :         // these are not user configurable so it cannot fail dynamically
     782            0 :         .expect("building response payload should not fail");
     783            0 : 
     784            0 :     // count the egress bytes - we miss the TLS and header overhead but oh well...
     785            0 :     // moving this later in the stack is going to be a lot of effort and ehhhh
     786            0 :     metrics.record_egress(len as u64);
     787            0 :     metrics.record_ingress(request_len as u64);
     788            0 : 
     789            0 :     Metrics::get()
     790            0 :         .proxy
     791            0 :         .http_conn_content_length_bytes
     792            0 :         .observe(HttpDirection::Response, len as f64);
     793            0 : 
     794            0 :     Ok(response)
     795            0 : }
     796              : 
     797              : static HEADERS_TO_FORWARD: &[&HeaderName] = &[
     798              :     &AUTHORIZATION,
     799              :     &CONN_STRING,
     800              :     &RAW_TEXT_OUTPUT,
     801              :     &ARRAY_MODE,
     802              :     &TXN_ISOLATION_LEVEL,
     803              :     &TXN_READ_ONLY,
     804              :     &TXN_DEFERRABLE,
     805              : ];
     806              : 
     807            0 : pub(crate) fn uuid_to_header_value(id: Uuid) -> HeaderValue {
     808            0 :     let mut uuid = [0; uuid::fmt::Hyphenated::LENGTH];
     809            0 :     HeaderValue::from_str(id.as_hyphenated().encode_lower(&mut uuid[..]))
     810            0 :         .expect("uuid hyphenated format should be all valid header characters")
     811            0 : }
     812              : 
     813            0 : async fn handle_auth_broker_inner(
     814            0 :     ctx: &RequestContext,
     815            0 :     request: Request<Incoming>,
     816            0 :     conn_info: ConnInfo,
     817            0 :     jwt: String,
     818            0 :     backend: Arc<PoolingBackend>,
     819            0 : ) -> Result<Response<BoxBody<Bytes, hyper::Error>>, SqlOverHttpError> {
     820            0 :     backend
     821            0 :         .authenticate_with_jwt(ctx, &conn_info.user_info, jwt)
     822            0 :         .await
     823            0 :         .map_err(HttpConnError::from)?;
     824              : 
     825            0 :     let mut client = backend.connect_to_local_proxy(ctx, conn_info).await?;
     826              : 
     827            0 :     let local_proxy_uri = ::http::Uri::from_static("http://proxy.local/sql");
     828            0 : 
     829            0 :     let (mut parts, body) = request.into_parts();
     830            0 :     let mut req = Request::builder().method(Method::POST).uri(local_proxy_uri);
     831              : 
     832              :     // todo(conradludgate): maybe auth-broker should parse these and re-serialize
     833              :     // these instead just to ensure they remain normalised.
     834            0 :     for &h in HEADERS_TO_FORWARD {
     835            0 :         if let Some(hv) = parts.headers.remove(h) {
     836            0 :             req = req.header(h, hv);
     837            0 :         }
     838              :     }
     839            0 :     req = req.header(&NEON_REQUEST_ID, uuid_to_header_value(ctx.session_id()));
     840            0 : 
     841            0 :     let req = req
     842            0 :         .body(body)
     843            0 :         .expect("all headers and params received via hyper should be valid for request");
     844            0 : 
     845            0 :     // todo: map body to count egress
     846            0 :     let _metrics = client.metrics(ctx);
     847            0 : 
     848            0 :     Ok(client
     849            0 :         .inner
     850            0 :         .inner
     851            0 :         .send_request(req)
     852            0 :         .await
     853            0 :         .map_err(LocalProxyConnError::from)
     854            0 :         .map_err(HttpConnError::from)?
     855            0 :         .map(|b| b.boxed()))
     856            0 : }
     857              : 
     858              : impl QueryData {
     859            0 :     async fn process(
     860            0 :         self,
     861            0 :         config: &'static HttpConfig,
     862            0 :         cancel: CancellationToken,
     863            0 :         client: &mut Client,
     864            0 :         parsed_headers: HttpHeaders,
     865            0 :     ) -> Result<String, SqlOverHttpError> {
     866            0 :         let (inner, mut discard) = client.inner();
     867            0 :         let cancel_token = inner.cancel_token();
     868              : 
     869            0 :         let res = match select(
     870            0 :             pin!(query_to_json(
     871            0 :                 config,
     872            0 :                 &mut *inner,
     873            0 :                 self,
     874            0 :                 &mut 0,
     875            0 :                 parsed_headers
     876            0 :             )),
     877            0 :             pin!(cancel.cancelled()),
     878            0 :         )
     879            0 :         .await
     880              :         {
     881              :             // The query successfully completed.
     882            0 :             Either::Left((Ok((status, results)), __not_yet_cancelled)) => {
     883            0 :                 discard.check_idle(status);
     884            0 : 
     885            0 :                 let json_output =
     886            0 :                     serde_json::to_string(&results).expect("json serialization should not fail");
     887            0 :                 Ok(json_output)
     888              :             }
     889              :             // The query failed with an error
     890            0 :             Either::Left((Err(e), __not_yet_cancelled)) => {
     891            0 :                 discard.discard();
     892            0 :                 return Err(e);
     893              :             }
     894              :             // The query was cancelled.
     895            0 :             Either::Right((_cancelled, query)) => {
     896            0 :                 tracing::info!("cancelling query");
     897            0 :                 if let Err(err) = cancel_token.cancel_query(NoTls).await {
     898            0 :                     tracing::warn!(?err, "could not cancel query");
     899            0 :                 }
     900              :                 // wait for the query cancellation
     901            0 :                 match time::timeout(time::Duration::from_millis(100), query).await {
     902              :                     // query successed before it was cancelled.
     903            0 :                     Ok(Ok((status, results))) => {
     904            0 :                         discard.check_idle(status);
     905            0 : 
     906            0 :                         let json_output = serde_json::to_string(&results)
     907            0 :                             .expect("json serialization should not fail");
     908            0 :                         Ok(json_output)
     909              :                     }
     910              :                     // query failed or was cancelled.
     911            0 :                     Ok(Err(error)) => {
     912            0 :                         let db_error = match &error {
     913              :                             SqlOverHttpError::ConnectCompute(
     914            0 :                                 HttpConnError::PostgresConnectionError(e),
     915              :                             )
     916            0 :                             | SqlOverHttpError::Postgres(e) => e.as_db_error(),
     917            0 :                             _ => None,
     918              :                         };
     919              : 
     920              :                         // if errored for some other reason, it might not be safe to return
     921            0 :                         if !db_error.is_some_and(|e| *e.code() == SqlState::QUERY_CANCELED) {
     922            0 :                             discard.discard();
     923            0 :                         }
     924              : 
     925            0 :                         Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres))
     926              :                     }
     927            0 :                     Err(_timeout) => {
     928            0 :                         discard.discard();
     929            0 :                         Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres))
     930              :                     }
     931              :                 }
     932              :             }
     933              :         };
     934            0 :         res
     935            0 :     }
     936              : }
     937              : 
     938              : impl BatchQueryData {
     939            0 :     async fn process(
     940            0 :         self,
     941            0 :         config: &'static HttpConfig,
     942            0 :         cancel: CancellationToken,
     943            0 :         client: &mut Client,
     944            0 :         parsed_headers: HttpHeaders,
     945            0 :     ) -> Result<String, SqlOverHttpError> {
     946            0 :         info!("starting transaction");
     947            0 :         let (inner, mut discard) = client.inner();
     948            0 :         let cancel_token = inner.cancel_token();
     949            0 :         let mut builder = inner.build_transaction();
     950            0 :         if let Some(isolation_level) = parsed_headers.txn_isolation_level {
     951            0 :             builder = builder.isolation_level(isolation_level);
     952            0 :         }
     953            0 :         if parsed_headers.txn_read_only {
     954            0 :             builder = builder.read_only(true);
     955            0 :         }
     956            0 :         if parsed_headers.txn_deferrable {
     957            0 :             builder = builder.deferrable(true);
     958            0 :         }
     959              : 
     960            0 :         let mut transaction = builder
     961            0 :             .start()
     962            0 :             .await
     963            0 :             .inspect_err(|_| {
     964            0 :                 // if we cannot start a transaction, we should return immediately
     965            0 :                 // and not return to the pool. connection is clearly broken
     966            0 :                 discard.discard();
     967            0 :             })
     968            0 :             .map_err(SqlOverHttpError::Postgres)?;
     969              : 
     970            0 :         let json_output = match query_batch(
     971            0 :             config,
     972            0 :             cancel.child_token(),
     973            0 :             &mut transaction,
     974            0 :             self,
     975            0 :             parsed_headers,
     976            0 :         )
     977            0 :         .await
     978              :         {
     979            0 :             Ok(json_output) => {
     980            0 :                 info!("commit");
     981            0 :                 let status = transaction
     982            0 :                     .commit()
     983            0 :                     .await
     984            0 :                     .inspect_err(|_| {
     985            0 :                         // if we cannot commit - for now don't return connection to pool
     986            0 :                         // TODO: get a query status from the error
     987            0 :                         discard.discard();
     988            0 :                     })
     989            0 :                     .map_err(SqlOverHttpError::Postgres)?;
     990            0 :                 discard.check_idle(status);
     991            0 :                 json_output
     992              :             }
     993              :             Err(SqlOverHttpError::Cancelled(_)) => {
     994            0 :                 if let Err(err) = cancel_token.cancel_query(NoTls).await {
     995            0 :                     tracing::warn!(?err, "could not cancel query");
     996            0 :                 }
     997              :                 // TODO: after cancelling, wait to see if we can get a status. maybe the connection is still safe.
     998            0 :                 discard.discard();
     999            0 : 
    1000            0 :                 return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres));
    1001              :             }
    1002            0 :             Err(err) => {
    1003            0 :                 info!("rollback");
    1004            0 :                 let status = transaction
    1005            0 :                     .rollback()
    1006            0 :                     .await
    1007            0 :                     .inspect_err(|_| {
    1008            0 :                         // if we cannot rollback - for now don't return connection to pool
    1009            0 :                         // TODO: get a query status from the error
    1010            0 :                         discard.discard();
    1011            0 :                     })
    1012            0 :                     .map_err(SqlOverHttpError::Postgres)?;
    1013            0 :                 discard.check_idle(status);
    1014            0 :                 return Err(err);
    1015              :             }
    1016              :         };
    1017              : 
    1018            0 :         Ok(json_output)
    1019            0 :     }
    1020              : }
    1021              : 
    1022            0 : async fn query_batch(
    1023            0 :     config: &'static HttpConfig,
    1024            0 :     cancel: CancellationToken,
    1025            0 :     transaction: &mut Transaction<'_>,
    1026            0 :     queries: BatchQueryData,
    1027            0 :     parsed_headers: HttpHeaders,
    1028            0 : ) -> Result<String, SqlOverHttpError> {
    1029            0 :     let mut results = Vec::with_capacity(queries.queries.len());
    1030            0 :     let mut current_size = 0;
    1031            0 :     for stmt in queries.queries {
    1032            0 :         let query = pin!(query_to_json(
    1033            0 :             config,
    1034            0 :             transaction,
    1035            0 :             stmt,
    1036            0 :             &mut current_size,
    1037            0 :             parsed_headers,
    1038            0 :         ));
    1039            0 :         let cancelled = pin!(cancel.cancelled());
    1040            0 :         let res = select(query, cancelled).await;
    1041            0 :         match res {
    1042              :             // TODO: maybe we should check that the transaction bit is set here
    1043            0 :             Either::Left((Ok((_, values)), _cancelled)) => {
    1044            0 :                 results.push(values);
    1045            0 :             }
    1046            0 :             Either::Left((Err(e), _cancelled)) => {
    1047            0 :                 return Err(e);
    1048              :             }
    1049            0 :             Either::Right((_cancelled, _)) => {
    1050            0 :                 return Err(SqlOverHttpError::Cancelled(SqlOverHttpCancel::Postgres));
    1051              :             }
    1052              :         }
    1053              :     }
    1054              : 
    1055            0 :     let results = json!({ "results": results });
    1056            0 :     let json_output = serde_json::to_string(&results).expect("json serialization should not fail");
    1057            0 : 
    1058            0 :     Ok(json_output)
    1059            0 : }
    1060              : 
    1061            0 : async fn query_to_json<T: GenericClient>(
    1062            0 :     config: &'static HttpConfig,
    1063            0 :     client: &mut T,
    1064            0 :     data: QueryData,
    1065            0 :     current_size: &mut usize,
    1066            0 :     parsed_headers: HttpHeaders,
    1067            0 : ) -> Result<(ReadyForQueryStatus, impl Serialize + use<T>), SqlOverHttpError> {
    1068            0 :     let query_start = Instant::now();
    1069            0 : 
    1070            0 :     let query_params = data.params;
    1071            0 :     let mut row_stream = std::pin::pin!(
    1072            0 :         client
    1073            0 :             .query_raw_txt(&data.query, query_params)
    1074            0 :             .await
    1075            0 :             .map_err(SqlOverHttpError::Postgres)?
    1076              :     );
    1077            0 :     let query_acknowledged = Instant::now();
    1078            0 : 
    1079            0 :     // Manually drain the stream into a vector to leave row_stream hanging
    1080            0 :     // around to get a command tag. Also check that the response is not too
    1081            0 :     // big.
    1082            0 :     let mut rows: Vec<postgres_client::Row> = Vec::new();
    1083            0 :     while let Some(row) = row_stream.next().await {
    1084            0 :         let row = row.map_err(SqlOverHttpError::Postgres)?;
    1085            0 :         *current_size += row.body_len();
    1086            0 :         rows.push(row);
    1087            0 :         // we don't have a streaming response support yet so this is to prevent OOM
    1088            0 :         // from a malicious query (eg a cross join)
    1089            0 :         if *current_size > config.max_response_size_bytes {
    1090            0 :             return Err(SqlOverHttpError::ResponseTooLarge(
    1091            0 :                 config.max_response_size_bytes,
    1092            0 :             ));
    1093            0 :         }
    1094              :     }
    1095              : 
    1096            0 :     let query_resp_end = Instant::now();
    1097            0 :     let ready = row_stream.ready_status();
    1098            0 : 
    1099            0 :     // grab the command tag and number of rows affected
    1100            0 :     let command_tag = row_stream.command_tag().unwrap_or_default();
    1101            0 :     let mut command_tag_split = command_tag.split(' ');
    1102            0 :     let command_tag_name = command_tag_split.next().unwrap_or_default();
    1103            0 :     let command_tag_count = if command_tag_name == "INSERT" {
    1104              :         // INSERT returns OID first and then number of rows
    1105            0 :         command_tag_split.nth(1)
    1106              :     } else {
    1107              :         // other commands return number of rows (if any)
    1108            0 :         command_tag_split.next()
    1109              :     }
    1110            0 :     .and_then(|s| s.parse::<i64>().ok());
    1111            0 : 
    1112            0 :     info!(
    1113            0 :         rows = rows.len(),
    1114            0 :         ?ready,
    1115            0 :         command_tag,
    1116            0 :         acknowledgement = ?(query_acknowledged - query_start),
    1117            0 :         response = ?(query_resp_end - query_start),
    1118            0 :         "finished executing query"
    1119              :     );
    1120              : 
    1121            0 :     let columns_len = row_stream.columns().len();
    1122            0 :     let mut fields = Vec::with_capacity(columns_len);
    1123            0 :     let mut columns = Vec::with_capacity(columns_len);
    1124              : 
    1125            0 :     for c in row_stream.columns() {
    1126            0 :         fields.push(json!({
    1127            0 :             "name": c.name().to_owned(),
    1128            0 :             "dataTypeID": c.type_().oid(),
    1129            0 :             "tableID": c.table_oid(),
    1130            0 :             "columnID": c.column_id(),
    1131            0 :             "dataTypeSize": c.type_size(),
    1132            0 :             "dataTypeModifier": c.type_modifier(),
    1133            0 :             "format": "text",
    1134            0 :         }));
    1135            0 : 
    1136            0 :         match client.get_type(c.type_oid()).await {
    1137            0 :             Ok(t) => columns.push(t),
    1138            0 :             Err(err) => {
    1139            0 :                 tracing::warn!(?err, "unable to query type information");
    1140            0 :                 return Err(SqlOverHttpError::InternalPostgres(err));
    1141              :             }
    1142              :         }
    1143              :     }
    1144              : 
    1145            0 :     let array_mode = data.array_mode.unwrap_or(parsed_headers.default_array_mode);
    1146              : 
    1147              :     // convert rows to JSON
    1148            0 :     let rows = rows
    1149            0 :         .iter()
    1150            0 :         .map(|row| pg_text_row_to_json(row, &columns, parsed_headers.raw_output, array_mode))
    1151            0 :         .collect::<Result<Vec<_>, _>>()?;
    1152              : 
    1153              :     // Resulting JSON format is based on the format of node-postgres result.
    1154            0 :     let results = json!({
    1155            0 :         "command": command_tag_name.to_string(),
    1156            0 :         "rowCount": command_tag_count,
    1157            0 :         "rows": rows,
    1158            0 :         "fields": fields,
    1159            0 :         "rowAsArray": array_mode,
    1160            0 :     });
    1161            0 : 
    1162            0 :     Ok((ready, results))
    1163            0 : }
    1164              : 
    1165              : enum Client {
    1166              :     Remote(conn_pool_lib::Client<postgres_client::Client>),
    1167              :     Local(conn_pool_lib::Client<postgres_client::Client>),
    1168              : }
    1169              : 
    1170              : enum Discard<'a> {
    1171              :     Remote(conn_pool_lib::Discard<'a, postgres_client::Client>),
    1172              :     Local(conn_pool_lib::Discard<'a, postgres_client::Client>),
    1173              : }
    1174              : 
    1175              : impl Client {
    1176            0 :     fn metrics(&self, ctx: &RequestContext) -> Arc<MetricCounter> {
    1177            0 :         match self {
    1178            0 :             Client::Remote(client) => client.metrics(ctx),
    1179            0 :             Client::Local(local_client) => local_client.metrics(ctx),
    1180              :         }
    1181            0 :     }
    1182              : 
    1183            0 :     fn inner(&mut self) -> (&mut postgres_client::Client, Discard<'_>) {
    1184            0 :         match self {
    1185            0 :             Client::Remote(client) => {
    1186            0 :                 let (c, d) = client.inner();
    1187            0 :                 (c, Discard::Remote(d))
    1188              :             }
    1189            0 :             Client::Local(local_client) => {
    1190            0 :                 let (c, d) = local_client.inner();
    1191            0 :                 (c, Discard::Local(d))
    1192              :             }
    1193              :         }
    1194            0 :     }
    1195              : }
    1196              : 
    1197              : impl Discard<'_> {
    1198            0 :     fn check_idle(&mut self, status: ReadyForQueryStatus) {
    1199            0 :         match self {
    1200            0 :             Discard::Remote(discard) => discard.check_idle(status),
    1201            0 :             Discard::Local(discard) => discard.check_idle(status),
    1202              :         }
    1203            0 :     }
    1204            0 :     fn discard(&mut self) {
    1205            0 :         match self {
    1206            0 :             Discard::Remote(discard) => discard.discard(),
    1207            0 :             Discard::Local(discard) => discard.discard(),
    1208              :         }
    1209            0 :     }
    1210              : }
    1211              : 
    1212              : #[cfg(test)]
    1213              : #[expect(clippy::unwrap_used)]
    1214              : mod tests {
    1215              :     use super::*;
    1216              : 
    1217              :     #[test]
    1218            1 :     fn test_payload() {
    1219            1 :         let payload = "{\"query\":\"SELECT * FROM users WHERE name = ?\",\"params\":[\"test\"],\"arrayMode\":true}";
    1220            1 :         let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
    1221            1 : 
    1222            1 :         match deserialized_payload {
    1223              :             Payload::Single(QueryData {
    1224            1 :                 query,
    1225            1 :                 params,
    1226            1 :                 array_mode,
    1227            1 :             }) => {
    1228            1 :                 assert_eq!(query, "SELECT * FROM users WHERE name = ?");
    1229            1 :                 assert_eq!(params, vec![Some(String::from("test"))]);
    1230            1 :                 assert!(array_mode.unwrap());
    1231              :             }
    1232              :             Payload::Batch(_) => {
    1233            0 :                 panic!("deserialization failed: case with single query, one param, and array mode")
    1234              :             }
    1235              :         }
    1236              : 
    1237            1 :         let payload = "{\"queries\":[{\"query\":\"SELECT * FROM users0 WHERE name = ?\",\"params\":[\"test0\"], \"arrayMode\":false},{\"query\":\"SELECT * FROM users1 WHERE name = ?\",\"params\":[\"test1\"],\"arrayMode\":true}]}";
    1238            1 :         let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
    1239            1 : 
    1240            1 :         match deserialized_payload {
    1241            1 :             Payload::Batch(BatchQueryData { queries }) => {
    1242            1 :                 assert_eq!(queries.len(), 2);
    1243            2 :                 for (i, query) in queries.into_iter().enumerate() {
    1244            2 :                     assert_eq!(
    1245            2 :                         query.query,
    1246            2 :                         format!("SELECT * FROM users{i} WHERE name = ?")
    1247            2 :                     );
    1248            2 :                     assert_eq!(query.params, vec![Some(format!("test{i}"))]);
    1249            2 :                     assert_eq!(query.array_mode.unwrap(), i > 0);
    1250              :                 }
    1251              :             }
    1252            0 :             Payload::Single(_) => panic!("deserialization failed: case with multiple queries"),
    1253              :         }
    1254              : 
    1255            1 :         let payload = "{\"query\":\"SELECT 1\"}";
    1256            1 :         let deserialized_payload: Payload = serde_json::from_str(payload).unwrap();
    1257            1 : 
    1258            1 :         match deserialized_payload {
    1259              :             Payload::Single(QueryData {
    1260            1 :                 query,
    1261            1 :                 params,
    1262            1 :                 array_mode,
    1263            1 :             }) => {
    1264            1 :                 assert_eq!(query, "SELECT 1");
    1265            1 :                 assert_eq!(params, vec![]);
    1266            1 :                 assert!(array_mode.is_none());
    1267              :             }
    1268            0 :             Payload::Batch(_) => panic!("deserialization failed: case with only one query"),
    1269              :         }
    1270            1 :     }
    1271              : }
        

Generated by: LCOV version 2.1-beta