wip-http 0.2.0

Framework-independent standard WIP over HTTP v1 binding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use std::error::Error;
use std::fmt::{self, Display, Formatter};

use http::header::{ACCEPT, CONTENT_ENCODING, CONTENT_TYPE};
use http::{HeaderMap, Method, Request, Response, StatusCode};
use wip_protocol::{
    CallOperationRequest, CallOperationResponse, FetchInterfaceRequest, FetchInterfaceResponse,
    INTERFACE_FORMAT_V1, InterfaceDescriptor, InterfaceTarget, ObserveRequest, ObserveResponse,
    ProtocolError, ProtocolInteraction, Target,
};

use crate::response::{self, DecodedResponse};
use crate::{
    BodyKind, ClientResponseError, CodecError, Endpoint, InvalidResponseKind, JSON_CONTENT_TYPE,
    Limits, Route, json,
};

fn request(
    endpoint: &Endpoint,
    route: Route,
    body: Vec<u8>,
) -> Result<Request<Vec<u8>>, http::Error> {
    Request::builder()
        .method(Method::POST)
        .uri(endpoint.route(route).as_str())
        .header(CONTENT_TYPE, JSON_CONTENT_TYPE)
        .header(ACCEPT, "application/json")
        .body(body)
}

fn response(status: StatusCode, body: Vec<u8>) -> Result<Response<Vec<u8>>, http::Error> {
    Response::builder()
        .status(status)
        .header(CONTENT_TYPE, JSON_CONTENT_TYPE)
        .body(body)
}

