Protocol parsing and checksums
Protocol parsing and checksums turn an asynchronous HTTP exchange into a trusted service result. The parsing path maps each service protocol to a botocore-compatible response parser, while the checksum path protects request bodies and validates response bodies without blocking async streams.
Protocol response parsing
The response parser is the boundary between wire payloads and the result shape exposed by a client operation. aiobotocore/parsers.py selects a parser for the service protocol, preserves common botocore parsing rules, and adds async waits where payload work can consume an async stream.
Supported service protocols use these parser families:
| Protocol family | Result handling |
|---|---|
| EC2 Query and Query | XML-shaped response values and service errors |
| JSON and REST JSON | JSON values, metadata, and event-stream starts |
| REST XML | XML values and service errors |
| Smithy RPC V2 CBOR | CBOR values and event-stream starts |
The parser adds HTTP status and normalized headers to ResponseMetadata for ordinary dictionary results. Modeled errors remain separate from this metadata decoration. Event-stream results instead expose an async event stream so later events are parsed as the caller consumes them.
References: aiobotocore/parsers.py• L29-71 aiobotocore/parsers.py• L79-207
Response processing flow
The endpoint prepares the response body according to the operation shape before checksum handling and parsing. This order lets checksum validation see the original body while still allowing the parser to receive either bytes or an async stream.
The response processing flow is:
flowchart LR
A[HTTP response] --> B[Response dictionary]
B --> C[Response checksum handling]
C --> D[Protocol parser]
D --> E[Normalized result]
D --> F[Async event stream]
An error response is read into memory so the parser can identify generic and modeled error data. A streaming output stays lazy. Its body is read only when the caller consumes it. The parser also performs a second modeled-error parse when the service error code maps to a known shape.
References: aiobotocore/endpoint.py• L30-66 aiobotocore/endpoint.py• L183-251 aiobotocore/response.py• L297-317
Event-stream parsing
An event stream is a response whose body carries a sequence of framed service events. The parser consumes the initial response event before returning the operation result. It then gives the caller an async event stream for the remaining events.
Each event is decoded with the same protocol-aware parser used for ordinary responses. A successful event becomes a parsed value. A non-success event becomes an EventStreamError. If the first event is not an initial-response event, parsing fails because the operation has no valid initial result.
References: aiobotocore/parsers.py• L73-76 aiobotocore/parsers.py• L124-147 aiobotocore/parsers.py• L181-191 aiobotocore/eventstream.py• L9-49
Request checksum behavior
Request checksums protect payloads before the transport sends them. The checksum context selects the algorithm and its wire location. Header checksums can be calculated before sending. Trailer checksums must be calculated while the body is read, so the async path wraps the body in AWS chunked encoding.
The request path applies these choices:
- No request algorithm leaves the request unchanged.
- A conditional MD5 follows the service trait for required HTTP checksums.
- A header checksum is placed in the named request header.
- A trailer checksum changes the body to a chunked async stream and emits the final digest in the trailer.
- A caller-provided digest is preserved instead of being recalculated.
Trailer mode also removes Content-Length when chunked transfer is used. If the decoded length is known, it is retained in X-Amz-Decoded-Content-Length. Existing content encoding is preserved and extended with aws-chunked.
References: aiobotocore/httpchecksum.py• L213-286 tests/…/test_httpchecksum.py• L109-277
Asynchronous request body checksums
The async chunk wrapper reads the source body in pieces and updates the selected checksum as each piece moves onto the wire. At end of stream it emits the zero-length chunk and the final checksum trailer. This keeps checksum calculation compatible with non-seekable bodies and avoids reading the full request before transmission.
The wrapper supports both whole-body reads and async iteration. A retry can reset it to the start so the next attempt sends the complete body and computes the same final digest.
References: aiobotocore/httpchecksum.py• L19-70 tests/…/test_httpchecksum.py• L434-550
Response checksum behavior
Response checksums confirm that the service returned the body expected by the client. The response checksum context lists acceptable algorithms. The handler selects the first matching checksum header that can be checked and records the algorithm used in the response context.
The body form determines when validation completes:
| Response form | Validation point | Effect on caller |
|---|---|---|
| Buffered body | Before parsing returns | A mismatch raises immediately |
| Streaming body | At end of stream | Reads remain async and a mismatch raises when all data is consumed |
Responses with no usable checksum header pass through without validation. Multipart-derived checksum values with a suffix are also skipped because they do not represent a directly decodable body checksum. A streaming checksum body exposes its active checksum and skips the final comparison when no expected digest is available.
References: aiobotocore/httpchecksum.py• L73-151 aiobotocore/httpchecksum.py• L154-210 tests/…/test_httpchecksum.py• L279-431
Checksum validation failures
The checksum body tracks every chunk returned by read, readinto, or async iteration. It compares the calculated digest with the service header only when the body reaches end of stream. A mismatch raises FlexibleChecksumError, which stops the result from being accepted as valid.
This delayed check matters for streaming operations: the endpoint can return a usable async body without blocking on the full payload, but the caller must consume the body to trigger its final integrity check.
References: aiobotocore/httpchecksum.py• L73-115 tests/…/test_httpchecksum.py• L552-648
Protocol compatibility checks
Protocol compliance checks compare modeled service responses with the normalized values produced by the async parser families. They cover the supported wire formats and make async-specific adjustments before comparison so the check measures protocol behavior rather than the shape of an async stream object.
The checks confirm that:
- Response bodies are supplied in the byte form expected at the parser boundary.
- Event streams are consumed with async iteration before expected values are compared.
- Response metadata and other transport-only fields do not affect protocol results.
- Error bodies and special value forms are compared using the service protocol’s expected meaning.
References: tests/…/test_protocols.py• L14-24 tests/…/test_protocols.py• L146-277
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the protocol-name map aligned with the service model protocols and the compliance test runner.
- Preserve the response-body order: checksum wrapping must happen before protocol parsing.
- Keep event-stream initial-response handling aligned with the event parser’s async iterator contract.
- Keep trailer checksum headers, chunk framing, and decoded-length headers consistent with the selected request algorithm.
- Keep buffered and streaming response checksum paths aligned so they report the same digest failure.
- Keep the streaming checksum body API aligned with the response body consumers when checksum state or missing expected digests change.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A protocol name has no parser mapping | The operation cannot turn its response into a result | Add the protocol to the parser factory and protocol compliance map together |
| A streamed body is not fully consumed | The final checksum comparison does not run | Consume the body through async reads or iteration when integrity matters |
| A response checksum does not match the body | FlexibleChecksumError stops the result from being accepted | Treat the response as invalid and handle the checksum error |
| A trailer checksum is sent with a fixed content length | The HTTP request has conflicting transfer framing | Use chunked transfer and retain decoded length separately when known |
| An event stream has no initial-response event | Parsing raises ResponseParserError before the operation result is returned | Ensure the service sends the required initial event |
Key files
| File | Why to read it |
|---|---|
| aiobotocore/endpoint.py | Shows where response conversion, checksum handling, parsing, and modeled errors meet in request execution |
| aiobotocore/parsers.py | Defines the async parser factory, protocol families, metadata behavior, and initial event handling |
| aiobotocore/httpchecksum.py | Defines async request checksum framing and buffered or streaming response validation |
| aiobotocore/eventstream.py | Shows how parsed events are exposed through async iteration and how event errors surface |
| aiobotocore/response.py | Defines async response bodies and the boundary that hands response dictionaries to parsers |
| tests/…/test_protocols.py | Shows the protocol compatibility contract across response formats and event streams |
| tests/…/test_httpchecksum.py | Shows the request framing and response validation guarantees for async bodies |