Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better error handling.

  • Loading branch information...
commit 7821f793ac83bc9a6bd80a29eb60c435b7ab13f0 1 parent eac796e
Yichun Zhang authored
112 src/chunked_parser.c
View
@@ -25,6 +25,7 @@ static const int chunked_en_main = 1;
#line 15 "src/chunked_parser.rl"
+
ngx_int_t
ngx_http_chunkin_init_chunked_parser(ngx_http_request_t *r,
ngx_http_chunkin_ctx_t *ctx)
@@ -32,12 +33,12 @@ ngx_http_chunkin_init_chunked_parser(ngx_http_request_t *r,
int cs;
-#line 36 "src/chunked_parser.c"
+#line 37 "src/chunked_parser.c"
{
cs = chunked_start;
}
-#line 23 "src/chunked_parser.rl"
+#line 24 "src/chunked_parser.rl"
ctx->chunks = NULL;
ctx->next_chunk = NULL;
@@ -53,6 +54,7 @@ ngx_http_chunkin_init_chunked_parser(ngx_http_request_t *r,
return NGX_OK;
}
+
ngx_int_t
ngx_http_chunkin_run_chunked_parser(ngx_http_request_t *r,
ngx_http_chunkin_ctx_t *ctx, u_char **pos_addr, u_char *last, char *caller_info)
@@ -70,11 +72,11 @@ ngx_http_chunkin_run_chunked_parser(ngx_http_request_t *r,
ngx_str_t user_agent = ngx_string("");
-#line 175 "src/chunked_parser.rl"
+#line 177 "src/chunked_parser.rl"
-#line 78 "src/chunked_parser.c"
+#line 80 "src/chunked_parser.c"
{
int _widec;
if ( p == pe )
@@ -103,59 +105,59 @@ case 1:
goto tr3;
goto st0;
tr4:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 167 "src/chunked_parser.rl"
+#line 169 "src/chunked_parser.rl"
{ err_ctx = "last_chunk"; }
goto st0;
tr10:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 167 "src/chunked_parser.rl"
+#line 169 "src/chunked_parser.rl"
{ err_ctx = "last_chunk"; }
-#line 170 "src/chunked_parser.rl"
+#line 172 "src/chunked_parser.rl"
{ err_ctx = "parser"; }
goto st0;
tr12:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 170 "src/chunked_parser.rl"
+#line 172 "src/chunked_parser.rl"
{ err_ctx = "parser"; }
goto st0;
tr14:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 163 "src/chunked_parser.rl"
+#line 165 "src/chunked_parser.rl"
{ err_ctx = "chunk_size"; }
goto st0;
tr18:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 163 "src/chunked_parser.rl"
+#line 165 "src/chunked_parser.rl"
{ err_ctx = "chunk_size"; }
-#line 151 "src/chunked_parser.rl"
+#line 153 "src/chunked_parser.rl"
{ err_ctx = "chunk_data"; }
goto st0;
tr20:
-#line 151 "src/chunked_parser.rl"
+#line 153 "src/chunked_parser.rl"
{ err_ctx = "chunk_data"; }
goto st0;
tr23:
-#line 155 "src/chunked_parser.rl"
+#line 157 "src/chunked_parser.rl"
{ err_ctx = "chunk_data_terminator"; }
goto st0;
-#line 148 "src/chunked_parser.c"
+#line 150 "src/chunked_parser.c"
st0:
cs = 0;
goto _out;
tr2:
-#line 78 "src/chunked_parser.rl"
+#line 80 "src/chunked_parser.rl"
{
ctx->chunk_bytes_read = 0;
ctx->chunk_size = 0;
ctx->chunk_size_order = 0;
}
-#line 84 "src/chunked_parser.rl"
+#line 86 "src/chunked_parser.rl"
{
ctx->chunk_size <<= 4;
ctx->chunk_size_order++;
@@ -172,7 +174,7 @@ cs = 0;
}
goto st2;
tr7:
-#line 84 "src/chunked_parser.rl"
+#line 86 "src/chunked_parser.rl"
{
ctx->chunk_size <<= 4;
ctx->chunk_size_order++;
@@ -192,7 +194,7 @@ cs = 0;
if ( ++p == pe )
goto _test_eof2;
case 2:
-#line 196 "src/chunked_parser.c"
+#line 198 "src/chunked_parser.c"
switch( (*p) ) {
case 13: goto st3;
case 32: goto st6;
@@ -229,7 +231,7 @@ case 5:
goto tr13;
goto tr12;
tr13:
-#line 57 "src/chunked_parser.rl"
+#line 59 "src/chunked_parser.rl"
{
done = 1;
}
@@ -238,7 +240,7 @@ case 5:
if ( ++p == pe )
goto _test_eof14;
case 14:
-#line 242 "src/chunked_parser.c"
+#line 244 "src/chunked_parser.c"
goto tr12;
st6:
if ( ++p == pe )
@@ -250,13 +252,13 @@ case 6:
}
goto tr4;
tr3:
-#line 78 "src/chunked_parser.rl"
+#line 80 "src/chunked_parser.rl"
{
ctx->chunk_bytes_read = 0;
ctx->chunk_size = 0;
ctx->chunk_size_order = 0;
}
-#line 84 "src/chunked_parser.rl"
+#line 86 "src/chunked_parser.rl"
{
ctx->chunk_size <<= 4;
ctx->chunk_size_order++;
@@ -273,7 +275,7 @@ case 6:
}
goto st7;
tr8:
-#line 84 "src/chunked_parser.rl"
+#line 86 "src/chunked_parser.rl"
{
ctx->chunk_size <<= 4;
ctx->chunk_size_order++;
@@ -293,7 +295,7 @@ case 6:
if ( ++p == pe )
goto _test_eof7;
case 7:
-#line 297 "src/chunked_parser.c"
+#line 299 "src/chunked_parser.c"
switch( (*p) ) {
case 9: goto st8;
case 13: goto st9;
@@ -332,7 +334,7 @@ case 10:
_widec = (*p);
_widec = (int)(32768 + ((*p) - -32768));
if (
-#line 61 "src/chunked_parser.rl"
+#line 63 "src/chunked_parser.rl"
ctx->chunk_bytes_read < ctx->chunk_size
) _widec += 65536;
@@ -340,7 +342,7 @@ case 10:
goto tr19;
goto tr18;
tr19:
-#line 99 "src/chunked_parser.rl"
+#line 101 "src/chunked_parser.rl"
{
ctx->chunk = ngx_http_chunkin_get_buf(r->pool, ctx);
@@ -357,7 +359,7 @@ case 10:
b->last = b->pos = p;
b->memory = 1;
}
-#line 65 "src/chunked_parser.rl"
+#line 67 "src/chunked_parser.rl"
{
ctx->chunk_bytes_read++;
@@ -372,7 +374,7 @@ case 10:
}
goto st11;
tr22:
-#line 65 "src/chunked_parser.rl"
+#line 67 "src/chunked_parser.rl"
{
ctx->chunk_bytes_read++;
@@ -390,13 +392,13 @@ case 10:
if ( ++p == pe )
goto _test_eof11;
case 11:
-#line 394 "src/chunked_parser.c"
+#line 396 "src/chunked_parser.c"
_widec = (*p);
if ( (*p) < 13 ) {
if ( (*p) <= 12 ) {
_widec = (int)(32768 + ((*p) - -32768));
if (
-#line 61 "src/chunked_parser.rl"
+#line 63 "src/chunked_parser.rl"
ctx->chunk_bytes_read < ctx->chunk_size
) _widec += 65536;
@@ -405,7 +407,7 @@ case 11:
if ( 14 <= (*p) )
{ _widec = (int)(32768 + ((*p) - -32768));
if (
-#line 61 "src/chunked_parser.rl"
+#line 63 "src/chunked_parser.rl"
ctx->chunk_bytes_read < ctx->chunk_size
) _widec += 65536;
@@ -413,7 +415,7 @@ case 11:
} else {
_widec = (int)(32768 + ((*p) - -32768));
if (
-#line 61 "src/chunked_parser.rl"
+#line 63 "src/chunked_parser.rl"
ctx->chunk_bytes_read < ctx->chunk_size
) _widec += 65536;
@@ -431,7 +433,7 @@ case 12:
goto tr24;
goto tr23;
tr24:
-#line 116 "src/chunked_parser.rl"
+#line 118 "src/chunked_parser.rl"
{
if (ctx->chunk_bytes_read != ctx->chunk_size) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
@@ -462,7 +464,7 @@ case 12:
if ( ++p == pe )
goto _test_eof13;
case 13:
-#line 466 "src/chunked_parser.c"
+#line 468 "src/chunked_parser.c"
if ( (*p) == 48 )
goto tr2;
if ( (*p) < 65 ) {
@@ -495,60 +497,60 @@ case 13:
{
switch ( cs ) {
case 11:
-#line 151 "src/chunked_parser.rl"
+#line 153 "src/chunked_parser.rl"
{ err_ctx = "chunk_data"; }
break;
case 12:
case 13:
-#line 155 "src/chunked_parser.rl"
+#line 157 "src/chunked_parser.rl"
{ err_ctx = "chunk_data_terminator"; }
break;
case 7:
case 8:
case 9:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 163 "src/chunked_parser.rl"
+#line 165 "src/chunked_parser.rl"
{ err_ctx = "chunk_size"; }
break;
case 2:
case 3:
case 6:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 167 "src/chunked_parser.rl"
+#line 169 "src/chunked_parser.rl"
{ err_ctx = "last_chunk"; }
break;
case 5:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 170 "src/chunked_parser.rl"
+#line 172 "src/chunked_parser.rl"
{ err_ctx = "parser"; }
break;
case 10:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 163 "src/chunked_parser.rl"
+#line 165 "src/chunked_parser.rl"
{ err_ctx = "chunk_size"; }
-#line 151 "src/chunked_parser.rl"
+#line 153 "src/chunked_parser.rl"
{ err_ctx = "chunk_data"; }
break;
case 4:
-#line 142 "src/chunked_parser.rl"
+#line 144 "src/chunked_parser.rl"
{ err_ctx = "CRLF"; }
-#line 167 "src/chunked_parser.rl"
+#line 169 "src/chunked_parser.rl"
{ err_ctx = "last_chunk"; }
-#line 170 "src/chunked_parser.rl"
+#line 172 "src/chunked_parser.rl"
{ err_ctx = "parser"; }
break;
-#line 545 "src/chunked_parser.c"
+#line 547 "src/chunked_parser.c"
}
}
_out: {}
}
-#line 178 "src/chunked_parser.rl"
+#line 180 "src/chunked_parser.rl"
ctx->parser_state = cs;
17 src/ngx_http_chunkin_filter_module.c
View
@@ -211,6 +211,8 @@ ngx_http_chunkin_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
if (last) {
dd("ignore last body...");
+ ctx->ignore_body = 0;
+
r->discard_body = 0;
r->error_page = 0;
r->err_status = 0;
@@ -230,12 +232,23 @@ ngx_http_chunkin_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
r->headers_in.transfer_encoding->value.data = (u_char*) "";
}
+ /*
+ r->method = NGX_HTTP_PUT;
+ r->headers_in.content_length = NULL;
+ r->headers_in.content_length_n = -1;
+ */
+
rc = ngx_http_chunkin_process_request_header(r);
if (rc != NGX_OK) {
- return NGX_OK;
+ return rc;
}
- ctx->ignore_body = 0;
+ /* r->plain_http = 1; */
+
+ rc = ngx_http_chunkin_process_request(r);
+ if (rc != NGX_OK) {
+ return rc;
+ }
return ngx_http_chunkin_restart_request(r);
}
33 src/ngx_http_chunkin_util.c
View
@@ -1,4 +1,4 @@
-#define DDEBUG 0
+#define DDEBUG 1
#include "ddebug.h"
@@ -169,10 +169,12 @@ ngx_http_chunkin_restart_request(ngx_http_request_t *r)
ngx_int_t
ngx_http_chunkin_process_request_header(ngx_http_request_t *r)
{
+ dd("entered process_request_header");
+
if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent HTTP/1.1 request without \"Host\" header");
- return NGX_ERROR;
+ return NGX_HTTP_BAD_REQUEST;
}
if (r->headers_in.content_length) {
@@ -183,21 +185,24 @@ ngx_http_chunkin_process_request_header(ngx_http_request_t *r)
if (r->headers_in.content_length_n == NGX_ERROR) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent invalid \"Content-Length\" header");
- return NGX_ERROR;
+ return NGX_HTTP_LENGTH_REQUIRED;
}
}
+ dd("method: %d (%d)", (int)r->method, (int)NGX_HTTP_PUT);
+ dd("content_length_n: %d (%d)", (int)r->headers_in.content_length_n, -1);
+
if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent %V method without \"Content-Length\" header",
&r->method_name);
- return NGX_ERROR;
+ return NGX_HTTP_LENGTH_REQUIRED;
}
if (r->method & NGX_HTTP_TRACE) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent TRACE method");
- return NGX_ERROR;
+ return NGX_HTTP_NOT_ALLOWED;
}
if (r->headers_in.transfer_encoding
@@ -206,7 +211,7 @@ ngx_http_chunkin_process_request_header(ngx_http_request_t *r)
{
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent \"Transfer-Encoding: chunked\" header");
- return NGX_ERROR;
+ return NGX_HTTP_LENGTH_REQUIRED;
}
if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
@@ -221,7 +226,7 @@ ngx_http_chunkin_process_request_header(ngx_http_request_t *r)
}
-void
+ngx_int_t
ngx_http_chunkin_process_request(ngx_http_request_t *r)
{
ngx_connection_t *c;
@@ -231,8 +236,8 @@ ngx_http_chunkin_process_request(ngx_http_request_t *r)
if (r->plain_http) {
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"client sent plain HTTP request to HTTPS port");
- ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
- return;
+ /* ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS); */
+ return NGX_HTTP_TO_HTTPS;
}
#if (NGX_HTTP_SSL)
@@ -255,8 +260,8 @@ ngx_http_chunkin_process_request(ngx_http_request_t *r)
ngx_ssl_remove_cached_session(sscf->ssl.ctx,
(SSL_get0_session(c->ssl->connection)));
- ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
- return;
+ /* ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR); */
+ return NGX_HTTPS_CERT_ERROR;
}
if (sscf->verify == 1) {
@@ -269,8 +274,8 @@ ngx_http_chunkin_process_request(ngx_http_request_t *r)
ngx_ssl_remove_cached_session(sscf->ssl.ctx,
(SSL_get0_session(c->ssl->connection)));
- ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
- return;
+ /* ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT); */
+ return NGX_HTTPS_NO_CERT;
}
X509_free(cert);
@@ -290,5 +295,7 @@ ngx_http_chunkin_process_request(ngx_http_request_t *r)
(void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
r->stat_writing = 1;
#endif
+
+ return NGX_OK;
}
2  src/ngx_http_chunkin_util.h
View
@@ -15,7 +15,7 @@ ngx_int_t ngx_http_chunkin_restart_request(ngx_http_request_t *r);
ngx_int_t ngx_http_chunkin_process_request_header(ngx_http_request_t *r);
-void ngx_http_chunkin_process_request(ngx_http_request_t *r);
+ngx_int_t ngx_http_chunkin_process_request(ngx_http_request_t *r);
#endif /* NGX_HTTP_CHUNKIN_UTIL_H */
Please sign in to comment.
Something went wrong with that request. Please try again.