ngx_int_t
ngx_http_set_exten(ngx_http_request_t *r)
{
    ngx_int_t  i;
    r->exten.len = 0;
    r->exten.data = NULL;
    for (i = r->uri.len - 1; i > 1; i--) {
        if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
            r->exten.len = r->uri.len - i - 1;
            r->exten.data = &r->uri.data[i + 1];
            break;
        } else if (r->uri.data[i] == '/') {
            break;
        }
    }
    return NGX_OK;
}
['static void\nngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)\n{\n    ngx_http_request_t            *r;\n    ngx_http_upstream_resolved_t  *ur;\n    r = ctx->data;\n    r->upstream->resolved->ctx = NULL;\n    if (ctx->state) {\n        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n                      "%V could not be resolved (%i: %s)",\n                      &ctx->name, ctx->state,\n                      ngx_resolver_strerror(ctx->state));\n        ngx_resolve_name_done(ctx);\n        ngx_http_finalize_request(r, NGX_HTTP_BAD_GATEWAY);\n        return;\n    }\n    ur = r->upstream->resolved;\n    ur->naddrs = ctx->naddrs;\n    ur->addrs = ctx->addrs;\n#if (NGX_DEBUG)\n    {\n    in_addr_t   addr;\n    ngx_uint_t  i;\n    for (i = 0; i < ctx->naddrs; i++) {\n        addr = ntohl(ur->addrs[i]);\n        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n                       "name was resolved to %ud.%ud.%ud.%ud",\n                       (addr >> 24) & 0xff, (addr >> 16) & 0xff,\n                       (addr >> 8) & 0xff, addr & 0xff);\n    }\n    }\n#endif\n    if (ngx_http_upstream_create_round_robin_peer(r, ur) != NGX_OK) {\n        ngx_resolve_name_done(ctx);\n        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n        return;\n    }\n    ngx_resolve_name_done(ctx);\n    ngx_http_upstream_connect(r, r->upstream);\n}', 'static void\nngx_http_upstream_connect(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n    ngx_int_t          rc;\n    ngx_time_t        *tp;\n    ngx_connection_t  *c;\n    r->connection->log->action = "connecting to upstream";\n    r->connection->single_connection = 0;\n    if (u->state && u->state->response_sec) {\n        tp = ngx_timeofday();\n        u->state->response_sec = tp->sec - u->state->response_sec;\n        u->state->response_msec = tp->msec - u->state->response_msec;\n    }\n    u->state = ngx_array_push(r->upstream_states);\n    if (u->state == NULL) {\n        ngx_http_upstream_finalize_request(r, u,\n                                           NGX_HTTP_INTERNAL_SERVER_ERROR);\n        return;\n    }\n    ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));\n    tp = ngx_timeofday();\n    u->state->response_sec = tp->sec;\n    u->state->response_msec = tp->msec;\n    rc = ngx_event_connect_peer(&u->peer);\n    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n                   "http upstream connect: %i", rc);\n    if (rc == NGX_ERROR) {\n        ngx_http_upstream_finalize_request(r, u,\n                                           NGX_HTTP_INTERNAL_SERVER_ERROR);\n        return;\n    }\n    u->state->peer = u->peer.name;\n    if (rc == NGX_BUSY) {\n        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no live upstreams");\n        ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_NOLIVE);\n        return;\n    }\n    if (rc == NGX_DECLINED) {\n        ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);\n        return;\n    }\n    c = u->peer.connection;\n    c->data = r;\n    c->write->handler = ngx_http_upstream_handler;\n    c->read->handler = ngx_http_upstream_handler;\n    u->write_event_handler = ngx_http_upstream_send_request_handler;\n    u->read_event_handler = ngx_http_upstream_process_header;\n    c->sendfile &= r->connection->sendfile;\n    u->output.sendfile = c->sendfile;\n    c->pool = r->pool;\n    c->read->log = c->write->log = c->log = r->connection->log;\n    u->writer.out = NULL;\n    u->writer.last = &u->writer.out;\n    u->writer.connection = c;\n    u->writer.limit = 0;\n    if (u->request_sent) {\n        if (ngx_http_upstream_reinit(r, u) != NGX_OK) {\n            ngx_http_upstream_finalize_request(r, u,\n                                               NGX_HTTP_INTERNAL_SERVER_ERROR);\n            return;\n        }\n    }\n    if (r->request_body\n        && r->request_body->buf\n        && r->request_body->temp_file\n        && r == r->main)\n    {\n        u->output.free = ngx_alloc_chain_link(r->pool);\n        if (u->output.free == NULL) {\n            ngx_http_upstream_finalize_request(r, u,\n                                               NGX_HTTP_INTERNAL_SERVER_ERROR);\n            return;\n        }\n        u->output.free->buf = r->request_body->buf;\n        u->output.free->next = NULL;\n        u->output.allocated = 1;\n        r->request_body->buf->pos = r->request_body->buf->start;\n        r->request_body->buf->last = r->request_body->buf->start;\n        r->request_body->buf->tag = u->output.tag;\n    }\n    u->request_sent = 0;\n    if (rc == NGX_AGAIN) {\n        ngx_add_timer(c->write, u->conf->connect_timeout);\n        return;\n    }\n#if (NGX_HTTP_SSL)\n    if (u->ssl && c->ssl == NULL) {\n        ngx_http_upstream_ssl_init_connection(r, u, c);\n        return;\n    }\n#endif\n    ngx_http_upstream_send_request(r, u);\n}', 'static void\nngx_http_upstream_next(ngx_http_request_t *r, ngx_http_upstream_t *u,\n    ngx_uint_t ft_type)\n{\n    ngx_uint_t  status, state;\n    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n                   "http next upstream, %xi", ft_type);\n#if 0\n    ngx_http_busy_unlock(u->conf->busy_lock, &u->busy_lock);\n#endif\n    if (ft_type == NGX_HTTP_UPSTREAM_FT_HTTP_404) {\n        state = NGX_PEER_NEXT;\n    } else {\n        state = NGX_PEER_FAILED;\n    }\n    if (ft_type != NGX_HTTP_UPSTREAM_FT_NOLIVE) {\n        u->peer.free(&u->peer, u->peer.data, state);\n    }\n    if (ft_type == NGX_HTTP_UPSTREAM_FT_TIMEOUT) {\n        ngx_log_error(NGX_LOG_ERR, r->connection->log, NGX_ETIMEDOUT,\n                      "upstream timed out");\n    }\n    if (u->peer.cached && ft_type == NGX_HTTP_UPSTREAM_FT_ERROR) {\n        status = 0;\n    } else {\n        switch(ft_type) {\n        case NGX_HTTP_UPSTREAM_FT_TIMEOUT:\n            status = NGX_HTTP_GATEWAY_TIME_OUT;\n            break;\n        case NGX_HTTP_UPSTREAM_FT_HTTP_500:\n            status = NGX_HTTP_INTERNAL_SERVER_ERROR;\n            break;\n        case NGX_HTTP_UPSTREAM_FT_HTTP_404:\n            status = NGX_HTTP_NOT_FOUND;\n            break;\n        default:\n            status = NGX_HTTP_BAD_GATEWAY;\n        }\n    }\n    if (r->connection->error) {\n        ngx_http_upstream_finalize_request(r, u,\n                                           NGX_HTTP_CLIENT_CLOSED_REQUEST);\n        return;\n    }\n    if (status) {\n        u->state->status = status;\n        if (u->peer.tries == 0 || !(u->conf->next_upstream & ft_type)) {\n#if (NGX_HTTP_CACHE)\n            if (u->stale_cache && (u->conf->cache_use_stale & ft_type)) {\n                ngx_http_upstream_finalize_request(r, u,\n                                           ngx_http_upstream_cache_send(r, u));\n                return;\n            }\n#endif\n            ngx_http_upstream_finalize_request(r, u, status);\n            return;\n        }\n    }\n    if (u->peer.connection) {\n        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n                       "close http upstream connection: %d",\n                       u->peer.connection->fd);\n#if (NGX_HTTP_SSL)\n        if (u->peer.connection->ssl) {\n            u->peer.connection->ssl->no_wait_shutdown = 1;\n            u->peer.connection->ssl->no_send_shutdown = 1;\n            (void) ngx_ssl_shutdown(u->peer.connection);\n        }\n#endif\n        ngx_close_connection(u->peer.connection);\n    }\n#if 0\n    if (u->conf->busy_lock && !u->busy_locked) {\n        ngx_http_upstream_busy_lock(p);\n        return;\n    }\n#endif\n    ngx_http_upstream_connect(r, u);\n}', 'static ngx_int_t\nngx_http_upstream_cache_send(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n    ngx_int_t          rc;\n    ngx_http_cache_t  *c;\n    c = r->cache;\n    u->buffer = *c->buf;\n    u->buffer.pos += c->header_start;\n    ngx_memzero(&u->headers_in, sizeof(ngx_http_upstream_headers_in_t));\n    if (ngx_list_init(&u->headers_in.headers, r->pool, 8,\n                      sizeof(ngx_table_elt_t))\n        != NGX_OK)\n    {\n        return NGX_ERROR;\n    }\n    rc = u->process_header(r);\n    if (rc == NGX_OK) {\n        if (ngx_http_upstream_process_headers(r, u) != NGX_OK) {\n            return NGX_DONE;\n        }\n        return ngx_http_cache_send(r);\n    }\n    if (rc == NGX_ERROR) {\n        return NGX_ERROR;\n    }\n    return rc;\n}', 'static ngx_int_t\nngx_http_upstream_process_headers(ngx_http_request_t *r, ngx_http_upstream_t *u)\n{\n    ngx_str_t                      *uri, args;\n    ngx_uint_t                      i, flags;\n    ngx_list_part_t                *part;\n    ngx_table_elt_t                *h;\n    ngx_http_upstream_header_t     *hh;\n    ngx_http_upstream_main_conf_t  *umcf;\n    umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);\n    if (u->headers_in.x_accel_redirect) {\n        ngx_http_upstream_finalize_request(r, u, NGX_DECLINED);\n        part = &u->headers_in.headers.part;\n        h = part->elts;\n        for (i = 0;  ; i++) {\n            if (i >= part->nelts) {\n                if (part->next == NULL) {\n                    break;\n                }\n                part = part->next;\n                h = part->elts;\n                i = 0;\n            }\n            hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,\n                               h[i].lowcase_key, h[i].key.len);\n            if (hh && hh->redirect) {\n                if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {\n                    ngx_http_finalize_request(r,\n                                              NGX_HTTP_INTERNAL_SERVER_ERROR);\n                    return NGX_DONE;\n                }\n            }\n        }\n        uri = &u->headers_in.x_accel_redirect->value;\n        args.len = 0;\n        args.data = NULL;\n        flags = 0;\n        if (ngx_http_parse_unsafe_uri(r, uri, &args, &flags) != NGX_OK) {\n            ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);\n            return NGX_DONE;\n        }\n        if (flags & NGX_HTTP_ZERO_IN_URI) {\n            r->zero_in_uri = 1;\n        }\n        if (r->method != NGX_HTTP_HEAD) {\n            r->method = NGX_HTTP_GET;\n        }\n        r->valid_unparsed_uri = 0;\n        ngx_http_internal_redirect(r, uri, &args);\n        return NGX_DONE;\n    }\n    part = &u->headers_in.headers.part;\n    h = part->elts;\n    for (i = 0;  ; i++) {\n        if (i >= part->nelts) {\n            if (part->next == NULL) {\n                break;\n            }\n            part = part->next;\n            h = part->elts;\n            i = 0;\n        }\n        if (ngx_hash_find(&u->conf->hide_headers_hash, h[i].hash,\n                          h[i].lowcase_key, h[i].key.len))\n        {\n            continue;\n        }\n        hh = ngx_hash_find(&umcf->headers_in_hash, h[i].hash,\n                           h[i].lowcase_key, h[i].key.len);\n        if (hh) {\n            if (hh->copy_handler(r, &h[i], hh->conf) != NGX_OK) {\n                ngx_http_upstream_finalize_request(r, u,\n                                               NGX_HTTP_INTERNAL_SERVER_ERROR);\n                return NGX_DONE;\n            }\n            continue;\n        }\n        if (ngx_http_upstream_copy_header_line(r, &h[i], 0) != NGX_OK) {\n            ngx_http_upstream_finalize_request(r, u,\n                                               NGX_HTTP_INTERNAL_SERVER_ERROR);\n            return NGX_DONE;\n        }\n    }\n    if (r->headers_out.server && r->headers_out.server->value.data == NULL) {\n        r->headers_out.server->hash = 0;\n    }\n    if (r->headers_out.date && r->headers_out.date->value.data == NULL) {\n        r->headers_out.date->hash = 0;\n    }\n    r->headers_out.status = u->headers_in.status_n;\n    r->headers_out.status_line = u->headers_in.status_line;\n    u->headers_in.content_length_n = r->headers_out.content_length_n;\n    if (r->headers_out.content_length_n != -1) {\n        u->length = (size_t) r->headers_out.content_length_n;\n    } else {\n        u->length = NGX_MAX_SIZE_T_VALUE;\n    }\n    return NGX_OK;\n}', 'ngx_int_t\nngx_http_internal_redirect(ngx_http_request_t *r,\n    ngx_str_t *uri, ngx_str_t *args)\n{\n    ngx_http_core_srv_conf_t  *cscf;\n    r->uri_changes--;\n    if (r->uri_changes == 0) {\n        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,\n                      "rewrite or internal redirection cycle "\n                      "while internal redirect to \\"%V\\"", uri);\n        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n        return NGX_DONE;\n    }\n    r->uri = *uri;\n    if (args) {\n        r->args = *args;\n    } else {\n        r->args.len = 0;\n        r->args.data = NULL;\n    }\n    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,\n                   "internal redirect: \\"%V?%V\\"", uri, &r->args);\n    if (ngx_http_set_exten(r) != NGX_OK) {\n        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);\n        return NGX_DONE;\n    }\n    ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);\n    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);\n    r->loc_conf = cscf->ctx->loc_conf;\n    ngx_http_update_location_config(r);\n#if (NGX_HTTP_CACHE)\n    r->cache = NULL;\n#endif\n    r->internal = 1;\n    ngx_http_handler(r);\n    return NGX_DONE;\n}', "ngx_int_t\nngx_http_set_exten(ngx_http_request_t *r)\n{\n    ngx_int_t  i;\n    r->exten.len = 0;\n    r->exten.data = NULL;\n    for (i = r->uri.len - 1; i > 1; i--) {\n        if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {\n            r->exten.len = r->uri.len - i - 1;\n            r->exten.data = &r->uri.data[i + 1];\n            break;\n        } else if (r->uri.data[i] == '/') {\n            break;\n        }\n    }\n    return NGX_OK;\n}"]