reserve the correct amount of headers

This commit is contained in:
Jade Ellis 2024-12-17 22:04:17 +00:00
parent a204cb56db
commit ad417daad8
No known key found for this signature in database
GPG Key ID: 8705A2A3EBF77BD2

View File

@ -40,11 +40,14 @@ impl Request {
quote! { "" } quote! { "" }
}; };
let mut additional_headers: usize = 0;
// If there are no body fields, the request body will be empty (not `{}`), so the // If there are no body fields, the request body will be empty (not `{}`), so the
// `application/json` content-type would be wrong. It may also cause problems with CORS // `application/json` content-type would be wrong. It may also cause problems with CORS
// policies that don't allow the `Content-Type` header (for things such as `.well-known` // policies that don't allow the `Content-Type` header (for things such as `.well-known`
// that are commonly handled by something else than a homeserver). // that are commonly handled by something else than a homeserver).
let mut header_kvs = if self.raw_body_field().is_some() || self.has_body_fields() { let mut header_kvs = if self.raw_body_field().is_some() || self.has_body_fields() {
additional_headers += 1;
quote! { quote! {
req_headers.insert( req_headers.insert(
#http::header::CONTENT_TYPE, #http::header::CONTENT_TYPE,
@ -80,6 +83,7 @@ impl Request {
} }
})); }));
additional_headers += 1;
header_kvs.extend(quote! { header_kvs.extend(quote! {
req_headers.extend(METADATA.authorization_header(access_token)?); req_headers.extend(METADATA.authorization_header(access_token)?);
}); });
@ -99,7 +103,8 @@ impl Request {
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl(); let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
let reserve_headers = 1 + self.header_fields().count(); let reserve_headers = self.header_fields().count() + additional_headers;
quote! { quote! {
#[automatically_derived] #[automatically_derived]
#[cfg(feature = "client")] #[cfg(feature = "client")]