天天看點

nginx 編寫簡單HTTP子產品以及nginx http handler的hello world示例編寫

缂???nginx http handler妯″??浠ヤ究寮?????宸辨ā??锛???????渚?hello缂????扮?璇???璇??姝ラ??,???绔???????渚??翠釜绀轟?浠g??

缂???http handler妯″??????涓?缁????ㄥ??璁茶В锛?

1??ngx_command_t

绀轟?锛?

static ngx_command_t  ngx_http_mytest_commands[] =
{
    {
        ngx_string("mytest"),
        NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
        ngx_http_mytest,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        NULL
    },

    ngx_null_command
};      

??ginx.conf 涓?缂???????缃?椤?mytest ?ヨ?達? nginx 棣???浼???????????妯″??锛?modules锛?锛???瀵逛?姣?涓?妯″??锛? 浼?????浠???瀵瑰???ngx_command_t ?扮?锛? 璇??炬?懼?闆?充???浠?????缃?椤?ytest ??瑙f???瑰???

command涓??ㄤ?澶?????缃?椤瑰???扮??set ?規?锛??芥?闆???煎???娉?ngx_http_xxxxx锛?濡?涓???绀猴?

static char *
ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void{
    ngx_http_core_loc_conf_t  *clcf;

    //棣????懼??ytest??缃?椤規??灞?????缃???锛?clcf璨?浼兼??location???????版??
//缁???锛??跺??涓??訛?瀹???浠ユ??main??srv????loc绾у????缃?椤癸?涔?灏辨??璇村?ㄦ??涓?
//http{}??server{}??涔??芥??涓?涓?ngx_http_core_loc_conf_t缁???浣?
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

    //http妗??跺?ㄥ????ㄦ?瘋?鋒?杩?琛???GX_HTTP_CONTENT_PHASE?舵?墊?訛?濡???
//璇鋒???涓繪?哄??????URI涓?mytest??缃?椤規???ㄧ????缃????稿?歸??锛?灏卞?璋??ㄦ??浠?
//瀹??扮??ngx_http_mytest_handler?規?澶???杩?涓?璇鋒?
    clcf->handler = ngx_http_mytest_handler;

    return      

?充?ngx_http_conf_get_module_loc_conf ??瀹?涔???浠ュ????锛??????http://lxr.nginx.org/source/src/http/ngx_http_config.h#0065???????璐?? 灏辨??璁劇疆ngx_http_mytest_handler, ?歸??椤矽???涓????跺??锛? 搴?璇ュ?浣?瑙f????

ngx_command_t??瀹?涔?锛?浣?浜?src/core/ngx_conf_file.h涓???

struct ngx_command_s {
    ngx_str_t             name;
    ngx_uint_t            type;
    char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
    ngx_uint_t            conf;
    ngx_uint_t            offset;
    void                 *post;
};      

name:

type:

  • NGX_CONF_NOARGS锛???缃???浠や??ュ??浠諱????般??
  • NGX_CONF_TAKE1锛???缃???浠ゆ?ュ??1涓????般??
  • NGX_CONF_TAKE2锛???缃???浠ゆ?ュ??2涓????般??
  • NGX_CONF_TAKE3锛???缃???浠ゆ?ュ??3涓????般??
  • NGX_CONF_TAKE4锛???缃???浠ゆ?ュ??4涓????般??
  • NGX_CONF_TAKE5锛???缃???浠ゆ?ュ??5涓????般??
  • NGX_CONF_TAKE6锛???缃???浠ゆ?ュ??6涓????般??
  • NGX_CONF_TAKE7锛???缃???浠ゆ?ュ??7涓????般??

??浠ョ???澶?涓?灞??э?姣?濡?涓?涓???浠ゅ?沖??浠ヤ?濉????幫?涔???浠ユ?ュ??1涓?????2涓????般???d?灏辨??NGX_CONF_NOARGS|NGX_CONF_TAKE1|NGX_CONF_TAKE2??濡?????涓????涓?灞??у?ㄤ?璧鳳?浣?瑙?寰?楹葷????d?娌℃???崇郴锛?nginx??渚?浜?涓?浜?瀹?涔?锛?浣跨?ㄨ搗?ユ?寸??娲???

  • NGX_CONF_TAKE12锛???缃???浠ゆ?ュ??1涓?????2涓????般??
  • NGX_CONF_TAKE13锛???缃???浠ゆ?ュ??1涓?????3涓????般??
  • NGX_CONF_TAKE23锛???缃???浠ゆ?ュ??2涓?????3涓????般??
  • NGX_CONF_TAKE123锛???缃???浠ゆ?ュ??1涓?????2涓?????3???般??
  • NGX_CONF_TAKE1234锛???缃???浠ゆ?ュ??1涓?????2涓?????3涓?????4涓????般??
  • NGX_CONF_1MORE锛???缃???浠ゆ?ュ???沖?涓?涓????般??
  • NGX_CONF_2MORE锛???缃???浠ゆ?ュ???沖?涓や釜???般??
  • NGX_CONF_MULTI: ??缃???浠ゅ??浠ユ?ュ??澶?涓????幫??充釜?頒?瀹???
  • NGX_CONF_BLOCK锛???缃???浠ゅ??浠ユ?ュ?????兼??涓?涓???缃?淇℃??????涔?灏辨??涓?瀵瑰ぇ???鋒??璧鋒?ョ????瀹廣?????㈠??浠ュ??????寰?澶?????缃???浠ゃ??姣?濡?甯歌???server??浠ゅ氨??杩?涓?灞??х????
  • NGX_CONF_FLAG锛???缃???浠ゅ??浠ユ?ュ?????兼????on????????off??锛???缁?浼?琚?杞???bool?箋??
  • NGX_CONF_ANY锛???缃???浠ゅ??浠ユ?ュ????浠繪???????闆?箋??涓?涓?????澶?涓?锛???????on????????off??锛?????????缃????? ????瑕?璇存??????锛???璁哄?浣?锛?nginx????缃???浠ょ?????頒釜?頒???浠ヨ?杩?NGX_CONF_MAX_ARGS涓???????杩?涓??艱?瀹?涔?涓?锛?涔?灏辨??涓??借?杩?8涓????闆?箋??

涓????缁?涓?缁?璇存????缃???浠ゅ??浠ュ?虹?扮??浣?缃???灞??с??

  • NGX_DIRECT_CONF锛???浠ュ?虹?闆?ㄩ??缃???浠朵腑??澶?灞???渚?濡?宸茬???渚?????缃???浠?aemon锛?master_process绛???
  • NGX_MAIN_CONF: http??mail??events??error_log绛???
  • NGX_ANY_CONF: 璇ラ??缃???浠ゅ??浠ュ?虹?闆?ㄤ換????缃?绾у??涓??? 瀵逛???浠?缂?????澶у??版ā????瑷?锛??芥???ㄥ???http?稿?崇??浜???锛?涔?灏辨????璋????芥??NGX_HTTP_MODULE锛?瀵逛?杩??風被????妯″??锛??堕??缃????藉?虹?扮??浣?缃?涔?????涓虹?存?ュ?虹?闆??ttp?????浠ュ???朵?浣?缃???
  • NGX_HTTP_MAIN_CONF: ??浠ョ?存?ュ?虹?闆??ttp??缃???浠ら????
  • NGX_HTTP_SRV_CONF: ??浠ュ?虹?闆??ttp???㈢??server??缃???浠ら????
  • NGX_HTTP_LOC_CONF: ??浠ュ?虹?闆??ttp server?????㈢??location??缃???浠ら????
  • NGX_HTTP_UPS_CONF: ??浠ュ?虹?闆??ttp???㈢??upstream??缃???浠ら????
  • NGX_HTTP_SIF_CONF: ??浠ュ?虹?闆??ttp???㈢??server??缃???浠ら????if璇??ユ???ㄧ??block涓???
  • NGX_HTTP_LMT_CONF: ??浠ュ?虹?闆??ttp???㈢??limit_except??浠ょ??block涓???
  • NGX_HTTP_LIF_CONF: ??浠ュ?虹?闆??ttp server?????㈢??location??缃???浠ら????if璇??ユ???ㄧ??block涓???

set:

char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void      

2??瀹?涔?ngx_http_module_t ?ュ??/h2>

杩??ㄥ????浠g??锛? ???ㄤ?http妗??剁??锛? ?稿?浜?http妗??剁???????芥?幫? ?變?杩???骞朵???瑕?妗??跺??浠諱???浣?锛??煎?ngx_http_xxxxx_module_ctx??

static ngx_http_module_t  ngx_http_mytest_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 */      

3??ngx_module_t妯″????瀹?涔?

绀轟?锛???煎?ngx_http_xxxx_module

????瑕?绠?????璁劇疆3涓?椤圭??锛? ctx锛?????妯″????涓?涓???锛?锛? commands锛? type锛?妯″??绫誨??锛?

ngx_module_t  ngx_http_mytest_module =
{
    NGX_MODULE_V1,
    &ngx_http_mytest_module_ctx,           /* module context */
    ngx_http_mytest_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
};      
?ュ????涓?涓?娣卞?ョ??瑙?ginx涔?腑瀵?ttp妯″???版??缁???????瑙o?
瀹?涔? HTTP 妯″???瑰?寰?绠???锛?渚?濡?锛?
ngx_module_t ngx_http_mytest_module;
?朵腑锛?ngx_module_t ??涓?涓? Nginx 妯″?????版??缁???锛?璇?? 8.2 ??锛???涓??㈡?ュ????涓?涓?
Nginx 妯″??涓???????????锛?濡?涓???绀猴?
typedef struct ngx_module_s ngx_module_t;
struct ngx_module_s
/* 涓??㈢?? ctx_index??index??spare0??spare1??spare2??spare3??version ????涓???瑕??ㄥ??涔??惰??鹼?
??浠ョ??Nginx ??澶?濂界??瀹? NGX_MODULE_V1 ?ュ??涔?锛?瀹?宸茬?瀹?涔?濂戒?杩? 7 涓??箋??
#define NGX_MODULE_V1 0, 0, 0, 0, 0, 0, 1
瀵逛?涓?绫繪ā??锛??變??㈢?? type ?????沖??绫誨??锛???瑷?锛?ctx_index 琛ㄧず褰???妯″???ㄨ?绫繪ā??涓???搴??楓??杩?
涓?????甯稿父???辯?$??杩?绫繪ā????涓?涓? Nginx ?稿?妯″??璁劇疆??锛?瀵逛??????? HTTP 妯″????瑷?锛?ctx_index ???辨?稿?妯??? ngx_http_module 璁劇疆????ctx_index ??甯擱??瑕?锛?Nginx ??妯″????璁捐?¢??甯鎬?璧?浜???涓?妯″????椤哄?锛?瀹?浠??㈢??浜?琛ㄨ揪浼???绾э?涔??ㄤ?琛ㄦ??姣?涓?妯″????浣?缃?锛???浠ュ府??Nginx 妗??跺揩???峰???涓?妯″?????版??锛?HTTP 妗??惰?劇疆 ctx_
index ??杩?绋???瑙? 10.7 ??锛?*/
ngx_uint_t ctx_index;
/*index 琛ㄧず褰???妯″????ngx_modules ?扮?涓???搴??楓??娉ㄦ??锛?ctx_index 琛ㄧず????褰???妯″???ㄤ?绫繪ā
??涓???搴??鳳??? index 琛ㄧず褰???妯″???ㄦ????妯″??涓???搴??鳳?瀹????峰?抽????Nginx ???ㄦ?朵??規?? ngx_modules ?扮?
璁劇疆??妯″???? index ?箋??渚?濡?锛?
ngx_max_module = 0;
for (i = 0; ngx_modules[i]; i++) {
ngx_modules[i]->index = ngx_max_module++;
}
*/
ngx_uint_t index;
//spare 绯誨????淇???????锛?????浣跨??ngx_uint_t spare0;
ngx_uint_t spare1;
ngx_uint_t spare2;
ngx_uint_t spare3;
// 妯″????????锛?渚誇?灏??ョ???╁???????????涓?绉?锛?榛?璁や負 1
ngx_uint_t version;
/*ctx ?ㄤ?????涓?绫繪ā????涓?涓???缁???浣?锛?涓轟?涔???瑕? ctx ?????涓哄???㈣?磋?锛?Nginx 妯″????璁稿?绉?绫夥?
涓???绫繪ā??涔??寸?????藉樊??寰?澶с??渚?濡?锛?浜?浠剁被????妯″??涓昏?澶??? I/O 浜?浠剁?稿?崇?????斤?HTTP 绫誨????妯″??涓昏?澶???
HTTP 搴??ㄥ??????姐??杩??鳳?姣?涓?妯″???芥??浜???宸辯???規?э??? ctx 灏?浼??????瑰??绫誨??妯″???????辨?ュ?c??渚?濡?锛???HTTP
妯″??涓?锛?ctx ??瑕????? ngx_http_module_t 缁???浣? */
void *ctx;
//commands 灏?澶??? nginx.conf 涓?????缃?椤癸?璇??绗? 4 绔?
ngx_command_t *commands;
/*type 琛ㄧず璇ユā????绫誨??锛?瀹?涓? ctx ??????绱у???稿?崇?????ㄥ????Nginx 涓?锛?瀹??????艱???存??浠ヤ? 5 绉? 锛?
NGX_HTTP_MODULE??NGX_CORE_MODULE??NGX_CONF_MODULE??NGX_EVENT_MODULE??NGX_MAIL_MODULE??杩?
5 绉?妯″???寸???崇郴??????8-2??瀹???涓?锛?杩???浠ヨ??瀹?涔??扮??妯″??绫誨?? */
ngx_uint_t type;
/* ??Nginx ?????ㄣ????姝㈣?绋?涓?锛?浠ヤ? 7 涓??芥?版????琛ㄧず?? 7 涓??ц??逛?????璋??ㄨ? 7 绉??規?锛???瑙?
8.4 ??锝? 8.6 ??锛???瀵逛?浠諱?涓??規???瑷?锛?濡???涓???瑕? Nginx ?ㄦ??涓??跺?繪?ц?瀹?锛??d?绠????版??瀹?璁句負 NULL 绌烘????
?沖?? */
/* ?界?朵?瀛??????瑙e?褰???master 杩?绋????ㄦ?跺??璋? init_master锛?浣??扮????涓烘???妗??朵唬??浠??ヤ?浼?
璋??ㄥ??锛???姝わ???灏? init_master 璁句負 NULL */
ngx_int_t (*init_master)(ngx_log_t *log);
/*init_module ??璋??規??ㄥ??濮???????妯″???惰?璋??ㄣ????master/worker 妯″?涓?锛?杩?涓??舵?靛??ㄥ????worker 瀛?杩?绋???瀹??? */
ngx_int_t (*init_module)(ngx_cycle_t *cycle);
/* init_process ??璋??規??ㄦ?e父???″??琚?璋??ㄣ????master/worker 妯″?涓?锛?澶?涓? worker 瀛?杩?绋?宸茬?浜???锛??ㄦ??涓? worker 杩?绋?????濮???杩?绋?浼?璋??ㄦ????妯″???? init_process ?芥??*/
ngx_int_t (*init_process)(ngx_cycle_t *cycle);
/* ?變? Nginx ??涓?????澶?绾跨?妯″?锛???浠?init_thread ?ㄦ??朵唬??涓?娌℃??琚?璋??ㄨ?锛?璁句負 NULL*/
ngx_int_t (*init_thread)(ngx_cycle_t *cycle);
// ??涓?锛?exit_thread 涔?涓?????锛?璁句負 NULL
void (*exit_thread)(ngx_cycle_t *cycle);
/* exit_process ??璋??規??ㄦ???″??姝㈠??璋??ㄣ????master/worker 妯″?涓?锛?worker 杩?绋?浼??ㄩ???哄??璋??ㄥ?? */
void (*exit_process)(ngx_cycle_t *cycle);
// exit_master ??璋??規?灏???master 杩?绋????哄??琚?璋???void (*exit_master)(ngx_cycle_t *cycle);
/* 浠ヤ? 8 涓? spare_hook ????涔???淇???瀛?娈碉?????娌℃??浣跨???浣?????Nginx ??渚??? NGX_MODULE_V1_
PADDING 瀹??ュ~??????涓?涓?璇ュ????瀹?涔?锛?#define NGX_MODULE_V1_PADDING 0, 0, 0, 0, 0, 0, 0, 0*/
uintptr_t spare_hook0;
uintptr_t spare_hook1;
uintptr_t spare_hook2;
绗? 3 绔? 寮???涓?涓?绠????? HTTP 妯″?? 87
uintptr_t spare_hook3;
uintptr_t spare_hook4;
uintptr_t spare_hook5;
uintptr_t spare_hook6;
uintptr_t      
  1. 瀹?涔?涓?涓? HTTP 妯″???訛??″??? type 瀛?娈佃?句負 NGX_HTTP_MODULE??
  2. 瀵逛?涓?????璋??規? 锛?init_module??init_process??exit_process??exit_master锛?璋??ㄥ??浠??? ?? Nginx ??妗??朵唬?????㈠?ヨ??璇達?杩? 4 涓???璋??規?涓? HTTP 妗??舵???籌??充嬌 nginx.conf 涓?娌?????缃? http {...} 杩?绉?寮??? HTTP ???界????缃?椤癸?杩?浜???璋??規?浠??朵?琚?璋??ㄣ????姝わ???甯?寮??? HTTP 妯″???堕?芥??瀹?浠?璁句負 NULL 绌烘??????杩??鳳?褰? Nginx 涓?浣?涓?Web ???″?ㄤ嬌?ㄦ?訛? 涓?浼??ц? HTTP 妯″????浠諱?浠g????
  3. 瀹?涔? HTTP 妯″???訛?????瑕?????瑕?璁劇疆 ctx ?? commands 杩?涓や釜??????瀵逛? HTTP 绫誨?? ??妯″???ヨ?達?ngx_module_t 涓??? ctx ????蹇?椤繪???? ngx_http_module_t ?ュ?o?HTTP 妗??剁??瑕? 姹?锛???涓??㈠???ュ???? ngx_http_module_t 缁???浣?????????
  4. HTTP 妗??跺?ㄨ?誨??????杞介??缃???浠舵?跺??涔?浜???ngx_http_module_t ?ュ?f??杩扮?? 8 涓??舵?碉? HTTP 妗??跺?ㄥ???ㄨ?绋?涓?浼??ㄦ??涓??舵?典腑璋???ngx_http_module_t 涓??稿????規???褰??訛?濡??? ngx_http_module_t 涓?????涓???璋??規?璁句負 NULL 绌烘????锛??d? HTTP 妗??舵??涓?浼?璋??ㄥ??????

4.缂???config??浠訛?缂?璇?浠g?????跺????/h2>

config??浠跺??缂???c??浠舵?懼?ㄥ??涓?涓???浠朵?锛?姣?濡????懼???

[root@hadoop2 nginx-1.13.8]# pwd
/root/nginx-1.13.8
[root@hadoop2 nginx-1.13.8]# cd mytesthttp/
[root@hadoop2 mytesthttp]# ll
total 16
-rw-r--r-- 1 root root  163 Jan 18 09:56 config
-rw-r--r-- 1 root root 8915 Jan 18 15:52 ngx_http_mytest_module.c
[root@hadoop2 mytesthttp]#      
?朵腑config??浠跺??瀹瑰?烘??涓??㈢??褰㈠?锛?
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"      

$ngx_addon_dir 杩?涓??兼???ц?configure ?跺??娣誨??????绾?-add-module??渚???锛?--add-module=/root/nginx-1.13.8/mytesthttp

?ц?nginx??褰?涓?configure????makefile锛?--prefix=/root/nginx-1.13.8/bin??渚?nginx??瀹?瑁?璺?寰?锛???渚胯?劇疆??浣???宸辯???沖??瑁??扮????褰??芥????浠ョ????

./configure \
  --prefix=/root/nginx-1.13.8/bin  \
  --user=root\
  --group=root\
  --add-module=/root/nginx-1.13.8/mytesthttp  --with-http_stub_status_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --with-http_gunzip_module \
  --with-pcre \
  --with-debug      

5.??缃?nginx??缃???浠?ginx.conf??涓???nginx??conf??浠跺?ㄥ??瑁?璺?寰???褰?涓?锛?--prefix??渚????d釜璺?寰?锛?锛?

server {
        listen       8017;
        server_name  localhost;

        access_log  logs/get.log  main;
        #access_log off;

        location / 
        {
            root   html;
            index  index.html index.htm;
        }

        location /hello  
        {
    
            mytest;
        }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html 
       {
            root   html;
       }
    }      
http://192.168.0.153:8017/hello/mytest      
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

#define PB_SIZE (1024 * 2)
#define CONTENT_TYPE "application/json;charset=GB2312"


static char* ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);