fn require_response_content_type<B>(response: &Response<B>) -> Result<(), ClientResponseError> {
    if !has_supported_content_coding(response.headers()) {
        return Err(response::unsupported_content_coding(response.status()));
    }
    let valid = response
        .headers()
        .get(CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .is_some_and(is_json_content_type);
    if valid {
        Ok(())
    } else if response.status().is_success() {
        Err(ClientResponseError::invalid(
            InvalidResponseKind::InvalidSuccessBody,
            "missing or non-UTF-8 application/json Content-Type",
        ))
    } else {
        Err(response::transport_failure(response.status()))
    }
}

fn is_utf8_charset(value: &str) -> bool {
    let value = value.trim();
    value.eq_ignore_ascii_case("utf-8")
        || value
            .strip_prefix('"')
            .and_then(|value| value.strip_suffix('"'))
            .is_some_and(|value| value.eq_ignore_ascii_case("utf-8"))
}

/// Returns whether a Content-Type value denotes UTF-8 `application/json`.
///
/// The charset parameter is optional because JSON exchanged by this binding is
/// always UTF-8; when present, its value must be `utf-8`. Other parameters are
/// rejected so intermediaries cannot silently select another representation.
#[must_use]
pub fn is_json_content_type(value: &str) -> bool {
    let mut parts = value.split(';');
    if !parts
        .next()
        .is_some_and(|media| media.trim().eq_ignore_ascii_case("application/json"))
    {
        return false;
    }
    let mut saw_charset = false;
    for parameter in parts {
        let Some((name, value)) = parameter.trim().split_once('=') else {
            return false;
        };
        if !name.trim().eq_ignore_ascii_case("charset") || !is_utf8_charset(value) || saw_charset {
            return false;
        }
        saw_charset = true;
    }
    true
}

fn accepts_json(headers: &HeaderMap) -> bool {
    let mut present = false;
    let mut best_match: Option<((u8, u8), f32)> = None;
    for value in headers.get_all(ACCEPT) {
        present = true;
        let Ok(value) = value.to_str() else {
            continue;
        };
        for range in value.split(',') {
            let mut parts = range.split(';');
            let media_range = parts.next().unwrap_or_default().trim();
            let mut quality = 1.0_f32;
            let mut saw_quality = false;
            let mut saw_charset = false;
            let mut media_parameter_count = 0;
            let mut valid = true;
            for parameter in parts {
                let parameter = parameter.trim();
                let (name, value) = parameter
                    .split_once('=')
                    .map_or((parameter, None), |(name, value)| {
                        (name.trim(), Some(value))
                    });
                if name.is_empty() {
                    valid = false;
                    break;
                }
                if saw_quality {
                    // Parameters after q are Accept extensions and do not
                    // constrain the selected representation.
                    continue;
                }
                if name.eq_ignore_ascii_case("q") {
                    let Some(value) = value else {
                        valid = false;
                        break;
                    };
                    saw_quality = true;
                    match value.trim().parse::<f32>() {
                        Ok(value) if (0.0..=1.0).contains(&value) => quality = value,
                        _ => {
                            valid = false;
                            break;
                        }
                    }
                } else {
                    let Some(value) = value else {
                        valid = false;
                        break;
                    };
                    if !name.eq_ignore_ascii_case("charset")
                        || !is_utf8_charset(value)
                        || saw_charset
                    {
                        valid = false;
                        break;
                    }
                    saw_charset = true;
                    media_parameter_count += 1;
                }
            }
            let media_specificity = if media_range.eq_ignore_ascii_case("application/json") {
                Some(2)
            } else if media_range.eq_ignore_ascii_case("application/*") {
                Some(1)
            } else if media_range == "*/*" {
                Some(0)
            } else {
                None
            };
            if valid && let Some(media_specificity) = media_specificity {
                let specificity = (media_specificity, media_parameter_count);
                match best_match {
                    Some((best_specificity, _)) if best_specificity > specificity => {}
                    Some((best_specificity, best_quality)) if best_specificity == specificity => {
                        best_match = Some((specificity, best_quality.max(quality)));
                    }
                    _ => best_match = Some((specificity, quality)),
                }
            }
        }
    }
    if present {
        best_match.is_some_and(|(_, quality)| quality > 0.0)
    } else {
        true
    }
}

/// A failure while validating inbound HTTP request metadata.
#[derive(Debug)]
pub enum RequestMetadataError {
    /// The request URI does not exactly identify the selected standard route.
    NotFound,
    /// A known standard route was requested with a method other than `POST`.
    MethodNotAllowed,
    /// The request body does not use UTF-8 `application/json` or identity coding.
    UnsupportedMediaType,
    /// The request's `Accept` metadata excludes `application/json`.
    NotAcceptable,
}

impl RequestMetadataError {
    /// Returns the HTTP status for this metadata failure.
    #[must_use]
    pub const fn status(&self) -> StatusCode {
        match self {
            Self::NotFound => StatusCode::NOT_FOUND,
            Self::MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED,
            Self::UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE,
            Self::NotAcceptable => StatusCode::NOT_ACCEPTABLE,
        }
    }

    /// Returns the value for an `Allow` response header, when required.
    #[must_use]
    pub const fn allow(&self) -> Option<&'static str> {
        match self {
            Self::MethodNotAllowed => Some("POST"),
            Self::NotFound | Self::UnsupportedMediaType | Self::NotAcceptable => None,
        }
    }
}

impl Display for RequestMetadataError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::NotFound => formatter.write_str("request URI is not a standard WIP route"),
            Self::MethodNotAllowed => formatter.write_str("standard WIP routes require POST"),
            Self::UnsupportedMediaType => {
                formatter.write_str("expected identity-coded UTF-8 application/json Content-Type")
            }
            Self::NotAcceptable => formatter.write_str("request does not accept application/json"),
        }
    }
}

impl Error for RequestMetadataError {}

pub(crate) fn has_supported_content_coding(headers: &HeaderMap) -> bool {
    headers.get_all(CONTENT_ENCODING).iter().all(|value| {
        value.to_str().is_ok_and(|value| {
            let mut codings = value.split(',').map(str::trim);
            let first = codings.next();
            first.is_some_and(|coding| coding.eq_ignore_ascii_case("identity"))
                && codings.all(|coding| coding.eq_ignore_ascii_case("identity"))
        })
    })
}

