Welcome! Log In Create A New Profile

Advanced

Re: How to parse post data in nginx module ?

March 16, 2012 10:30AM
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>



static char *ngx_http_helloworld(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void ngx_http_form_input_post_read(ngx_http_request_t *r);

typedef struct {
ngx_flag_t done : 1;
ngx_flag_t waiting_more_body : 1;
} ngx_http_helloworld_ctx_t;

static ngx_command_t ngx_http_helloworld_commands[] = {

{ ngx_string("helloworld"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF |
NGX_CONF_NOARGS,
ngx_http_helloworld,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL},

ngx_null_command
};




static u_char ngx_helloworld[] = "ABCD";


static ngx_http_module_t ngx_http_helloworld_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */

NULL, /* create main configuration */
NULL, /* init main configuration */

NULL, /* create server configuration */
NULL, /* merge server configuration */

NULL, /* create location configuration */
NULL /* merge location configuration */
};


ngx_module_t ngx_http_helloworld_module = {
NGX_MODULE_V1,
&ngx_http_helloworld_module_ctx, /* module context */
ngx_http_helloworld_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};


static ngx_str_t ngx_http_gif_type = ngx_string("text/html");

static ngx_int_t
ngx_http_helloworld_handler(ngx_http_request_t *r) {
ngx_http_complex_value_t cv;
char* response;



if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_POST))) {
return NGX_HTTP_NOT_ALLOWED;
}

ngx_memzero(&cv, sizeof (ngx_http_complex_value_t));
if (r->method == NGX_HTTP_POST) {
ngx_int_t rc = ngx_http_read_client_request_body(r,
ngx_http_form_input_post_read);
}

//do some processing here
return ngx_http_send_response(r, NGX_HTTP_OK, &ngx_http_gif_type, &cv);
}

static char *
ngx_http_helloworld(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_core_loc_conf_t *clcf;

clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_helloworld_handler;

return NGX_CONF_OK;
}

static void ngx_http_form_input_post_read(ngx_http_request_t *r) {
ngx_http_helloworld_input_ctx_t *ctx;

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http form_input post read request body");

r->read_event_handler = ngx_http_request_empty_handler;

ctx = ngx_http_get_module_ctx(r, ngx_http_helloworld_module);
ctx->done = 1;

#if defined(nginx_version) && nginx_version >= 8011

r->main->count--;
#endif




if (ctx->waiting_more_body) {
ctx->waiting_more_body = 0;

ngx_http_core_run_phases(r);
}
}


regards
Vivek Goel



On Fri, Mar 16, 2012 at 7:56 PM, vivek goel <goelvivek2011@gmail.com> wrote:

> I was tried to create hello world module to read post request but I am
> getting ctx as null value on line no 112
> ctx = ngx_http_get_module_ctx(r, ngx_http_helloworld_module);
>
> I am attaching the source code
>
> regards
> Vivek Goel
>
>
>
> On Sun, Feb 26, 2012 at 11:15 PM, vivek goel <goelvivek2011@gmail.com>wrote:
>
>> For reading get request I can use function ngx_http_form_input_arg but I
>> couldn't find any function by using which I can read post data in nginx ?
>> Is there any function in devel module or nginx api I can use to read post
>> data ?
>>
>> regards
>> Vivek Goel
>>
>>
>
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

How to parse post data in nginx module ?

vivek goel 6670 February 26, 2012 12:46PM

Re: How to parse post data in nginx module ?

kyprizel 2280 February 26, 2012 12:54PM

Re: How to parse post data in nginx module ?

goelviek2011@gmail.com 1889 March 16, 2012 10:28AM

Re: How to parse post data in nginx module ?

goelviek2011@gmail.com 3292 March 16, 2012 10:30AM

Re: How to parse post data in nginx module ?

Valentin V. Bartenev 1325 March 16, 2012 11:18AM

Re: How to parse post data in nginx module ?

goelviek2011@gmail.com 1864 March 17, 2012 12:52AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 137
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready