URI

uri

uri

struct evhttp_uri {
    unsigned flags;
    char *scheme; /* scheme; e.g http, ftp etc */
    char *userinfo; /* userinfo (typically username:pass), or NULL */
    char *host; /* hostname, IP address, or NULL */
    int port; /* port, or zero */
    char *path; /* path, or "". */
    char *query; /* query, or NULL */
    char *fragment; /* fragment or NULL */
};

libevent evhttp_uri_parse_with_flags

struct evhttp_uri *
evhttp_uri_parse_with_flags(const char *source_uri, unsigned flags)
{
    char *readbuf = NULL, *readp = NULL, *token = NULL, *query = NULL;
    char *path = NULL, *fragment = NULL;
    int got_authority = 0;

    struct evhttp_uri *uri = mm_calloc(1, sizeof(struct evhttp_uri));
    if (uri == NULL) {
        event_warn("%s: calloc", __func__);
        goto err;
    }
    uri->port = -1;
    uri->flags = flags;

    readbuf = mm_strdup(source_uri);
    if (readbuf == NULL) {
        event_warn("%s: strdup", __func__);
        goto err;
    }

    readp = readbuf;
    token = NULL;

    /* We try to follow RFC3986 here as much as we can, and match
       the productions

          URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

          relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
     */

    /* 1. scheme: */
    token = strchr(readp, ':');
    if (token && scheme_ok(readp,token)) {
        *token = '\0';
        uri->scheme = mm_strdup(readp);
        if (uri->scheme == NULL) {
            event_warn("%s: strdup", __func__);
            goto err;
        }
        readp = token+1; /* eat : */
    }

    /* 2. Optionally, "//" then an 'authority' part. */
    if (readp[0]=='/' && readp[1] == '/') {
        char *authority;
        readp += 2;
        authority = readp;
        path = end_of_authority(readp);
        if (parse_authority(uri, authority, path) < 0)
            goto err;
        readp = path;
        got_authority = 1;
    }

    /* 3. Query: path-abempty, path-absolute, path-rootless, or path-empty
     */
    path = readp;
    readp = end_of_path(path, PART_PATH, flags);

    /* Query */
    if (*readp == '?') {
        *readp = '\0';
        ++readp;
        query = readp;
        readp = end_of_path(readp, PART_QUERY, flags);
    }
    /* fragment */
    if (*readp == '#') {
        *readp = '\0';
        ++readp;
        fragment = readp;
        readp = end_of_path(readp, PART_FRAGMENT, flags);
    }
    if (*readp != '\0') {
        goto err;
    }

    /* These next two cases may be unreachable; I'm leaving them
     * in to be defensive. */
    /* If you didn't get an authority, the path can't begin with "//" */
    if (!got_authority && path[0]=='/' && path[1]=='/')
        goto err;
    /* If you did get an authority, the path must begin with "/" or be
     * empty. */
    if (got_authority && path[0] != '/' && path[0] != '\0')
        goto err;
    /* (End of maybe-unreachable cases) */

    /* If there was no scheme, the first part of the path (if any) must
     * have no colon in it. */
    if (! uri->scheme && !path_matches_noscheme(path))
        goto err;

    EVUTIL_ASSERT(path);
    uri->path = mm_strdup(path);
    if (uri->path == NULL) {
        event_warn("%s: strdup", __func__);
        goto err;
    }

    if (query) {
        uri->query = mm_strdup(query);
        if (uri->query == NULL) {
            event_warn("%s: strdup", __func__);
            goto err;
        }
    }
    if (fragment) {
        uri->fragment = mm_strdup(fragment);
        if (uri->fragment == NULL) {
            event_warn("%s: strdup", __func__);
            goto err;
        }
    }

    mm_free(readbuf);

    return uri;
err:
    if (uri)
        evhttp_uri_free(uri);
    if (readbuf)
        mm_free(readbuf);
    return NULL;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值