@@ -111,8 +111,6 @@ struct SizeDimensions {
111111 height : i64 ,
112112}
113113
114- impl SizeDimensions { }
115-
116114struct ValidatedSize < F > ( SizeDimensions , PhantomData < F > ) ;
117115
118116async fn extract_size < F > ( ctx : & RequestContext ) -> Result < ValidatedSize < F > , EdgeError >
@@ -180,6 +178,24 @@ fn build_response(status: StatusCode, body: Body) -> Response {
180178 . expect ( "static response builder should not fail" )
181179}
182180
181+ fn json_response ( body : Body ) -> Response {
182+ let mut response = build_response ( StatusCode :: OK , body) ;
183+ response. headers_mut ( ) . insert (
184+ header:: CONTENT_TYPE ,
185+ HeaderValue :: from_static ( "application/json" ) ,
186+ ) ;
187+ response
188+ }
189+
190+ fn html_response ( body : Body ) -> Response {
191+ let mut response = build_response ( StatusCode :: OK , body) ;
192+ response. headers_mut ( ) . insert (
193+ header:: CONTENT_TYPE ,
194+ HeaderValue :: from_static ( "text/html; charset=utf-8" ) ,
195+ ) ;
196+ response
197+ }
198+
183199fn apply_cors ( headers : & mut HeaderMap ) {
184200 headers. insert ( "Access-Control-Allow-Origin" , HeaderValue :: from_static ( "*" ) ) ;
185201 headers. insert (
@@ -225,12 +241,7 @@ fn options_response() -> Response {
225241#[ action]
226242pub async fn handle_root ( ForwardedHost ( host) : ForwardedHost ) -> Response {
227243 let html = info_html ( & host) ;
228- let mut response = build_response ( StatusCode :: OK , Body :: text ( html) ) ;
229- response. headers_mut ( ) . insert (
230- header:: CONTENT_TYPE ,
231- HeaderValue :: from_static ( "text/html; charset=utf-8" ) ,
232- ) ;
233- response
244+ html_response ( Body :: text ( html) )
234245}
235246
236247#[ action]
@@ -266,12 +277,7 @@ pub async fn handle_openrtb_auction(
266277 log:: error!( "Failed to serialize OpenRTB response: {}" , e) ;
267278 EdgeError :: internal ( e)
268279 } ) ?;
269- let mut response = build_response ( StatusCode :: OK , body) ;
270- response. headers_mut ( ) . insert (
271- header:: CONTENT_TYPE ,
272- HeaderValue :: from_static ( "application/json" ) ,
273- ) ;
274- Ok ( response)
280+ Ok ( json_response ( body) )
275281}
276282
277283#[ action]
@@ -305,12 +311,7 @@ pub async fn handle_static_creatives(
305311 let pixel_html = query. pixel_html . unwrap_or ( true ) ;
306312 let pixel_js = query. pixel_js . unwrap_or ( false ) ;
307313 let html = creative_html ( w, h, pixel_html, pixel_js, & host) ;
308- let mut response = build_response ( StatusCode :: OK , Body :: from ( html) ) ;
309- response. headers_mut ( ) . insert (
310- header:: CONTENT_TYPE ,
311- HeaderValue :: from_static ( "text/html; charset=utf-8" ) ,
312- ) ;
313- response
314+ html_response ( Body :: from ( html) )
314315}
315316
316317fn parse_cookie < ' a > ( cookie_header : & ' a str , name : & str ) -> Option < & ' a str > {
@@ -330,13 +331,11 @@ const PIXEL_GIF: &[u8] = include_bytes!("../static/pixel.gif");
330331#[ action]
331332pub async fn handle_pixel (
332333 Headers ( headers) : Headers ,
333- ValidatedQuery ( params ) : ValidatedQuery < PixelQueryParams > ,
334+ ValidatedQuery ( _params ) : ValidatedQuery < PixelQueryParams > ,
334335) -> Response {
335336 let cookie_name = "mtkid" ;
336337 let mut set_cookie = None ;
337338
338- let PixelQueryParams { pid : _ } = params;
339-
340339 let existing = headers
341340 . get ( header:: COOKIE )
342341 . and_then ( |c| c. to_str ( ) . ok ( ) )
@@ -353,23 +352,21 @@ pub async fn handle_pixel(
353352 }
354353
355354 let mut response = build_response ( StatusCode :: OK , Body :: from ( PIXEL_GIF ) ) ;
356- {
357- let headers = response. headers_mut ( ) ;
358- headers. insert ( header:: CONTENT_TYPE , HeaderValue :: from_static ( "image/gif" ) ) ;
359- headers. insert (
360- header:: CACHE_CONTROL ,
361- HeaderValue :: from_static ( "no-store, no-cache, must-revalidate, max-age=0" ) ,
362- ) ;
363- headers. insert ( "Pragma" , HeaderValue :: from_static ( "no-cache" ) ) ;
364- headers. insert (
365- header:: CONTENT_LENGTH ,
366- HeaderValue :: from_str ( & PIXEL_GIF . len ( ) . to_string ( ) ) . expect ( "length" ) ,
367- ) ;
368- }
355+ let resp_headers = response. headers_mut ( ) ;
356+ resp_headers. insert ( header:: CONTENT_TYPE , HeaderValue :: from_static ( "image/gif" ) ) ;
357+ resp_headers. insert (
358+ header:: CACHE_CONTROL ,
359+ HeaderValue :: from_static ( "no-store, no-cache, must-revalidate, max-age=0" ) ,
360+ ) ;
361+ resp_headers. insert ( "Pragma" , HeaderValue :: from_static ( "no-cache" ) ) ;
362+ resp_headers. insert (
363+ header:: CONTENT_LENGTH ,
364+ HeaderValue :: from_str ( & PIXEL_GIF . len ( ) . to_string ( ) ) . expect ( "length" ) ,
365+ ) ;
369366
370367 if let Some ( cookie) = set_cookie {
371368 if let Ok ( value) = HeaderValue :: from_str ( & cookie) {
372- response . headers_mut ( ) . append ( "Set-Cookie" , value) ;
369+ resp_headers . append ( "Set-Cookie" , value) ;
373370 }
374371 }
375372
@@ -400,12 +397,7 @@ pub async fn handle_aps_bid(
400397 log:: error!( "Failed to serialize APS response: {}" , e) ;
401398 EdgeError :: internal ( e)
402399 } ) ?;
403- let mut response = build_response ( StatusCode :: OK , body) ;
404- response. headers_mut ( ) . insert (
405- header:: CONTENT_TYPE ,
406- HeaderValue :: from_static ( "application/json" ) ,
407- ) ;
408- Ok ( response)
400+ Ok ( json_response ( body) )
409401}
410402
411403#[ action]
@@ -442,12 +434,7 @@ pub async fn handle_adserver_mediate(
442434 log:: error!( "Failed to serialize mediation response: {}" , e) ;
443435 EdgeError :: internal ( e)
444436 } ) ?;
445- let mut response = build_response ( StatusCode :: OK , body) ;
446- response. headers_mut ( ) . insert (
447- header:: CONTENT_TYPE ,
448- HeaderValue :: from_static ( "application/json" ) ,
449- ) ;
450- Ok ( response)
437+ Ok ( json_response ( body) )
451438}
452439
453440#[ action]
@@ -473,12 +460,7 @@ pub async fn handle_click(ValidatedQuery(params): ValidatedQuery<ClickQueryParam
473460 "EXTRA" : extra_json,
474461 } ) ,
475462 ) ;
476- let mut response = build_response ( StatusCode :: OK , Body :: from ( html) ) ;
477- response. headers_mut ( ) . insert (
478- header:: CONTENT_TYPE ,
479- HeaderValue :: from_static ( "text/html; charset=utf-8" ) ,
480- ) ;
481- response
463+ html_response ( Body :: from ( html) )
482464}
483465
484466/// Returns all standard ad sizes as JSON array.
@@ -509,12 +491,7 @@ pub async fn handle_sizes() -> Response {
509491 . collect ( ) ;
510492
511493 let body = serde_json:: json!( { "sizes" : sizes } ) ;
512- let mut response = build_response ( StatusCode :: OK , Body :: from ( body. to_string ( ) ) ) ;
513- response. headers_mut ( ) . insert (
514- header:: CONTENT_TYPE ,
515- HeaderValue :: from_static ( "application/json" ) ,
516- ) ;
517- response
494+ json_response ( Body :: from ( body. to_string ( ) ) )
518495}
519496
520497#[ cfg( test) ]
0 commit comments