Skip to main content

qualia_core_db/bundle/stream/http/
client.rs

1//! HTTP/1.1 GET of the same authenticated objects as native service.
2//! Range + identity encoding + strong If-Range. LIG reauthorizes origin change.
3//! Credentials never appear in URLs. TLS is not PQ publisher authority.
4
5use super::range::{
6    eq_ignore_ascii_case, parse_u64_ascii, starts_ignore_ascii_case, trim_ows, write_range_header,
7    InclusiveRange, RangeError,
8};
9use super::status::{
10    admit_headers, strong_etag, AdmittedTransfer, FallbackPolicy, HeaderView, StatusError,
11    Validator,
12};
13use crate::net::qdnf::authority::ContactState;
14use crate::net::qdnf::errors::QdnfError;
15use crate::net::qdnf::session::fetch::{
16    authorize_fetch, reveal_observed, FetchKind, FetchRequest,
17};
18use crate::net::qdnf::types::ObservedLocator;
19use std::io::{Read, Write};
20
21pub const HEADER_CAP: usize = 4096;
22const REQUEST_CAP: usize = 1024;
23const MAX_HEADER_FIELDS: usize = 16;
24
25/// Honest residual: Web PKI / TLS on this hop never substitutes for QPR proofs.
26pub const fn tls_is_pq_publisher_authority() -> bool {
27    false
28}
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub enum ClientError {
32    Range(RangeError),
33    Status(StatusError),
34    Lig(QdnfError),
35    Truncated,
36    Closed,
37    Capacity,
38    CredentialsInUrl,
39    OriginChange,
40}
41
42impl From<RangeError> for ClientError {
43    fn from(e: RangeError) -> Self {
44        Self::Range(e)
45    }
46}
47
48impl From<StatusError> for ClientError {
49    fn from(e: StatusError) -> Self {
50        Self::Status(e)
51    }
52}
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq)]
55pub struct LigGate {
56    pub private_consent: bool,
57    pub contact: ContactState,
58    pub group_member: bool,
59    pub paid: bool,
60}
61
62impl LigGate {
63    pub const fn consented_active() -> Self {
64        Self {
65            private_consent: true,
66            contact: ContactState::Active,
67            group_member: false,
68            paid: false,
69        }
70    }
71}
72
73#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74pub struct FetchBudget {
75    pub max_body: u64,
76    pub max_headers: usize,
77}
78
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
80pub struct RangeRequest<'a> {
81    pub host: &'a [u8],
82    pub path: &'a [u8],
83    pub range: InclusiveRange,
84    pub pinned_total: u64,
85    pub if_range: Option<&'a [u8]>,
86    pub expected: Validator<'a>,
87    pub fallback: FallbackPolicy,
88    pub budget: FetchBudget,
89    pub lig: LigGate,
90}
91
92#[derive(Clone, Copy, Debug, PartialEq, Eq)]
93pub enum Fetched {
94    Partial { length: usize },
95    Full { length: usize },
96}
97
98fn append(out: &mut [u8], n: &mut usize, bytes: &[u8]) -> Result<(), ClientError> {
99    let end = n.checked_add(bytes.len()).ok_or(ClientError::Capacity)?;
100    if end > out.len() {
101        return Err(ClientError::Capacity);
102    }
103    out[*n..end].copy_from_slice(bytes);
104    *n = end;
105    Ok(())
106}
107
108fn path_forbidden(path: &[u8]) -> Result<(), ClientError> {
109    if path.first() != Some(&b'/') {
110        return Err(ClientError::Status(StatusError::Syntax));
111    }
112    if path.iter().any(|&c| c == b'\r' || c == b'\n' || c == b'@' || c == 0) {
113        return Err(ClientError::CredentialsInUrl);
114    }
115    if find_sub(path, b"://").is_some() {
116        return Err(ClientError::CredentialsInUrl);
117    }
118    Ok(())
119}
120
121pub fn url_has_userinfo(url: &[u8]) -> bool {
122    let start = match find_sub(url, b"://") {
123        Some(i) => i + 3,
124        None => return false,
125    };
126    let mut i = start;
127    while i < url.len() && url[i] != b'/' && url[i] != b'?' {
128        if url[i] == b'@' {
129            return true;
130        }
131        i += 1;
132    }
133    false
134}
135
136fn find_sub(hay: &[u8], needle: &[u8]) -> Option<usize> {
137    hay.windows(needle.len()).position(|w| w == needle)
138}
139
140fn location_authority(url: &[u8]) -> Result<Option<&[u8]>, ClientError> {
141    if url_has_userinfo(url) {
142        return Err(ClientError::CredentialsInUrl);
143    }
144    let Some(scheme) = find_sub(url, b"://") else {
145        return Ok(None);
146    };
147    let rest = &url[scheme + 3..];
148    let end = rest
149        .iter()
150        .position(|&c| c == b'/' || c == b'?' || c == b'#')
151        .unwrap_or(rest.len());
152    Ok(Some(&rest[..end]))
153}
154
155fn origin_changed(pinned_host: &[u8], location: &[u8]) -> Result<bool, ClientError> {
156    match location_authority(location)? {
157        None => Ok(false),
158        Some(auth) => Ok(!eq_ignore_ascii_case(trim_ows(auth), trim_ows(pinned_host))),
159    }
160}
161
162fn reauthorize(
163    lig: LigGate,
164    observed: ObservedLocator,
165    out: &mut ObservedLocator,
166) -> Result<(), ClientError> {
167    let mut scratch = ObservedLocator::EMPTY;
168    let handle = authorize_fetch(
169        FetchRequest {
170            kind: FetchKind::Lig,
171            group_member: lig.group_member,
172            private_consent: lig.private_consent,
173            paid: lig.paid,
174        },
175        lig.contact,
176        &mut scratch,
177    )
178    .map_err(ClientError::Lig)?;
179    reveal_observed(handle, observed, out).map_err(ClientError::Lig)
180}
181
182fn write_request(req: &RangeRequest<'_>, out: &mut [u8]) -> Result<usize, ClientError> {
183    path_forbidden(req.path)?;
184    if req.host.iter().any(|&c| c == b'\r' || c == b'\n' || c == b'@') {
185        return Err(ClientError::CredentialsInUrl);
186    }
187    if let Some(tag) = req.if_range {
188        if strong_etag(tag).is_none() {
189            return Err(ClientError::Status(StatusError::ChangedValidator));
190        }
191    }
192    if !req.range.fits(req.pinned_total) {
193        return Err(ClientError::Status(StatusError::Unsatisfiable));
194    }
195    let mut n = 0;
196    append(out, &mut n, b"GET ")?;
197    append(out, &mut n, req.path)?;
198    append(out, &mut n, b" HTTP/1.1\r\nHost: ")?;
199    append(out, &mut n, req.host)?;
200    append(out, &mut n, b"\r\nRange: ")?;
201    let mut range_buf = [0u8; 64];
202    let rn = write_range_header(req.range, &mut range_buf)?;
203    append(out, &mut n, &range_buf[..rn])?;
204    append(out, &mut n, b"\r\nAccept-Encoding: identity\r\n")?;
205    if let Some(tag) = req.if_range {
206        append(out, &mut n, b"If-Range: ")?;
207        append(out, &mut n, tag)?;
208        append(out, &mut n, b"\r\n")?;
209    }
210    append(out, &mut n, b"Connection: close\r\n\r\n")?;
211    Ok(n)
212}
213
214fn header_end(buf: &[u8]) -> Option<usize> {
215    buf.windows(4).position(|w| w == b"\r\n\r\n")
216}
217
218fn read_headers<R: Read>(io: &mut R, buf: &mut [u8], cap: usize) -> Result<(usize, usize), ClientError> {
219    if cap == 0 || cap > buf.len() {
220        return Err(ClientError::Capacity);
221    }
222    let mut filled = 0;
223    while filled < cap {
224        let n = io
225            .read(&mut buf[filled..cap])
226            .map_err(|_| ClientError::Closed)?;
227        if n == 0 {
228            return Err(ClientError::Truncated);
229        }
230        filled += n;
231        if let Some(end) = header_end(&buf[..filled]) {
232            return Ok((end, filled));
233        }
234    }
235    Err(ClientError::Capacity)
236}
237
238#[derive(Clone, Copy)]
239struct Field<'a> {
240    name: &'a [u8],
241    value: &'a [u8],
242}
243
244fn parse_headers(block: &[u8]) -> Result<(u16, [Field<'_>; MAX_HEADER_FIELDS], usize), ClientError> {
245    let mut line_end = None;
246    for i in 0..block.len().saturating_sub(1) {
247        if block[i] == b'\r' && block[i + 1] == b'\n' {
248            line_end = Some(i);
249            break;
250        }
251    }
252    let line_end = line_end.ok_or(ClientError::Status(StatusError::Syntax))?;
253    let line = &block[..line_end];
254    if !starts_ignore_ascii_case(line, b"HTTP/1.1 ") || line.len() < 12 {
255        return Err(ClientError::Status(StatusError::Unsupported));
256    }
257    let code = parse_u64_ascii(&line[9..12]).map_err(ClientError::from)?;
258    if code > 999 {
259        return Err(ClientError::Status(StatusError::Syntax));
260    }
261    let mut fields = [Field {
262        name: b"",
263        value: b"",
264    }; MAX_HEADER_FIELDS];
265    let mut count = 0;
266    let mut rest = &block[line_end + 2..];
267    while !rest.is_empty() {
268        let mut eol = None;
269        for i in 0..rest.len().saturating_sub(1) {
270            if rest[i] == b'\r' && rest[i + 1] == b'\n' {
271                eol = Some(i);
272                break;
273            }
274        }
275        let eol = eol.ok_or(ClientError::Status(StatusError::Syntax))?;
276        if eol == 0 {
277            break;
278        }
279        let line = &rest[..eol];
280        let colon = line
281            .iter()
282            .position(|&c| c == b':')
283            .ok_or(ClientError::Status(StatusError::Syntax))?;
284        let name = trim_ows(&line[..colon]);
285        let value = trim_ows(&line[colon + 1..]);
286        if name.is_empty() {
287            return Err(ClientError::Status(StatusError::Syntax));
288        }
289        if is_critical_header(name) {
290            for field in &fields[..count] {
291                if eq_ignore_ascii_case(field.name, name) {
292                    return Err(ClientError::Status(StatusError::Syntax));
293                }
294            }
295        }
296        if count >= MAX_HEADER_FIELDS {
297            return Err(ClientError::Capacity);
298        }
299        fields[count] = Field { name, value };
300        count += 1;
301        rest = &rest[eol + 2..];
302    }
303    Ok((code as u16, fields, count))
304}
305
306fn is_critical_header(name: &[u8]) -> bool {
307    eq_ignore_ascii_case(name, b"Content-Range")
308        || eq_ignore_ascii_case(name, b"Content-Encoding")
309        || eq_ignore_ascii_case(name, b"Content-Length")
310        || eq_ignore_ascii_case(name, b"Content-Type")
311        || eq_ignore_ascii_case(name, b"ETag")
312        || eq_ignore_ascii_case(name, b"Last-Modified")
313        || eq_ignore_ascii_case(name, b"Location")
314        || eq_ignore_ascii_case(name, b"Transfer-Encoding")
315}
316
317fn find_field<'a>(fields: &[Field<'a>], count: usize, name: &[u8]) -> Option<&'a [u8]> {
318    fields[..count]
319        .iter()
320        .find(|f| eq_ignore_ascii_case(f.name, name))
321        .map(|f| f.value)
322}
323
324fn view<'a>(status: u16, fields: &[Field<'a>], count: usize) -> HeaderView<'a> {
325    HeaderView {
326        status,
327        content_range: find_field(fields, count, b"Content-Range"),
328        content_encoding: find_field(fields, count, b"Content-Encoding"),
329        content_length: find_field(fields, count, b"Content-Length"),
330        content_type: find_field(fields, count, b"Content-Type"),
331        etag: find_field(fields, count, b"ETag"),
332        last_modified: find_field(fields, count, b"Last-Modified"),
333        location: find_field(fields, count, b"Location"),
334        transfer_encoding: find_field(fields, count, b"Transfer-Encoding"),
335    }
336}
337
338fn locator_from_authority(auth: &[u8]) -> Result<ObservedLocator, ClientError> {
339    ObservedLocator::from_slice(auth).map_err(ClientError::Lig)
340}
341
342fn refuse_redirect(req: &RangeRequest<'_>, location: &[u8]) -> ClientError {
343    if url_has_userinfo(location) {
344        return ClientError::CredentialsInUrl;
345    }
346    let changed = match origin_changed(req.host, location) {
347        Ok(v) => v,
348        Err(e) => return e,
349    };
350    let auth = match location_authority(location) {
351        Ok(Some(a)) => a,
352        Ok(None) => req.host,
353        Err(e) => return e,
354    };
355    let src = match locator_from_authority(auth) {
356        Ok(v) => v,
357        Err(e) => return e,
358    };
359    let mut revealed = ObservedLocator::EMPTY;
360    if let Err(e) = reauthorize(req.lig, src, &mut revealed) {
361        return e;
362    }
363    if changed {
364        ClientError::OriginChange
365    } else {
366        ClientError::Status(StatusError::Redirect)
367    }
368}
369
370fn read_body<R: Read>(
371    io: &mut R,
372    already: &[u8],
373    length: usize,
374    out: &mut [u8],
375) -> Result<usize, ClientError> {
376    if length > out.len() {
377        return Err(ClientError::Capacity);
378    }
379    if already.len() > length {
380        return Err(ClientError::Status(StatusError::Length));
381    }
382    out[..already.len()].copy_from_slice(already);
383    let mut got = already.len();
384    while got < length {
385        let n = io.read(&mut out[got..length]).map_err(|_| ClientError::Closed)?;
386        if n == 0 {
387            return Err(ClientError::Truncated);
388        }
389        got += n;
390    }
391    Ok(length)
392}
393
394/// GET one range. Classify headers before copying any body. Does not follow 3xx.
395pub fn get_range<S: Read + Write>(
396    io: &mut S,
397    req: &RangeRequest<'_>,
398    out: &mut [u8],
399) -> Result<Fetched, ClientError> {
400    debug_assert!(!tls_is_pq_publisher_authority());
401    if req.budget.max_headers == 0 || req.budget.max_headers > HEADER_CAP {
402        return Err(ClientError::Capacity);
403    }
404    if req.range.length() > req.budget.max_body {
405        return Err(ClientError::Status(StatusError::OverBudget));
406    }
407    let mut request = [0u8; REQUEST_CAP];
408    let n = write_request(req, &mut request)?;
409    io.write_all(&request[..n]).map_err(|_| ClientError::Closed)?;
410    io.flush().map_err(|_| ClientError::Closed)?;
411
412    let mut header_buf = [0u8; HEADER_CAP];
413    let (end, filled) = read_headers(io, &mut header_buf, req.budget.max_headers)?;
414    let (status, fields, count) = parse_headers(&header_buf[..end + 4])?;
415    let headers = view(status, &fields, count);
416    let body_budget = req.budget.max_body.min(out.len() as u64);
417    let admitted = admit_headers(
418        headers,
419        req.range,
420        req.pinned_total,
421        req.expected,
422        req.fallback,
423        body_budget,
424    )?;
425    let extra = &header_buf[end + 4..filled];
426    match admitted {
427        AdmittedTransfer::Redirect { .. } => {
428            let location = headers
429                .location
430                .ok_or(ClientError::Status(StatusError::Syntax))?;
431            Err(refuse_redirect(req, location))
432        }
433        AdmittedTransfer::Unsatisfiable { .. } => Err(ClientError::Status(StatusError::Unsatisfiable)),
434        AdmittedTransfer::Partial { length } | AdmittedTransfer::FullObject { length } => {
435            let n = usize::try_from(length).map_err(|_| ClientError::Capacity)?;
436            read_body(io, extra, n, out)?;
437            if matches!(admitted, AdmittedTransfer::Partial { .. }) {
438                Ok(Fetched::Partial { length: n })
439            } else {
440                Ok(Fetched::Full { length: n })
441            }
442        }
443    }
444}