static void ngx_http_read_client_request_body_handler(ngx_http_request_t *r);

static ngx_command_t  ngx_http_mytest_commands[] =
{

    {
        ngx_string("mytest"),
        NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
        ngx_http_mytest,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        NULL
    },

    ngx_null_command
};

static ngx_http_module_t  ngx_http_mytest_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_mytest_module =
{
    NGX_MODULE_V1,
    &ngx_http_mytest_module_ctx,           /* module context */
    ngx_http_mytest_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 char *ngx_http_mytest(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_mytest_handler;

    return NGX_CONF_OK;
}

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
    static char uri[PB_SIZE];
    static char decode[PB_SIZE];
    static char args[PB_SIZE];
    
    char* src;
    char* dst;
    int status=NGX_HTTP_OK;
    //int reply_len=0;
    //char *reply=0;
    ngx_int_t     rc;
    ngx_chain_t   out;
    
    //post handle
    if ((r->method & (NGX_HTTP_POST|NGX_HTTP_HEAD))) 
    {
        //get body
        rc = ngx_http_read_client_request_body(r, ngx_http_read_client_request_body_handler);
        if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
            return rc;
        }
        return NGX_DONE;
    }
    //get handle
    else if ((r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) 
    {
        //get uri
        if (r->uri.len>=PB_SIZE)
            return NGX_HTTP_NOT_ALLOWED;    
        ngx_memcpy(uri,r->uri.data,r->uri.len);
        uri[r->uri.len]=0;
        src = uri;
        dst = decode;
        ngx_unescape_uri((u_char**)&dst, (u_char**)&src, r->uri.len, 0);
        ngx_memcpy(uri,decode,dst - decode);
        uri[dst - decode] = '\0';
    
        //get args
        if (r->args.len>=PB_SIZE)
            return NGX_HTTP_NOT_ALLOWED;
        ngx_memcpy(args,r->args.data,r->args.len);
        args[r->args.len]=0;
        src = args;
        dst =decode;
        ngx_unescape_uri((u_char**)&dst, (u_char**)&src, r->args.len, 0);
        ngx_memcpy(args,decode,dst - decode);
        args[dst - decode] = '\0';

        //reply=request(uri,args,&status,&reply_len);
        ngx_str_t response = ngx_string("Hello World!");

        if (status!=NGX_HTTP_OK)
        {
            return status;
        }
         
        ngx_str_t type =ngx_string(CONTENT_TYPE);
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_type = type;
        //r->headers_out.content_length_n = reply_len;
        r->headers_out.content_length_n = response.len;

        ngx_buf_t  *b = ngx_create_temp_buf(r->pool, response.len);
        if(b == NULL)
        {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
    
        ngx_memcpy(b->pos, response.data, response.len);
        b->last = b->pos+response.len;
        b->last_buf = 1;
    
        out.buf = b;
        out.next = NULL;
    
        rc = ngx_http_send_header(r);
        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only)
        {
            return rc;
        }

        return ngx_http_output_filter(r, &out); 
    }
    else
    {
        return NGX_HTTP_NOT_ALLOWED;
    }   
}

