Skip to main content

Module expect

Module expect 

Source
Expand description

HTTP Expect: 100-continue handling.

This module provides support for the HTTP Expect: 100-continue mechanism as defined in RFC 7231 Section 5.1.1.

§Overview

When a client sends a request with Expect: 100-continue, it is indicating that it will wait for a 100 Continue interim response before sending the request body. This allows the server to:

  • Validate request headers before receiving potentially large body data
  • Reject unauthorized requests without reading the body
  • Check Content-Type and Content-Length before accepting uploads

§Example

use fastapi_http::expect::{ExpectHandler, ExpectValidation, CONTINUE_RESPONSE};

// Check for Expect: 100-continue
if let Some(validation) = ExpectHandler::check_expect(&request) {
    // Run pre-body validation (auth, content-type, etc.)
    if !validate_auth(&request) {
        return validation.reject_unauthorized("Invalid credentials");
    }
    if !validate_content_type(&request) {
        return validation.reject_unsupported_media_type("Expected application/json");
    }

    // Validation passed - send 100 Continue
    stream.write_all(CONTINUE_RESPONSE).await?;
}

// Now proceed to read body and handle request

§Error Responses

When pre-body validation fails, the server should NOT send 100 Continue. Instead, it should send an appropriate error response:

  • 417 Expectation Failed - The expectation cannot be met
  • 401 Unauthorized - Authentication required
  • 403 Forbidden - Authorization failed
  • 413 Payload Too Large - Content-Length exceeds limits
  • 415 Unsupported Media Type - Content-Type not accepted

§Wire Format

The 100 Continue response is a simple interim response:

HTTP/1.1 100 Continue\r\n
\r\n

After sending this, the server proceeds to read the request body.

Structs§

ExpectHandler
Handler for HTTP Expect header processing.
FnValidator
A simple function-based pre-body validator.
PreBodyValidators
A collection of pre-body validators.

Enums§

ExpectResult
Result of checking the Expect header.

Constants§

CONTINUE_RESPONSE
The raw bytes for an HTTP/1.1 100 Continue response.
EXPECT_100_CONTINUE
The Expect header value that triggers 100-continue handling.

Traits§

PreBodyValidator
Trait for pre-body validation hooks.