Merge branch 'nginx' (nginx-1.13.2).

Change-Id: I634b781be1ff4da30bbf0a75a60c3f50b847175d
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
diff --git a/.hgtags b/.hgtags
index 653a6d1..849abee 100644
--- a/.hgtags
+++ b/.hgtags
@@ -414,3 +414,4 @@
 3d0e8655f897959e48cc74e87670bb5492a58871 release-1.11.13
 3671096a45bce570a2afa20b9faf42c7fb0f7e66 release-1.13.0
 539f7893ecb96bee60965528c8958d7eb2f1ce6b release-1.13.1
+5be2b25bdc65775a85f18f68a4be4f58c7384415 release-1.13.2
diff --git a/BUILD b/BUILD
index 3650a92..0a3141c 100644
--- a/BUILD
+++ b/BUILD
@@ -1468,5 +1468,5 @@
     preinst = "@nginx_pkgoss//:debian_preinst",
     prerm = "@nginx_pkgoss//:debian_prerm",
     section = "httpd",
-    version = "1.13.1",
+    version = "1.13.2",
 )
diff --git a/auto/cc/bcc b/auto/cc/bcc
index ec82e60..e990a9f 100644
--- a/auto/cc/bcc
+++ b/auto/cc/bcc
@@ -62,7 +62,6 @@
 ngx_objout="-o"
 ngx_binout="-e"
 ngx_objext="obj"
-ngx_binext=".exe"
 
 ngx_long_start='@&&|
 	'
diff --git a/auto/cc/msvc b/auto/cc/msvc
index de5d325..c2b9f45 100644
--- a/auto/cc/msvc
+++ b/auto/cc/msvc
@@ -142,7 +142,6 @@
 ngx_objout="-Fo"
 ngx_binout="-Fe"
 ngx_objext="obj"
-ngx_binext=".exe"
 
 ngx_long_start='@<<
 	'
diff --git a/auto/cc/owc b/auto/cc/owc
index ebb877d..1e5ea17 100644
--- a/auto/cc/owc
+++ b/auto/cc/owc
@@ -84,7 +84,6 @@
 ngx_objout="-fo"
 ngx_binout="-fe="
 ngx_objext="obj"
-ngx_binext=".exe"
 
 ngx_regex_dirsep='\\'
 ngx_dirsep="\\"
diff --git a/auto/configure b/auto/configure
index 7625305..3284956 100755
--- a/auto/configure
+++ b/auto/configure
@@ -39,7 +39,7 @@
     NGX_PLATFORM="$NGX_SYSTEM:$NGX_RELEASE:$NGX_MACHINE";
 
     case "$NGX_SYSTEM" in
-        MINGW32_*)
+        MINGW32_* | MINGW64_* | MSYS_*)
             NGX_PLATFORM=win32
         ;;
     esac
diff --git a/auto/os/conf b/auto/os/conf
index b17a8ff..449279b 100644
--- a/auto/os/conf
+++ b/auto/os/conf
@@ -41,6 +41,14 @@
 '
     ;;
 
+    NetBSD:*)
+        CORE_INCS="$UNIX_INCS"
+        CORE_DEPS="$UNIX_DEPS $POSIX_DEPS"
+        CORE_SRCS="$UNIX_SRCS"
+
+        NGX_RPATH=YES
+    ;;
+
     HP-UX:*)
         # HP/UX
         have=NGX_HPUX . $NGX_AUTO/have_headers
diff --git a/auto/os/win32 b/auto/os/win32
index d5147ca..a0c9862 100644
--- a/auto/os/win32
+++ b/auto/os/win32
@@ -13,6 +13,7 @@
 SELECT_SRCS=$WIN32_SELECT_SRCS
 
 ngx_pic_opt=
+ngx_binext=".exe"
 
 case "$NGX_CC_NAME" in
 
diff --git a/auto/unix b/auto/unix
index 5d75639..f721192 100644
--- a/auto/unix
+++ b/auto/unix
@@ -428,7 +428,10 @@
                   #include <netinet/in.h>"
 ngx_feature_path=
 ngx_feature_libs=
