Ejemplo n.º 1
0
ngx_int_t
ngx_http_xray_header_output_filter(ngx_http_request_t *r)
{
    ngx_http_xray_ctx_t  *xctx = ngx_http_get_module_ctx(r, ngx_http_xray_module);

    if (!xctx || !xctx->xray || xctx->xray->last == xctx->xray->start) {
        /* nothing to be done */
        return next_header_filter(r);
    }

    /** WARN ** Dont add XRAY logs beyond this point! **/
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
        "%s: xray buffer used: %i", __func__, xctx->xray->last - xctx->xray->start);

    if (r->headers_out.content_length_n >= 0) {

        r->headers_out.content_length_n += xctx->xray->last - xctx->xray->start;

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

        ngx_log_debug(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
            "%s: new content length: %i", __func__, r->headers_out.content_length_n);
    }

    return next_header_filter(r);
}
static ngx_int_t
ngx_captcha_get_request_parameter_value( ngx_http_request_t *r, u_char *buffer, ngx_str_t *name, ngx_str_t *value ) {
    
    u_char              *p      = NULL;
    u_char              *v      = NULL; 
    u_char              *last   = NULL;
 
    value->data = NULL;
    value->len = 0;

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

    ngx_log_debug( NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "searching for %s (%d)", name->data, name->len );

    last = buffer + ngx_strlen( buffer ) ;
    
    for ( p = buffer; p < last; p++ ) {
        // we need '=' after name, so drop one char from last 
        p = ngx_strlcasestrn(p, last - 1, name->data, name->len - 1);
        if ( p == NULL ) {
            return NGX_ERROR;
        }

        if ((p == buffer || *(p - 1) == '&') && *(p + name->len) == '=') {
            size_t val_len = 0; 
            size_t dst_len = 0;

            v = p + name->len + 1;
            
            p = ngx_strlchr(p, last, '&');
            if (p == NULL) {
                p = last;
            }
            
            val_len = (p-v);
            
            /* Allocate buffer for request parameter value */
            value->data = ngx_pcalloc(r->pool, val_len + 1);
            if ( value->data == NULL ) {
                ngx_log_debug( NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "--->> BOOOM" );
                return NGX_ERROR;
            }
            /* Unescape parameter value */
            dst_len = (size_t)value->data;
            ngx_unescape_uri(&value->data, &v, val_len, NGX_UNESCAPE_URI);
            dst_len = (size_t) value->data - dst_len;
            *(value->data) = '\0';
            value->data -= dst_len;            
            value->len = dst_len;

            return NGX_OK;
        }
    }

    return ( (value->data == NULL) ? NGX_ERROR: NGX_OK );
}
Ejemplo n.º 3
0
static ngx_int_t
ngx_select_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
{
    ngx_connection_t  *c;

    c = ev->data;

    ngx_log_debug(NGX_LOG_DEBUG_EVENT, ev->log, 0,
                   "%s: select add event fd:%d ev:%i", __func__, c->fd, event);

    if (ev->index != NGX_INVALID_INDEX) {
        ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
                      "select event fd:%d ev:%i is already set", c->fd, event);
        return NGX_OK;
    }

    if ((event == NGX_READ_EVENT && ev->write)
        || (event == NGX_WRITE_EVENT && !ev->write))
    {
        ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
                      "invalid select %s event fd:%d ev:%i",
                      ev->write ? "write" : "read", c->fd, event);
        return NGX_ERROR;
    }


        ngx_log_debug(NGX_LOG_DEBUG_EVENT, ev->log, 0,
                      "select %s event fd:%d  event:%d",
                      ev->write ? "write" : "read", c->fd, event);

#if !(OFP_NOTIFY)
    if (event == NGX_READ_EVENT) {
	if (CHK_FD_BIT(c->fd)) {
            FD_SET(c->fd, (ofp_fd_set *)&master_read_fd_set);
	}
    } else if (event == NGX_WRITE_EVENT) {
        FD_SET(c->fd, &master_write_fd_set);
    }
#endif

    if (max_fd != -1 && max_fd < c->fd) {
        max_fd = c->fd;
    }

    ngx_log_debug(NGX_LOG_DEBUG_EVENT, ev->log, 0,
                      "%s: select event fd:%d  max_fd:%d",
                       __func__, c->fd, max_fd);
    ev->active = 1;

    event_index[nevents] = ev;
    ev->index = nevents;
    nevents++;

    return NGX_OK;
}
/*
 *  The handler for POST requests (that update the RRD database). The
 * thing here is to remember that when this is called, the body might
 * not be available. So, you must to register an extra callback that
 * will be called when the body is available.
 */
