# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../nginx/nginx_upload_module-2.2-branch-update-20111130.diff
# Copyright (C) 2012 The OpenSDE Project
#
# More information can be found in the files COPYING and README.
#
# This patch file is dual-licensed. It is available under the license the
# patched project is licensed under, as long as it is an OpenSource license
# as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
# of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
# --- SDE-COPYRIGHT-NOTE-END ---

From 7990a22bca14b128ec865fca296fdf8924ff320f Mon Sep 17 00:00:00 2001
From: Valery Kholodkov <valery@unimatrix1.(none)>
Date: Tue, 14 Dec 2010 11:48:32 +0100
Subject: [PATCH 1/6] A directive upload_add_header that allows to add headers to 201 Created responses

---
 ngx_http_upload_module.c |  134 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 134 insertions(+), 0 deletions(-)

diff --git nginx-upload-module-2.2.0/ngx_http_upload_module.c nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
index b95e12b..d762005 100644
--- nginx-upload-module-2.2.0/ngx_http_upload_module.c
+++ nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
@@ -95,6 +95,14 @@ typedef struct {
 } ngx_http_upload_field_template_t;
 
 /*
+ * Template for a header
+ */
+typedef struct {
+    ngx_http_complex_value_t      *name;
+    ngx_http_complex_value_t      *value;
+} ngx_http_upload_header_template_t;
+
+/*
  * Filter for fields in output form
  */
 typedef struct {
@@ -137,6 +145,7 @@ typedef struct {
     ngx_array_t                   *aggregate_field_templates;
     ngx_array_t                   *field_filters;
     ngx_array_t                   *cleanup_statuses;
+    ngx_array_t                   *header_templates;
     ngx_flag_t                    forward_args;
     ngx_flag_t                    tame_arrays;
     ngx_flag_t                    resumable_uploads;
@@ -279,6 +288,8 @@ static ngx_int_t ngx_http_read_upload_client_request_body(ngx_http_request_t *r)
 
 static char *ngx_http_upload_set_form_field(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
+static char *ngx_http_upload_add_header(ngx_conf_t *cf, ngx_command_t *cmd,
+    void *conf);
 static char *ngx_http_upload_pass_form_field(ngx_conf_t *cf, ngx_command_t *cmd,
     void *conf);
 static char *ngx_http_upload_cleanup(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -575,6 +586,17 @@ static ngx_command_t  ngx_http_upload_commands[] = { /* {{{ */
        offsetof(ngx_http_upload_loc_conf_t, resumable_uploads),
        NULL },
 
+    /*
+     * Specifies the name and content of the header that will be added to the response
+     */
+    { ngx_string("upload_add_header"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_HTTP_LIF_CONF
+                        |NGX_CONF_TAKE2,
+      ngx_http_upload_add_header,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_upload_loc_conf_t, header_templates),
+      NULL},
+
       ngx_null_command
 }; /* }}} */
 
@@ -758,6 +780,42 @@ ngx_http_upload_handler(ngx_http_request_t *r)
     return NGX_DONE;
 } /* }}} */
 
+static ngx_int_t ngx_http_upload_add_headers(ngx_http_request_t *r, ngx_http_upload_loc_conf_t *ulcf) { /* {{{ */
+    ngx_str_t                            name;
+    ngx_str_t                            value;
+    ngx_http_upload_header_template_t    *t;
+    ngx_table_elt_t                      *h;
+    ngx_uint_t                           i;
+
+    t = ulcf->header_templates->elts;
+    for(i = 0; i < ulcf->header_templates->nelts; i++) {
+        if(ngx_http_complex_value(r, t->name, &name) != NGX_OK) {
+            return NGX_ERROR;
+        }
+
+        if(ngx_http_complex_value(r, t->value, &value) != NGX_OK) {
+            return NGX_ERROR;
+        }
+
+        if(name.len != 0 && value.len != 0) {
+            h = ngx_list_push(&r->headers_out.headers);
+            if(h == NULL) {
+                return NGX_ERROR;
+            }
+
+            h->hash = 1;
+            h->key.len = name.len;
+            h->key.data = name.data;
+            h->value.len = value.len;
+            h->value.data = value.data;
+        }
+
+        t++;
+    }
+
+    return NGX_OK;
+} /* }}} */
+
 static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r) { /* {{{ */
     ngx_http_upload_loc_conf_t  *ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
     ngx_http_upload_ctx_t       *ctx = ngx_http_get_module_ctx(r, ngx_http_upload_module);
@@ -774,6 +832,10 @@ static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r) { /* {{{ */
     if(ctx->prevent_output) {
         r->headers_out.status = NGX_HTTP_CREATED;
 
+        if(ngx_http_upload_add_headers(r, ulcf) != NGX_OK) {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        }
+
         /*
          * Add range header and body
          */
@@ -1809,6 +1871,7 @@ ngx_http_upload_create_loc_conf(ngx_conf_t *cf)
     conf->limit_rate = NGX_CONF_UNSET_SIZE;
 
     /*
+     * conf->header_templates,
      * conf->field_templates,
      * conf->aggregate_field_templates,
      * and conf->field_filters are
@@ -1925,6 +1988,10 @@ ngx_http_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
         conf->cleanup_statuses = prev->cleanup_statuses;
     }
 
+    if(conf->header_templates == NULL) {
+        conf->header_templates = prev->header_templates;
+    }
+
     return NGX_CONF_OK;
 } /* }}} */
 
@@ -2396,6 +2463,73 @@ ngx_http_upload_pass_form_field(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
     return NGX_CONF_OK;
 } /* }}} */
 
+static char * /* {{{ ngx_http_upload_add_header */
+ngx_http_upload_add_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    ngx_str_t                  *value;
+    ngx_http_upload_header_template_t *h;
+    ngx_array_t                 **field;
+    ngx_http_compile_complex_value_t   ccv;
+
+    field = (ngx_array_t**) (((u_char*)conf) + cmd->offset);
+
+    value = cf->args->elts;
+
+    /*
+     * Add new entry to header template list
+     */
+    if (*field == NULL) {
+        *field = ngx_array_create(cf->pool, 1,
+                                  sizeof(ngx_http_upload_header_template_t));
+        if (*field == NULL) {
+            return NGX_CONF_ERROR;
+        }
+    }
+
+    h = ngx_array_push(*field);
+    if (h == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    /*
+     * Compile header name
+     */
+    h->name = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
+    if(h->name == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+
+    ccv.cf = cf;
+    ccv.value = &value[1];
+    ccv.complex_value = h->name;
+
+    if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+        return NGX_CONF_ERROR;
+    }
+
+    /*
+     * Compile header value
+     */
+    h->value = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
+    if(h->value == NULL) {
+        return NGX_CONF_ERROR;
+    }
+
+    ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+
+    ccv.cf = cf;
+    ccv.value = &value[2];
+    ccv.complex_value = h->value;
+
+    if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+        return NGX_CONF_ERROR;
+    }
+
+    return NGX_CONF_OK;
+} /* }}} */
+
 static char * /* {{{ ngx_http_upload_cleanup */
 ngx_http_upload_cleanup(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-- 
1.6.6.2


From 8a9d65c678b739c333343a3c2bb7d582f7f5e3b1 Mon Sep 17 00:00:00 2001
From: Valery Kholodkov <valery@unimatrix1.(none)>
Date: Tue, 14 Dec 2010 12:34:20 +0100
Subject: [PATCH 2/6] A handler for OPTIONS method that facilitates HTML5 uploades

---
 ngx_http_upload_module.c |   26 ++++++++++++++++++++++++--
 1 files changed, 24 insertions(+), 2 deletions(-)

diff --git nginx-upload-module-2.2.0/ngx_http_upload_module.c nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
index d762005..650bfea 100644
--- nginx-upload-module-2.2.0/ngx_http_upload_module.c
+++ nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
@@ -243,6 +243,7 @@ typedef struct ngx_http_upload_ctx_s {
 } ngx_http_upload_ctx_t;
 
 static ngx_int_t ngx_http_upload_handler(ngx_http_request_t *r);
+static ngx_int_t ngx_http_upload_options_handler(ngx_http_request_t *r);
 static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r);
 
 static void *ngx_http_upload_create_loc_conf(ngx_conf_t *cf);
@@ -710,11 +711,14 @@ ngx_http_upload_handler(ngx_http_request_t *r)
     ngx_http_upload_ctx_t     *u;
     ngx_int_t                 rc;
 
+    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
+
+    if((r->method & NGX_HTTP_OPTIONS) && ulcf->resumable_uploads)
+        return ngx_http_upload_options_handler(r);
+
     if (!(r->method & NGX_HTTP_POST))
         return NGX_HTTP_NOT_ALLOWED;
 
-    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
-
     u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
 
     if (u == NULL) {
@@ -816,6 +820,24 @@ static ngx_int_t ngx_http_upload_add_headers(ngx_http_request_t *r, ngx_http_upl
     return NGX_OK;
 } /* }}} */
 
+static ngx_int_t ngx_http_upload_options_handler(ngx_http_request_t *r) { /* {{{ */
+    ngx_http_upload_loc_conf_t *ulcf;
+
+    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
+
+    r->headers_out.status = NGX_HTTP_OK;
+
+    if(ngx_http_upload_add_headers(r, ulcf) != NGX_OK) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    r->header_only = 1;
+    r->headers_out.content_length_n = 0;
+    r->allow_ranges = 0;
+
+    return ngx_http_send_header(r);
+} /* }}} */
+
 static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r) { /* {{{ */
     ngx_http_upload_loc_conf_t  *ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
     ngx_http_upload_ctx_t       *ctx = ngx_http_get_module_ctx(r, ngx_http_upload_module);
-- 
1.6.6.2


From 1f9b976d08486c91c9b820ce9f427ac0f7a8d2e6 Mon Sep 17 00:00:00 2001
From: Valery Kholodkov <valery@unimatrix1.(none)>
Date: Tue, 14 Dec 2010 15:56:13 +0100
Subject: [PATCH 3/6] Enable OPTIONS method processing for normal requests

---
 ngx_http_upload_module.c |   54 +++++++++++++++++++++++----------------------
 1 files changed, 28 insertions(+), 26 deletions(-)

diff --git nginx-upload-module-2.2.0/ngx_http_upload_module.c nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
index 650bfea..cd60f29 100644
--- nginx-upload-module-2.2.0/ngx_http_upload_module.c
+++ nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
@@ -711,14 +711,14 @@ ngx_http_upload_handler(ngx_http_request_t *r)
     ngx_http_upload_ctx_t     *u;
     ngx_int_t                 rc;
 
-    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
-
-    if((r->method & NGX_HTTP_OPTIONS) && ulcf->resumable_uploads)
+    if(r->method & NGX_HTTP_OPTIONS)
         return ngx_http_upload_options_handler(r);
 
     if (!(r->method & NGX_HTTP_POST))
         return NGX_HTTP_NOT_ALLOWED;
 
+    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_upload_module);
+
     u = ngx_http_get_module_ctx(r, ngx_http_upload_module);
 
     if (u == NULL) {
@@ -791,30 +791,32 @@ static ngx_int_t ngx_http_upload_add_headers(ngx_http_request_t *r, ngx_http_upl
     ngx_table_elt_t                      *h;
     ngx_uint_t                           i;
 
-    t = ulcf->header_templates->elts;
-    for(i = 0; i < ulcf->header_templates->nelts; i++) {
-        if(ngx_http_complex_value(r, t->name, &name) != NGX_OK) {
-            return NGX_ERROR;
-        }
-
-        if(ngx_http_complex_value(r, t->value, &value) != NGX_OK) {
-            return NGX_ERROR;
-        }
+    if(ulcf->header_templates != NULL) {
+        t = ulcf->header_templates->elts;
+        for(i = 0; i < ulcf->header_templates->nelts; i++) {
+            if(ngx_http_complex_value(r, t->name, &name) != NGX_OK) {
+                return NGX_ERROR;
+            }
 
-        if(name.len != 0 && value.len != 0) {
-            h = ngx_list_push(&r->headers_out.headers);
-            if(h == NULL) {
+            if(ngx_http_complex_value(r, t->value, &value) != NGX_OK) {
                 return NGX_ERROR;
             }
 
-            h->hash = 1;
-            h->key.len = name.len;
-            h->key.data = name.data;
-            h->value.len = value.len;
-            h->value.data = value.data;
-        }
+            if(name.len != 0 && value.len != 0) {
+                h = ngx_list_push(&r->headers_out.headers);
+                if(h == NULL) {
+                    return NGX_ERROR;
+                }
 
-        t++;
+                h->hash = 1;
+                h->key.len = name.len;
+                h->key.data = name.data;
+                h->value.len = value.len;
+                h->value.data = value.data;
+            }
+
+            t++;
+        }
     }
 
     return NGX_OK;
@@ -851,13 +853,13 @@ static ngx_int_t ngx_http_upload_body_handler(ngx_http_request_t *r) { /* {{{ */
     ngx_str_t                   dummy = ngx_string("<ngx_upload_module_dummy>");
     ngx_table_elt_t             *h;
 
+    if(ngx_http_upload_add_headers(r, ulcf) != NGX_OK) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
     if(ctx->prevent_output) {
         r->headers_out.status = NGX_HTTP_CREATED;
 
-        if(ngx_http_upload_add_headers(r, ulcf) != NGX_OK) {
-            return NGX_HTTP_INTERNAL_SERVER_ERROR;
-        }
-
         /*
          * Add range header and body
          */
-- 
1.6.6.2


From 5849956827b4246280e153d97a0a141eaaf9c5fa Mon Sep 17 00:00:00 2001
From: Valery Kholodkov <valery@iva.(none)>
Date: Tue, 1 Feb 2011 00:14:32 +0100
Subject: [PATCH 4/6] Added directive upload_empty_field_names: allow passing fields with empty names to backend

---
 ngx_http_upload_module.c |   25 +++++++++++++++++++++++--
 1 files changed, 23 insertions(+), 2 deletions(-)

diff --git nginx-upload-module-2.2.0/ngx_http_upload_module.c nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
index cd60f29..6e3c9c9 100644
--- nginx-upload-module-2.2.0/ngx_http_upload_module.c
+++ nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
@@ -149,6 +149,7 @@ typedef struct {
     ngx_flag_t                    forward_args;
     ngx_flag_t                    tame_arrays;
     ngx_flag_t                    resumable_uploads;
+    ngx_flag_t                    empty_field_names;
     size_t                        limit_rate;
 
     unsigned int                  md5:1;
@@ -587,6 +588,17 @@ static ngx_command_t  ngx_http_upload_commands[] = { /* {{{ */
        offsetof(ngx_http_upload_loc_conf_t, resumable_uploads),
        NULL },
 
+     /*
+      * Specifies whether empty field names are allowed
+      */
+     { ngx_string("upload_empty_fiels_names"),
+       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_HTTP_LIF_CONF
+                         |NGX_CONF_FLAG,
+       ngx_conf_set_flag_slot,
+       NGX_HTTP_LOC_CONF_OFFSET,
+       offsetof(ngx_http_upload_loc_conf_t, empty_field_names),
+       NULL },
+
     /*
      * Specifies the name and content of the header that will be added to the response
      */
@@ -1236,7 +1248,10 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
 #if (NGX_PCRE)
                 rc = ngx_regex_exec(f[i].regex, &u->field_name, NULL, 0);
 
-                if (rc != NGX_REGEX_NO_MATCHED && rc < 0) {
+                /* Modified by Naren to work around iMovie and Quicktime which send empty values Added:  &&  u->field_name.len > 0 */
+                if ((ulcf->empty_field_names && rc != NGX_REGEX_NO_MATCHED && rc < 0 && u->field_name.len != 0)
+                    || (!ulcf->empty_field_names && rc != NGX_REGEX_NO_MATCHED && rc < 0))
+                {
                     return NGX_UPLOAD_SCRIPTERROR;
                 }
 
@@ -1252,7 +1267,7 @@ static ngx_int_t ngx_http_upload_start_handler(ngx_http_upload_ctx_t *u) { /* {{
             }
         }
 
-        if(pass_field && u->field_name.len > 0) { 
+        if(pass_field && u->field_name.len != 0) { 
             /*
              * Here we do a small hack: the content of a non-file field
              * is not known until ngx_http_upload_flush_output_buffer
@@ -1885,6 +1900,7 @@ ngx_http_upload_create_loc_conf(ngx_conf_t *cf)
     conf->forward_args = NGX_CONF_UNSET;
     conf->tame_arrays = NGX_CONF_UNSET;
     conf->resumable_uploads = NGX_CONF_UNSET;
+    conf->empty_field_names = NGX_CONF_UNSET;
 
     conf->buffer_size = NGX_CONF_UNSET_SIZE;
     conf->merge_buffer_size = NGX_CONF_UNSET_SIZE;
@@ -1984,6 +2000,11 @@ ngx_http_upload_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
             prev->resumable_uploads : 0;
     }
 
+    if(conf->empty_field_names == NGX_CONF_UNSET) {
+        conf->empty_field_names = (prev->empty_field_names != NGX_CONF_UNSET) ?
+            prev->empty_field_names : 0;
+    }
+
     if(conf->field_templates == NULL) {
         conf->field_templates = prev->field_templates;
     }
-- 
1.6.6.2


From b100ed9971612c4f16279e188638b6bea1d9cb59 Mon Sep 17 00:00:00 2001
From: Edward Dale <scompt@scompt.com>
Date: Thu, 10 Feb 2011 15:50:06 +0100
Subject: [PATCH 5/6] Allowing status codes as low as 200 to trigger cleanup.

---
 ngx_http_upload_module.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git nginx-upload-module-2.2.0/ngx_http_upload_module.c nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
index 6e3c9c9..3e1fcc5 100644
--- nginx-upload-module-2.2.0/ngx_http_upload_module.c
+++ nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
@@ -2632,9 +2632,9 @@ ngx_http_upload_cleanup(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
             hi = lo = status;
         }
 
-        if (lo < 400 || hi > 599) {
+        if (lo < 200 || hi > 599) {
             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                               "value(s) \"%V\" must be between 400 and 599",
+                               "value(s) \"%V\" must be between 200 and 599",
                                &value[i]);
             return NGX_CONF_ERROR;
         }
-- 
1.6.6.2


From b18a0707e3aeaca8eb409ee6323d4c7abbe05d4b Mon Sep 17 00:00:00 2001
From: Simon <bigplum@gmail.com>
Date: Sat, 23 Apr 2011 17:39:33 +0800
Subject: [PATCH 6/6] range length must equal to content length

---
 ngx_http_upload_module.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git nginx-upload-module-2.2.0/ngx_http_upload_module.c nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
index 3e1fcc5..01347c9 100644
--- nginx-upload-module-2.2.0/ngx_http_upload_module.c
+++ nginx-upload-module-2.2-branch-update/ngx_http_upload_module.c
@@ -3478,6 +3478,14 @@ static ngx_int_t upload_parse_request_headers(ngx_http_upload_ctx_t *upload_ctx,
                     return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
                 }
 
+                if( (upload_ctx->content_range_n.end - upload_ctx->content_range_n.start + 1)
+                    != headers_in->content_length_n) 
+                {
+                    ngx_log_error(NGX_LOG_ERR, upload_ctx->log, 0,
+                                  "range length is not equal to content length");
+                    return NGX_HTTP_RANGE_NOT_SATISFIABLE;
+                }
+
                 upload_ctx->partial_content = 1;
             }
         }
-- 
1.6.6.2