/// Validates mandatory HTTP metadata for an inbound WIP request.
///
/// Both absolute-form and origin-form request URIs are accepted, but the path
/// must exactly equal the selected endpoint route and a query is forbidden.
/// Unsupported content negotiation is reported as a Transport Binding metadata
/// failure rather than a Core `invalid_request`.
pub fn validate_http_request<B>(
    endpoint: &Endpoint,
    route: Route,
    value: &Request<B>,
) -> Result<(), RequestMetadataError> {
    let expected = endpoint.route(route);
    if value.uri().path() != expected.path() || value.uri().query().is_some() {
        return Err(RequestMetadataError::NotFound);
    }
    if value.method() != Method::POST {
        return Err(RequestMetadataError::MethodNotAllowed);
    }
    let valid_content_type = value
        .headers()
        .get(CONTENT_TYPE)
        .and_then(|value| value.to_str().ok())
        .is_some_and(is_json_content_type);
    if !valid_content_type || !has_supported_content_coding(value.headers()) {
        return Err(RequestMetadataError::UnsupportedMediaType);
    }
    if !accepts_json(value.headers()) {
        return Err(RequestMetadataError::NotAcceptable);
    }
    Ok(())
}

/// Encodes an `observe` HTTP request.
pub fn encode_observe_request(
    endpoint: &Endpoint,
    value: &ObserveRequest,
    limits: Limits,
) -> Result<Request<Vec<u8>>, EncodeHttpError> {
    value.validate().map_err(CodecError::from)?;
    let body = json::serialize(
        &json::observe_request_to_json(value),
        BodyKind::Request,
        limits,
    )?;
    Ok(request(endpoint, Route::Observe, body)?)
}

/// Decodes an `observe` request body.
pub fn decode_observe_request(body: &[u8], limits: Limits) -> Result<ObserveRequest, CodecError> {
    let value = json::parse(body, BodyKind::Request, limits)?;
    json::observe_request_from_json(&value)
}

/// Encodes a `fetch_interface` HTTP request.
///
/// Validates reference shape without requiring a target Object path.
pub fn encode_fetch_interface_request(
    endpoint: &Endpoint,
    value: &FetchInterfaceRequest,
    limits: Limits,
) -> Result<Request<Vec<u8>>, EncodeHttpError> {
    value.interface.validate().map_err(CodecError::from)?;
    let body = json::serialize(
        &json::fetch_interface_request_to_json(value),
        BodyKind::Request,
        limits,
    )?;
    Ok(request(endpoint, Route::FetchInterface, body)?)
}

/// Decodes a `fetch_interface` request body.
pub fn decode_fetch_interface_request(
    body: &[u8],
    limits: Limits,
) -> Result<FetchInterfaceRequest, CodecError> {
    let value = json::parse(body, BodyKind::Request, limits)?;
    json::fetch_interface_request_from_json(&value)
}

/// Descriptor-independent fields needed to resolve a `call_operation` request.
///
/// A Host decodes this metadata first, fixes the current interface descriptor,
/// then passes the same body and descriptor to [`decode_call_operation_request`].
/// This preserves canonical path/interface/operation lookup and error precedence
/// without treating unvalidated argument JSON as a logical [`wip_protocol::Value`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CallOperationMetadata {
    /// Path and optional object-state precondition.
    pub target: Target,
    /// Interface reference and optional interface precondition.
    pub interface: InterfaceTarget,
    /// Operation name used to select a declaration.
    pub operation: String,
}

impl CallOperationMetadata {
    /// Decodes the descriptor-bound request and verifies that routing metadata
    /// still matches the values used to resolve `descriptor`.
    pub fn decode_request(
        &self,
        body: &[u8],
        descriptor: &InterfaceDescriptor,
        limits: Limits,
    ) -> Result<CallOperationRequest, CodecError> {
        let request = decode_call_operation_request(body, descriptor, limits)?;
        if request.target != self.target
            || request.interface != self.interface
            || request.operation != self.operation
        {
            return Err(CodecError::InvalidField {
                field: "request".into(),
                reason: "call metadata changed after descriptor resolution".into(),
            });
        }
        Ok(request)
    }
}

