create ssl buffer on demand and free it before keep-alive
diff --git a/src/event/ngx_event_openssl.c b/src/event/ngx_event_openssl.c
index cc43e39..d116209 100644
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -344,14 +344,7 @@
return NGX_ERROR;
}
- if (flags & NGX_SSL_BUFFER) {
- sc->buffer = 1;
-
- sc->buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
- if (sc->buf == NULL) {
- return NGX_ERROR;
- }
- }
+ sc->buffer = ((flags & NGX_SSL_BUFFER) != 0);
sc->connection = SSL_new(ssl->ctx);
@@ -804,8 +797,28 @@
limit = NGX_MAX_UINT32_VALUE - ngx_pagesize;
}
-
buf = c->ssl->buf;
+
+ if (buf == NULL) {
+ buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE);
+ if (buf == NULL) {
+ return NGX_CHAIN_ERROR;
+ }
+
+ c->ssl->buf = buf;
+ }
+
+ if (buf->start == NULL) {
+ buf->start = ngx_palloc(c->pool, NGX_SSL_BUFSIZE);
+ if (buf->start == NULL) {
+ return NGX_CHAIN_ERROR;
+ }
+
+ buf->pos = buf->start;
+ buf->last = buf->start;
+ buf->end = buf->start + NGX_SSL_BUFSIZE;
+ }
+
send = 0;
flush = (in == NULL) ? 1 : 0;
@@ -980,6 +993,15 @@
}
+void
+ngx_ssl_free_buffer(ngx_connection_t *c)
+{
+ if (ngx_pfree(c->pool, c->ssl->buf->start) == NGX_OK) {
+ c->ssl->buf->start = NULL;
+ }
+}
+
+
ngx_int_t
ngx_ssl_shutdown(ngx_connection_t *c)
{
diff --git a/src/event/ngx_event_openssl.h b/src/event/ngx_event_openssl.h
index 7592ff2..e027dde 100644
--- a/src/event/ngx_event_openssl.h
+++ b/src/event/ngx_event_openssl.h
@@ -132,6 +132,7 @@
ssize_t ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl);
ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
off_t limit);
+void ngx_ssl_free_buffer(ngx_connection_t *c);
ngx_int_t ngx_ssl_shutdown(ngx_connection_t *c);
void ngx_cdecl ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
char *fmt, ...);
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 060de03..b2c8733 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2112,6 +2112,12 @@
hc->nbusy = 0;
}
+#if (NGX_HTTP_SSL)
+ if (c->ssl) {
+ ngx_ssl_free_buffer(c);
+ }
+#endif
+
rev->handler = ngx_http_keepalive_handler;
if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {