Skip to content

Commit

Permalink
Stream: the $status variable.
Browse files Browse the repository at this point in the history
The stream session status is one of the following:

200 - normal completion
403 - access forbidden
500 - internal server error
502 - bad gateway
503 - limit conn
  • Loading branch information
arut committed Aug 11, 2016
1 parent ea9a1d7 commit be6024f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 57 deletions.
11 changes: 10 additions & 1 deletion src/stream/ngx_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ typedef struct ngx_stream_session_s ngx_stream_session_t;
#include <ngx_stream_upstream_round_robin.h>


#define NGX_STREAM_OK 200
#define NGX_STREAM_FORBIDDEN 403
#define NGX_STREAM_INTERNAL_SERVER_ERROR 500
#define NGX_STREAM_BAD_GATEWAY 502
#define NGX_STREAM_SERVICE_UNAVAILABLE 503


typedef struct {
void **main_conf;
void **srv_conf;
Expand Down Expand Up @@ -173,6 +180,8 @@ struct ngx_stream_session_s {
int *captures;
u_char *captures_data;
#endif

ngx_uint_t status;
};


Expand Down Expand Up @@ -223,7 +232,7 @@ typedef struct {


void ngx_stream_init_connection(ngx_connection_t *c);
void ngx_stream_close_connection(ngx_connection_t *c);
void ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc);


extern ngx_module_t ngx_stream_module;
Expand Down
46 changes: 34 additions & 12 deletions src/stream/ngx_stream_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <ngx_stream.h>


static void ngx_stream_close_connection(ngx_connection_t *c);
static u_char *ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len);
static void ngx_stream_init_session(ngx_connection_t *c);

Expand Down Expand Up @@ -166,17 +167,27 @@ ngx_stream_init_connection(ngx_connection_t *c)
if (cmcf->limit_conn_handler) {
rc = cmcf->limit_conn_handler(s);

if (rc != NGX_DECLINED) {
ngx_stream_close_connection(c);
if (rc == NGX_ERROR) {
ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

if (rc == NGX_ABORT) {
ngx_stream_finalize_session(s, NGX_STREAM_SERVICE_UNAVAILABLE);
return;
}
}

if (cmcf->access_handler) {
rc = cmcf->access_handler(s);

if (rc != NGX_OK && rc != NGX_DECLINED) {
ngx_stream_close_connection(c);
if (rc == NGX_ERROR) {
ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

if (rc == NGX_ABORT) {
ngx_stream_finalize_session(s, NGX_STREAM_FORBIDDEN);
return;
}
}
Expand All @@ -194,7 +205,7 @@ ngx_stream_init_connection(ngx_connection_t *c)
{
ngx_connection_error(c, ngx_socket_errno,
"setsockopt(TCP_NODELAY) failed");
ngx_stream_close_connection(c);
ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

Expand All @@ -215,7 +226,7 @@ ngx_stream_init_connection(ngx_connection_t *c)
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"no \"ssl_certificate\" is defined "
"in server listening on SSL port");
ngx_stream_close_connection(c);
ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

Expand All @@ -242,7 +253,7 @@ ngx_stream_init_session(ngx_connection_t *c)

s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_stream_max_module);
if (s->ctx == NULL) {
ngx_stream_close_connection(c);
ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

Expand All @@ -258,15 +269,14 @@ ngx_stream_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
ngx_stream_session_t *s;
ngx_stream_ssl_conf_t *sslcf;

s = c->data;

if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
ngx_stream_close_connection(c);
ngx_stream_finalize_session(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

if (ngx_ssl_handshake(c) == NGX_AGAIN) {

s = c->data;

sslcf = ngx_stream_get_module_srv_conf(s, ngx_stream_ssl_module);

ngx_add_timer(c->read, sslcf->handshake_timeout);
Expand All @@ -284,7 +294,7 @@ static void
ngx_stream_ssl_handshake_handler(ngx_connection_t *c)
{
if (!c->ssl->handshaked) {
ngx_stream_close_connection(c);
ngx_stream_finalize_session(c->data, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}

Expand All @@ -299,6 +309,18 @@ ngx_stream_ssl_handshake_handler(ngx_connection_t *c)


void
ngx_stream_finalize_session(ngx_stream_session_t *s, ngx_uint_t rc)
{
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"finalize stream session: %i", rc);

s->status = rc;

ngx_stream_close_connection(s->connection);
}


static void
ngx_stream_close_connection(ngx_connection_t *c)
{
ngx_pool_t *pool;
Expand Down
Loading

0 comments on commit be6024f

Please sign in to comment.