/// Decodes descriptor-independent `call_operation` routing metadata.
///
/// Reference shape is validated, but scope ancestry and `scope_ref` identity
/// are left to Host resolution so `NotFound`, validator failures, and
/// `InterfaceMismatch` retain their protocol precedence.
pub fn decode_call_operation_metadata(
    body: &[u8],
    limits: Limits,
) -> Result<CallOperationMetadata, CodecError> {
    let value = json::parse(body, BodyKind::Request, limits)?;
    let (target, interface, operation) = json::call_request_metadata_from_json(&value)?;
    Ok(CallOperationMetadata {
        target,
        interface,
        operation,
    })
}

/// Encodes a descriptor-bound `call_operation` HTTP request.
pub fn encode_call_operation_request(
    endpoint: &Endpoint,
    value: &CallOperationRequest,
    descriptor: &InterfaceDescriptor,
    limits: Limits,
) -> Result<Request<Vec<u8>>, EncodeHttpError> {
    let value = json::call_request_to_json(value, descriptor)?;
    let body = json::serialize(&value, BodyKind::Request, limits)?;
    Ok(request(endpoint, Route::CallOperation, body)?)
}

/// Decodes a descriptor-bound `call_operation` request body.
pub fn decode_call_operation_request(
    body: &[u8],
    descriptor: &InterfaceDescriptor,
    limits: Limits,
) -> Result<CallOperationRequest, CodecError> {
    let value = json::parse(body, BodyKind::Request, limits)?;
    json::call_request_from_json(&value, descriptor)
}

/// Encodes a successful `observe` response as a direct body with HTTP 200.
pub fn encode_observe_response(
    request_value: &ObserveRequest,
    value: &ObserveResponse,
    limits: Limits,
) -> Result<Response<Vec<u8>>, EncodeHttpError> {
    request_value
        .validate_response(value)
        .map_err(CodecError::from)?;
    let body = json::serialize(
        &json::observe_response_to_json(value),
        BodyKind::Response,
        limits,
    )?;
    Ok(response(StatusCode::OK, body)?)
}

/// Decodes and classifies an `observe` client response.
pub fn decode_observe_response<B: AsRef<[u8]>>(
    request_value: &ObserveRequest,
    value: &Response<B>,
    limits: Limits,
) -> Result<DecodedResponse<ObserveResponse>, ClientResponseError> {
    require_response_content_type(value)?;
    response::decode(
        value.status(),
        value.body().as_ref(),
        ProtocolInteraction::Observe,
        limits,
        |json| {
            let observation = json::observe_response_from_json(json)?;
            request_value.validate_response(&observation)?;
            Ok(observation)
        },
    )
}

/// Encodes a successful `fetch_interface` response as a direct body with HTTP 200.
pub fn encode_fetch_interface_response(
    request_value: &FetchInterfaceRequest,
    value: &FetchInterfaceResponse,
    limits: Limits,
) -> Result<Response<Vec<u8>>, EncodeHttpError> {
    value
        .validate_for(request_value)
        .map_err(CodecError::from)?;
    let body = json::serialize(
        &json::fetch_interface_response_to_json(value),
        BodyKind::Response,
        limits,
    )?;
    Ok(response(StatusCode::OK, body)?)
}

/// Decodes and classifies a `fetch_interface` client response.
pub fn decode_fetch_interface_response<B: AsRef<[u8]>>(
    request_value: &FetchInterfaceRequest,
    value: &Response<B>,
    limits: Limits,
) -> Result<DecodedResponse<FetchInterfaceResponse>, ClientResponseError> {
    require_response_content_type(value)?;
    let decoded = response::decode(
        value.status(),
        value.body().as_ref(),
        ProtocolInteraction::FetchInterface,
        limits,
        |json| {
            let interface = json::fetch_interface_response_from_json(json)?;
            interface.validate_for(request_value)?;
            Ok(interface)
        },
    )?;
    if let DecodedResponse::Success(success) = &decoded
        && success.descriptor.format != INTERFACE_FORMAT_V1
    {
        return Err(ClientResponseError::UnsupportedDescriptorFormat {
            format: success.descriptor.format.clone(),
        });
    }
    Ok(decoded)
}