ngx_int_t ngx_http_rrd_update_database(ngx_http_request_t *r)
{
    ngx_log_t *log = r->connection->log;
    ngx_int_t rc;

    /* One could think of using ngx_http_test_content_type (I did) but this
     * is irrelevant as ngx_http_test_content_type works on the response
     * content type, not the request. On top of that, there is really
     * no need to go through a hash-table to check ONE value. */
    if (r->headers_in.content_type == NULL
            || r->headers_in.content_type->value.data == NULL
            || r->headers_in.content_type->value.len != WWW_FORM_URLENCODED.len
            || ngx_strncasecmp(r->headers_in.content_type->value.data,
                    WWW_FORM_URLENCODED.data,
                    WWW_FORM_URLENCODED.len) != 0)
    {
        ngx_log_error(NGX_LOG_ALERT, log, 0,
                              (char *) ERR_BAD_CONTENT_TYPE_MSG.data);
        ngx_http_complex_value_t cv = {ERR_BAD_CONTENT_TYPE_MSG, NULL, NULL, NULL};

        return ngx_http_send_response(r, NGX_HTTP_NOT_ALLOWED,
                                      &TEXT_PLAIN, &cv);
    }
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
                          "rrd module: Content-type is OK. Proceeding.");

    ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
                   "rrd module: start reading client request body");

    rc = ngx_http_read_client_request_body(r, ngx_http_rrd_body_received);

    if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
        return rc;
    }

    if (rc == NGX_AGAIN) {
        /*  nginx will call the body_received when needed. Returning
         * NGX_DONE will prevent nginx from calling ngx_http_finalize_request
         * (which we will call in body_received) */
        return NGX_DONE;
    }
    if (NGX_OK == rc) {
        ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
                      "rrd module: client request body already read");
        return rc;
    }

    ngx_log_error(NGX_LOG_ALERT, log, 0,
                   "rrd module: unexpected response code from"
                   "ngx_http_read_client_request_body : %u", rc);
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
Ejemplo n.º 5
0
static ngx_int_t 
kmp_search(u_char *s, ngx_int_t s_len, u_char *p, ngx_int_t p_len, ngx_log_t *log)
{
	if (s_len < 1) return -1;
	if (p_len < 1) return -1;
	if (s_len < p_len) return -1;
	ngx_int_t		 i = 0;
	ngx_int_t		 j = 0;
	ngx_int_t		 next[p_len + 1];
	ngx_memzero(next, p_len + 1);
	get_next(p, next, p_len);

	ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "kmp before while");

	while (i < s_len && j < p_len) {
		// if j == -1 or S[i] == P[j], then i++, j++
		if (j == -1 || s[i] == p[j]) {
			i++;
			j++;
		} else {
			// if j != -1 and S[i] != P[j], then i stay, j = next[j]
			j = next[j];
		}	
	}
	if (j == p_len)
		return i - j;
	else 
		return -1;
}
Ejemplo n.º 6
0
static ngx_int_t 
ngx_xlog_redis_header_filter(ngx_http_request_t *r)
{
	ngx_log_redis_loc_conf_t  *conf;
	int rc = 0;
	char ttm[32];

    conf = (ngx_log_redis_loc_conf_t  *)ngx_http_get_module_loc_conf
		(r, ngx_xlog_redis);
	if (conf == NULL)
		return ngx_http_next_header_filter(r);
		
	if (redis == NULL) {
		redis = credis_connect(NULL, 0, 10000);
	
		if (redis == NULL) {
			return ngx_http_next_header_filter(r); 
		}
	}
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 
			"ngx_log_redis_header_filter:%s", conf->xlog_redis.data);
	snprintf(ttm, sizeof(ttm), "%d", (int)time(NULL));	
	rc = credis_lpush(redis, "xlog_redis", ttm);	
	//rc = credis_hset();
	rc = rc;
	if (r != r->main) {
		return ngx_http_next_header_filter(r);  
	} 

	r->headers_out.content_length_n += sizeof("Nginx-HelightXu\n") - 1;  
	credis_close(redis);
	
	return ngx_http_next_header_filter(r);  
}
Ejemplo n.º 7
0
ngx_int_t
ss_ftp_ssl_certificate(ss_ftp_request *r, ngx_ssl_t *ssl)
{
   assert(NULL != r);
   assert(NULL != ssl);

   ngx_conf_t                 cf;
   ss_ftp_conf_ctx_t         *context;
   ss_ftp_ssl_srv_conf_t     *scf;
   ngx_str_t                  cert;
   ngx_str_t                  key;

   context = r->ctx;
   scf = context->srv_conf[ss_ftp_ssl_module.ctx_index];

   cert = scf->certificate;
   key = scf->certificate_key;
   cf.cycle = *cycle_g;
   if (ngx_ssl_certificate(&cf, ssl, &cert, &key) != NGX_OK) {

      ngx_log_debug(NGX_LOG_DEBUG_FTP, r->connection->log, 0, "ftp:ngx_ssl_certificate error");
      ss_ftp_reply(r, "451", "Server error in processing");
      return NGX_ERROR;
   }

   return NGX_OK;
}
static void ngx_http_upstream_dynamic_server_resolve(ngx_event_t *ev) {
    ngx_http_upstream_dynamic_server_main_conf_t  *udsmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle, ngx_http_upstream_dynamic_servers_module);
    ngx_http_upstream_dynamic_server_conf_t *dynamic_server;
    ngx_resolver_ctx_t *ctx;

    dynamic_server = ev->data;

    ctx = ngx_resolve_start(udsmcf->resolver, NULL);
    if (ctx == NULL) {
        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "upstream-dynamic-servers: resolver start error for '%V'", &dynamic_server->host);
        return;
    }

    if (ctx == NGX_NO_RESOLVER) {
        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "upstream-dynamic-servers: no resolver defined to resolve '%V'", &dynamic_server->host);
        return;
    }

    ctx->name = dynamic_server->host;
    ctx->handler = ngx_http_upstream_dynamic_server_resolve_handler;
    ctx->data = dynamic_server;
    ctx->timeout = udsmcf->resolver_timeout;

    ngx_log_debug(NGX_LOG_DEBUG_CORE, ev->log, 0, "upstream-dynamic-servers: Resolving '%V'", &ctx->name);
    if (ngx_resolve_name(ctx) != NGX_OK) {
        ngx_log_error(NGX_LOG_ALERT, ev->log, 0, "upstream-dynamic-servers: ngx_resolve_name failed for '%V'", &ctx->name);
        ngx_add_timer(&dynamic_server->timer, 1000);
    }
}
ngx_int_t ngx_http_cookie_build_fh(ngx_http_cookie_clean_conf_t *conf)
{
    ngx_array_t *fh = NULL;
    ngx_pool_t  *pool = conf->ctx->pool;
    ngx_log_t   *log = conf->ctx->log;

    fh = ngx_array_create(pool, 10, sizeof(ngx_str_t));

    if (ngx_http_cookie_conf_each_line_proc(conf, &conf->focus_hosts_f, ngx_http_cookie_fh_insert, (void *)fh, NULL) != NGX_OK) {
        ngx_array_destroy(fh);
        ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], fh insert proc failed.", __FUNCTION__, __LINE__);
        return NGX_ERROR;
    }
    conf->fh = fh;
    ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], build focus hosts succeed.", __FUNCTION__, __LINE__);
    return NGX_OK;
}
Ejemplo n.º 10
0
ngx_socket_t ngx_tcp_reuse_get_active_conn(ngx_log_t *log)
{
	ngx_socket_t fd = -1;
	ngx_err_t    err;
	u_char test[2];
	while (!ngx_queue_empty(&active_conns)) {
		ngx_queue_t *head_conn = ngx_queue_head(&active_conns);
		ngx_tcp_reuse_conn_t *active_conn = ngx_queue_data(head_conn, ngx_tcp_reuse_conn_t, q_elt);
		fd = active_conn->fd;
		if (active_conn->read.timer_set) {
			ngx_del_timer(&active_conn->read);
		}
		if (active_conn->write.timer_set) {
			ngx_del_timer(&active_conn->write);
		}
		if (active_conn->read.active) {
			ngx_del_event(&active_conn->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
		}
		if (active_conn->write.active) {
			ngx_del_event(&active_conn->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
		}
		ngx_queue_remove(&active_conn->q_elt);
		ngx_memzero(active_conn, sizeof(ngx_tcp_reuse_conn_t));
		ngx_queue_insert_tail(&empty_conns, &active_conn->q_elt);

		if (recv(fd, test, 0, 0) == 0) {
			ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "0 : errno:%d, %s", ngx_socket_errno, strerror(errno));
			close(fd);
			fd = -1;
		} else {
			ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "!0 : errno:%d, %s", ngx_socket_errno, strerror(errno));
			err = ngx_socket_errno;
			if (err == 11)
				break;
			else {
				close(fd);
				fd = -1;
			}
		}
		ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "fd:%d", fd);

	}
	return fd;
}
/*
 *  Handles the GET requests by getting RRD to graph and sending the result
 * as an HTTP response.
 */
static ngx_int_t ngx_http_rrd_show_graph(ngx_http_request_t *r)
{
    ngx_log_t* log = r->connection->log;
    ngx_http_rrd_loc_conf_t *rrd_conf;
    rrd_conf = ngx_http_get_module_loc_conf(r, ngx_http_rrd_module);

    /* Prepare args for rrdgraph */
    int rrd_argc = -1;
    char** rrd_arg = ngx_http_rrd_create_graph_arg(&rrd_argc, r,
                                                   rrd_conf->db_name_cstyle);
    if (NULL == rrd_arg) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    rrd_clear_error();
    rrd_info_t* const rrd_graph_info = rrd_graph_v(rrd_argc, rrd_arg);
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0,
                  "rrd_graph (%s, %s, %s, %s) returned %p.",
                  rrd_arg[0], rrd_arg[1], rrd_arg[2], rrd_arg[3],
                  rrd_graph_info);
    if (NULL == rrd_graph_info) {
        ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
        return ngx_http_rrd_outprintf(r, NGX_HTTP_INTERNAL_SERVER_ERROR,
                            "Error graphing DB %s: %s", rrd_conf->db_name_cstyle,
                            rrd_get_error());
    }
    rrd_info_t* it;
    u_char* image_data = NULL;
    size_t image_size = 0;
    for(it = rrd_graph_info; NULL == image_data && it != NULL; it = it->next) {
        if (it->key != NULL
            && ngx_strncmp(it->key, "image", sizeof("image")) == 0) {
            image_data = it->value.u_blo.ptr;
            image_size = it->value.u_blo.size;
        }
    }
    if (NULL == image_data) {
        ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
        rrd_info_free(rrd_graph_info);
        return ngx_http_rrd_outprintf(r, NGX_HTTP_INTERNAL_SERVER_ERROR,
                            "No image returned graphing DB %s.",
                            rrd_conf->db_name_cstyle);
    }
    ngx_chain_t* out_chain = ngx_http_rrd_get_picture_outchain(r->pool,
                                                      image_data, image_size);
    if (NULL == out_chain) {
        ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
        rrd_info_free(rrd_graph_info);
        return ngx_http_rrd_outprintf(r, NGX_HTTP_INTERNAL_SERVER_ERROR,
                            "Problem returning image for DB %s.",
                            rrd_conf->db_name_cstyle);
    }
    ngx_http_rrd_free_graph_arg(r->pool, rrd_argc, rrd_arg);
    rrd_info_free(rrd_graph_info);
    return ngx_http_rrd_png_file_200(r, out_chain);
}
// old url: http://cdn_hostname/download/nginx.zip/9405801ae719fb8458b0c479efe87f30/536D4489
// new url: http://cdn_hostname/download/nginx.zip?k=9405801ae719fb8458b0c479efe87f30&e=536D4489
static ngx_int_t ngx_http_secure_download_split_uri(ngx_http_request_t *r, ngx_http_secure_download_split_uri_t *sdsu)
{
  int len = r->uri.len;
  size_t args_len = r->args.len;
  u_char *args = r->args.data;
  const char *uri = (char*)r->uri.data;

  ngx_http_secure_download_loc_conf_t *sdc = ngx_http_get_module_loc_conf(r, ngx_http_secure_download_module);
  if (args == NULL){ 
	  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "timestamp and md5 is not found");
	  return NGX_ERROR;
  }

  // potential problems:
  // 1) args may not ends with "\0"
  // 2) checking the length of timestamp and md5 is postponed
  sdsu->timestamp = (char*)ngx_strnstr(args, "e=", args_len);
  if (sdsu->timestamp == NULL) {
	  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "timestamp not found");
	  return NGX_ERROR;
  } else {
	  sdsu->timestamp += 2;
  }

  sdsu->md5 = (char*)ngx_strnstr(args, "k=", args_len);
  if (sdsu->md5 == NULL) {
	  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "md5 not found");
	  return NGX_ERROR;
  } else {
	  sdsu->md5 += 2;
  }

  sdsu->path = uri;
  sdsu->path_len = len;

  if(sdc->path_mode == FOLDER_MODE) {
	  while(len && uri[--len] != '/');
  }
  sdsu->path_to_hash_len = len;

  return NGX_OK;
}
char * ngx_http_cookie_clean_merge_conf(ngx_conf_t *cf,
        void *parent, void *child)
{
    ngx_http_cookie_clean_conf_t *prev = parent;
    ngx_http_cookie_clean_conf_t *conf = child;

    ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], start merge cookie clean config", __FUNCTION__, __LINE__);

    if (conf->enable == NGX_HTTP_COOKIE_CLEAN_UNSET) {
        conf->enable        = prev->enable;
        if (conf->enable == NGX_HTTP_COOKIE_CLEAN_UNSET) {
            conf->enable = NGX_HTTP_COOKIE_CLEAN_OFF;
        }
        conf->ac_builded    = prev->ac_builded;
        conf->fh_builded    = prev->fh_builded;
        conf->focus_hosts_f = prev->focus_hosts_f;
        conf->cookie_list_f = prev->cookie_list_f;
    }

    if (conf->enable == NGX_HTTP_COOKIE_CLEAN_ON) {
        if (conf->fh_builded == 0) {
            if (ngx_http_cookie_build_fh(conf) != NGX_OK) {
                ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], cookie clean build fh failed.", __FUNCTION__, __LINE__);
                return NGX_CONF_ERROR;
            }
            ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], cookie clean build fh succeed.", __FUNCTION__, __LINE__);
            conf->fh_builded = 1;
        } else {
            if (conf->fh == NULL) {
                conf->fh = prev->fh;
                ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], cookie clean using parent fh.", __FUNCTION__, __LINE__);
            }
        }
        if (conf->ac_builded == 0) {
            if (ngx_http_cookie_build_ac(conf) != NGX_OK) {
                ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], cookie clean build ac failed.", __FUNCTION__, __LINE__);
                return NGX_CONF_ERROR;
            }
            conf->ac_builded = 1;
            ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], cookie clean build ac succeed.", __FUNCTION__, __LINE__);
        } else {
            if (conf->ac == NULL) {
                conf->ac = prev->ac;
                ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], cookie clean using parent ac.", __FUNCTION__, __LINE__);
            }
        }
    }
    ngx_log_debug(NGX_LOG_NOTICE, cf->log, 0, "[%s:%d], merge cookie clean config succeed.", __FUNCTION__, __LINE__);

    return NGX_CONF_OK;
}
static ngx_int_t ngx_http_secure_download_split_uri(ngx_http_request_t *r, ngx_http_secure_download_split_uri_t *sdsu)
{
  int md5_len = 0;
  int tstamp_len = 0;
  int len = r->uri.len;
  const char *uri = (char*)r->uri.data;

  ngx_http_secure_download_loc_conf_t *sdc = ngx_http_get_module_loc_conf(r, ngx_http_secure_download_module);

  while(len && uri[--len] != '/')
	  ++tstamp_len;
  if(tstamp_len != 8) {
	  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: timestamp size mismatch: %d", tstamp_len);
	  return NGX_ERROR;
  }
  sdsu->timestamp = uri + len + 1;

  while(len && uri[--len] != '/')
	  ++md5_len;
  if(md5_len != 32) {
	  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: md5 size mismatch: %d", md5_len);
	  return NGX_ERROR;
  }
  sdsu->md5 = uri + len + 1;

  if(len == 0) {
	  ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "securedownload: bad path", 0);
	  return NGX_ERROR;
  }

  sdsu->path = uri;
  sdsu->path_len = len;

  if(sdc->path_mode == FOLDER_MODE) {
	  while(len && uri[--len] != '/');
  }
  sdsu->path_to_hash_len = len;

  return NGX_OK;
}
Ejemplo n.º 15
0
void ngx_http_foo_post_handler(ngx_http_request_t *r){
    // 请求全部读完后从这里入口, 可以产生响应
    /*ngx_http_finalize_request(r, NGX_HTTP_OK);*/

    int result = get_route_id(r);
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "get_route_id result:%d", result);
    if (result < 0)
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "get_route_id fail, result:%d", result);
        result = DFT_ROUTE_ID;
    }
    ngx_http_finalize_request(r, result);
}
ngx_int_t ngx_http_cookie_build_ac(ngx_http_cookie_clean_conf_t *conf)
{
    ngx_http_ac_t *ac = NULL;
    ngx_log_t     *log = conf->ctx->log;
    ngx_pool_t    *pool = conf->ctx->pool;

    ngx_int_t  value = 1;

    ac = ngx_pcalloc(pool, sizeof(ngx_http_ac_t));
    if (ac == NULL) {
        ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], pcalloc ac failed.", __FUNCTION__, __LINE__);
        return NGX_ERROR;
    }
    ac->node_num = 0;
    ac->set_size = 0;
    ac->ctx      = conf->ctx;
    memset(ac->set, -1, sizeof(ac->set));

    ac->root = ngx_ac_node_calloc(ac);
    if (ac->root == NULL) {
        ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], ac node calloc failed.", __FUNCTION__, __LINE__);
        ngx_ac_destroy(ac);
        return NGX_ERROR;
    }
    if (ngx_http_cookie_conf_each_line_proc(conf, &conf->cookie_list_f,
                                            ngx_http_cookie_ac_insert, (void *)ac, (void *)&value) != NGX_OK) {
        ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], ac insert proc failed ", __FUNCTION__, __LINE__);
        ngx_ac_destroy(ac);
        return NGX_ERROR;
    }
    if (ngx_ac_build(ac) != NGX_OK) {
        ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], ac build failed ", __FUNCTION__, __LINE__);
        ngx_ac_destroy(ac);
        return NGX_ERROR;
    }
    conf->ac = ac;
    ngx_log_debug(NGX_LOG_NOTICE, log, 0, "[%s:%d], build cookie white list ac succeed.", __FUNCTION__, __LINE__);
    return NGX_OK;
}
Ejemplo n.º 17
0
int ngx_rpc_notify_unregister(ngx_rpc_notify_t* notify)
{
    ngx_log_debug(NGX_LOG_DEBUG_ALL, ngx_cycle->log, 0,
                  "ngx_rpc_notify_unregister notify:%p eventfd:%d",
                  notify, notify->event_fd);

    ngx_del_conn(notify->notify_conn, 0);
    close(notify->event_fd);

    notify->notify_conn->pool = NULL;
    ngx_free_connection(notify->notify_conn);

    return NGX_OK;
}
static void
ngx_http_cloudrouter_peer_preconnect_close(ngx_connection_t *c, ngx_http_cloudrouter_peer_preconnect_data_t *pcd, ngx_int_t status) {
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pcd->r->connection->log, 0, "preconnect_close %d", status);
    ngx_close_connection(c); c->destroyed = 1;
    pcd->r->main->count--;
    if (status==NGX_OK) {
        ngx_http_upstream_connect_real(pcd->r, pcd->u);
    } else {
        ngx_log_debug(NGX_LOG_DEBUG_HTTP, pcd->r->connection->log, 0,
                       "preconnect: ERROR");
        ngx_http_upstream_next(pcd->r,pcd->u,NGX_HTTP_UPSTREAM_FT_ERROR);
    }
    return;
}
Ejemplo n.º 19
0
static void ngx_rpc_notify_write_handler(ngx_event_t *ev)
{

    ngx_connection_t *notify_con = ev->data;
    ngx_rpc_notify_t *notify = (ngx_rpc_notify_t *)(notify_con->sockaddr);


    ngx_log_debug(NGX_LOG_DEBUG_ALL, ev->log, 0,
                  "ngx_rpc_notify_write_handler notify:%p eventfd:%d",
                  notify, notify->event_fd);

    ngx_del_event(notify_con->write, NGX_WRITE_EVENT, 0);

    notify->write_hanlder(notify->ctx);
}
Ejemplo n.º 20
0
ngx_int_t
ss_ftp_ssl_create(ss_ftp_request *r, ngx_ssl_t *ssl)
{
   assert(NULL != r);
   assert(NULL != ssl);

   if (ngx_ssl_create(ssl, 14, NULL) != NGX_OK) {
      /* make "14" to be more elegant */
      ngx_log_debug(NGX_LOG_DEBUG_FTP, r->connection->log, 0, "ftp:ngx_ssl_create error");
      ss_ftp_reply(r, "451", "Server error in processing");
      return NGX_ERROR;
   }

   return NGX_OK;
}
static void
ngx_http_upstream_free_chash_peer(ngx_peer_connection_t *pc, void *data,
    ngx_uint_t state)
{
    ngx_http_upstream_chash_peer_data_t *uchpd = data;
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, pc->log, 0,
                  "consistent hash free  peer %ui", state);

    if (uchpd->server == NULL) {
        return;
    }

    if (state & NGX_PEER_FAILED) {
        uchpd->server->peer->fails++;
    }
}
static ngx_int_t ngx_http_upstream_init_q_chash_peer(ngx_http_request_t *r, ngx_http_upstream_srv_conf_t *us)
{
    ngx_int_t                                       rc;
    ngx_http_upstream_q_chash_srv_conf_t            *uchscf;
    ngx_http_upstream_q_chash_peer_data_t           *qchp;
    ngx_http_upstream_q_chash_ring                  *q_chash_ring;
    ngx_str_t                                       evaluated_key_to_hash;

    uchscf = ngx_http_conf_upstream_srv_conf(us, ngx_http_upstream_q_chash_module);
    if (uchscf == NULL) {
        return NGX_ERROR;
    }

    q_chash_ring = uchscf->q_chash_ring;

    qchp = ngx_pcalloc(r->pool, sizeof(*qchp));
    if(qchp == NULL)
        return NGX_ERROR;
    r->upstream->peer.data = &qchp->rrp;

    qchp->q_chash_ring = q_chash_ring;
    qchp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
    qchp->tries = 0;
    qchp->ignore = 0;
    qchp->rr_mode = 0;

    rc = ngx_http_upstream_init_round_robin_peer(r, us);
    if(rc != NGX_OK)
        return NGX_ERROR;

    r->upstream->peer.get = ngx_http_upstream_get_q_chash_peer;

    // calculate the vnode_index
    if(q_chash_ring->nr_valid_peers > 1) {
        if (ngx_http_script_run(r, &evaluated_key_to_hash, uchscf->lengths->elts, 0, uchscf->values->elts) == NULL)
            return NGX_ERROR;

        qchp->point = (uint32_t)ngx_crc32_long(evaluated_key_to_hash.data, evaluated_key_to_hash.len);
        qchp->vnode_index = q_chash_find(q_chash_ring, qchp->point);

        ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "q_chash key %V, point %uD, vnode_index %ui", &evaluated_key_to_hash, qchp->point, qchp->vnode_index);
    }

    return NGX_OK;
}
Ejemplo n.º 23
0
int ngx_tcp_reuse_pool_init(ngx_log_t *log)
{

	ngx_reuse_pool = ngx_create_pool(ngx_tcp_reuse_pool_size, log);
	if (ngx_reuse_pool == NULL) {
		ngx_log_error(NGX_LOG_EMERG, log, 0, "could not create ngx_reuse_pool");
		exit(1);
	}
	conns.elts = ngx_pcalloc(ngx_reuse_pool, ngx_tcp_reuse_conns_init_size * sizeof (ngx_tcp_reuse_conn_t));
	conns.nelts = 0;
	conns.size = sizeof (ngx_tcp_reuse_conn_t);
	conns.nalloc = ngx_tcp_reuse_conns_init_size;
	conns.pool = ngx_reuse_pool;

	ngx_queue_init(&empty_conns);
	ngx_queue_init(&active_conns);
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_tcp_reuse_pool_init ok");
	return NGX_OK;
}
static char *ngx_http_upstream_dynamic_servers_merge_conf(ngx_conf_t *cf, void *parent, void *child) {
    // If any dynamic servers are present, verify that a "resolver" is setup as
    // the http level.
    ngx_http_upstream_dynamic_server_main_conf_t  *udsmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_dynamic_servers_module);

    if (udsmcf->dynamic_servers.nelts > 0) {
        ngx_http_core_loc_conf_t *core_loc_conf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
#if nginx_version >= 1009011
        if (core_loc_conf->resolver == NULL || core_loc_conf->resolver->connections.nelts == 0) {
#else
        if (core_loc_conf->resolver == NULL || core_loc_conf->resolver->udp_connections.nelts == 0) {
#endif
            ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "resolver must be defined at the 'http' level of the config");
            return NGX_CONF_ERROR;
        }
        udsmcf->conf_ctx = cf->ctx;
        udsmcf->resolver = core_loc_conf->resolver;
        ngx_conf_merge_msec_value(udsmcf->resolver_timeout, core_loc_conf->resolver_timeout, 30000);
    }

    return NGX_CONF_OK;
}

