Ejemplo n.º 1
0
static ngx_int_t ngx_statsd_init_endpoint(ngx_conf_t *cf, ngx_udp_endpoint_t *endpoint) {
    ngx_pool_cleanup_t    *cln;
    ngx_udp_connection_t  *uc;

	ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
			   "statsd: initting endpoint");

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if(cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_statsd_updater_cleanup;
    cln->data = endpoint;

    uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log);
    if (uc == NULL) {
        return NGX_ERROR;
    }

    endpoint->udp_connection = uc;

    uc->sockaddr = endpoint->peer_addr.sockaddr;
    uc->socklen = endpoint->peer_addr.socklen;
    uc->server = endpoint->peer_addr.name;

    endpoint->log = &cf->cycle->new_log;

    return NGX_OK;
}
Ejemplo n.º 2
0
static ngx_int_t
ngx_http_lua_ngx_ctx_add_cleanup(ngx_http_request_t *r, int ref)
{
    lua_State                   *L;
    ngx_pool_cleanup_t          *cln;
    ngx_http_lua_ctx_t          *ctx;

    ngx_http_lua_ngx_ctx_cleanup_data_t    *data;

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
    L = ngx_http_lua_get_lua_vm(r, ctx);

    cln = ngx_pool_cleanup_add(r->pool,
                               sizeof(ngx_http_lua_ngx_ctx_cleanup_data_t));
    if (cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_http_lua_ngx_ctx_cleanup;

    data = cln->data;
    data->vm = L;
    data->ref = ref;

    return NGX_OK;
}
static ngx_int_t
ngx_http_poller_set_ssl(ngx_conf_t *cf, ngx_http_poller_t *poller)
{
  ngx_pool_cleanup_t *cln;
  ngx_uint_t          protocols;

  poller->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
  if (poller->upstream.ssl == NULL) {
    return NGX_ERROR;
  }

  poller->upstream.ssl->log = cf->log;
  protocols = NGX_SSL_SSLv2 
    | NGX_SSL_SSLv3
    | NGX_SSL_TLSv1
    | NGX_SSL_TLSv1_1
    | NGX_SSL_TLSv1_2;

  if (ngx_ssl_create(poller->upstream.ssl, protocols, NULL) != NGX_OK) {
    return NGX_ERROR;
  }

  cln = ngx_pool_cleanup_add(cf->pool, 0);
  if (cln == NULL) {
    return NGX_ERROR;
  }

  cln->handler = ngx_ssl_cleanup_ctx;
  cln->data = poller->upstream.ssl;

  return NGX_OK;
}
Ejemplo n.º 4
0
ngx_int_t
ngx_http_file_cache_create(ngx_http_request_t *r)
{
    ngx_http_cache_t       *c;
    ngx_pool_cleanup_t     *cln;
    ngx_http_file_cache_t  *cache;

    c = r->cache;
    cache = c->file_cache;

    cln = ngx_pool_cleanup_add(r->pool, 0);
    if (cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_http_file_cache_cleanup;
    cln->data = c;

    if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
        return NGX_ERROR;
    }

    if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
        return NGX_ERROR;
    }

    return NGX_OK;
}
ngx_open_file_cache_t *
ngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive)
{
    ngx_pool_cleanup_t     *cln;
    ngx_open_file_cache_t  *cache;

    cache = ngx_palloc(pool, sizeof(ngx_open_file_cache_t));
    if (cache == NULL) {
        return NULL;
    }

    ngx_rbtree_init(&cache->rbtree, &cache->sentinel,
                    ngx_open_file_cache_rbtree_insert_value);

    ngx_queue_init(&cache->expire_queue);

    cache->current = 0;
    cache->max = max;
    cache->inactive = inactive;

    cln = ngx_pool_cleanup_add(pool, 0);
    if (cln == NULL) {
        return NULL;
    }

    cln->handler = ngx_open_file_cache_cleanup;
    cln->data = cache;

    return cache;
}
Ejemplo n.º 6
0
static ngx_int_t ngx_fluentd_init_endpoint(ngx_conf_t *cf, ngx_udp_endpoint_t *endpoint) {
    ngx_pool_cleanup_t    *cln;
    ngx_udp_connection_t  *uc;

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if(cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_fluentd_cleanup;
    cln->data = endpoint;

    uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log);
    if (uc == NULL) {
        return NGX_ERROR;
    }

    endpoint->udp_connection = uc;

    uc->sockaddr = endpoint->peer_addr.sockaddr;
    uc->socklen = endpoint->peer_addr.socklen;
    uc->server = endpoint->peer_addr.name;
#if defined nginx_version && ( nginx_version >= 7054 && nginx_version < 8055 )
    uc->log = cf->cycle->new_log;
#else
    uc->log = cf->cycle->new_log;
#if defined nginx_version && nginx_version >= 8032
    uc->log.handler = NULL;
    uc->log.data = NULL;
    uc->log.action = "logging";
#endif
#endif

    return NGX_OK;
}
Ejemplo n.º 7
0
static char *
ngx_http_xrlt(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
    ngx_http_xrlt_loc_conf_t  *xlcf = conf;
    ngx_http_core_loc_conf_t  *clcf;

    ngx_str_t                 *value;
    ngx_pool_cleanup_t        *cln;

    xmlDocPtr                 doc;
    xrltRequestsheetPtr       sheet;

    value = cf->args->elts;

    if (xlcf->sheet != NULL) {
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "Duplicate xrlt instruction");
        return NGX_CONF_ERROR;
    }

    if (ngx_conf_full_name(cf->cycle, &value[1], 0) != NGX_OK) {
        return NGX_CONF_ERROR;
    }

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL) {
        return NGX_CONF_ERROR;
    }


    doc = xmlParseFile((const char *)value[1].data);
    if (doc == NULL) {
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
                           "xmlParseFile(\"%s\") failed", value[1].data);
        return NGX_CONF_ERROR;
    }

    sheet = xrltRequestsheetCreate(doc);
    if (sheet == NULL) {
        xmlFreeDoc(doc);
        ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
                           "xrltRequestsheetCreate(\"%s\") failed",
                           value[1].data);
        return NGX_CONF_ERROR;
    }

    xlcf->sheet = sheet;

    cln->handler = ngx_http_xrlt_cleanup_requestsheet;
    cln->data = sheet;

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    if (clcf == NULL) {
        return NGX_CONF_ERROR;
    }

    clcf->handler = ngx_http_xrlt_handler;

    return NGX_CONF_OK;
}
Ejemplo n.º 8
0
static ngx_int_t
ngx_http_js_init_vm(ngx_http_request_t *r)
{
    void                    **ext;
    ngx_http_js_ctx_t        *ctx;
    ngx_pool_cleanup_t       *cln;
    nxt_mem_cache_pool_t     *mcp;
    ngx_http_js_loc_conf_t   *jlcf;

    jlcf = ngx_http_get_module_loc_conf(r, ngx_http_js_module);
    if (jlcf->vm == NULL) {
        return NGX_DECLINED;
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);

    if (ctx == NULL) {
        ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_js_ctx_t));
        if (ctx == NULL) {
            return NGX_ERROR;
        }

        ngx_http_set_ctx(r, ctx, ngx_http_js_module);
    }

    if (ctx->vm) {
        return NGX_OK;
    }

    mcp = ngx_http_js_create_mem_cache_pool();
    if (mcp == NULL) {
        return NGX_ERROR;
    }

    cln = ngx_pool_cleanup_add(r->pool, 0);
    if (cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_http_js_cleanup_mem_cache_pool;
    cln->data = mcp;

    ext = ngx_palloc(r->pool, sizeof(void *));
    if (ext == NULL) {
        return NGX_ERROR;
    }

    *ext = r;

    ctx->vm = njs_vm_clone(jlcf->vm, mcp, ext);
    if (ctx->vm == NULL) {
        return NGX_ERROR;
    }

    ctx->args = &jlcf->args[0];

    return NGX_OK;
}
//allocates message and responds to subscriber
ngx_int_t ngx_http_push_alloc_for_subscriber_response(ngx_pool_t *pool, ngx_int_t shared, ngx_http_push_msg_t *msg, ngx_chain_t **chain, ngx_str_t **content_type, ngx_str_t **etag, time_t *last_modified) {
  if(etag != NULL && (*etag = ngx_http_push_store->message_etag(msg, pool))==NULL) {
    //oh, nevermind...
    ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "push module: unable to allocate memory for Etag header");
    return NGX_ERROR;
  }
  if(content_type != NULL && (*content_type= ngx_http_push_store->message_content_type(msg, pool))==NULL) {
    //oh, nevermind...
    ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "push module: unable to allocate memory for Content Type header");
    if(pool == NULL) {
      ngx_free(*etag);
    }
    else {
      ngx_pfree(pool, *etag);
    }
    return NGX_ERROR;
  }
  
  //preallocate output chain. yes, same one for every waiting subscriber
  if(chain != NULL && (*chain = ngx_http_push_create_output_chain(msg->buf, pool, ngx_cycle->log))==NULL) {
    ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "push module: unable to allocate buffer chain while responding to subscriber request");
    if(pool == NULL) {
      ngx_free(*etag);
      ngx_free(*content_type);
    }
    else {
      ngx_pfree(pool, *etag);
      ngx_pfree(pool, *content_type);
    }
    return NGX_ERROR;
  }
  
  if(last_modified != NULL) {
    *last_modified = msg->message_time;
  }
  ngx_http_push_store->unlock();
  
  
  if(pool!=NULL && shared == 0 && ((*chain)->buf->file!=NULL)) {
    //close file when we're done with it
    ngx_pool_cleanup_t *cln;
    ngx_pool_cleanup_file_t *clnf;
    
    if((cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)))==NULL) {
      ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "push module: unable to allocate buffer chain pool cleanup while responding to subscriber request");
      ngx_pfree(pool, *etag);
      ngx_pfree(pool, *content_type);
      ngx_pfree(pool, *chain);
      return NGX_ERROR;
    }
    cln->handler = ngx_pool_cleanup_file;
    clnf = cln->data;
    clnf->fd = (*chain)->buf->file->fd;
    clnf->name = (*chain)->buf->file->name.data;
    clnf->log = ngx_cycle->log;
  }
  return NGX_OK;
}
Ejemplo n.º 10
0
static ngx_int_t
ngx_http_js_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
    uintptr_t data)
{
    njs_vm_t *vm = (njs_vm_t *) data;

    nxt_str_t              value;
    njs_vm_t             *nvm;
    ngx_pool_cleanup_t   *cln;
    nxt_mem_cache_pool_t  *mcp;

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "http js variable handler");

    mcp = ngx_http_js_create_mem_cache_pool();
    if (mcp == NULL) {
        return NGX_ERROR;
    }

    cln = ngx_pool_cleanup_add(r->pool, 0);
    if (cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_http_js_cleanup_mem_cache_pool;
    cln->data = mcp;

    nvm = njs_vm_clone(vm, mcp, (void **) &r);
    if (nvm == NULL) {
        return NGX_ERROR;
    }

    if (njs_vm_run(nvm) == NJS_OK) {
        if (njs_vm_retval(nvm, &value) != NJS_OK) {
            return NGX_ERROR;
        }

        v->len = value.len;
        v->valid = 1;
        v->no_cacheable = 0;
        v->not_found = 0;
        v->data = value.data;

    } else {
        njs_vm_exception(nvm, &value);

        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "js exception: %*s", value.len, value.data);

        v->not_found = 1;
    }

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "http js variable done");

    return NGX_OK;
}
static ngx_int_t
ngx_http_gettoken_init_connections(ngx_cycle_t *cycle)
{
    ngx_http_gettoken_connection_t *c;
    ngx_http_gettoken_main_conf_t *halmcf;
    ngx_http_gettoken_server_t *server;
    ngx_pool_cleanup_t *cleanup;
    ngx_connection_t *dummy_conn;
    ngx_uint_t i, j;
    //int option;

    halmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_gettoken_module);
    if (halmcf == NULL || halmcf->servers == NULL) {
        return NGX_OK;
    }


    for (i = 0; i < halmcf->servers->nelts; i++) {
        server = &((ngx_http_gettoken_server_t *) halmcf->servers->elts)[i];
        ngx_queue_init(&server->free_connections);
        ngx_queue_init(&server->waiting_requests);
        if (server->connections <= 1) {
            server->connections = 1;
        }

        for (j = 0; j < server->connections; j++) {
            c = ngx_pcalloc(cycle->pool, sizeof(ngx_http_gettoken_connection_t));
            cleanup = ngx_pool_cleanup_add(cycle->pool, 0);
            dummy_conn = ngx_pcalloc(cycle->pool, sizeof(ngx_connection_t));
            if (c == NULL || cleanup == NULL || dummy_conn == NULL) {
                return NGX_ERROR;
            }

            cleanup->handler = &ngx_http_gettoken_connection_cleanup;
            cleanup->data = c;

            c->log = cycle->log;
            c->server = server;
            c->state = STATE_DISCONNECTED;

            /* Various debug logging around timer management assume that the field
               'data' in ngx_event_t is a pointer to ngx_connection_t, therefore we
               have a dummy such structure around so that it does not crash etc. */
            dummy_conn->data = c;
            c->reconnect_event.log = c->log;
            c->reconnect_event.data = dummy_conn;
            c->reconnect_event.handler = ngx_http_gettoken_reconnect_handler;


            ngx_http_gettoken_connect(c);
        }
    }

    return NGX_OK;

}
Ejemplo n.º 12
0
static ngx_int_t
ngx_http_xslt_send(ngx_http_request_t *r, ngx_http_xslt_filter_ctx_t *ctx,
    ngx_buf_t *b)
{
    ngx_int_t                         rc;
    ngx_chain_t                       out;
    ngx_pool_cleanup_t               *cln;
    ngx_http_xslt_filter_loc_conf_t  *conf;

    ctx->done = 1;

    if (b == NULL) {
        return ngx_http_filter_finalize_request(r, &ngx_http_xslt_filter_module,
                                               NGX_HTTP_INTERNAL_SERVER_ERROR);
    }

    cln = ngx_pool_cleanup_add(r->pool, 0);

    if (cln == NULL) {
        ngx_free(b->pos);
        return ngx_http_filter_finalize_request(r, &ngx_http_xslt_filter_module,
                                               NGX_HTTP_INTERNAL_SERVER_ERROR);
    }

    if (r == r->main) {
        r->headers_out.content_length_n = b->last - b->pos;

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

        ngx_http_clear_etag(r);

        conf = ngx_http_get_module_loc_conf(r, ngx_http_xslt_filter_module);

        if (!conf->last_modified) {
            ngx_http_clear_last_modified(r);
        }
    }

    rc = ngx_http_next_header_filter(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        ngx_free(b->pos);
        return rc;
    }

    cln->handler = ngx_http_xslt_cleanup;
    cln->data = b->pos;

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

    return ngx_http_next_body_filter(r, &out);
}
static ngx_int_t
ngx_stream_lua_init(ngx_conf_t *cf)
{
    ngx_int_t                   rc;
    volatile ngx_cycle_t       *saved_cycle;
    ngx_stream_lua_main_conf_t *lmcf;
#ifndef NGX_LUA_NO_FFI_API
    ngx_pool_cleanup_t         *cln;
#endif

    lmcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_lua_module);