/// Encodes a successful `call_operation` response as a direct body with HTTP 200.
pub fn encode_call_operation_response(
    request_value: &CallOperationRequest,
    descriptor: &InterfaceDescriptor,
    value: &CallOperationResponse,
    limits: Limits,
) -> Result<Response<Vec<u8>>, EncodeHttpError> {
    let json = json::call_response_to_json(value, descriptor, &request_value.operation)?;
    let body = json::serialize(&json, BodyKind::Response, limits)?;
    Ok(response(StatusCode::OK, body)?)
}

/// Decodes and classifies a descriptor-bound `call_operation` client response.
pub fn decode_call_operation_response<B: AsRef<[u8]>>(
    request_value: &CallOperationRequest,
    descriptor: &InterfaceDescriptor,
    value: &Response<B>,
    limits: Limits,
) -> Result<DecodedResponse<CallOperationResponse>, ClientResponseError> {
    require_response_content_type(value)?;
    response::decode(
        value.status(),
        value.body().as_ref(),
        ProtocolInteraction::CallOperation,
        limits,
        |json| json::call_response_from_json(json, descriptor, &request_value.operation),
    )
}

fn validate_protocol_error(
    interaction: ProtocolInteraction,
    error: &ProtocolError,
) -> Result<(), CodecError> {
    if !error.code.is_allowed_for(interaction) {
        return Err(CodecError::InvalidField {
            field: "error.code".into(),
            reason: format!(
                "code `{}` is not allowed for {interaction:?}",
                response::error_code_name(error.code)
            ),
        });
    }
    Ok(())
}

fn encode_protocol_error_with_status(
    interaction: ProtocolInteraction,
    error: &ProtocolError,
    status: StatusCode,
    limits: Limits,
) -> Result<Response<Vec<u8>>, EncodeHttpError> {
    validate_protocol_error(interaction, error)?;
    let body = json::serialize(&response::error_to_json(error), BodyKind::Response, limits)?;
    Ok(response(status, body)?)
}

/// Encodes a canonical Host error envelope using its standard HTTP status.
///
/// The code must be allowed for `interaction`. This API accepts only
/// [`ProtocolError`], so client-local `InvalidResponse` and
/// `UnsupportedDescriptorFormat` classifications cannot be emitted as Host
/// error envelopes. Logical `resource_limit_exceeded` failures use HTTP 422;
/// representation and deployment limits are not protocol errors.
pub fn encode_protocol_error_response(
    interaction: ProtocolInteraction,
    error: &ProtocolError,
    limits: Limits,
) -> Result<Response<Vec<u8>>, EncodeHttpError> {
    encode_protocol_error_with_status(
        interaction,
        error,
        response::status_for_error(error.code),
        limits,
    )
}

/// Failure while building an outbound framework-neutral HTTP message.
#[derive(Debug)]
pub enum EncodeHttpError {
    /// JSON or protocol encoding failed.
    Codec(CodecError),
    /// The framework-neutral HTTP message builder rejected the URI or headers.
    Http(http::Error),
}

impl std::fmt::Display for EncodeHttpError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Codec(error) => error.fmt(formatter),
            Self::Http(error) => error.fmt(formatter),
        }
    }
}

impl std::error::Error for EncodeHttpError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Codec(error) => Some(error),
            Self::Http(error) => Some(error),
        }
    }
}

impl From<CodecError> for EncodeHttpError {
    fn from(value: CodecError) -> Self {
        Self::Codec(value)
    }
}

impl From<http::Error> for EncodeHttpError {
    fn from(value: http::Error) -> Self {
        Self::Http(value)
    }
}