From 56e7d89615bbe416ddde6512db7403c9b16d23a4 Mon Sep 17 00:00:00 2001
From: Ori Yechieli <ori.yechieli@nortecview.com>
Date: Sat, 21 Dec 2019 15:44:17 +0200
Subject: [PATCH] Support alternative http code for PUT, PATCH and DELETE
 methods

---
 README.md          | 4 ++++
 default.conf       | 4 ++++
 docker-compose.yml | 3 +++
 run.sh             | 4 ++++
 4 files changed, 15 insertions(+)

diff --git a/README.md b/README.md
index 659836e..0bbed12 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,10 @@ A very simple container to redirect HTTP traffic to another server, based on `ng
     - useful if client should not change the request method from POST to GET
     - if not set or not in allowed Codes `SERVER_REDIRECT_CODE` is used
     - so per default all requests will be redirected with the same status code
+ - `SERVER_REDIRECT_PUT_PATCH_DELETE_CODE` - optionally define the http code to use for PUT, PATCH and DELETE redirection
+    - useful if client should not change the request method from PUT, PATCH and DELETE to GET
+    - if not set or not in allowed Codes `SERVER_REDIRECT_CODE` is used
+    - so per default all requests will be redirected with the same status code
 - `SERVER_ACCESS_LOG` - optionally define the location where nginx will write its access log
    - if not set /dev/stdout is used
 - `SERVER_ERROR_LOG` - optionally define the location where nginx will write its error log
diff --git a/default.conf b/default.conf
index 8ac9d3c..3e15cbb 100644
--- a/default.conf
+++ b/default.conf
@@ -12,6 +12,10 @@ server {
         return ${SERVER_REDIRECT_POST_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH};
     }
 
+    if ($request_method ~ PUT|PATCH|DELETE) {
+        return ${SERVER_REDIRECT_PUT_PATCH_DELETE_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH};
+    }
+
     return ${SERVER_REDIRECT_CODE} ${SERVER_REDIRECT_SCHEME}://${SERVER_REDIRECT}${SERVER_REDIRECT_PATH};
 
     # redirect server error pages to the static page /50x.html
diff --git a/docker-compose.yml b/docker-compose.yml
index 9ccc112..462f290 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -17,6 +17,9 @@ to:
     # optionally define the http code to redirect POST requests
     # if not set or not in allowed Codes, SERVER_REDIRECT_CODE will be used
     #- SERVER_REDIRECT_POST_CODE=
+    # optionally define the http code to redirect PUT, PATCH and DELETE requests
+    # if not set or not in allowed Codes, SERVER_REDIRECT_CODE will be used
+    #- SERVER_REDIRECT_PUT_PATCH_DELETE_CODE=
     # optionally define the location for the nginx access log
     # if not set /dev/stdout is used
     #- SERVER_ACCESS_LOG=/dev/null
diff --git a/run.sh b/run.sh
index 844316c..ada7209 100644
--- a/run.sh
+++ b/run.sh
@@ -17,6 +17,9 @@ expr match "$SERVER_REDIRECT_CODE" '30[12378]$' > /dev/null || SERVER_REDIRECT_C
 # set redirect code from optional ENV var for POST requests
 expr match "$SERVER_REDIRECT_POST_CODE" '30[12378]$' > /dev/null || SERVER_REDIRECT_POST_CODE=$SERVER_REDIRECT_CODE
 
+# set redirect code from optional ENV var for PUT, PATCH and DELETE requests
+expr match "$SERVER_REDIRECT_PUT_PATCH_DELETE_CODE" '30[12378]$' > /dev/null || SERVER_REDIRECT_PUT_PATCH_DELETE_CODE=$SERVER_REDIRECT_CODE
+
 # set redirect path from optional ENV var
 if [ ! -n "$SERVER_REDIRECT_PATH" ] ; then
     SERVER_REDIRECT_PATH='$request_uri'
@@ -41,6 +44,7 @@ sed -i "s|\${SERVER_REDIRECT}|${SERVER_REDIRECT}|" /etc/nginx/conf.d/default.con
 sed -i "s|\${SERVER_NAME}|${SERVER_NAME}|" /etc/nginx/conf.d/default.conf
 sed -i "s|\${SERVER_REDIRECT_CODE}|${SERVER_REDIRECT_CODE}|" /etc/nginx/conf.d/default.conf
 sed -i "s|\${SERVER_REDIRECT_POST_CODE}|${SERVER_REDIRECT_POST_CODE}|" /etc/nginx/conf.d/default.conf
+sed -i "s|\${SERVER_REDIRECT_PUT_PATCH_DELETE_CODE}|${SERVER_REDIRECT_PUT_PATCH_DELETE_CODE}|" /etc/nginx/conf.d/default.conf
 sed -i "s|\${SERVER_REDIRECT_PATH}|${SERVER_REDIRECT_PATH}|" /etc/nginx/conf.d/default.conf
 sed -i "s|\${SERVER_REDIRECT_SCHEME}|${SERVER_REDIRECT_SCHEME}|" /etc/nginx/conf.d/default.conf
 
-- 
GitLab