#ifndef NGX_LUA_NO_FFI_API

    /* add the cleanup of semaphores after the lua_close */
    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL) {
        return NGX_ERROR;
    }

    cln->data = lmcf;
    cln->handler = ngx_stream_lua_cleanup_semaphore_mm;

#endif

    if (lmcf->lua == NULL) {
        dd("initializing lua vm");

        lmcf->lua = ngx_stream_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
                                           cf->log, NULL);
        if (lmcf->lua == NULL) {
            ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
                               "failed to initialize Lua VM");
            return NGX_ERROR;
        }

        if (!lmcf->requires_shm && lmcf->init_handler) {
            saved_cycle = ngx_cycle;
            ngx_cycle = cf->cycle;

            rc = lmcf->init_handler(cf->log, lmcf, lmcf->lua);

            ngx_cycle = saved_cycle;

            if (rc != NGX_OK) {
                /* an error happened */
                return NGX_ERROR;
            }
        }

        dd("Lua VM initialized!");
    }

    return NGX_OK;
}
static char *
ngx_http_ip2location_init_main_conf(ngx_conf_t *cf, void *data)
{
    ngx_http_ip2location_main_conf_t  *imcf = data;
    ngx_pool_cleanup_t                *cln;

    if (imcf->access_type == NGX_CONF_UNSET) {
        imcf->access_type = IP2LOCATION_SHARED_MEMORY;
    }

    if (imcf->enabled) {
        if (imcf->filename.len == 0) {
            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                          "ip2location enabled with no database specified in %s:%ui",
                          imcf->enable_file, imcf->enable_line);
            return NGX_CONF_ERROR;
        }

        // open database
        imcf->database = IP2Location_open((char *)imcf->filename.data);

        if (imcf->database == NULL) {
            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                          "can not open database file \"%V\" in %s:%ui",
                          &imcf->filename, imcf->database_file, imcf->database_line);
            return NGX_CONF_ERROR;
        }

        if (IP2Location_open_mem(imcf->database, imcf->access_type) == -1) {

            IP2Location_close(imcf->database);

            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                          "can not load database file %V using \"%V\" access type in %s:%ui",
                          &imcf->filename,
                          &imcf->access_type_name,
                          imcf->database_file,
                          imcf->database_line);

            return NGX_CONF_ERROR;
        }

        cln = ngx_pool_cleanup_add(cf->pool, 0);
        if (cln == NULL) {
            return NGX_CONF_ERROR;
        }

        cln->data = imcf->database;
        cln->handler = ngx_http_ip2location_cleanup;
    }

    return NGX_CONF_OK;
}
static char *
ngx_http_lua_init_vm(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
{
    ngx_pool_cleanup_t              *cln;
    ngx_http_lua_preload_hook_t     *hook;
    lua_State                       *L;
    ngx_uint_t                       i;

    ngx_http_lua_content_length_hash =
        ngx_http_lua_hash_literal("content-length");

    ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location");

    /* add new cleanup handler to config mem pool */
    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL) {
        return NGX_CONF_ERROR;
    }

    /* create new Lua VM instance */
    lmcf->lua = ngx_http_lua_new_state(cf, lmcf);
    if (lmcf->lua == NULL) {
        return NGX_CONF_ERROR;
    }

    /* register cleanup handler for Lua VM */
    cln->handler = ngx_http_lua_cleanup_vm;
    cln->data = lmcf->lua;

    if (lmcf->preload_hooks) {

        /* register the 3rd-party module's preload hooks */

        L = lmcf->lua;

        lua_getglobal(L, "package");
        lua_getfield(L, -1, "preload");

        hook = lmcf->preload_hooks->elts;

        for (i = 0; i < lmcf->preload_hooks->nelts; i++) {

            ngx_http_lua_probe_register_preload_package(L, hook[i].package);

            lua_pushcfunction(L, hook[i].loader);
            lua_setfield(L, -2, (char *) hook[i].package);
        }

        lua_pop(L, 2);
    }

    return NGX_CONF_OK;
}
Ejemplo n.º 16
0
static ngx_int_t
ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip, size_t len)
{
    u_char                 *p;
    ngx_int_t               rc;
    ngx_addr_t              addr;
    ngx_connection_t       *c;
    ngx_pool_cleanup_t     *cln;
    ngx_http_realip_ctx_t  *ctx;

    cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
    if (cln == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ctx = cln->data;
    ngx_http_set_ctx(r, ctx, ngx_http_realip_module);

    c = r->connection;

    rc = ngx_parse_addr(c->pool, &addr, ip, len);

    switch (rc) {
    case NGX_DECLINED:
        return NGX_DECLINED;
    case NGX_ERROR:
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    default: /* NGX_OK */
        break;
    }

    p = ngx_pnalloc(c->pool, len);
    if (p == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ngx_memcpy(p, ip, len);

    cln->handler = ngx_http_realip_cleanup;

    ctx->connection = c;
    ctx->sockaddr = c->sockaddr;
    ctx->socklen = c->socklen;
    ctx->addr_text = c->addr_text;

    c->sockaddr = addr.sockaddr;
    c->socklen = addr.socklen;
    c->addr_text.len = len;
    c->addr_text.data = p;

    return NGX_DECLINED;
}
Ejemplo n.º 17
0
static void *
ngx_dso_create_conf(ngx_cycle_t *cycle)
{
    ngx_dso_conf_ctx_t  *ctx;
    ngx_pool_cleanup_t  *cln;

    ctx = ngx_pcalloc(cycle->pool, sizeof(ngx_dso_conf_ctx_t));

    if (ctx == NULL) {
        return NULL;
    }

    if (NGX_DSO_MAX < ngx_max_module) {
        ngx_log_error(NGX_LOG_EMERG, cycle->log,
                      0, "please set max dso module"
                      "(use configure with --dso-max-modules),"
                      "current is %ud, expect %ud",
                      NGX_DSO_MAX, ngx_max_module + 1);
        return NULL;
    }

    if (ngx_is_init_cycle(cycle->old_cycle)) {
        ngx_memcpy(ngx_static_modules, ngx_modules,
                   sizeof(ngx_module_t *) * ngx_max_module);
        ngx_memcpy(ngx_static_module_names, ngx_module_names,
                   sizeof(u_char *) * ngx_max_module);

    } else {
        ngx_memcpy(ngx_old_modules, ngx_modules,
                   sizeof(ngx_module_t *) * NGX_DSO_MAX);
        ngx_memcpy(ngx_modules, ngx_static_modules,
                   sizeof(ngx_module_t *) * NGX_DSO_MAX);

        ngx_memcpy(ngx_old_module_names, ngx_module_names,
                   sizeof(u_char *) * NGX_DSO_MAX);
        ngx_memcpy(ngx_module_names, ngx_static_module_names,
                   sizeof(u_char *) * NGX_DSO_MAX);
    }

    cln = ngx_pool_cleanup_add(cycle->pool, 0);
    if (cln == NULL) {
        return NULL;
    }

    cln->handler = ngx_dso_cleanup;
    cln->data = cycle;

    return ctx;
}
Ejemplo n.º 18
0
static ngx_int_t
ngx_http_realip_set_addr(ngx_http_request_t *r, ngx_addr_t *addr)
{
    size_t                  len;
    u_char                 *p;
    u_char                  text[NGX_SOCKADDR_STRLEN];
    ngx_connection_t       *c;
    ngx_pool_cleanup_t     *cln;
    ngx_http_realip_ctx_t  *ctx;

    cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
    if (cln == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ctx = cln->data;
    ngx_http_set_ctx(r, ctx, ngx_http_realip_module);

    c = r->connection;

    len = ngx_sock_ntop(addr->sockaddr, addr->socklen, text,
                        NGX_SOCKADDR_STRLEN, 0);
    if (len == 0) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    p = ngx_pnalloc(c->pool, len);
    if (p == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ngx_memcpy(p, text, len);

    cln->handler = ngx_http_realip_cleanup;

    ctx->connection = c;
    ctx->sockaddr = c->sockaddr;
    ctx->socklen = c->socklen;
    ctx->addr_text = c->addr_text;

    c->sockaddr = addr->sockaddr;
    c->socklen = addr->socklen;
    c->addr_text.len = len;
    c->addr_text.data = p;

    return NGX_DECLINED;
}
Ejemplo n.º 19
0
ngx_int_t
ngx_pool_set_ctx(ngx_pool_t *pool, ngx_uint_t index, void *data)
{
    ngx_uint_t              hash;
    uint32_t                key;
    ngx_pool_context_node_t *node;
    ngx_pool_cleanup_t     *cln;

    hash = (ngx_uint_t) pool + index;
    key = ngx_murmur_hash2((u_char *)&hash,
            sizeof(hash)) % ngx_pool_context_hash_size;

    node = ngx_pool_context_hash[key];

    while (node) {

        if (node->pool == pool
                && node->index == index) {


            node->data = data;
            return NGX_OK;
        }
        node = node->next;
    }

    cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_context_node_t));

    if (cln == NULL) {

        return NGX_ERROR;
    }

    cln->handler = ngx_pool_context_cleanup;
    node = cln->data;

    node->prev = NULL;
    node->next = NULL;
    node->pool = pool;
    node->index = index;
    node->data = data;

    ngx_pool_context_link(&ngx_pool_context_hash[key], node);

    return NGX_OK;
}
Ejemplo n.º 20
0
int
ngx_tcl_cleanup_add_Tcl_Obj(ngx_pool_t *p, Tcl_Obj *obj)
{
    ngx_pool_cleanup_t *cln;

printf("%s(%p)\n", __FUNCTION__, obj); fflush(stdout);
    cln = ngx_pool_cleanup_add(p, sizeof(Tcl_Obj*));

    if (cln == NULL) {
        return TCL_ERROR;
    }

    cln->handler = ngx_tcl_cleanup_Tcl_Obj;
    *(Tcl_Obj**)cln->data = obj;
    Tcl_IncrRefCount(obj);

    return TCL_OK;
}
ngx_inline ngx_http_modsecurity_ctx_t *
ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
{
    ngx_http_modsecurity_ctx_t *ctx;
    ngx_http_modsecurity_loc_conf_t *loc_cf = NULL;
    ngx_http_modsecurity_main_conf_t *cf = NULL;
    ngx_pool_cleanup_t *cln = NULL;

    ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_modsecurity_ctx_t));
    if (ctx == NULL)
    {
        dd("failed to allocate memory for the context.");
        return NULL;
    }
    cf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity);
    loc_cf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity);

    dd("creating transaction with the following rules: '%p' -- ms: '%p'", loc_cf->rules_set, cf->modsec);

    ctx->modsec_transaction = msc_new_transaction(cf->modsec, loc_cf->rules_set, r->connection->log);

    dd("transaction created");

    ngx_http_set_ctx(r, ctx, ngx_http_modsecurity);

    cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_modsecurity_ctx_t));
    if (cln == NULL)
    {
        dd("failed to create the ModSecurity context cleanup");
        return NGX_CONF_ERROR;
    }
    cln->handler = ngx_http_modsecurity_cleanup;
    cln->data = ctx;

