static ngx_int_t 
ngx_http_status_gethandler(ngx_http_request_t *r, 
            ngx_http_variable_value_t *v, uintptr_t data)
{
    ngx_http_advertise_ctx_t          *ctx;
    ngx_http_variable_value_t         *vv;
    ngx_http_advertise_conf_t         *accf;
    
    accf = ngx_http_get_module_loc_conf(r, ngx_http_advertise_module);

    vv = ngx_http_get_indexed_variable(r, (ngx_uint_t)accf->index);
    if (vv == NULL) {
        return NGX_ERROR;
    }

    ctx = (ngx_http_advertise_ctx_t*)(vv->data);
    if (ctx == NULL) {
        return NGX_ERROR;
    }
    v->len = ngx_strlen(ad_status[ctx->status]);
    v->data = ad_status[ctx->status];
    
    v->valid = 1;
    v->not_found = 0;
    v->no_cacheable = 0;
    return NGX_OK;
}
static ngx_int_t
ngx_http_hsjson_test_config(ngx_http_request_t *r, ngx_http_hsjson_loc_conf_t  *lcf,ngx_http_hsjson_ctx_t *ctx )
{
    if (!lcf->hs_command.len) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed coniguration. The hs_json_fieldlist is undefined.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if (!lcf->tablename.len) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed coniguration. The hs_json_table is undefined.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if (!lcf->dbname.len) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed coniguration. The hs_json_db is undefined.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ctx->hs_request = ngx_http_get_indexed_variable(r,lcf->variable_index);
    if ( !ctx->hs_request->len ) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed coniguration. The variable $hs_rquest is undefined.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    return NGX_OK;
}
static ngx_int_t
ngx_http_reqstat_init_handler(ngx_http_request_t *r)
{
    ngx_http_reqstat_conf_t      *rmcf, *rlcf;
    ngx_http_reqstat_store_t     *store;

    store = ngx_http_get_module_ctx(r, ngx_http_reqstat_module);
    rmcf = ngx_http_get_module_main_conf(r, ngx_http_reqstat_module);
    rlcf = ngx_http_get_module_loc_conf(r, ngx_http_reqstat_module);

    if (store == NULL) {
        if (r->variables[rmcf->index].valid) {
            return NGX_DECLINED;
        }

        store = ngx_http_reqstat_create_store(r, rlcf);
        if (store == NULL) {
            return NGX_ERROR;
        }

        ngx_http_set_ctx(r, store, ngx_http_reqstat_module);
    }

    if (ngx_http_get_indexed_variable(r, rmcf->index) == NULL) {
        return NGX_ERROR;
    }

    return NGX_DECLINED;
}
static ngx_int_t 
ngx_http_sample_ids_variable(ngx_http_request_t *r, 
    ngx_http_variable_value_t *v, uintptr_t data)
{
    ngx_http_sample_loc_conf_t *slcf = NULL;
    ngx_http_variable_value_t *sample_ids_var_value = NULL;


    slcf = ngx_http_get_module_loc_conf(r, ngx_http_sample_module);
    if (slcf == NULL) {
    	v->not_found = 1;
        return NGX_OK;
    }

    sample_ids_var_value = ngx_http_get_indexed_variable(r, slcf->sample_ids_var_index);
    if (sample_ids_var_value == NULL) {
    	v->not_found = 1;
    	return NGX_OK;
    }

    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;

    v->data = sample_ids_var_value->data;
    v->len = sample_ids_var_value->len;

    return NGX_OK;
}
static ngx_int_t 
ngx_http_sample_handler(ngx_http_request_t *r) 
{
    ngx_http_sample_loc_conf_t *slcf = NULL;
    ngx_http_variable_value_t *sample_ids_var_value = NULL;
    ngx_str_t sample_ids = ngx_string("456_789_123");
    ngx_int_t sample_ids_var_index;


    slcf = ngx_http_get_module_loc_conf(r, ngx_http_sample_module);
    if (slcf->sample_enable == NGX_CONF_UNSET) {
        ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "sample module is not enable!");
        return NGX_DECLINED;
    }

    sample_ids_var_value = ngx_http_get_indexed_variable(r, slcf->sample_ids_var_index);
    if (sample_ids_var_value == NULL) {
    	return NGX_ERROR;
    }

    sample_ids_var_value->data = sample_ids.data;
    sample_ids_var_value->len = sample_ids.len;
    sample_ids_var_value->valid = 1;
    sample_ids_var_value->no_cacheable = 0;
    sample_ids_var_value->not_found = 0;
    	
    return NGX_DECLINED;
}
static void
ngx_http_replace_script_copy_var_code(ngx_http_replace_script_engine_t *e)
{
    u_char                              *p;
    ngx_http_variable_value_t           *value;
    ngx_http_replace_script_var_code_t  *code;

    code = (ngx_http_replace_script_var_code_t *) e->ip;

    e->ip += sizeof(ngx_http_replace_script_var_code_t);

    if (!e->skip) {

        value = ngx_http_get_indexed_variable(e->request, code->index);

        if (value && !value->not_found) {
            p = e->pos;
            e->pos = ngx_copy(p, value->data, value->len);

            ngx_log_debug2(NGX_LOG_DEBUG_HTTP,
                           e->request->connection->log, 0,
                           "http replace script var: \"%*s\"", e->pos - p, p);
        }
    }
}
static ngx_str_t *
ngx_http_push_stream_get_channel_id(ngx_http_request_t *r, ngx_http_push_stream_loc_conf_t *cf)
{
    ngx_http_variable_value_t      *vv = ngx_http_get_indexed_variable(r, cf->index_channel_id);
    ngx_str_t                      *id;

    if (vv == NULL || vv->not_found || vv->len == 0) {
        return NGX_HTTP_PUSH_STREAM_UNSET_CHANNEL_ID;
    }

    // maximum length limiter for channel id
    if ((ngx_http_push_stream_module_main_conf->max_channel_id_length != NGX_CONF_UNSET_UINT) && (vv->len > ngx_http_push_stream_module_main_conf->max_channel_id_length)) {
        ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, "push stream module: channel id is larger than allowed %d", vv->len);
        return NGX_HTTP_PUSH_STREAM_TOO_LARGE_CHANNEL_ID;
    }

    if ((id = ngx_http_push_stream_create_str(r->pool, vv->len)) == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for $push_stream_channel_id string");
        return NULL;
    }

    ngx_memcpy(id->data, vv->data, vv->len);

    return id;
}
ngx_int_t
ngx_http_drizzle_set_thread_id_variable(ngx_http_request_t *r,
        ngx_http_upstream_t *u)
{
    ngx_http_drizzle_loc_conf_t    *dlcf;
    size_t                          size;
    ngx_http_variable_value_t      *vv;
    uint32_t                        tid;
    drizzle_con_st                 *dc;

    ngx_http_upstream_drizzle_peer_data_t       *dp;

    dp = r->upstream->peer.data;
    if (dp == NULL) {
        return NGX_ERROR;
    }

    dc = dp->drizzle_con;
    if (dc == NULL) {
        return NGX_ERROR;
    }

    tid = drizzle_con_thread_id(dc);

    dd("tid = %d", (int) tid);

    if (tid == 0) {
        /* invalid thread id */
        return NGX_OK;
    }

    size = ngx_http_drizzle_get_num_size(tid);

    dlcf = ngx_http_get_module_loc_conf(r, ngx_http_drizzle_module);

    vv = ngx_http_get_indexed_variable(r, dlcf->tid_var_index);

    if (vv == NULL) {
        return NGX_ERROR;
    }

    vv->not_found = 0;
    vv->valid = 1;
    vv->no_cacheable = 0;

    vv->data = ngx_palloc(r->pool, size);
    if (vv->data == NULL) {
        return NGX_ERROR;
    }

    vv->len = size;

    ngx_sprintf(vv->data, "%uD", tid);

    dd("$drizzle_thread_id set");

    return NGX_OK;
}
static ngx_int_t
ngx_http_memcached_hash_create_request(ngx_http_request_t *r)
{
    size_t                          len;
    ngx_buf_t                      *b;
    ngx_chain_t                    *cl;
    ngx_http_memcached_hash_ctx_t       *ctx;
    ngx_http_variable_value_t      *vv;
    ngx_http_memcached_hash_loc_conf_t  *mlcf;

    mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_hash_module);

    vv = ngx_http_get_indexed_variable(r, mlcf->index);

    if (vv == NULL || vv->not_found || vv->len == 0) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "the \"$memcached_hash_key\" variable is not set");
        return NGX_ERROR;
    }

    len = sizeof("get ") - 1 + 64 + sizeof(CRLF) - 1;

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

    cl = ngx_alloc_chain_link(r->pool);
    if (cl == NULL) {
        return NGX_ERROR;
    }

    cl->buf = b;
    cl->next = NULL;

    r->upstream->request_bufs = cl;

    *b->last++ = 'g'; *b->last++ = 'e'; *b->last++ = 't'; *b->last++ = ' ';

    ctx = ngx_http_get_module_ctx(r, ngx_http_memcached_hash_module);

    ctx->key.data = b->last;

    b->last = ngx_http_memcached_hash_sha256_hash(b->last, vv->data, vv->len);

    ctx->key.len = b->last - ctx->key.data;

    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "http memcached hash request: \"%v\" \"%V\"", vv, &ctx->key);

    *b->last++ = CR; *b->last++ = LF;

    return NGX_OK;
}
static ngx_int_t
ngx_http_zm_sso_redirect (ngx_http_request_t *r)
{
    ngx_http_zm_sso_loc_conf_t   *zlcf;
    ngx_http_variable_value_t    *host_var;
    ngx_str_t                     host_value;

    zlcf = ngx_http_get_module_loc_conf(r, ngx_http_zm_sso_module);
    ngx_table_elt_t * location = ngx_list_push(&r->headers_out.headers);
    if (location == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ngx_str_set(&location->key, "Location");

    //redirect to https://<redirect_url>/?ignoreLoginURL=1
    if (zlcf->redirect_url->wildcard) {
        //url contain only port, just use $host
        host_var =
                ngx_http_get_indexed_variable(r, zlcf->host_index);
        host_value.data = host_var->data;
        host_value.len = host_var->len;

        location->value.len = zlcf->redirect_schema.len +
                              3 +     /* for "://" */
                              host_var->len +
                              1 +     /* for ":" */
                              zlcf->redirect_url->port_text.len +
                              sizeof ("/?ignoreLoginURL=1") - 1;

        location->value.data = ngx_palloc(r->pool, location->value.len);
        ngx_sprintf(location->value.data, "%V://%V:%V/?ignoreLoginURL=1",
                &zlcf->redirect_schema,
                &host_value,
                &zlcf->redirect_url->port_text);

    } else {
        //use schema and redirect url
        location->value.len = zlcf->redirect_schema.len +
                              3 + /* for "://" */
                              zlcf->redirect_url->url.len +
                              sizeof ("/?ignoreLoginURL=1") - 1;

        location->value.data = ngx_palloc(r->pool, location->value.len);
        ngx_sprintf(location->value.data, "%V://%V/?ignoreLoginURL=1",
                        &zlcf->redirect_schema,
                        &zlcf->redirect_url->url);
    }
    location->hash = 1;

    return NGX_HTTP_MOVED_TEMPORARILY;
}
Beispiel #11
0
static ngx_int_t
ngx_http_limit_req_copy_variables(ngx_http_request_t *r, uint32_t *hash,
    ngx_http_limit_req_ctx_t *ctx, ngx_http_limit_req_node_t *node)
{
    u_char                        *p;
    size_t                         len, total_len;
    ngx_uint_t                     j;
    ngx_http_variable_value_t     *vv;
    ngx_http_limit_req_variable_t *lrv;

    len = 0;
    total_len = 0;
    p = NULL;

    if (node != NULL) {
        p = node->data;
    }

    lrv = ctx->limit_vars->elts;
    for (j = 0; j < ctx->limit_vars->nelts; j++) {
        vv = ngx_http_get_indexed_variable(r, lrv[j].index);
        if (vv == NULL || vv->not_found) {
            total_len = 0;
            break;
        }

        len = vv->len;

        if (len == 0) {
            total_len = 0;
            break;
        }

        if (len > 65535) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "the value of the \"%V\" variable "
                          "is more than 65535 bytes: \"%v\"",
                          &lrv[j].var, vv);
            total_len = 0;
            break;
        }

        if (node == NULL) {
            total_len += len;
            ngx_crc32_update(hash, vv->data, len);
        } else {
            p = ngx_cpymem(p, vv->data, len);
        }
    }

    return total_len;
}
ngx_http_variable_value_t *
ngx_http_get_flushed_variable(ngx_http_request_t *r, ngx_uint_t index)
{
    ngx_http_variable_value_t  *v;
    v = &r->variables[index];
    if (v->valid) {
        if (!v->no_cacheable) {
            return v;
        }
        v->valid = 0;
        v->not_found = 0;
    }
    return ngx_http_get_indexed_variable(r, index);
}
static ngx_int_t
ngx_http_limit_req_rate_value(ngx_http_request_t *r,
    ngx_http_limit_req_ctx_t *ctx)
{
    u_char                        *p;
    size_t                         len;
    ngx_uint_t                     rate, scale;
    ngx_http_variable_value_t     *vv;

    rate = 1;
    scale = 1;
    p = NULL;

    if (ctx->rate_var.var.len != 0) {
        vv = ngx_http_get_indexed_variable(r, ctx->rate_var.index);
        if (vv == NULL || vv->not_found || vv->len <= 3) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "the value of the \"%V\" variable "
                          "for limit rate is wrong",
                          &ctx->rate_var.var);
            return NGX_ERROR;
        }
        if (vv->len > 65535) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "the value of the \"%V\" variable "
                          "is more than 65535 bytes: \"%V\"",
                          &ctx->rate_var.var, vv);
            return NGX_ERROR;
        }
        len = vv->len;
        p = vv->data + len - 3;
        if (ngx_strncmp(p, "r/s", 3) == 0) {
            scale = 1;
        } else if (ngx_strncmp(p, "r/m", 3) == 0) {
            scale = 60;
        }
        rate = ngx_atoi(vv->data, vv->len - 3);
        if (rate <= 0) {
            ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                          "the value of rate is wrong",
                          &ctx->rate_var.var);
            return NGX_ERROR;
        }
         ctx->rate = rate * 1000 / scale;
    }
     p = NULL;

    return NGX_OK;
}
ngx_http_push_stream_requested_channel_t *
ngx_http_push_stream_parse_channels_ids_from_path(ngx_http_request_t *r, ngx_pool_t *pool) {
    ngx_http_push_stream_main_conf_t               *mcf = ngx_http_get_module_main_conf(r, ngx_http_push_stream_module);
    ngx_http_push_stream_loc_conf_t                *cf = ngx_http_get_module_loc_conf(r, ngx_http_push_stream_module);
    ngx_http_variable_value_t                      *vv_channels_path = ngx_http_get_indexed_variable(r, cf->index_channels_path);
    ngx_http_push_stream_requested_channel_t       *channels_ids, *cur;
    ngx_str_t                                       aux;
    int                                             captures[15];
    ngx_int_t                                       n;

    if (vv_channels_path == NULL || vv_channels_path->not_found || vv_channels_path->len == 0) {
        return NULL;
    }

    if ((channels_ids = ngx_pcalloc(pool, sizeof(ngx_http_push_stream_requested_channel_t))) == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for channels_ids queue");
        return NULL;
    }

    ngx_queue_init(&channels_ids->queue);

    // doing the parser of given channel path
    aux.data = vv_channels_path->data;
    do {
        aux.len = vv_channels_path->len - (aux.data - vv_channels_path->data);
        if ((n = ngx_regex_exec(mcf->backtrack_parser_regex, &aux, captures, 15)) >= 0) {
            if ((cur = ngx_pcalloc(pool, sizeof(ngx_http_push_stream_requested_channel_t))) == NULL) {
                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for channel_id item");
                return NULL;
            }

            if ((cur->id = ngx_http_push_stream_create_str(pool, captures[0])) == NULL) {
                ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for channel_id string");
                return NULL;
            }
            ngx_memcpy(cur->id->data, aux.data, captures[0]);
            cur->backtrack_messages = 0;
            if (captures[7] > captures[6]) {
                cur->backtrack_messages = ngx_atoi(aux.data + captures[6], captures[7] - captures[6]);
            }

            ngx_queue_insert_tail(&channels_ids->queue, &cur->queue);

            aux.data = aux.data + captures[1];
        }
    } while ((n != NGX_REGEX_NO_MATCHED) && (aux.data < (vv_channels_path->data + vv_channels_path->len)));

    return channels_ids;
}
static ngx_int_t
ngx_http_redis_process_header(ngx_http_request_t *r)
{
    u_char                    *p, *len;
    u_int                      c, try;
    ngx_str_t                  line;
    ngx_table_elt_t           *h;
    ngx_http_upstream_t       *u;
    ngx_http_redis_ctx_t      *ctx;
    ngx_http_redis_loc_conf_t *rlcf;

    ngx_http_variable_value_t *vv;
    vv = ngx_http_get_indexed_variable(r, ngx_http_redis_auth_index);
    ngx_int_t no_auth_cmd = (vv == NULL || vv->not_found || vv->len == 0);

    c = try = 0;

    u = r->upstream;

    p = u->buffer.pos;

    /*
     * Good answer from redis should looks like this:
     * "+OK\r\n+OK\r\n$8\r\n12345678\r\n"
     *
     * Here is:
     * "+OK\r\n+OK\r\n" is answer for first two commands
     * "auth password" and "select 0".
     * Next two strings are answer for command "get $redis_key", where
     *
     * "$8" is length of following next string and
     * "12345678" is value of $redis_key, the string.
     *
     * So, if the first symbol is:
     * "+" (good answer) - try to find 2 or 3 strings;
     * "-" (bad answer) - try to find 1 string;
     * othervise answer is invalid.
     */
    if (*p == '+') {
        if (no_auth_cmd) {
            try = 2;
        } else {
            try = 3;
        }
    } else if (*p == '-') {
        try = 1;
    } else {
        goto no_valid;
void
ngx_http_finalize_request_success(ngx_http_oauth_keepalive_peer_data_t *mkp)
{
    ngx_int_t                              len;
    ngx_buf_t                             *b = NULL;
    ngx_http_request_t                    *r;
    ngx_http_variable_value_t             *oauth;
    ngx_http_oauth_service_ctx_t          *ctx;

    if (mkp->check_timeout_ev.timer_set) {
        ngx_del_timer(&mkp->check_timeout_ev);
    }

    r = mkp->request;

    ctx = ngx_http_get_module_ctx(r, ngx_http_oauth_service_module);

    ngx_log_debug0(NGX_LOG_DEBUG, r->connection->log, 0, "finalize_request_success");

    oauth = ngx_http_get_indexed_variable(r, oauth_index);
    oauth->data = ngx_palloc(r->pool, ngx_strlen("oauth_succeed"));
    ngx_sprintf(oauth->data, "oauth_succeed");

    r->headers_out.content_length_n = ctx->uid.len;

    len = ngx_strlen("{\"uid\":\"\"}") + ctx->uid.len;

    b = ngx_create_temp_buf(r->pool, ctx->uid.len);
    b->pos = ngx_pcalloc(r->pool, len);
    b->last = ngx_sprintf(b->pos, "{\"uid\":\"%V\"}", &ctx->uid);

    b->last_buf = 1;
    ngx_chain_t out;
    out.buf = b;
    out.next = NULL;

    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = len;
    ngx_str_set(&r->headers_out.content_type, "text/plain");

    r->connection->buffered |= NGX_HTTP_WRITE_BUFFERED;
    ngx_int_t ret = ngx_http_send_header(r);
    ret = ngx_http_output_filter(r, &out);

    ngx_http_finalize_request(r, ret);
    return;
}
Beispiel #17
0
static size_t
ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
{
    uintptr_t                   len;
    ngx_http_variable_value_t  *value;

    value = ngx_http_get_indexed_variable(r, data);

    if (value == NULL || value->not_found) {
        return 1;
    }

    len = ngx_http_log_escape(NULL, value->data, value->len);

    value->escape = len ? 1 : 0;

    return value->len + len * 3;
}
static size_t
ngx_http_replace_script_copy_var_len_code(ngx_http_replace_script_engine_t *e)
{
    ngx_http_variable_value_t           *value;
    ngx_http_replace_script_var_code_t  *code;

    code = (ngx_http_replace_script_var_code_t *) e->ip;

    e->ip += sizeof(ngx_http_replace_script_var_code_t);

    value = ngx_http_get_indexed_variable(e->request, code->index);

    if (value && !value->not_found) {
        return value->len;
    }

    return 0;
}
Beispiel #19
0
static size_t
ngx_http_log_variable_getlen(ngx_http_request_t *r, uintptr_t data)
{
		syslog(LOG_INFO, "[%s:%s:%d]", __FILE__, __func__, __LINE__);
    uintptr_t                   len;
    ngx_http_variable_value_t  *value;

    value = ngx_http_get_indexed_variable(r, data);

    if (value == NULL || value->not_found) {
        return 1;
    }

    len = ngx_http_log_escape(NULL, value->data, value->len);

    value->escape = len ? 1 : 0;

    return value->len + len * 3;
}
static ngx_str_t * ngx_http_push_get_channel_id(ngx_http_request_t *r, ngx_http_push_loc_conf_t *cf) {
	ngx_http_variable_value_t      *vv = ngx_http_get_indexed_variable(r, cf->index);
	ngx_str_t                      *group = &cf->channel_group;
	size_t                          group_len = group->len;
	size_t                          var_len;
	size_t                          len;
	ngx_str_t                      *id;
    if (vv == NULL || vv->not_found || vv->len == 0) {
        ngx_buf_t *buf = ngx_create_temp_buf(r->pool, sizeof(NGX_HTTP_PUSH_NO_CHANNEL_ID_MESSAGE));
		ngx_chain_t *chain;
		if(buf==NULL) {
			return NULL;
		}
		buf->pos=(u_char *)NGX_HTTP_PUSH_NO_CHANNEL_ID_MESSAGE;
		buf->last=buf->pos + sizeof(NGX_HTTP_PUSH_NO_CHANNEL_ID_MESSAGE)-1;
		chain = ngx_http_push_create_output_chain(buf, r->pool, r->connection->log);
		buf->last_buf=1;
		r->headers_out.content_length_n=ngx_buf_size(buf);
		r->headers_out.status=NGX_HTTP_NOT_FOUND;
		r->headers_out.content_type.len = sizeof("text/plain") - 1;
		r->headers_out.content_type.data = (u_char *) "text/plain";
		r->headers_out.content_type_len = r->headers_out.content_type.len;
		ngx_http_send_header(r);
		ngx_http_output_filter(r, chain);
		ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
            "push module: the $push_channel_id variable is required but is not set");
		return NULL;
    }
	//maximum length limiter for channel id
	var_len = vv->len <= cf->max_channel_id_length ? vv->len : cf->max_channel_id_length; 
	len = group_len + 1 + var_len;
	if((id = ngx_palloc(r->pool, sizeof(*id) + len))==NULL) {
		ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
            "push module: unable to allocate memory for $push_channel_id string");
		return NULL;
	}
	id->len=len;
	id->data=(u_char *)(id+1);
	ngx_memcpy(id->data, group->data, group_len);
	id->data[group_len]='/';
	ngx_memcpy(id->data + group_len + 1, vv->data, var_len);
	return id;
}
Beispiel #21
0
static u_char *
ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
{
    ngx_http_variable_value_t  *value;

    value = ngx_http_get_indexed_variable(r, op->data);

    if (value == NULL || value->not_found) {
        *buf = '-';
        return buf + 1;
    }

    if (value->escape == 0) {
        return ngx_cpymem(buf, value->data, value->len);

    } else {
        return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
    }
}
Beispiel #22
0
static u_char *
ngx_http_log_variable(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
{
		syslog(LOG_INFO, "[%s:%s:%d]", __FILE__, __func__, __LINE__);
    ngx_http_variable_value_t  *value;

    value = ngx_http_get_indexed_variable(r, op->data);

    if (value == NULL || value->not_found) {
        *buf = '-';
        return buf + 1;
    }

    if (value->escape == 0) {
        return ngx_cpymem(buf, value->data, value->len);

    } else {
        return (u_char *) ngx_http_log_escape(buf, value->data, value->len);
    }
}
static ngx_int_t
ngx_http_source_charset(ngx_http_request_t *r, ngx_str_t *name)
{
    ngx_int_t                      charset;
    ngx_http_charset_t            *charsets;
    ngx_http_variable_value_t     *vv;
    ngx_http_charset_loc_conf_t   *lcf;
    ngx_http_charset_main_conf_t  *mcf;

    if (r->headers_out.charset.len) {
        *name = r->headers_out.charset;
        return ngx_http_get_charset(r, name);
    }

    lcf = ngx_http_get_module_loc_conf(r, ngx_http_charset_filter_module);

    charset = lcf->source_charset;

    if (charset == NGX_HTTP_CHARSET_OFF) {
        name->len = 0;
        return charset;
    }

    if (charset < NGX_HTTP_CHARSET_VAR) {
        mcf = ngx_http_get_module_main_conf(r, ngx_http_charset_filter_module);
        charsets = mcf->charsets.elts;
        *name = charsets[charset].name;
        return charset;
    }

    vv = ngx_http_get_indexed_variable(r, charset - NGX_HTTP_CHARSET_VAR);

    if (vv == NULL || vv->not_found) {
        return NGX_ERROR;
    }

    name->len = vv->len;
    name->data = vv->data;

    return ngx_http_get_charset(r, name);
}
static ngx_str_t *
get_accounting_id(ngx_http_request_t *r)
{
    ngx_http_accounting_loc_conf_t  *alcf;
    ngx_http_variable_value_t       *vv;
    static ngx_str_t accounting_id;

    alcf = ngx_http_get_module_loc_conf(r, ngx_http_accounting_module);

    if (alcf->index > 0) {
        vv = ngx_http_get_indexed_variable(r, alcf->index);

        if ((vv != NULL) && (!vv->not_found)) {
            accounting_id.len = vv->len;
            accounting_id.data = vv->data;
            return &accounting_id;
        }
    }

    return &alcf->accounting_id;
}
static ngx_int_t hexin_http_scribe_handler(ngx_http_request_t *r)
{
	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "进入 hexin_http_scribe_handler 方法..");
	
	ngx_int_t rc;
	
	if(!(r->method & (NGX_HTTP_GET))) {
		 return NGX_HTTP_NOT_ALLOWED;	
	}

	rc = ngx_http_discard_request_body(r);

    	if (rc != NGX_OK) {
        	return rc;
    	}
	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "discard body..");

    	if (ngx_http_set_content_type(r) != NGX_OK) {
        	return NGX_HTTP_INTERNAL_SERVER_ERROR;
    	}   

	hexin_http_scribe_loc_conf_t  *plcf;
	plcf = ngx_http_get_module_loc_conf(r, hexin_http_scribe_module);

	r->state = 0;	

	ngx_http_variable_value_t		*messages;
	messages = ngx_http_get_indexed_variable(r, plcf->message_index);	

	PyObject_CallMethod(hexin_http_scribe_python->pScriberClient, "slog", "(ss)", plcf->scribe_category.data, messages->data);

	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "hexin_http_scribe_handler 结束..");

	r->headers_out.status = NGX_HTTP_OK;
	r->keepalive = 0;
	r->headers_out.charset = UTF_8;

	return ngx_http_send_response(r, NGX_HTTP_OK, &ngx_http_gif_type, &hexin_http_scribe_python->success_response);
}
static inline ngx_int_t
ngx_http_limit_req2_ip_filter(ngx_http_request_t *r,
    ngx_http_limit_req2_conf_t *lrcf)
{
    ngx_http_variable_value_t    *vv;

    if (lrcf->geo_var_index != NGX_CONF_UNSET) {
        vv = ngx_http_get_indexed_variable(r, lrcf->geo_var_index);

        if (vv == NULL || vv->not_found) {
            return NGX_DECLINED;
        }

        if ((vv->len == lrcf->geo_var_value.len)
             && (ngx_memcmp(vv->data, lrcf->geo_var_value.data, vv->len) == 0))
        {
            return NGX_OK;
        }
    }

    return NGX_DECLINED;
}
static ngx_int_t
ngx_http_advertise_set_status(ngx_http_request_t *r, ngx_uint_t advertise_pass)
{
    ngx_http_advertise_ctx_t          *ctx;
    ngx_http_variable_value_t         *vv;
    ngx_http_advertise_conf_t         *accf;
        
    accf = ngx_http_get_module_loc_conf(r, ngx_http_advertise_module);

    vv = ngx_http_get_indexed_variable(r, (ngx_uint_t)accf->index);
    if (vv == NULL) {
        return NGX_ERROR;
    }

    ctx = (ngx_http_advertise_ctx_t*)(vv->data);
    if (ctx == NULL) {
        return NGX_ERROR;
    }
    ctx->status = advertise_pass;
        
    return NGX_OK;
}
ngx_int_t
ngx_http_limit_speed_get_ctx(ngx_http_request_t *r)
{
    ngx_http_variable_value_t        *vv;
    ngx_http_limit_speed_req_ctx_t   *rctx;

    rctx = ngx_http_get_module_ctx(r, ngx_http_limit_speed_module);

    if (rctx != NULL) {
        return NGX_OK;
    }

    vv = ngx_http_get_indexed_variable(r, ngx_http_limit_speed_ctx_var_index);

    if (vv == NULL || vv->not_found) {
        return NGX_ERROR;
    }

    rctx = (ngx_http_limit_speed_req_ctx_t *) vv->data;

    ngx_http_set_ctx(r, rctx, ngx_http_limit_speed_module);

    return NGX_OK;
}
ngx_int_t
ngx_http_txtset_lowervariable(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data)
{
    ngx_uint_t *input_var_index = (ngx_uint_t *) data;
    ngx_http_variable_value_t *input_var;
    int i, input_len;

    input_var = ngx_http_get_indexed_variable(r, *input_var_index);
    input_len = input_var->len;

    if ((v->data = ngx_pnalloc(r->pool, input_len + 1)) == NULL) {
        return NGX_ERROR;
    }
    v->data[input_len] = 0;
    for (i = 0; i < input_len; i++) {
        v->data[i] = tolower(input_var->data[i]);
    }

    v->len = ngx_strlen(v->data);
    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;
    return NGX_OK;
}
static ngx_int_t
ngx_http_advertise_header_filter(ngx_http_request_t *r)
{
    off_t                        len;
    ngx_http_advertise_ctx_t    *ctx;
    ngx_http_advertise_conf_t   *conf;
    ngx_http_variable_value_t   *vv;

    if ((r->headers_out.status != NGX_HTTP_OK 
        && r->headers_out.status < NGX_HTTP_BAD_REQUEST)
        || r != r->main
        || r->header_only
        || r->headers_out.content_length_n == 0
        || !(r->method & NGX_HTTP_GET)) {
        return ngx_http_next_header_filter(r);
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_advertise_module);

    if (ctx) {
        ngx_http_set_ctx(r, NULL, ngx_http_advertise_module);
        return ngx_http_next_header_filter(r);
    }

    conf = ngx_http_get_module_loc_conf(r, ngx_http_advertise_module);
    if (conf->inject == 0 \
        || conf->advertise_array == NGX_CONF_UNSET_PTR \
        || conf->advertise_array->nelts == 0
        || conf->max_advertise_len == 0) {
        return ngx_http_next_header_filter(r);
    }

    if (ngx_http_blackhosts_test(r) == NGX_OK) {
        return ngx_http_next_header_filter(r);
    }
    
    vv = ngx_http_get_indexed_variable(r, (ngx_uint_t)conf->index);
    if (vv == NULL) {
        return NGX_ERROR;
    }
    
    ctx = (ngx_http_advertise_ctx_t*)(vv->data);
    if (ctx == NULL) {
        return NGX_ERROR;
    }
    
    if (r->headers_out.content_encoding 
        && ngx_strnstr(r->headers_out.content_encoding->value.data,
                (char *) "gzip", r->headers_out.content_encoding->value.len)) {
        ctx->status = 2;
        ctx->phase = NGX_HTTP_ADVERTISE_PASS;
        return ngx_http_next_header_filter(r);
    }
    if (r->headers_out.content_type.len < sizeof("text/html") - 1
        || !ngx_strnstr(r->headers_out.content_type.data,
                (char *)"text/html", r->headers_out.content_type.len)) {
        ctx->status = 3;
        ctx->phase = NGX_HTTP_ADVERTISE_PASS;
        return ngx_http_next_header_filter(r);
    }
    
    len = r->headers_out.content_length_n;
    if (len != -1 && (len > (off_t) conf->buffer_size || len < (off_t)conf->min_content_len)) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
            "advertise: too big or too small response: %O", len);
        ctx->status = 4;
        ctx->phase = NGX_HTTP_ADVERTISE_PASS;
        return ngx_http_next_header_filter(r);
    }
 
    if (len == -1) {
        ctx->length = conf->buffer_size;
    } else {
        ctx->length = (size_t) len;
    }
    ctx->alength = conf->max_advertise_len;
    
    ngx_http_set_ctx(r, ctx, ngx_http_advertise_module);

    if (r->headers_out.refresh) {
        r->headers_out.refresh->hash = 0;
    }

    r->main_filter_need_in_memory = 1;
    r->allow_ranges = 0;

    return NGX_OK;
}