Пример #1
0
static int
ngx_http_lua_delete_upstream(lua_State *L)
{
    ngx_int_t  status;
    ngx_str_t  name, rv;

    if (lua_gettop(L) != 1) {
        return luaL_error(L, "exactly 1 argument expected");
    }

    name.data = (u_char *) luaL_checklstring(L, 1, &name.len);

    status = ngx_dyups_delete_upstream(&name, &rv);

    lua_pushinteger(L, (lua_Integer) status);
    lua_pushlstring(L, (char *) rv.data, rv.len);

    return 2;
}
static ngx_http_dyups_srv_conf_t *
ngx_dyups_find_upstream(ngx_str_t *name, ngx_int_t *idx)
{
    ngx_uint_t                      i;
    ngx_http_dyups_srv_conf_t      *duscfs, *duscf, *duscf_del;
    ngx_http_dyups_main_conf_t     *dumcf;
    ngx_http_upstream_srv_conf_t   *uscf;
    ngx_http_upstream_main_conf_t  *umcf;

    umcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
                                               ngx_http_upstream_module);
    dumcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
                                                ngx_http_dyups_module);
    *idx = -1;
    duscf_del = NULL;

    duscfs = dumcf->dy_upstreams.elts;
    for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {

        duscf = &duscfs[i];
        if (!duscf->dynamic) {
            continue;
        }

        if (duscf->deleted == NGX_DYUPS_DELETING && *(duscf->count) == 0) {
            if (duscf->pool) {

                ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
                               "free dynamic upstream");

                ngx_destroy_pool(duscf->pool);
                duscf->pool = NULL;
            }

            duscf->deleted = NGX_DYUPS_DELETED;
        }

        if (duscf->deleted == NGX_DYUPS_DELETED) {
            *idx = i;
            duscf_del = duscf;
            continue;
        }

        uscf = duscf->upstream;

        if (uscf->host.len != name->len
            || ngx_strncasecmp(uscf->host.data, name->data, uscf->host.len)
               != 0)
        {
            continue;
        }

        if (duscf->count != NULL && *(duscf->count) != 0) {
            (void) ngx_dyups_delete_upstream(duscf);
            continue;
        }

        *idx = i;

        return duscf;
    }

    return duscf_del;
}
static ngx_int_t
ngx_http_dyups_do_delete(ngx_http_request_t *r, ngx_array_t *resource)
{
    ngx_str_t                  *value, name, rv;
    ngx_int_t                   dumy, status, rc;
    ngx_buf_t                  *b;
    ngx_chain_t                 out;
    ngx_http_dyups_srv_conf_t  *duscf;

    rc = ngx_http_discard_request_body(r);
    if (rc != NGX_OK) {
        return rc;
    }

    if (resource->nelts != 2) {
        ngx_str_set(&rv, "not support this interface");
        status = NGX_HTTP_NOT_ALLOWED;
        goto finish;
    }

    value = resource->elts;

    if (value[0].len == 8
        && ngx_strncasecmp(value[0].data, (u_char *) "upstream", 8) != 0)
    {
        ngx_str_set(&rv, "not support this api");
        status = NGX_HTTP_NOT_ALLOWED;
        goto finish;
    }

    name = value[1];

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "delete upstream name: %V", &name);

    duscf = ngx_dyups_find_upstream(&name, &dumy);

    if (duscf == NULL || duscf->deleted) {
        ngx_str_set(&rv, "not found uptream");
        status = NGX_HTTP_NOT_FOUND;
        goto finish;
    }

    rc = ngx_dyups_delete_upstream(duscf);
    if (rc != NGX_OK) {
        ngx_str_set(&rv, "failed");
        status = NGX_HTTP_INTERNAL_SERVER_ERROR;
        goto finish;
    }

    ngx_str_set(&rv, "success");
    status = NGX_HTTP_OK;

finish:

    r->headers_out.status = status;
    r->headers_out.content_length_n = rv.len;

    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK) {
        return rc;
    }

    if (rv.len == 0) {
        return ngx_http_send_special(r, NGX_HTTP_FLUSH);
    }

    b = ngx_create_temp_buf(r->pool, rv.len);
    if (b == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    b->pos = rv.data;
    b->last = rv.data + rv.len;
    b->last_buf = 1;

    out.buf = b;
    out.next = NULL;

    return ngx_http_output_filter(r, &out);
}