-ngx_feature_test="setsockopt(0, IPPROTO_IP, IP_PKTINFO, NULL, 0)"
+ngx_feature_test="struct in_pktinfo  pkt;
+                  pkt.ipi_spec_dst.s_addr = INADDR_ANY;
+                  (void) pkt;
+                  setsockopt(0, IPPROTO_IP, IP_PKTINFO, NULL, 0)"
 . $NGX_AUTO/feature
 
 
diff --git a/build.bzl b/build.bzl
index 331b58c..b5a8c99 100644
--- a/build.bzl
+++ b/build.bzl
@@ -663,7 +663,7 @@
         name = "nginx_pkgoss",
         build_file_content = _PKGOSS_BUILD_FILE.format(nginx = nginx) +
                              _PKGOSS_BUILD_FILE_TAIL,
-        commit = "9ff23479da00823d9e0e93288f9050d6b6c8328a",  # nginx-1.13.1
+        commit = "0be78ae1bf9b0fbb97b94e2569581bb77ff4cb60",  # nginx-1.13.2
         remote = "https://nginx.googlesource.com/nginx-pkgoss",
     )
 
diff --git a/docs/xml/nginx/changes.xml b/docs/xml/nginx/changes.xml
index c43b6f7..535704c 100644
--- a/docs/xml/nginx/changes.xml
+++ b/docs/xml/nginx/changes.xml
@@ -5,6 +5,79 @@
 <change_log title="nginx">
 
 
+<changes ver="1.13.2" date="2017-06-27">
+
+<change type="change">
+<para lang="ru">
+теперь при запросе диапазона, начинающегося с 0, из пустого файла
+nginx возвращает ответ 200 вместо 416.
+</para>
+<para lang="en">
+nginx now returns 200 instead of 416
+when a range starting with 0 is requested from an empty file.
+</para>
+</change>
+
+<change type="feature">
+<para lang="ru">
+директива add_trailer.<br/>
+Спасибо Piotr Sikora.
+</para>
+<para lang="en">
+the "add_trailer" directive.<br/>
+Thanks to Piotr Sikora.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+nginx не собирался под Cygwin и NetBSD;
+ошибка появилась в 1.13.0.
+</para>
+<para lang="en">
+nginx could not be built on Cygwin and NetBSD;
+the bug had appeared in 1.13.0.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+nginx не собирался под MSYS2 / MinGW 64-bit.<br/>
+Спасибо Orgad Shaneh.
+</para>
+<para lang="en">
+nginx could not be built under MSYS2 / MinGW 64-bit.<br/>
+Thanks to Orgad Shaneh.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+при использовании SSI с большим количеством подзапросов
+и proxy_pass с переменными
+в рабочем процессе мог произойти segmentation fault.
+</para>
+<para lang="en">
+a segmentation fault might occur in a worker process
+when using SSI with many includes
+and proxy_pass with variables.
+</para>
+</change>
+
+<change type="bugfix">
+<para lang="ru">
+в модуле ngx_http_v2_module.<br/>
+Спасибо Piotr Sikora.
+</para>
+<para lang="en">
+in the ngx_http_v2_module.<br/>
+Thanks to Piotr Sikora.
+</para>
+</change>
+
+</changes>
+
+
 <changes ver="1.13.1" date="2017-05-30">
 
 <change type="feature">
diff --git a/src/core/nginx.h b/src/core/nginx.h
index 9386644..9718bd2 100644
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -13,8 +13,8 @@
 #define NGINX_NAME         "nginx"
 #endif
 
-#define nginx_version      1013001
-#define NGINX_VERSION      "1.13.1"
+#define nginx_version      1013002
+#define NGINX_VERSION      "1.13.2"
 #define NGINX_VER          NGINX_NAME "/" NGINX_VERSION
 
 #ifdef NGX_BUILD
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
index aee7a58..675a506 100644
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -1124,9 +1124,7 @@
         if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
             ngx_file_info_t  fi;
 
