ngx_http_arg()函数详解

ngx_http_arg()可以解析url中的参数(key=value),

/*把请求中GET /download/nginx-1.9.2.rar?st=xhWL03HbtjrojpEAfiD6Mw&e=1452139931 HTTP/1.1的st和e形成变量$arg_st $arg_e,value分别
为xhWL03HbtjrojpEAfiD6Mw 1452139931即$arg_st=xhWL03HbtjrojpEAfiD6Mw,$arg_e=1452139931 */


ngx_int_t
ngx_http_arg(ngx_http_request_t *r, u_char *name, size_t len, ngx_str_t *value)
{
    u_char  *p, *last;

    if (r->args.len == 0) {           //args指向用户请求中的URL参数。没有参数,则返回
        return NGX_DECLINED;
    }

    p = r->args.data;             //p指向参数的第一位置
    last = p + r->args.len;    //last指向参数的最末位置

    for ( /* void */ ; p < last; p++) {       //循环遍历

        /* we need '=' after name, so drop one char from last */

        p = ngx_strlcasestrn(p, last - 1, name, len - 1);//第一,第二个参数是起始指针,第三个参数是要找的字串指针,第四个参数长度,len减一原因可以去看实现

        if (p == NULL) { //如果不存在字串情况
            return NGX_DECLINED;
        }

        if ((p == r->args.data || *(p - 1) == '&') && *(p + len) == '=') {

            value->data = p + len + 1; //value的data ,1是等号的存在

            p = ngx_strlchr(p, last, '&');//&符号出现的位置

            if (p == NULL) {
                p = r->args.data + r->args.len;
            }

            value->len = p - value->data;//value的长度

            return NGX_OK;
        }
    }

    return NGX_DECLINED;
}

--------------------------------------------------------------------------------------------------------------------------

/*
 * ngx_strlcasestrn() is intended to search for static substring
 * with known length in string until the argument last. The argument n
 * must be length of the second substring - 1.
 */

/*ngx_strlcasestrn()目的在于寻找已知长度的固定子串,指导参数last,参数n的长度必须是第二个字串减一

u_char *
ngx_strlcasestrn(u_char *s1, u_char *last, u_char *s2, size_t n)
{
    ngx_uint_t  c1, c2;

    c2 = (ngx_uint_t) *s2++;
    c2 = (c2 >= 'A' && c2 <= 'Z') ? (c2 | 0x20) : c2;
    last -= n;

    do {
        do {
            if (s1 >= last) {
                return NULL;
            }

            c1 = (ngx_uint_t) *s1++;

            c1 = (c1 >= 'A' && c1 <= 'Z') ? (c1 | 0x20) : c1;

        } while (c1 != c2);

    } while (ngx_strncasecmp(s1, s2, n) != 0);

    return --s1;

}

------------------------------------------------------------------------------------------------------------------------------------

//查找字符串s中首次出现字符c的位置
#define ngx_strchr(s1, c)   strchr((const char *) s1, (int) c)

static ngx_inline u_char *
ngx_strlchr(u_char *p, u_char *last, u_char c)
{
    while (p < last) {

        if (*p == c) {
            return p;
        }

        p++;
    }

    return NULL;
}

最后,举个离子

比如请求为https://1.1.1.1/url?username=abc&password=123456

解析的时候可以

ngx_str_t arg_username = ngx_string("username");

ngx_str_t username = ngx_null_string;

ngx_str_t arg_password = ngx_string("password");

ngx_str_t password = ngx_null_string;

/*username*/

if(NGX_OK !  = ngx_http_arg(r,arg_username.data,arg_username.len,&username) ){

    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,

                                "get arg \"%V\" failed.", &arg_username);

        return NGX_ERROR;

}

/*password*/

if(NGX_OK != ngx_http_arg(r,arg_password.data,arg_password.len,&password)) {

    ngx_log_error(NGX_LOG_ERR,r->connection->log,0,

                                "get arg \"%V\" failed.",& arg_password);

        return NGX_ERROR;

}

--------------------------------

 

也可以直接写

ngx_str_t * username;

ngx_str_t * password;

/*username*/

if(NGX_OK == ngx_http_arg(r,(u_char*)“username",8,username) ){

   。。。。。

}

/*password*/

if(NGX_OK ==  ngx_http_arg(r,(u_char*)"password",8,password)) {

   。。。。。

}

 

 

 

 

转载于:https://my.oschina.net/floristgao/blog/749845

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值