예제 #1
0
파일: proxy.c 프로젝트: bowlofstew/h2o
static h2o_iovec_t rewrite_location(h2o_mem_pool_t *pool, const char *location, size_t location_len, h2o_proxy_location_t *upstream, h2o_iovec_t req_scheme, h2o_iovec_t req_authority, h2o_iovec_t req_basepath)
{
    h2o_iovec_t loc_scheme, loc_host, loc_path;
    uint16_t loc_port;

    if (h2o_parse_url(location, location_len, &loc_scheme, &loc_host, &loc_port, &loc_path) != 0
        || ! test_location_match(upstream, loc_scheme, loc_host, loc_port, loc_path))
        return h2o_iovec_init(location, location_len);

    return h2o_concat(pool,
        req_scheme,
        h2o_iovec_init(H2O_STRLIT("://")),
        req_authority,
        req_basepath,
        h2o_iovec_init(loc_path.base + upstream->path.len, loc_path.len - upstream->path.len));
}
예제 #2
0
static void start_request(h2o_http1client_ctx_t *ctx)
{
    char *scheme, *host, *path;
    uint16_t port;
    h2o_iovec_t *req;
    h2o_http1client_t *client;

    /* clear memory pool */
    h2o_mempool_clear(&pool);

    /* parse URL */
    if (h2o_parse_url(&pool, url, &scheme, &host, &port, &path) != 0) {
        fprintf(stderr, "unrecognized type of URL: %s\n", url);
        exit(1);
    }
    if (strcmp(scheme, "https") == 0) {
        fprintf(stderr, "https is not (yet) supported\n");
        exit(1);
    }
    /* build request */
    req = h2o_mempool_alloc(&pool, sizeof(*req));
    req->base = h2o_mempool_alloc(&pool, 1024);
    req->len = snprintf(req->base, 1024, "GET %s HTTP/1.1\r\nhost: %s:%u\r\n\r\n", path, host, (unsigned)port);
    assert(req->len < 1024);

    /* initiate the request */
    if (1) {
        if (sockpool == NULL) {
            sockpool = h2o_malloc(sizeof(*sockpool));
            h2o_socketpool_init(sockpool, host, port, 10);
            h2o_socketpool_set_timeout(sockpool, ctx->loop, 5000 /* in msec */);
        }
        client = h2o_http1client_connect_with_pool(ctx, &pool, sockpool, on_connect);
    } else {
        client = h2o_http1client_connect(ctx, &pool, host, port, on_connect);
    }
    assert(client != NULL);
    client->data = req;
}