@@ -664,6 +664,50 @@ function set_amz_headers(req, res) {
664
664
res . setHeader ( 'x-amz-id-2' , req . request_id ) ;
665
665
}
666
666
667
+ /**
668
+ * @param {Object } req
669
+ * @param {http.ServerResponse } res
670
+ * @param {Object } rules
671
+ */
672
+ async function set_expiration_header ( req , res , rules ) {
673
+ if ( req . method === 'HEAD' || req . method === 'GET' || req . method === 'PUT' ) {
674
+ // TODO: For every object key we need to apply the mupltiple rules obtained in lifecycle and then update the x-amz-expiration accordingly
675
+ // currently putting the first rule expiration to every object
676
+ if ( rules ?. length > 0 ) {
677
+ const expiration_head = parse_expiration_header ( rules [ 0 ] ?. expiration , rules [ 0 ] ?. id ) ;
678
+ if ( expiration_head ) {
679
+ res . setHeader ( 'x-amz-expiration' , expiration_head ) ;
680
+ }
681
+ }
682
+ }
683
+ }
684
+
685
+ /**
686
+ * parse_expiration_header converts an expiration rule (either with `date` or `days`)
687
+ * into an s3 style `x-amz-expiration` header value
688
+ *
689
+ * @param {Object } expiration - expiration object from lifecycle config
690
+ * @param {string } rule_id - id of the lifecycle rule
691
+ * @returns {string|undefined }
692
+ *
693
+ * Example output:
694
+ * expiry-date="Thu, 10 Apr 2025 00:00:00 GMT", rule-id="rule_id"
695
+ */
696
+ function parse_expiration_header ( expiration , rule_id ) {
697
+ if ( ! expiration || ( ! expiration . date && ! expiration . days ) ) return undefined ;
698
+
699
+ const expiration_date = expiration . date ?
700
+ new Date ( expiration . date ) :
701
+ new Date ( Date . UTC (
702
+ new Date ( ) . getUTCFullYear ( ) ,
703
+ new Date ( ) . getUTCMonth ( ) ,
704
+ new Date ( ) . getUTCDate ( ) + expiration . days
705
+ ) ) ;
706
+
707
+ return `expiry-date="${ expiration_date . toUTCString ( ) } ", rule-id="${ rule_id } "` ;
708
+ }
709
+
710
+
667
711
/**
668
712
* @typedef {{
669
713
* allow_origin: string;
@@ -945,6 +989,7 @@ exports.set_keep_alive_whitespace_interval = set_keep_alive_whitespace_interval;
945
989
exports . parse_xml_to_js = parse_xml_to_js ;
946
990
exports . check_headers = check_headers ;
947
991
exports . set_amz_headers = set_amz_headers ;
992
+ exports . set_expiration_header = set_expiration_header ;
948
993
exports . set_cors_headers = set_cors_headers ;
949
994
exports . set_cors_headers_s3 = set_cors_headers_s3 ;
950
995
exports . set_cors_headers_sts = set_cors_headers_sts ;
0 commit comments