11use std:: borrow:: Cow ;
22use std:: collections:: BTreeSet ;
3+ use std:: ops:: Deref ;
34use std:: sync:: Arc ;
45
56use anyhow:: Result ;
@@ -75,11 +76,8 @@ fn create_request_context(req: &Request<Body>, app_ctx: &AppContext) -> RequestC
7576 let allowed = upstream. allowed_headers ;
7677 let req_headers = create_allowed_headers ( req. headers ( ) , & allowed) ;
7778
78- let allowed = app_ctx. blueprint . server . get_experimental_headers ( ) ;
79- let experimental_headers = create_allowed_headers ( req. headers ( ) , & allowed) ;
80- RequestContext :: from ( app_ctx)
81- . req_headers ( req_headers)
82- . experimental_headers ( experimental_headers)
79+ let _allowed = app_ctx. blueprint . server . get_experimental_headers ( ) ;
80+ RequestContext :: from ( app_ctx) . request_headers ( req_headers)
8381}
8482
8583fn update_cache_control_header (
@@ -95,30 +93,25 @@ fn update_cache_control_header(
9593 response
9694}
9795
98- fn update_experimental_headers (
99- response : & mut hyper:: Response < hyper:: Body > ,
100- app_ctx : & AppContext ,
101- req_ctx : Arc < RequestContext > ,
102- ) {
103- if !app_ctx. blueprint . server . experimental_headers . is_empty ( ) {
104- response
105- . headers_mut ( )
106- . extend ( req_ctx. experimental_headers . clone ( ) ) ;
107- }
108- }
109-
11096pub fn update_response_headers (
11197 resp : & mut hyper:: Response < hyper:: Body > ,
112- cookie_headers : Option < HeaderMap > ,
98+ req_ctx : & RequestContext ,
11399 app_ctx : & AppContext ,
114100) {
115101 if !app_ctx. blueprint . server . response_headers . is_empty ( ) {
102+ // Add static response headers
116103 resp. headers_mut ( )
117104 . extend ( app_ctx. blueprint . server . response_headers . clone ( ) ) ;
118105 }
119- if let Some ( cookie_headers) = cookie_headers {
120- resp. headers_mut ( ) . extend ( cookie_headers) ;
106+
107+ // Insert Cookie Headers
108+ if let Some ( ref cookie_headers) = req_ctx. cookie_headers {
109+ let cookie_headers = cookie_headers. lock ( ) . unwrap ( ) ;
110+ resp. headers_mut ( ) . extend ( cookie_headers. deref ( ) . clone ( ) ) ;
121111 }
112+
113+ // Insert Experimental Headers
114+ req_ctx. extend_x_headers ( resp. headers_mut ( ) ) ;
122115}
123116
124117#[ tracing:: instrument( skip_all, fields( otel. name = "graphQL" , otel. kind = ?SpanKind :: Server ) ) ]
@@ -134,15 +127,10 @@ pub async fn graphql_request<T: DeserializeOwned + GraphQLRequestLike>(
134127 match graphql_request {
135128 Ok ( request) => {
136129 let mut response = request. data ( req_ctx. clone ( ) ) . execute ( & app_ctx. schema ) . await ;
137- let cookie_headers = req_ctx . cookie_headers . clone ( ) ;
130+
138131 response = update_cache_control_header ( response, app_ctx, req_ctx. clone ( ) ) ;
139132 let mut resp = response. to_response ( ) ?;
140- update_response_headers (
141- & mut resp,
142- cookie_headers. map ( |v| v. lock ( ) . unwrap ( ) . clone ( ) ) ,
143- app_ctx,
144- ) ;
145- update_experimental_headers ( & mut resp, app_ctx, req_ctx) ;
133+ update_response_headers ( & mut resp, & req_ctx, app_ctx) ;
146134 Ok ( resp)
147135 }
148136 Err ( err) => {
@@ -250,15 +238,9 @@ async fn handle_rest_apis(
250238 . data ( req_ctx. clone ( ) )
251239 . execute ( & app_ctx. schema )
252240 . await ;
253- let cookie_headers = req_ctx. cookie_headers . clone ( ) ;
254241 response = update_cache_control_header ( response, app_ctx. as_ref ( ) , req_ctx. clone ( ) ) ;
255242 let mut resp = response. to_rest_response ( ) ?;
256- update_response_headers (
257- & mut resp,
258- cookie_headers. map ( |v| v. lock ( ) . unwrap ( ) . clone ( ) ) ,
259- app_ctx. as_ref ( ) ,
260- ) ;
261- update_experimental_headers ( & mut resp, app_ctx. as_ref ( ) , req_ctx) ;
243+ update_response_headers ( & mut resp, & req_ctx, & app_ctx) ;
262244 Ok ( resp)
263245 }
264246 . instrument ( span)
0 commit comments