-            if (ngx_file_info((const char *) file[i].name.data, &fi)
-                == NGX_FILE_ERROR)
-            {
+            if (ngx_file_info(file[i].name.data, &fi) == NGX_FILE_ERROR) {
                 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                               ngx_file_info_n " \"%s\" failed",
                               file[i].name.data);
diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
index b7dd4bc..3a94089 100644
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -622,9 +622,7 @@
         {
         ngx_file_info_t   fi;
 
-        if (ngx_file_info((const char *) path[i]->name.data, &fi)
-            == NGX_FILE_ERROR)
-        {
+        if (ngx_file_info(path[i]->name.data, &fi) == NGX_FILE_ERROR) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
                           ngx_file_info_n " \"%s\" failed", path[i]->name.data);
             return NGX_ERROR;
diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c
index e140ab6..91f8a5e 100644
--- a/src/core/ngx_resolver.c
+++ b/src/core/ngx_resolver.c
@@ -443,7 +443,7 @@
 
         name.data = ngx_resolver_alloc(r, name.len);
         if (name.data == NULL) {
-            return NGX_ERROR;
+            goto failed;
         }
 
         if (slen == ctx->service.len) {
@@ -481,6 +481,8 @@
         ngx_resolver_free(r, ctx->event);
     }
 
+failed:
+
     ngx_resolver_free(r, ctx);
 
     return NGX_ERROR;
@@ -744,6 +746,7 @@
             last->next = rn->waiting;
             rn->waiting = ctx;
             ctx->state = NGX_AGAIN;
+            ctx->async = 1;
 
             do {
                 ctx->node = rn;
@@ -890,6 +893,7 @@
     rn->waiting = ctx;
 
     ctx->state = NGX_AGAIN;
+    ctx->async = 1;
 
     do {
         ctx->node = rn;
@@ -1021,6 +1025,7 @@
             ctx->next = rn->waiting;
             rn->waiting = ctx;
             ctx->state = NGX_AGAIN;
+            ctx->async = 1;
             ctx->node = rn;
 
             /* unlock addr mutex */
@@ -1117,6 +1122,7 @@
     /* unlock addr mutex */
 
     ctx->state = NGX_AGAIN;
+    ctx->async = 1;
     ctx->node = rn;
 
     return NGX_OK;
@@ -3017,6 +3023,7 @@
     srv = cctx->srvs;
 
     ctx->count--;
+    ctx->async |= cctx->async;
 
     srv->ctx = NULL;
     srv->state = cctx->state;
diff --git a/src/core/ngx_resolver.h b/src/core/ngx_resolver.h
index a0d6fc3..6f099b7 100644
--- a/src/core/ngx_resolver.h
+++ b/src/core/ngx_resolver.h
@@ -218,7 +218,8 @@
     void                     *data;
     ngx_msec_t                timeout;
 
-    ngx_uint_t                quick;  /* unsigned  quick:1; */
+    unsigned                  quick:1;
+    unsigned                  async:1;
     ngx_uint_t                recursion;
     ngx_event_t              *event;
 };
diff --git a/src/core/ngx_rwlock.c b/src/core/ngx_rwlock.c
index f368941..00bb920 100644
--- a/src/core/ngx_rwlock.c
+++ b/src/core/ngx_rwlock.c
@@ -92,7 +92,7 @@
     readers = ngx_atomic_load(lock);
 
     if (readers == NGX_RWLOCK_WLOCK) {
-        ngx_atomic_store(lock, 0);
+        (void) ngx_atomic_cmp_set(lock, NGX_RWLOCK_WLOCK, 0);
         return;
     }
 
@@ -107,6 +107,19 @@
 }
 
 
+void
+ngx_rwlock_downgrade(ngx_atomic_t *lock)
+{
+    ngx_atomic_uint_t  readers;
+
+    readers = ngx_atomic_load(lock);
+
+    if (readers == NGX_RWLOCK_WLOCK) {
+        (void) ngx_atomic_cmp_set(lock, NGX_RWLOCK_WLOCK, 1);
+    }
+}
+
+
 #else
 
 #if (NGX_HTTP_UPSTREAM_ZONE || NGX_STREAM_UPSTREAM_ZONE || NGX_COMPAT)
diff --git a/src/core/ngx_rwlock.h b/src/core/ngx_rwlock.h
index 8b16eca..41b42aa 100644
--- a/src/core/ngx_rwlock.h
+++ b/src/core/ngx_rwlock.h
@@ -16,6 +16,7 @@
 void ngx_rwlock_wlock(ngx_atomic_t *lock);
 void ngx_rwlock_rlock(ngx_atomic_t *lock);
 void ngx_rwlock_unlock(ngx_atomic_t *lock);
+void ngx_rwlock_downgrade(ngx_atomic_t *lock);
 
 
 #endif /* _NGX_RWLOCK_H_INCLUDED_ */
diff --git a/src/http/modules/ngx_http_gzip_filter_module.c b/src/http/modules/ngx_http_gzip_filter_module.c
index 287fd36..73b6d89 100644
--- a/src/http/modules/ngx_http_gzip_filter_module.c
+++ b/src/http/modules/ngx_http_gzip_filter_module.c
@@ -1084,10 +1084,6 @@
     ngx_uint_t            zint, zfrac;
     ngx_http_gzip_ctx_t  *ctx;
 
-    v->valid = 1;
-    v->no_cacheable = 0;
-    v->not_found = 0;
-
     ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
 
     if (ctx == NULL || ctx->zout == 0) {
@@ -1095,6 +1091,10 @@
         return NGX_OK;
     }
 
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
     v->data = ngx_pnalloc(r->pool, NGX_INT32_LEN + 3);
     if (v->data == NULL) {
         return NGX_ERROR;
diff --git a/src/http/modules/ngx_http_headers_filter_module.c b/src/http/modules/ngx_http_headers_filter_module.c
index 29e2787..e5f1eb5 100644
--- a/src/http/modules/ngx_http_headers_filter_module.c
+++ b/src/http/modules/ngx_http_headers_filter_module.c
@@ -99,7 +99,7 @@
       ngx_http_headers_expires,
       NGX_HTTP_LOC_CONF_OFFSET,
       0,
-      NULL},
+      NULL },
 
     { ngx_string("add_header"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
diff --git a/src/http/modules/ngx_http_range_filter_module.c b/src/http/modules/ngx_http_range_filter_module.c
index 8ffca82..7ad9db9 100644
--- a/src/http/modules/ngx_http_range_filter_module.c
+++ b/src/http/modules/ngx_http_range_filter_module.c
@@ -382,6 +382,9 @@
             if (ranges-- == 0) {
                 return NGX_DECLINED;
             }
+
+        } else if (start == 0) {
+            return NGX_DECLINED;
         }
 
         if (*p++ != ',') {
diff --git a/src/http/modules/ngx_http_ssi_filter_module.c b/src/http/modules/ngx_http_ssi_filter_module.c
index 0fbd6e1..5ba1453 100644
--- a/src/http/modules/ngx_http_ssi_filter_module.c
+++ b/src/http/modules/ngx_http_ssi_filter_module.c
@@ -2389,7 +2389,7 @@
         ctx->timefmt.len = value->len;
         ctx->timefmt.data = ngx_pnalloc(r->pool, value->len + 1);
         if (ctx->timefmt.data == NULL) {
-            return NGX_HTTP_SSI_ERROR;
+            return NGX_ERROR;
         }
 
         ngx_cpystrn(ctx->timefmt.data, value->data, value->len + 1);
diff --git a/src/http/modules/ngx_http_userid_filter_module.c b/src/http/modules/ngx_http_userid_filter_module.c
index 0dbacba..a1a5493 100644
--- a/src/http/modules/ngx_http_userid_filter_module.c
+++ b/src/http/modules/ngx_http_userid_filter_module.c
@@ -472,6 +472,10 @@
 
         vv = ngx_http_get_indexed_variable(r, ngx_http_userid_reset_index);
 
+        if (vv == NULL || vv->not_found) {
+            return NGX_ERROR;
+        }
+
         if (vv->len == 0 || (vv->len == 1 && vv->data[0] == '0')) {
 
             if (conf->mark == '\0'
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 83f9016..7a179a4 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1353,6 +1353,7 @@
             r->uri.len = alias + path.len;
             r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
             if (r->uri.data == NULL) {
+                r->uri.len = 0;
                 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
                 return NGX_OK;
             }
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 9e99da8..66a241a 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -188,7 +188,9 @@
 #if (NGX_HTTP_SSL)
 static void ngx_http_upstream_ssl_init_connection(ngx_http_request_t *,
     ngx_http_upstream_t *u, ngx_connection_t *c);
-static void ngx_http_upstream_ssl_handshake(ngx_connection_t *c);
+static void ngx_http_upstream_ssl_handshake_handler(ngx_connection_t *c);
+static void ngx_http_upstream_ssl_handshake(ngx_http_request_t *,
+    ngx_http_upstream_t *u, ngx_connection_t *c);
 static ngx_int_t ngx_http_upstream_ssl_name(ngx_http_request_t *r,
     ngx_http_upstream_t *u, ngx_connection_t *c);
 #endif
@@ -1167,11 +1169,14 @@
 static void
 ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)
 {
+    ngx_uint_t                     run_posted;
     ngx_connection_t              *c;
     ngx_http_request_t            *r;
     ngx_http_upstream_t           *u;
     ngx_http_upstream_resolved_t  *ur;
 
+    run_posted = ctx->async;
+
     r = ctx->data;
     c = r->connection;
 
@@ -1235,7 +1240,9 @@
 
 failed:
 
-    ngx_http_run_posted_requests(c);
+    if (run_posted) {
+        ngx_http_run_posted_requests(c);
+    }
 }
 
 
@@ -1686,26 +1693,43 @@
             ngx_add_timer(c->write, u->conf->connect_timeout);
         }
 
-        c->ssl->handler = ngx_http_upstream_ssl_handshake;
+        c->ssl->handler = ngx_http_upstream_ssl_handshake_handler;
         return;
     }
 
-    ngx_http_upstream_ssl_handshake(c);
+    ngx_http_upstream_ssl_handshake(r, u, c);
 }
 
 
 static void
-ngx_http_upstream_ssl_handshake(ngx_connection_t *c)
+ngx_http_upstream_ssl_handshake_handler(ngx_connection_t *c)
 {
-    long                  rc;
     ngx_http_request_t   *r;
     ngx_http_upstream_t  *u;
 
     r = c->data;
+
     u = r->upstream;
+    c = r->connection;
 
     ngx_http_set_log_request(c->log, r);
 
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                   "http upstream ssl handshake: \"%V?%V\"",
+                   &r->uri, &r->args);
+
+    ngx_http_upstream_ssl_handshake(r, u, u->peer.connection);
+
+    ngx_http_run_posted_requests(c);
+}
+
+
+static void
+ngx_http_upstream_ssl_handshake(ngx_http_request_t *r, ngx_http_upstream_t *u,
+    ngx_connection_t *c)
+{
+    long  rc;
+
     if (c->ssl->handshaked) {
 
         if (u->conf->ssl_verify) {
@@ -1733,28 +1757,19 @@
         c->write->handler = ngx_http_upstream_handler;
         c->read->handler = ngx_http_upstream_handler;
 
-        c = r->connection;
-
         ngx_http_upstream_send_request(r, u, 1);
 
-        ngx_http_run_posted_requests(c);
         return;
     }
 
     if (c->write->timedout) {
-        c = r->connection;
         ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_TIMEOUT);
-        ngx_http_run_posted_requests(c);
         return;
     }
 
 failed:
 
-    c = r->connection;
-
     ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
-
-    ngx_http_run_posted_requests(c);
 }
 
 