static void ngx_http_read_client_request_body_handler(ngx_http_request_t *r)
{
    static char uri[PB_SIZE];
    static char decode[PB_SIZE];
    char* body = NULL;
    int body_size = 0;
    char* src;
    char* dst;
    //char *reply=0;
    int status=NGX_HTTP_OK;
    //int reply_len=0;
    ngx_int_t     rc;
    ngx_chain_t   out;
    
    ngx_chain_t* bufs = r->request_body->bufs;
    ngx_buf_t* buf = NULL;
    uint8_t* data_buf = NULL;
    size_t content_length = 0;
    size_t body_length = 0;
    
    //get uri
    if (r->uri.len>=PB_SIZE)
    {
        ngx_http_finalize_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);
        return;
    }   
    ngx_memcpy(uri,r->uri.data,r->uri.len);
    uri[r->uri.len]=0;
    src = uri;
    dst = decode;
    ngx_unescape_uri((u_char**)&dst, (u_char**)&src, r->uri.len, 0);
    ngx_memcpy(uri,decode,dst - decode);
    uri[dst - decode] = '\0';
        
    //get body
    if (r->request_body == NULL)
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "reqeust_body:null");
        ngx_http_finalize_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);
        return;
    } 
    if ( r->headers_in.content_length == NULL )
    {   
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "r->headers_in.content_length == NULL");
        ngx_http_finalize_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);
        return;
    }
    
    content_length = atoi( (char*)(r->headers_in.content_length->value.data) );
    data_buf = ( uint8_t* )ngx_palloc( r->pool , content_length + 1 );
    size_t buf_length = 0;
    while ( bufs )
    {
        buf = bufs->buf;
        bufs = bufs->next;
        buf_length = buf->last - buf->pos ;
        if( body_length + buf_length > content_length )
        {
            memcpy( data_buf + body_length, buf->pos, content_length - body_length);
            body_length = content_length ;
            break;
        }
        memcpy( data_buf + body_length, buf->pos, buf->last - buf->pos );
        body_length += buf->last - buf->pos;
    }
    if ( body_length )
    {
        data_buf[body_length] = 0;
    }
    body = (char *)data_buf;
    body_size = body_length;
    
    //int sequence = getSequence(r);

    //reply = mypost(uri, body, body_size,sequence,&status, &reply_len);
    //?芒?茂mypost?盲?碌戮????漏?鹿麓婁?鉚post?謾陸祿鹵鉚碌樓?媒戮?body攏盧驢???擄?麓忙碌陸db???廬??碌??盲?沒???芒?碌?帽??錄顱
    ngx_str_t response = ngx_string("Hello World!");
    if(status != NGX_HTTP_OK)
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Post failed.");
        ngx_http_finalize_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);
        return;
    }
    
    ngx_str_t type =ngx_string(CONTENT_TYPE);
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_type = type;
    //r->headers_out.content_length_n = reply_len;
    r->headers_out.content_length_n = response.len;
    
    ngx_buf_t  *b = ngx_create_temp_buf(r->pool, response.len);
    if(b == NULL)
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
        ngx_http_finalize_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);
        return;
    }
    
    ngx_memcpy(b->pos, response.data, response.len);
    b->last = b->pos+response.len;
    b->last_buf = 1;
    
    out.buf = b;
    out.next = NULL;
    
    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) 
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to do ngx_http_send_header.");
        ngx_http_finalize_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);
        return ;
    }
    
    ngx_http_finalize_request(r,ngx_http_output_filter(r, &out));   
    
    return;
}      

繼續閱讀