Nginx 函数解析之ngx_http_get_forwarded_addr_internal

static ngx_int_t
ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,
    u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)
{
    u_char      *p;
    ngx_int_t    rc;
    ngx_addr_t   paddr;

    //传入addr与过滤列表对比,如果列表中存在匹配,返回NGX_OK,不匹配返回NGX_DECLINED
    if (ngx_cidr_match(addr->sockaddr, proxies) != NGX_OK) {
        return NGX_DECLINED;
    }

    //从指定长度的xff的最右边开始往左查找,截取最后的ip
    for (p = xff + xfflen - 1; p > xff; p--, xfflen--) {
        if (*p != ' ' && *p != ',') {
            break;
        }
    }

    //去除截取到的ip左边的分割符号
    for ( /* void */ ; p > xff; p--) {
        if (*p == ' ' || *p == ',') {
            p++;
            break;
        }
    }

    //ip字符串解析ip和端口,存放到paddr
    if (ngx_parse_addr_port(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {
        return NGX_DECLINED;
    }

    //覆盖先前的addr
    *addr = paddr;

    //是否递归且未解析完,递归处理
    if (recursive && p > xff) {
        rc = ngx_http_get_forwarded_addr_internal(r, addr, xff, p - 1 - xff,
                                                  proxies, 1);

        if (rc == NGX_DECLINED) {
            return NGX_DONE;
        }

        /* rc == NGX_OK || rc == NGX_DONE  */
        return rc;
    }

    return NGX_OK;
}

  

posted @ 2017-06-29 14:56  御風ゝ  阅读(415)  评论(0编辑  收藏  举报