@@ -2811,7 +2826,7 @@
     rev = c->read;
 
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
-                   "http upstream process body on memory");
+                   "http upstream process body in memory");
 
     if (rev->timedout) {
         ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
diff --git a/src/os/unix/ngx_udp_sendmsg_chain.c b/src/os/unix/ngx_udp_sendmsg_chain.c
index 5f1cfa5..5399c79 100644
--- a/src/os/unix/ngx_udp_sendmsg_chain.c
+++ b/src/os/unix/ngx_udp_sendmsg_chain.c
@@ -206,13 +206,13 @@
 #if (NGX_HAVE_MSGHDR_MSG_CONTROL)
 
 #if (NGX_HAVE_IP_SENDSRCADDR)
-    u_char             msg_control[CMSG_SPACE(sizeof(struct in_addr))];
+    u_char         msg_control[CMSG_SPACE(sizeof(struct in_addr))];
 #elif (NGX_HAVE_IP_PKTINFO)
-    u_char             msg_control[CMSG_SPACE(sizeof(struct in_pktinfo))];
+    u_char         msg_control[CMSG_SPACE(sizeof(struct in_pktinfo))];
 #endif
 
 #if (NGX_HAVE_INET6 && NGX_HAVE_IPV6_RECVPKTINFO)
-    u_char             msg_control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
+    u_char         msg_control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
 #endif
 
 #endif