From 00bd4580a4675ae1cc096557149b2282c560af18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Thu, 23 May 2024 14:08:58 +0200 Subject: [PATCH 1/2] fix: cache /api/v0/preferences and attribute_groups --- html/js/product-preferences.js | 2 ++ lib/ProductOpener/Display.pm | 10 +++++--- lib/ProductOpener/HTTP.pm | 46 +++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/html/js/product-preferences.js b/html/js/product-preferences.js index f704d50891bdd..984e4e5275d6f 100644 --- a/html/js/product-preferences.js +++ b/html/js/product-preferences.js @@ -170,6 +170,7 @@ function display_user_product_preferences(target_selected, target_selection_form attribute_groups = data; display_user_product_preferences(target_selected, target_selection_form, change); }); + return; } if (!preferences) { @@ -179,6 +180,7 @@ function display_user_product_preferences(target_selected, target_selection_form preferences = data; display_user_product_preferences(target_selected, target_selection_form, change); }); + return; } if (attribute_groups && preferences && !displayed_user_product_preferences) { diff --git a/lib/ProductOpener/Display.pm b/lib/ProductOpener/Display.pm index 08e40f56c47db..f380f51ec3a58 100644 --- a/lib/ProductOpener/Display.pm +++ b/lib/ProductOpener/Display.pm @@ -142,7 +142,7 @@ BEGIN { use vars @EXPORT_OK; -use ProductOpener::HTTP qw(write_cors_headers); +use ProductOpener::HTTP qw(write_cors_headers set_http_response_header write_http_response_headers); use ProductOpener::Store qw(get_string_id_for_lang retrieve); use ProductOpener::Config qw(:all); use ProductOpener::Paths qw/%BASE_DIRS/; @@ -10193,6 +10193,8 @@ sub display_preferences_api ($request_ref, $target_lc) { push @{$request_ref->{structured_response}}, $preference_ref; } + set_http_response_header($request_ref, "Cache-Control", "public, max-age=86400"); + display_structured_response($request_ref); return; @@ -10240,6 +10242,8 @@ sub display_attribute_groups_api ($request_ref, $target_lc) { $request_ref->{structured_response} = $attribute_groups_ref; + set_http_response_header($request_ref, "Cache-Control", "public, max-age=86400"); + display_structured_response($request_ref); return; @@ -10539,6 +10543,8 @@ sub display_product_history ($request_ref, $code, $product_ref) { sub display_structured_response ($request_ref) { # directly serve structured data from $request_ref->{structured_response} + write_http_response_headers($request_ref); + $log->debug( "Displaying structured response", { @@ -10619,9 +10625,7 @@ sub display_structured_response ($request_ref) { . $data . ");"; } else { - $log->warning("XXXXXXXXXXXXXXXXXXXXXX"); write_cors_headers(); - $log->warning("YYYYYYYYYYYYYYYY"); print header( -status => $status_code, -type => 'application/json', diff --git a/lib/ProductOpener/HTTP.pm b/lib/ProductOpener/HTTP.pm index 5ffa242ce6bb0..9ee8f23887dfb 100644 --- a/lib/ProductOpener/HTTP.pm +++ b/lib/ProductOpener/HTTP.pm @@ -46,6 +46,8 @@ BEGIN { @EXPORT_OK = qw( &get_cors_headers &write_cors_headers + &set_http_response_header + &write_http_response_headers ); #the fucntions which are called outside this file %EXPORT_TAGS = (all => [@EXPORT_OK]); } @@ -147,7 +149,49 @@ sub write_cors_headers ($allow_credentials = 0, $sub_domain_only = 0) { my $headers_ref = get_cors_headers($allow_credentials, $sub_domain_only); my $r = Apache2::RequestUtil->request(); # write them - while (my ($header_name, $header_value) = each %$headers_ref) { + foreach my $header_name (sort keys %$headers_ref) { + my $header_value = $headers_ref->{$header_name}; + $r->err_headers_out->set($header_name, $header_value); + } + return; +} + +=head2 set_http_response_header($request_ref, $header_name, $header_value) + +This function sets a header in the response. + +=head3 Parameters + +=head4 $request_ref - Reference to the request object. + +=head4 $header_name - Name of the header. + +=head4 $header_value - Value of the header. + +=cut + +sub set_http_response_header($request_ref, $header_name, $header_value) { + not defined $request_ref->{http_response_headers} and $request_ref->{http_response_headers} = {}; + $request_ref->{http_response_headers}{$header_name} = $header_value; + return; +} + +=head2 write_http_response_headers($http_response_headers_ref) + +This function writes the headers in the response. + +=head3 Parameters + +=head4 $http_response_headers_ref - Reference to a hash with headers. + +=cut + +sub write_http_response_headers($request_ref) { + my $http_response_headers_ref = $request_ref->{http_response_headers}; + return unless $http_response_headers_ref; + my $r = Apache2::RequestUtil->request(); + foreach my $header_name (sort keys %$http_response_headers_ref) { + my $header_value = $http_response_headers_ref->{$header_name}; $r->err_headers_out->set($header_name, $header_value); } return; From 76de0dec59d982f51b81a056f50b8e4726fb2747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Thu, 23 May 2024 14:33:14 +0200 Subject: [PATCH 2/2] lint --- html/js/product-preferences.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/html/js/product-preferences.js b/html/js/product-preferences.js index 984e4e5275d6f..450bdf5433060 100644 --- a/html/js/product-preferences.js +++ b/html/js/product-preferences.js @@ -170,6 +170,7 @@ function display_user_product_preferences(target_selected, target_selection_form attribute_groups = data; display_user_product_preferences(target_selected, target_selection_form, change); }); + return; } @@ -180,6 +181,7 @@ function display_user_product_preferences(target_selected, target_selection_form preferences = data; display_user_product_preferences(target_selected, target_selection_form, change); }); + return; }