#ifdef MODSECURITY_SANITY_CHECKS
    ctx->sanity_headers_out = ngx_array_create(r->pool, 12, sizeof(ngx_http_modsecurity_header_t));
    if (ctx->sanity_headers_out == NULL) {
        return NGX_CONF_ERROR;
    }
#endif

    return ctx;
}
static void *
ngx_http_modsecurity_create_loc_conf(ngx_conf_t *cf)
{
    ngx_http_modsecurity_loc_conf_t  *conf;
    ngx_pool_cleanup_t *cln = NULL;

    conf = (ngx_http_modsecurity_loc_conf_t  *)
        ngx_palloc(cf->pool, sizeof(ngx_http_modsecurity_loc_conf_t));
    dd("creating a location specific conf");

    if (conf == NULL)
    {
        dd("Failed to allocate space for a location specific conf");
        return NGX_CONF_ERROR;
    }

    conf->enable = NGX_CONF_UNSET;
    conf->rules_remote_server.len = 0;
    conf->rules_remote_server.data = NULL;
    conf->rules_remote_key.len = 0;
    conf->rules_remote_key.data = NULL;
    conf->rules_file.len = 0;
    conf->rules_file.data = NULL;
    conf->rules.len = 0;
    conf->rules.data = NULL;
    conf->id = 0;

    /* Create a new rules instance */
    conf->rules_set = msc_create_rules_set();

    dd("created a location specific conf -- RuleSet is at: \"%p\"", conf->rules_set);

    //msc_rules_dump(conf->rules_set); // This was for debug
    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL) {
        dd("failed to create the ModSecurity location specific configuration cleanup");
        return NGX_CONF_ERROR;
    }
    cln->handler = ngx_http_modsecurity_config_cleanup;
    cln->data = conf;

    return conf;
}
static ngx_int_t
ngx_http_removeip_handler(ngx_http_request_t *r)
{
    ngx_connection_t             *c;
    ngx_pool_cleanup_t           *cln;
    ngx_http_removeip_ctx_t      *ctx;
    ngx_http_removeip_loc_conf_t *rlcf;

    ctx = ngx_http_get_module_ctx(r, ngx_http_removeip_module);

    if (ctx) {
        return NGX_DECLINED;
    }

    rlcf = ngx_http_get_module_loc_conf(r, ngx_http_removeip_module);

    if (!rlcf->enabled) {
        return NGX_DECLINED;
    }

    cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_removeip_ctx_t));
    if (cln == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ctx = cln->data;
    ngx_http_set_ctx(r, ctx, ngx_http_removeip_module);

    c = r->connection;

    cln->handler = ngx_http_removeip_cleanup;

    ctx->connection = c;
    ctx->sockaddr = c->sockaddr;
    ctx->socklen = c->socklen;
    ctx->addr_text = c->addr_text;

    c->sockaddr = removeip_addr.sockaddr;
    c->socklen = removeip_addr.socklen;
    ngx_str_set(&c->addr_text, REMOVEIP_ADDR);

    return NGX_DECLINED;
}
Ejemplo n.º 24
0
static ngx_int_t
ngx_http_js_handler(ngx_http_request_t *r)
{
    nxt_str_t                 value;
    njs_vm_t                *nvm;
    ngx_pool_cleanup_t      *cln;
    nxt_mem_cache_pool_t     *mcp;
    ngx_http_js_loc_conf_t  *jlcf;

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "http js handler");

    mcp = ngx_http_js_create_mem_cache_pool();
    if (mcp == NULL) {
        return NGX_ERROR;
    }

    cln = ngx_pool_cleanup_add(r->pool, 0);
    if (cln == NULL) {
        return NGX_ERROR;
    }

    cln->handler = ngx_http_js_cleanup_mem_cache_pool;
    cln->data = mcp;

    jlcf = ngx_http_get_module_loc_conf(r, ngx_http_js_module);

    nvm = njs_vm_clone(jlcf->vm, mcp, (void **) &r);
    if (nvm == NULL) {
        return NGX_ERROR;
    }

    if (njs_vm_run(nvm) != NJS_OK) {
        njs_vm_exception(nvm, &value);

        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "js exception: %*s", value.len, value.data);

        return NGX_ERROR;
    }

    return NGX_OK;
}
static void *ngx_stream_mruby_create_main_conf(ngx_conf_t *cf)
{
  ngx_stream_mruby_main_conf_t *mmcf;
  ngx_stream_mruby_internal_ctx_t *ictx;
  ngx_pool_cleanup_t *cln;

  cln = ngx_pool_cleanup_add(cf->pool, 0);
  if (cln == NULL) {
    return NULL;
  }

  mmcf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_mruby_main_conf_t));
  if (mmcf == NULL) {
    return NULL;
  }
  mmcf->ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_mruby_conf_ctx_t));
  if (mmcf->ctx == NULL) {
    return NULL;
  }

  mmcf->init_code = NGX_CONF_UNSET_PTR;
  mmcf->init_worker_code = NGX_CONF_UNSET_PTR;
  mmcf->exit_worker_code = NGX_CONF_UNSET_PTR;

  mmcf->ctx->mrb = mrb_open();
  if (mmcf->ctx->mrb == NULL)
    return NULL;
  ngx_stream_mrb_class_init(mmcf->ctx->mrb);

  cln->handler = ngx_stream_mruby_cleanup;
  cln->data = mmcf->ctx->mrb;

  ictx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_mruby_internal_ctx_t));
  if (ictx == NULL) {
    return NULL;
  }
  ictx->s = NULL;
  ictx->stream_status = NGX_DECLINED;
  mmcf->ctx->mrb->ud = ictx;

  return mmcf;
}
static ngx_int_t output(ngx_http_request_t *r,void *conf,ngx_str_t type)
{
    ngx_int_t status = 0;
	ngx_image_conf_t *info = conf;
	ngx_http_complex_value_t  cv;
    ngx_pool_cleanup_t            *cln;
    cln = ngx_pool_cleanup_add(r->pool, 0);
    if (cln == NULL) {
        gdFree(info->img_data);
        return status;
    }
    cln->handler = gd_clean_data;
    cln->data = info->img_data;

	ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
	cv.value.len = info->img_size;
	cv.value.data = (u_char *)info->img_data;
    status = ngx_http_send_response(r, NGX_HTTP_OK, &type, &cv);
    return status;
}
static void *
ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf)
{
    ngx_http_modsecurity_main_conf_t *conf;

    dd("creating the ModSecurity main configuration");

    /* ngx_pcalloc already sets all of this scructure to zeros. */
    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_modsecurity_main_conf_t));

    if (conf == NULL) {
        dd("failed to allocate space for the ModSecurity configuration");
        return NGX_CONF_ERROR;
    }

    /* Create our ModSecurity instace */
    conf->modsec = msc_init();
    if (conf->modsec == NULL)
    {
        dd("failed to create the ModSecurity instance");
        return NGX_CONF_ERROR;
    }

    ngx_pool_cleanup_t *cln = NULL;

    /* Provide our connector information to LibModSecurity */
    msc_set_connector_info(conf->modsec, "ModSecurity-nginx v0.0.2-alpha");
    msc_set_log_cb(conf->modsec, ngx_http_modsecurity_log);

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL)
    {
        dd("failed to create the ModSecurity main configuration cleanup");
        return NGX_CONF_ERROR;
    }
    cln->handler = ngx_http_modsecurity_main_config_cleanup;
    cln->data = conf;


    return conf;
}
Ejemplo n.º 28
0
static void *
ngx_stream_geoip_create_conf(ngx_conf_t *cf)
{
    ngx_pool_cleanup_t       *cln;
    ngx_stream_geoip_conf_t  *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_geoip_conf_t));
    if (conf == NULL) {
        return NULL;
    }

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    if (cln == NULL) {
        return NULL;
    }

    cln->handler = ngx_stream_geoip_cleanup;
    cln->data = conf;

    return conf;
}
Ejemplo n.º 29
0
static void *
ngx_dso_create_conf(ngx_cycle_t *cycle)
{
    ngx_dso_conf_ctx_t  *ctx;
    ngx_pool_cleanup_t  *cln;

    ctx = ngx_pcalloc(cycle->pool, sizeof(ngx_dso_conf_ctx_t));

    if (ctx == NULL) {
        return NULL;
    }

    if (ngx_is_init_cycle(cycle->old_cycle)) {
        ngx_memcpy(ngx_static_modules, ngx_modules,
                   sizeof(ngx_module_t *) * ngx_max_module);
        ngx_memcpy(ngx_static_module_names, ngx_module_names,
                   sizeof(u_char *) * ngx_max_module);

    } else {
        ngx_memcpy(ngx_old_modules, ngx_modules,
                   sizeof(ngx_module_t *) * NGX_DSO_MAX);
        ngx_memcpy(ngx_modules, ngx_static_modules,
                   sizeof(ngx_module_t *) * NGX_DSO_MAX);

        ngx_memcpy(ngx_old_module_names, ngx_module_names,
                   sizeof(u_char *) * NGX_DSO_MAX);
        ngx_memcpy(ngx_module_names, ngx_static_module_names,
                   sizeof(u_char *) * NGX_DSO_MAX);
    }

    cln = ngx_pool_cleanup_add(cycle->pool, 0);
    if (cln == NULL) {
        return NULL;
    }

    cln->handler = ngx_dso_cleanup;
    cln->data = cycle;

    return ctx;
}
void *
ngx_postgres_create_upstream_srv_conf(ngx_conf_t *cf)
{
    ngx_postgres_upstream_srv_conf_t  *conf;
    ngx_pool_cleanup_t                *cln;

    dd("entering");

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_postgres_upstream_srv_conf_t));
    if (conf == NULL) {
        dd("returning NULL");
        return NULL;
    }

    /*
     * set by ngx_pcalloc():
     *
     *     conf->peers = NULL
     *     conf->current = 0
     *     conf->servers = NULL
     *     conf->free = { NULL, NULL }
     *     conf->cache = { NULL, NULL }
     *     conf->active_conns = 0
     *     conf->reject = 0
     */

    conf->pool = cf->pool;

    /* enable keepalive (single) by default */
    conf->max_cached = 10;
    conf->single = 1;

    cln = ngx_pool_cleanup_add(cf->pool, 0);
    cln->handler = ngx_postgres_keepalive_cleanup;
    cln->data = conf;

    dd("returning");
    return conf;
}