From 088a5b9db650cde92d264be88901bfa60f302319 Mon Sep 17 00:00:00 2001 From: john-shaffer Date: Wed, 23 Oct 2019 08:58:00 -0500 Subject: [PATCH 1/7] Forward Expires and Last-Modified headers from S3. --- source/image-handler/image-request.js | 4 +++- source/image-handler/index.js | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 75b7da7e7..4fe315eee 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -46,6 +46,8 @@ class ImageRequest { const request = s3.getObject(imageLocation).promise(); try { const originalImage = await request; + this.Expires = originalImage.Expires; + this.LastModified = originalImage.LastModified; return Promise.resolve(originalImage.Body); } catch(err) { @@ -235,4 +237,4 @@ class ImageRequest { } // Exports -module.exports = ImageRequest; \ No newline at end of file +module.exports = ImageRequest; diff --git a/source/image-handler/index.js b/source/image-handler/index.js index 840b5bf74..cdb35ac8c 100755 --- a/source/image-handler/index.js +++ b/source/image-handler/index.js @@ -22,9 +22,12 @@ exports.handler = async (event) => { const request = await imageRequest.setup(event); console.log(request); const processedRequest = await imageHandler.process(request); + const headers = getResponseHeaders(); + headers["Expires"] = request.Expires; + headers["Last-Modified"] = request.LastModified; const response = { "statusCode": 200, - "headers" : getResponseHeaders(), + "headers" : headers, "body": processedRequest, "isBase64Encoded": true } @@ -61,4 +64,4 @@ const getResponseHeaders = (isErr) => { headers["Content-Type"] = "application/json" } return headers; -} \ No newline at end of file +} From 5eb5c87c54c2ca75ef0d120d32dc28d37ef879a8 Mon Sep 17 00:00:00 2001 From: john-shaffer Date: Wed, 23 Oct 2019 09:29:20 -0500 Subject: [PATCH 2/7] Forward Content-Type header from S3. --- source/image-handler/image-request.js | 1 + source/image-handler/index.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 4fe315eee..c83d2d506 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -46,6 +46,7 @@ class ImageRequest { const request = s3.getObject(imageLocation).promise(); try { const originalImage = await request; + this.ContentType = originalImage.ContentType; this.Expires = originalImage.Expires; this.LastModified = originalImage.LastModified; return Promise.resolve(originalImage.Body); diff --git a/source/image-handler/index.js b/source/image-handler/index.js index cdb35ac8c..253f12a7f 100755 --- a/source/image-handler/index.js +++ b/source/image-handler/index.js @@ -23,6 +23,7 @@ exports.handler = async (event) => { console.log(request); const processedRequest = await imageHandler.process(request); const headers = getResponseHeaders(); + headers["Content-Type"] = request.ContentType; headers["Expires"] = request.Expires; headers["Last-Modified"] = request.LastModified; const response = { @@ -54,8 +55,7 @@ const getResponseHeaders = (isErr) => { const headers = { "Access-Control-Allow-Methods": "GET", "Access-Control-Allow-Headers": "Content-Type, Authorization", - "Access-Control-Allow-Credentials": true, - "Content-Type": "image" + "Access-Control-Allow-Credentials": true } if (corsEnabled) { headers["Access-Control-Allow-Origin"] = process.env.CORS_ORIGIN; From 03a6e5b8c5334142bb7f7533fc44efac80e7ba84 Mon Sep 17 00:00:00 2001 From: John Shaffer <53870456+john-shaffer@users.noreply.github.com> Date: Thu, 24 Oct 2019 18:57:55 -0500 Subject: [PATCH 3/7] Use HTTP 404 status code for non-existent files. --- source/image-handler/image-request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index c83d2d506..6f9e7e035 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -53,7 +53,7 @@ class ImageRequest { } catch(err) { return Promise.reject({ - status: 500, + status: ("NoSuchKey" == err.code) ? 404 : 500, code: err.code, message: err.message }) From 8393774f4a25d04630407e31fe41d4d9aa2fb9e8 Mon Sep 17 00:00:00 2001 From: john-shaffer Date: Wed, 6 Nov 2019 17:52:20 -0600 Subject: [PATCH 4/7] Apply Flythe's fix to formatting of datetimes in HTTP headers. --- source/image-handler/image-request.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index c83d2d506..b2ecb1d4d 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -47,8 +47,8 @@ class ImageRequest { try { const originalImage = await request; this.ContentType = originalImage.ContentType; - this.Expires = originalImage.Expires; - this.LastModified = originalImage.LastModified; + this.Expires = new Date(originalImage.Expires).toUTCString(); + this.LastModified = new Date(originalImage.LastModified).toUTCString(); return Promise.resolve(originalImage.Body); } catch(err) { From 592cd16fcb0016fc80ea9ad3279cfaf456c56f8f Mon Sep 17 00:00:00 2001 From: Vital Ryabchinskiy Date: Fri, 20 Dec 2019 09:41:29 +0000 Subject: [PATCH 5/7] Froward headers --- source/image-handler/image-request.js | 8 ++++++-- source/image-handler/index.js | 12 ++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 405ba6313..40d81ce9a 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -46,11 +46,15 @@ class ImageRequest { const request = s3.getObject(imageLocation).promise(); try { const originalImage = await request; + this.ContentType = originalImage.ContentType; + this.Expires = new Date(originalImage.Expires).toUTCString(); + this.LastModified = new Date(originalImage.LastModified).toUTCString(); + this.CacheControl = originalImage.CacheControl; return Promise.resolve(originalImage.Body); } catch(err) { return Promise.reject({ - status: 500, + status: ("NoSuchKey" == err.code) ? 404 : 500, code: err.code, message: err.message }) @@ -235,4 +239,4 @@ class ImageRequest { } // Exports -module.exports = ImageRequest; \ No newline at end of file +module.exports = ImageRequest; diff --git a/source/image-handler/index.js b/source/image-handler/index.js index f6a9cba8a..3520a4dd7 100755 --- a/source/image-handler/index.js +++ b/source/image-handler/index.js @@ -22,9 +22,14 @@ exports.handler = async (event) => { const request = await imageRequest.setup(event); console.log(request); const processedRequest = await imageHandler.process(request); + const headers = getResponseHeaders(); + headers["Content-Type"] = request.ContentType; + headers["Expires"] = request.Expires; + headers["Last-Modified"] = request.LastModified; + headers["Cache-Control"] = request.CacheControl; const response = { "statusCode": 200, - "headers" : getResponseHeaders(), + "headers" : headers, "body": processedRequest, "isBase64Encoded": true } @@ -51,8 +56,7 @@ const getResponseHeaders = (isErr) => { const headers = { "Access-Control-Allow-Methods": "GET", "Access-Control-Allow-Headers": "Content-Type, Authorization", - "Access-Control-Allow-Credentials": true, - "Content-Type": "image" + "Access-Control-Allow-Credentials": true } if (corsEnabled) { headers["Access-Control-Allow-Origin"] = process.env.CORS_ORIGIN; @@ -61,4 +65,4 @@ const getResponseHeaders = (isErr) => { headers["Content-Type"] = "application/json" } return headers; -} \ No newline at end of file +} From 74de982a693442dc0b40d32e9ee851b9626762a4 Mon Sep 17 00:00:00 2001 From: Vital Ryabchinskiy Date: Fri, 20 Dec 2019 11:49:51 +0000 Subject: [PATCH 6/7] Added conditions --- source/image-handler/image-request.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 40d81ce9a..b0e1e9a8b 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -47,8 +47,12 @@ class ImageRequest { try { const originalImage = await request; this.ContentType = originalImage.ContentType; - this.Expires = new Date(originalImage.Expires).toUTCString(); - this.LastModified = new Date(originalImage.LastModified).toUTCString(); + if (originalImage.Expires) { + this.Expires = new Date(originalImage.Expires).toUTCString(); + } + if (originalImage.LastModified) { + this.LastModified = new Date(originalImage.LastModified).toUTCString(); + } this.CacheControl = originalImage.CacheControl; return Promise.resolve(originalImage.Body); } From 1176fd416df078999ec232dda15f77a8af3a8f21 Mon Sep 17 00:00:00 2001 From: john-shaffer Date: Fri, 20 Dec 2019 16:02:18 -0600 Subject: [PATCH 7/7] Only set ContentType and CacheControl on ImageRequest when they are defined. --- source/image-handler/image-request.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index b0e1e9a8b..90cc62b9f 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -46,14 +46,18 @@ class ImageRequest { const request = s3.getObject(imageLocation).promise(); try { const originalImage = await request; - this.ContentType = originalImage.ContentType; + if (originalImage.ContentType) { + this.ContentType = originalImage.ContentType; + } if (originalImage.Expires) { - this.Expires = new Date(originalImage.Expires).toUTCString(); + this.Expires = new Date(originalImage.Expires).toUTCString(); } if (originalImage.LastModified) { - this.LastModified = new Date(originalImage.LastModified).toUTCString(); + this.LastModified = new Date(originalImage.LastModified).toUTCString(); + } + if (originalImage.CacheControl) { + this.CacheControl = originalImage.CacheControl; } - this.CacheControl = originalImage.CacheControl; return Promise.resolve(originalImage.Body); } catch(err) {