static ngx_int_t ngx_http_upstream_dynamic_servers_init_process(ngx_cycle_t *cycle) {
    ngx_http_upstream_dynamic_server_main_conf_t  *udsmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_upstream_dynamic_servers_module);
    ngx_http_upstream_dynamic_server_conf_t       *dynamic_server = udsmcf->dynamic_servers.elts;
    ngx_uint_t i;
    ngx_event_t *timer;
    ngx_uint_t refresh_in;

    for (i = 0; i < udsmcf->dynamic_servers.nelts; i++) {
        timer = &dynamic_server[i].timer;
        timer->handler = ngx_http_upstream_dynamic_server_resolve;
        timer->log = cycle->log;
        timer->data = &dynamic_server[i];

        refresh_in = ngx_random() % 1000;
        ngx_log_debug(NGX_LOG_DEBUG_CORE, cycle->log, 0, "upstream-dynamic-servers: Initial DNS refresh of '%V' in %ims", &dynamic_server[i].host, refresh_in);
        ngx_add_timer(timer, refresh_in);
    }

    return NGX_OK;
}
static const char *
ngx_rtmp_control_drop_handler(ngx_http_request_t *r, ngx_rtmp_session_t *s,
    ngx_rtmp_core_srv_conf_t *cscf,
    ngx_rtmp_core_app_conf_t **cacf)
{
    ngx_rtmp_control_ctx_t  *ctx;

    ctx = ngx_http_get_module_ctx(r, ngx_rtmp_control_module);

    ngx_log_debug(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
            "control: drop static_relay='%d'",
            s->static_relay);

    s->static_relay = 0;
    ngx_rtmp_finalize_session(s);

    ++ctx->count;

    return NGX_CONF_OK;
}
Ejemplo n.º 26
0
ngx_int_t
ss_ftp_ssl_create_connection(ngx_connection_t *c, ngx_ssl_t *ssl)
{
   assert(NULL != c);
   assert(NULL != ssl);

   ss_ftp_request *r;

   r = (ss_ftp_request *) c->data;
   assert(NULL != r);

   if (ngx_ssl_create_connection(ssl, c, 1) != NGX_OK) {
      /* TODO : make "1" to be more elegant */
      ngx_log_debug(NGX_LOG_DEBUG_FTP, r->connection->log, 0, "ftp:ngx_ssl_create_connection error");
      ss_ftp_reply(r, "451", "Server error in processing");
      return NGX_ERROR;
   }

   return NGX_OK;
}
Ejemplo n.º 27
0
static void ngx_rpc_notify_read_handler(ngx_event_t *ev){

    ngx_connection_t *notify_con = ev->data;
    ngx_rpc_notify_t *notify = (ngx_rpc_notify_t *)(notify_con->sockaddr);

    //read and do all the work;
    ngx_uint_t signal = 1;

    int ret = 0;
    do{
        ret = read(notify->event_fd, (char*)&signal, sizeof(signal));

        ngx_log_debug(NGX_LOG_DEBUG_ALL, ev->log, 0,
                      "ngx_rpc_notify_read_handler notify:%p eventfd:%d signal:%d ,ret:%d",
                      notify, notify->event_fd, signal, ret);

        notify->read_hanlder(notify->ctx);

    }while(ret > 0);
}
static void
ngx_http_cloudrouter_peer_preconnect_write(ngx_event_t *wev) {
    ngx_connection_t *c;
    ngx_http_cloudrouter_peer_preconnect_data_t *pcd;
    ngx_http_upstream_t *u;
    ngx_http_request_t *r;
    ngx_http_cloudrouter_peer_t *peer;

    c = wev->data;
    pcd = c->data;

    r = pcd->r;
    u = pcd->u;

    if(wev->timedout) {
        ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                      "preconnect: write: timedout");
        return ngx_http_cloudrouter_peer_preconnect_close(c, pcd, NGX_ERROR);
    }

    if (pcd->done>0) {
        return;
    }

    if(r->main==NULL||r->request_complete||r->pool==NULL||r!=r->main) {
        ngx_close_connection(c); c->destroyed = 1;
        return;
    }

    peer = (ngx_http_cloudrouter_peer_t*)u->peer.data;

    if(pcd->sendbufpos < peer->sendbuf.len) {
        int n = ngx_send(c, peer->sendbuf.data + pcd->sendbufpos,
                         peer->sendbuf.len - pcd->sendbufpos);
        pcd->sendbufpos += n;
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                       "preconnect: write: %d of %d", pcd->sendbufpos,
                       peer->sendbuf.len);
    }
}
Ejemplo n.º 29
0
// Call to initialize xray buffer for this request
ngx_int_t
ngx_http_xray_init(ngx_http_request_t *r, ngx_int_t level)
{
    ngx_http_xray_main_conf_t *xmcf = ngx_http_get_module_main_conf(r, ngx_http_xray_module);
    ngx_http_xray_ctx_t       *xctx = ngx_http_get_module_ctx(r, ngx_http_xray_module);

    if (xctx == NULL) {
        xctx = ngx_pcalloc(r->pool, sizeof(ngx_http_xray_ctx_t));
    }

    if (xctx == NULL) {
        ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
            "%s: Error setting context", __func__);

        return NGX_ERROR;
    }

    ngx_http_set_ctx(r, xctx, ngx_http_xray_module);
    xctx->xray_level = level;

    if (level == 0) {
        return NGX_OK;
    }

    xctx->xray = ngx_create_temp_buf(r->pool, xmcf->xray_buffer_size);
    if (xctx->xray == NULL) {
        ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
            "%s: failed to ngx_create_temp_buf %z bytes", __func__, xmcf->xray_buffer_size);

        return NGX_ERROR;
    }

    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
        "%s: level=%i", __func__, level);

    return NGX_OK;
}
Ejemplo n.º 30
0
/*
** Parse a JSON request
*/
void
ngx_http_dummy_json_parse(ngx_http_request_ctx_t *ctx, 
			  ngx_http_request_t	 *r,
			  u_char		 *src,
			  u_int			 len)
{
  ngx_json_t				*js;
  
  
  js = ngx_pcalloc(r->pool, sizeof(ngx_json_t));
  if (!js) return ;
  js->json.data = js->src = src;
  js->json.len = js->len = len;
  js->r = r;
  js->ctx = ctx;
  js->loc_cf = ngx_http_get_module_loc_conf(r, ngx_http_naxsi_module);
  js->main_cf = ngx_http_get_module_main_conf(r, ngx_http_naxsi_module);
  
  if (ngx_http_nx_json_seek(js, '{')) {
    ngx_http_apply_rulematch_v_n(&nx_int__invalid_json, ctx, r, NULL, NULL, BODY, 1, 0);
    return ;
  }
  if (ngx_http_nx_json_obj(js) != NGX_OK) {
    ngx_http_apply_rulematch_v_n(&nx_int__invalid_json, ctx, r, NULL, NULL, BODY, 1, 0);
#ifdef naxsi_json_debug
    ngx_log_debug(NGX_LOG_DEBUG_HTTP, js->r->connection->log, 0, "nx_json_obj returned error, apply invalid_json.");
#endif        
    
  }
  /* we are now on closing bracket, check for garbage. */
  js->off++;
  ngx_http_nx_json_forward(js);
  if (js->off != js->len)
    ngx_http_apply_rulematch_v_n(&nx_int__invalid_json, ctx, r, NULL, NULL, BODY, 1, 0);
  return ;
}