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
// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#![allow(dead_code)]
use aws_smithy_http::header::set_request_header_if_absent;
use aws_smithy_types::config_bag::{Storable, StoreReplace};
use http_1x::header::{HeaderName, CONTENT_LENGTH, CONTENT_TYPE};
/// Configuration for how default protocol headers are serialized
#[derive(Clone, Debug, Default)]
pub(crate) struct HeaderSerializationSettings {
omit_default_content_length: bool,
omit_default_content_type: bool,
}
impl HeaderSerializationSettings {
/// Creates new [`HeaderSerializationSettings`]
pub(crate) fn new() -> Self {
Default::default()
}
/// Omit the default `Content-Length` header during serialization
pub(crate) fn omit_default_content_length(self) -> Self {
Self {
omit_default_content_length: true,
..self
}
}
/// Omit the default `Content-Type` header during serialization
pub(crate) fn omit_default_content_type(self) -> Self {
Self {
omit_default_content_type: true,
..self
}
}
/// Returns whether the default `Content-Length` header should be omitted.
/// Used by schema-serde-generated request serializers, which insert the
/// header directly on `aws_smithy_runtime_api::http::Request` rather than
/// going through `set_default_header`.
pub(crate) fn should_omit_default_content_length(&self) -> bool {
self.omit_default_content_length
}
/// Returns whether the default `Content-Type` header should be omitted.
/// Used by schema-serde-generated request serializers, which insert the
/// header directly on `aws_smithy_runtime_api::http::Request` rather than
/// going through `set_default_header`.
pub(crate) fn should_omit_default_content_type(&self) -> bool {
self.omit_default_content_type
}
/// Returns true if the given default header name should be serialized
fn include_header(&self, header: &HeaderName) -> bool {
(!self.omit_default_content_length || header != CONTENT_LENGTH) && (!self.omit_default_content_type || header != CONTENT_TYPE)
}
/// Sets a default header on the given request builder if it should be serialized
pub(crate) fn set_default_header(
&self,
mut request: http_1x::request::Builder,
header_name: HeaderName,
value: &str,
) -> http_1x::request::Builder {
if self.include_header(&header_name) {
request = set_request_header_if_absent(request, header_name, value);
}
request
}
}
impl Storable for HeaderSerializationSettings {
type Storer = StoreReplace<Self>;
}
// Bridge to the schema-serde runtime guard. The schema-serde runtime in
// `aws-smithy-schema` reads omit flags from the config bag via the abstract
// `HeaderOmitSettings` trait so it doesn't have to depend on this concrete
// inlineable type. The presigning interceptor stores the same settings under
// both `HeaderSerializationSettings` (read by codegen-emitted streaming/
// payload paths) and `SharedHeaderOmitSettings` (read by the runtime's
// standard-body path); this trait impl is what lets the same concrete value
// satisfy both lookups.
impl aws_smithy_schema::header_omit_settings::HeaderOmitSettings for HeaderSerializationSettings {
fn should_omit_default_content_type(&self) -> bool {
self.omit_default_content_type
}
fn should_omit_default_content_length(&self) -> bool {
self.omit_default_content_length
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_include_header() {
let settings = HeaderSerializationSettings::default();
assert!(settings.include_header(&CONTENT_LENGTH));
assert!(settings.include_header(&CONTENT_TYPE));
let settings = HeaderSerializationSettings::default().omit_default_content_length();
assert!(!settings.include_header(&CONTENT_LENGTH));
assert!(settings.include_header(&CONTENT_TYPE));
let settings = HeaderSerializationSettings::default().omit_default_content_type();
assert!(settings.include_header(&CONTENT_LENGTH));
assert!(!settings.include_header(&CONTENT_TYPE));
let settings = HeaderSerializationSettings::default()
.omit_default_content_type()
.omit_default_content_length();
assert!(!settings.include_header(&CONTENT_LENGTH));
assert!(!settings.include_header(&CONTENT_TYPE));
}
}