Skip to content

Commit

Permalink
Moving body data before headers for PHP POST.
Browse files Browse the repository at this point in the history
PHP SAPI tries to read body for POST request before registering
header-specific variables. For other methods, read_post_body() called by SAPI
after variables registration.

This closes #10 issue on GitHub.
  • Loading branch information
mar0x committed Sep 7, 2017
1 parent 789a101 commit d87a4fb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/nxt_php_sapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ nxt_php_read_request(nxt_task_t *task, nxt_app_rmsg_t *rmsg,
RC(nxt_app_msg_read_size(task, rmsg, &s));
h->parsed_content_length = s;

RC(nxt_app_msg_read_size(task, ctx->rmsg, &ctx->body_preread_size));

#undef NXT_READ
#undef RC

Expand Down Expand Up @@ -741,8 +743,6 @@ nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
NXT_PHP_SET(n.start, v);
}

nxt_app_msg_read_size(task, ctx->rmsg, &ctx->body_preread_size);

#undef NXT_PHP_SET
}

Expand Down
25 changes: 20 additions & 5 deletions src/nxt_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,7 @@ nxt_php_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
{
nxt_int_t rc;
nxt_buf_t *b;
nxt_bool_t method_is_post;
nxt_http_field_t *field;
nxt_app_request_header_t *h;

Expand Down Expand Up @@ -2916,6 +2917,20 @@ nxt_php_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
NXT_WRITE(&h->content_length);

RC(nxt_app_msg_write_size(task, wmsg, h->parsed_content_length));
RC(nxt_app_msg_write_size(task, wmsg, r->body.preread_size));

method_is_post = h->method.length == 4 &&
h->method.start[0] == 'P' &&
h->method.start[1] == 'O' &&
h->method.start[2] == 'S' &&
h->method.start[3] == 'T';

if (method_is_post) {
for(b = r->body.buf; b != NULL; b = b->next) {
RC(nxt_app_msg_write_raw(task, wmsg, b->mem.pos,
nxt_buf_mem_used_size(&b->mem)));
}
}

nxt_list_each(field, h->fields) {
RC(nxt_app_msg_write_prefixed_upcase(task, wmsg,
Expand All @@ -2927,11 +2942,11 @@ nxt_php_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
/* end-of-headers mark */
NXT_WRITE(&eof);

RC(nxt_app_msg_write_size(task, wmsg, r->body.preread_size));

for(b = r->body.buf; b != NULL; b = b->next) {
RC(nxt_app_msg_write_raw(task, wmsg, b->mem.pos,
nxt_buf_mem_used_size(&b->mem)));
if (!method_is_post) {
for(b = r->body.buf; b != NULL; b = b->next) {
RC(nxt_app_msg_write_raw(task, wmsg, b->mem.pos,
nxt_buf_mem_used_size(&b->mem)));
}
}

#undef NXT_WRITE
Expand Down

0 comments on commit d87a4fb

Please sign in to comment.