ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
	ngx_int_t rc = ngx_http_discard_request_body(r);
	if (rc != NGX_OK) {
		return rc;
	}

	ngx_str_t type = ngx_string("text/plain");
	ngx_str_t response = ngx_string("Hello World!");
	r->headers_out.status = NGX_HTTP_OK;
	r->headers_out.content_length_n = response.len;
	r->headers_out.content_type = type;
	rc = ngx_http_send_header(r);
	if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
		return rc;
	}

	ngx_buf_t *b;
	b = ngx_create_temp_buf(r->pool, response.len);
	if (b == NULL) {
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}
	ngx_memcpy(b->pos, response.data, response.len);
	b->last = b->pos + response.len;
	b->last_buf = 1;

	ngx_chain_t out;
	out.buf = b;
	out.next = NULL;
	return ngx_http_output_filter(r, &out);
}
static ngx_int_t
ngx_http_graphicsmagick_handler(ngx_http_request_t *r)
{
	ngx_int_t	  rc;

	if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_POST))) {
		return NGX_HTTP_NOT_ALLOWED;
	}
	
	if (r->method & NGX_HTTP_GET) {
		// handle get request, the convert source file is on local
		rc = ngx_http_discard_request_body(r);

		if (rc != NGX_OK) {
			return rc;
		}
	
		ngx_http_graphicsmagick_command_handler(r);
	} else {
		// handle post request, the convert source file is in request body
		r->request_body_in_file_only = 1;
		r->request_body_in_persistent_file = 1;
		r->request_body_in_clean_file = 1;
		r->request_body_file_group_access = 1;
		r->request_body_file_log_level = 0;
	
		rc = ngx_http_read_client_request_body(r, ngx_http_graphicsmagick_command_handler);
	
		if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
			return rc;
		}
	}
	
	return NGX_DONE;
}
示例#3
0
static int
ngx_http_lua_ngx_req_discard_body(lua_State *L)
{
    ngx_http_request_t          *r;
    ngx_int_t                    rc;
    int                          n;

    n = lua_gettop(L);

    if (n != 0) {
        return luaL_error(L, "expecting 0 arguments but seen %d", n);
    }

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "request object not found");
    }

    ngx_http_lua_check_fake_request(L, r);

    rc = ngx_http_discard_request_body(r);

    if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
        return luaL_error(L, "failed to discard request body");
    }

    return 0;
}
static int
ngx_http_lua_ngx_req_discard_body(lua_State *L)
{
    ngx_http_request_t          *r;
    ngx_int_t                    rc;
    int                          n;

    n = lua_gettop(L);

    if (n != 0) {
        return luaL_error(L, "expecting 0 arguments but seen %d", n);
    }

    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
    lua_rawget(L, LUA_GLOBALSINDEX);
    r = lua_touserdata(L, -1);
    lua_pop(L, 1);

    if (r == NULL) {
        return luaL_error(L, "request object not found");
    }

    rc = ngx_http_discard_request_body(r);

    if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
        return luaL_error(L, "failed to discard request body");
    }

    return 0;
}
static ngx_int_t
ngx_http_memcached_handler(ngx_http_request_t *r)
{
    ngx_int_t                       rc;
    ngx_http_upstream_t            *u;
    ngx_http_memcached_ctx_t       *ctx;
    ngx_http_memcached_loc_conf_t  *mlcf;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    rc = ngx_http_discard_request_body(r);

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

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

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

    u = r->upstream;

    ngx_str_set(&u->schema, "memcached://");
    u->output.tag = (ngx_buf_tag_t) &ngx_http_memcached_module;

    mlcf = ngx_http_get_module_loc_conf(r, ngx_http_memcached_module);

    u->conf = &mlcf->upstream;

    u->create_request = ngx_http_memcached_create_request;
    u->reinit_request = ngx_http_memcached_reinit_request;
    u->process_header = ngx_http_memcached_process_header;
    u->abort_request = ngx_http_memcached_abort_request;
    u->finalize_request = ngx_http_memcached_finalize_request;

    ctx = ngx_palloc(r->pool, sizeof(ngx_http_memcached_ctx_t));
    if (ctx == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ctx->request = r;

    ngx_http_set_ctx(r, ctx, ngx_http_memcached_module);

    u->input_filter_init = ngx_http_memcached_filter_init;
    u->input_filter = ngx_http_memcached_filter;
    u->input_filter_ctx = ctx;

    r->main->count++;

    ngx_http_upstream_init(r);

    return NGX_DONE;
}
static ngx_int_t
ngx_http_groonga_handler_get(ngx_http_request_t *r)
{
  ngx_int_t rc;
  ngx_str_t command_path;
  ngx_http_groonga_handler_data_t *data;

  rc = ngx_http_groonga_extract_command_path(r, &command_path);
  if (rc != NGX_OK) {
    return rc;
  }

  rc = ngx_http_groonga_handler_create_data(r, &data);
  if (rc != NGX_OK) {
    return rc;
  }

  rc = ngx_http_groonga_handler_process_command_path(r, &command_path, data);
  if (rc != NGX_OK) {
    return rc;
  }

  /* discard request body, since we don't need it here */
  rc = ngx_http_discard_request_body(r);
  if (rc != NGX_OK) {
    return rc;
  }

  rc = ngx_http_groonga_handler_send_response(r, data);

  return rc;
}
/*实际处理函数*/
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
    ngx_int_t rc = NGX_ERROR;
    /*必须是GET或者HEAD方法 否则返回405*/
    if( !(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD)) )
    {
        return NGX_HTTP_NOT_ALLOWED;
    }

    /*丢弃请求的包体*/
    rc = ngx_http_discard_request_body(r);
    if(rc != NGX_OK)
    {
        return rc;
    }

    /*设置http头*/
    ngx_str_t type = ngx_string("text/plain"); /*设置Content-Type*/
    ngx_str_t response = ngx_string("Hello World!"); /*响应的包体内容*/
    r->headers_out.status = NGX_HTTP_OK;  /*返回状态*/
    r->headers_out.content_length_n = response.len; /*响应包体的长度*/
    r->headers_out.content_type = type;  /*设置Content-Type*/

    ngx_table_elt_t *h = ngx_list_push(&(r->headers_out.headers));
    if(h == NULL)
    {
        return NGX_ERROR;
    }
    h->hash = 1;
    h->key.len = sizeof("TestHead") - 1;
    h->key.data = (u_char *) "TestHead";
    h->value.len = sizeof("TestHead") - 1;
    h->value.data = (u_char *) "TestHead";

    /*发送http头*/
    rc = ngx_http_send_header(r);
    if(rc == NGX_ERROR || rc > NGX_OK || r->header_only)
    {
        return rc;
    }

    ngx_buf_t *b;
    b = ngx_create_temp_buf(r->pool, response.len);
    if(b == NULL)
    {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    ngx_memcpy(b->pos, response.data, response.len);
    b->last = b->pos + response.len;
    b->last_buf = 1;

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

    return ngx_http_output_filter(r, &out);
}
示例#8
0
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
	// 处理头部
	if(!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD | NGX_HTTP_POST)))
	{
		return NGX_HTTP_NOT_ALLOWED;
	}
	
	ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "HEAD REACH13!\n");
	
	#ifndef HTTP_BODY_ON	
	// 丢弃请求的包体
	ngx_int_t rc = ngx_http_discard_request_body(r);
	if (rc != NGX_OK)
	{
		return rc;
	}
	#else
	// 接收包体
	ngx_int_t rc = ngx_http_read_client_request_body(r, ngx_http_mytest_body_handler);
	if (rc >= NGX_HTTP_SPECIAL_RESPONSE)
	{
		return rc;
	}

	#endif

	return 	NGX_DONE;
	
	/*构造发送内容*/
	// 构造发送头
	ngx_str_t type = ngx_string("text/plain");				// 包的种类
	ngx_str_t response = ngx_string("hello world!");		// 包的内容
	r->headers_out.status = NGX_HTTP_OK;					// 状态值
	r->headers_out.content_length_n = response.len;			// 包中内容的长度
	r->headers_out.content_type = type;						// 包中内容的种类
	
	rc = ngx_http_send_header(r);							// 发送头部
	
	ngx_buf_t *b;
	b = ngx_create_temp_buf(r->pool, response.len);			// 申请发送数据的内存,并初始化
	if (b == NULL)
	{
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}
	
	ngx_memcpy(b->pos, response.data, response.len);		// 将要发送的内容复制到内存
	b->last = b->pos + response.len;
	b->last_buf = 1;										// 声明这事最后一块缓存区
	
	// 构造发送时的ngx_chain_t结构体
	ngx_chain_t out;
	out.buf = b;											// 要发的内容初始化
	out.next = NULL;										// next为空
	ngx_http_output_filter(r, &out);						// 发送包体
	return 	NGX_DONE;
}
static ngx_int_t
ngx_http_static_delete_handler(ngx_http_request_t *r)
{
    u_char                              *last;
    ngx_int_t                            rc;
    ngx_str_t                            filename;
    ngx_str_t                            path;
    size_t                               root;
    ngx_http_static_delete_loc_conf_t   *sdlcf;

    sdlcf = ngx_http_get_module_loc_conf(r, ngx_http_static_delete_module);

    if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

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

    rc = ngx_http_complex_value(r, &sdlcf->filename, &filename);
    if (rc != NGX_OK) {
        return NGX_ERROR;
    }

    if (filename.data[filename.len - 1] == '/') {
        return NGX_HTTP_NOT_FOUND;
    }

    if (ngx_strstr(filename.data, "./")) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    if (ngx_strstr(filename.data, "../")) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    last = ngx_http_map_filename_to_path(r, &filename, &path);
    if (last == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    path.len = last - path.data;

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, 
                   "http filename: \"%s\"", path.data);

    if (ngx_delete_file(path.data) == NGX_FILE_ERROR) {
        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno, 
                ngx_delete_file_n " \"%s\" failed", path.data);
        return NGX_HTTP_NOT_FOUND;
    }

    return ngx_http_static_delete_send_response(r, &path);
}
示例#10
0
static ngx_int_t ngx_http_calc_handler(ngx_http_request_t *r)
{
	ngx_int_t rc;
	ngx_buf_t *b;
	ngx_chain_t out;
	u_char ngx_string[1024]={0};
	ngx_uint_t content_length = 0;
	
	ngx_str_t arg1;
	ngx_str_t arg2;
	ngx_int_t ans;

	ngx_http_arg(r,(u_char *)"arg1",4,&arg1);
	ngx_http_arg(r,(u_char *)"arg2",4,&arg2);
	ans = ngx_atoi(arg1.data,arg1.len)+ngx_atoi(arg2.data,arg2.len);

	ngx_sprintf(ngx_string,"%V + %V = %d",&arg1,&arg2,ans);
	content_length = ngx_strlen(ngx_string);

	if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))){
		return NGX_HTTP_NOT_ALLOWED;
	}

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

	ngx_str_t type = ngx_string("text/html");
	r->headers_out.status = NGX_HTTP_OK;
	r->headers_out.content_length_n = content_length;
	r->headers_out.content_type = type;

	/* HTTP框架提供的发送HTTP头部的方法 */
	/* 返回NGX_ERROR或返回值大于0就表示不正常 */
	rc = ngx_http_send_header(r);
	if (rc == NGX_ERROR || rc > NGX_OK || r->header_only){
		return rc;
	}

	b = ngx_pcalloc(r->pool,sizeof(ngx_buf_t));
	if(b==NULL){
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	b->pos = ngx_string;
	b->last = ngx_string+content_length;
	b->memory = 1;
	b->last_buf = 1;

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

	return ngx_http_output_filter(r,&out);
}
static ngx_int_t
ngx_http_empty_gif_handler(ngx_http_request_t *r)
{
    ngx_int_t     rc;
    ngx_buf_t    *b;
    ngx_chain_t   out;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    rc = ngx_http_discard_request_body(r);

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

    r->headers_out.content_type.len = sizeof("image/gif") - 1;
    r->headers_out.content_type.data = (u_char *) "image/gif";

    if (r->method == NGX_HTTP_HEAD) {
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_length_n = sizeof(ngx_empty_gif);
        r->headers_out.last_modified_time = 23349600;

        return ngx_http_send_header(r);
    }

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

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

    b->pos = ngx_empty_gif;
    b->last = ngx_empty_gif + sizeof(ngx_empty_gif);
    b->memory = 1;
    b->last_buf = 1;

    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = sizeof(ngx_empty_gif);
    r->headers_out.last_modified_time = 23349600;

    rc = ngx_http_send_header(r);

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

    return ngx_http_output_filter(r, &out);
}
示例#12
0
/*
 * Main handler function of the module.
 */
static ngx_int_t
ngx_http_hello_handler(ngx_http_request_t *r)
{
    ngx_int_t rc;
    ngx_buf_t *b;
    ngx_chain_t out;
    ngx_http_hello_loc_conf_t *hlcf;

    /* we response to 'GET' and 'HEAD' requests only */
    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }
    /* discard request body, since we don't need it here */
    rc = ngx_http_discard_request_body(r);
    if (rc != NGX_OK) {
        return rc;
    }
    hlcf = ngx_http_get_module_loc_conf(r, ngx_http_hello_module);
    /* set the 'Content-type' header */
    r->headers_out.content_type_len = sizeof("text/html") - 1;
    r->headers_out.content_type.data = (u_char *) "text/html";
    /* send the header only, if the request type is http 'HEAD' */
    if (r->method == NGX_HTTP_HEAD) {
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_length_n = hlcf->name.len;
        return ngx_http_send_header(r);
    }
    /* allocate a buffer for your response body */
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    /* attach this buffer to the buffer chain */
    out.buf = b;
    out.next = NULL;
    /* adjust the pointers of the buffer */
    b->pos = hlcf->name.data;
    b->last = hlcf->name.data + hlcf->name.len;
    b->memory = 1; /* this buffer is in memory */
    b->last_buf = 1; /* this is the last buffer in the buffer chain
    */
    /* set the status line */
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = hlcf->name.len;
    /* send the headers of your response */
    rc = ngx_http_send_header(r);
    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        return rc;
    }
    /* send the buffer chain of your response */
    return ngx_http_output_filter(r, &out);
}
示例#13
0
static ngx_int_t
ngx_http_udsproxy_handler(ngx_http_request_t *r)
{
    ngx_int_t                  rc;
    ngx_log_t                 *log;
    ngx_http_udsproxy_conf_t *ulcf;
    ngx_str_t url;
    ngx_str_t uri;

    log = r->connection->log;

    ngx_log_error(NGX_LOG_ERR, log, 0, "==!!== uri: %V args: %V exten: %V request_line: %V unparsed_uri: %V.", &r->uri, &r->args, &r->exten, &r->request_line, &r->unparsed_uri);

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }


    if (r->uri.data[r->uri.len - 1] == '/') {
        return NGX_DECLINED;
    }

    rc = ngx_http_discard_request_body(r);

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

    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

    ulcf = ngx_http_get_module_loc_conf(r, ngx_http_udsproxy_module);
    ngx_log_error(NGX_LOG_ERR, log, NGX_EACCES, "udsproxy local conf host: %V port: %d uri: %V prefix: %V", &ulcf->uds_host, ulcf->uds_port, &ulcf->uds_uri, &ulcf->uds_prefix);

    rc = get_flv_real_path(&ulcf->uds_host, ulcf->uds_port, &ulcf->uds_uri, &r->args, &uri);
    /*rc = get_uds_file_url(r, &ulcf->uds_host, ulcf->uds_port, &ulcf->uds_uri, &r->args, &uri);*/


    if ( ulcf->uds_prefix.len > 0 ){
        url.len = ulcf->uds_prefix.len + uri.len;
        url.data = (u_char*)ngx_pnalloc(r->pool, url.len);
        if ( url.data == NULL ) {
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
        ngx_sprintf(url.data, "%V%V", &ulcf->uds_prefix, &uri);
    } else {
        url = uri;
    }

    rc =_ngx_http_303_handler(r, &url);
    return rc;
}
static ngx_int_t 
ngx_http_test_check_handler(ngx_http_request_t*r)
{
    ngx_int_t				 rc;
    ngx_http_test_check_loc_conf_t	*tlcf;
    ngx_buf_t				*b;
    ngx_chain_t				 out;

    if(r->method!=NGX_HTTP_GET&&r->method!=NGX_HTTP_HEAD){
	return NGX_HTTP_NOT_ALLOWED;
    }

    rc=ngx_http_discard_request_body(r);

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

    r->headers_out.content_type.len=sizeof("text/html");
    r->headers_out.content_type.data=(u_char*)"text/html";
    r->headers_out.status=NGX_HTTP_OK;

    rc=ngx_http_send_header(r);

    if(rc==NGX_ERROR||rc>NGX_OK||r->header_only||r->method==NGX_HTTP_HEAD){
	return rc;
    }

    tlcf=ngx_http_get_module_loc_conf(r,ngx_http_test_check_module);
    
    b=ngx_create_temp_buf(r->pool,1024);
    if(b==NULL){
	return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

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

    b->last=ngx_sprintf(b->last,(char*)tlcf->test_check_str.data);
    b->last_buf=1;

    r->headers_out.content_length_n=b->last-b->pos;

    return ngx_http_output_filter(r,&out);



}
static ngx_int_t ngx_http_sandbox_handler(ngx_http_request_t *r)
{
    size_t             size;
    ngx_int_t          rc;
    ngx_buf_t         *b;
    ngx_chain_t        out;

  // ngx_http_sandbox_ctx_t  *sandboxconfig;
   // ngx_atomic_int_t   ap, hn, ac, rq, rd, wr;

    if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    rc = ngx_http_discard_request_body(r);

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

    r->headers_out.content_type.len = sizeof("text/html") - 1;
    r->headers_out.content_type.data = (u_char *) "text/html";

    if (r->method == NGX_HTTP_HEAD) {
        r->headers_out.status = NGX_HTTP_OK;

        rc = ngx_http_send_header(r);

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

    char debut[] = "<html><head></head><body><h1>Wellcome in the sandbox module</h1>\n";
    char suite[]=  " <pre>location /nginx_sandbox {\nsandbox on;\n}</pre><p>\n\
                   Le parametre <b>sandbox</b> est pris en charge par la fonction:<p>\n\
                  <pre>static ngx_command_t  ngx_http_sandbox_commands[] = {\n\
                  { ngx_string(\"sandbox\"),\n\
	          NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,\n\
                  <b>ngx_http_set_sandbox</b>,\n\
                  0,\n\
                  0,\n\
                  NULL },</pre>\
ngx_int_t
ngx_http_cache_purge_init(ngx_http_request_t *r, ngx_http_file_cache_t *cache,
    ngx_http_complex_value_t *cache_key)
{
    ngx_http_cache_t  *c;
    ngx_str_t         *key;
    ngx_int_t          rc;

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

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

    rc = ngx_array_init(&c->keys, r->pool, 1, sizeof(ngx_str_t));
    if (rc != NGX_OK) {
        return NGX_ERROR;
    }

    key = ngx_array_push(&c->keys);
    if (key == NULL) {
        return NGX_ERROR;
    }

    rc = ngx_http_complex_value(r, cache_key, key);
    if (rc != NGX_OK) {
        return NGX_ERROR;
    }

    r->cache = c;
    c->body_start = ngx_pagesize;
    c->file_cache = cache;
    c->file.log = r->connection->log;

    ngx_http_file_cache_create_key(r);

    return NGX_OK;
}
/* mostly a clone of the ngx_http_read_client_request_body_handler
 * function in ngx_http_request_body.c of nginx 0.8.20.
 * copyrighted by Igor Sysoev. */
static void
ngx_http_chunkin_read_chunked_request_body_handler(ngx_http_request_t *r)
{
    ngx_int_t  rc;

    if (r->connection->read->timedout) {
        r->connection->timedout = 1;

        (void) ngx_http_discard_request_body(r);

        ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
        return;
    }

    rc = ngx_http_chunkin_do_read_chunked_request_body(r);

    if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
        ngx_http_finalize_request(r, rc);
    }
}
ngx_int_t
ngx_selective_cache_purge_send_response(ngx_http_request_t *r, u_char *data, size_t len, ngx_uint_t status, ngx_str_t *content_type)
{
    ngx_int_t rc;

    if (ngx_http_discard_request_body(r) != NGX_OK) {
        return ngx_selective_cache_purge_send_header(r, 0, NGX_HTTP_INTERNAL_SERVER_ERROR, content_type);
    }

    if ((r->method == NGX_HTTP_HEAD) || (len == 0)) {
        return ngx_selective_cache_purge_send_header(r, len, status, content_type);
    }

    rc = ngx_selective_cache_purge_send_header(r, len, status, content_type);

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

    return ngx_selective_cache_purge_send_response_text(r, data, len, 1);
}
static ngx_int_t
ngx_http_methods_filter_send_response(ngx_http_request_t *r, u_char *data, size_t len, ngx_uint_t status)
{
    ngx_buf_t                                   *b;
    ngx_chain_t                                  out;
    ngx_int_t                                    rc;

    if (ngx_http_discard_request_body(r) != NGX_OK) {
        return ngx_http_methods_filter_send_header(r, 0, NGX_HTTP_INTERNAL_SERVER_ERROR);
    }

    if ((r->method == NGX_HTTP_HEAD) || (len == 0)) {
        return ngx_http_methods_filter_send_header(r, len, status);
    }

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

    b->last = ngx_copy(b->pos, data, len);
    b->memory = len ? 1 : 0;
    b->last_buf = (r == r->main) ? 1 : 0;
    b->last_in_chain = 1;

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

    rc = ngx_http_methods_filter_send_header(r, len, status);

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

    rc = ngx_http_output_filter(r, &out);
    ngx_http_finalize_request(r, NGX_DONE);

    return rc;
}
示例#20
0
static ngx_int_t
ngx_http_debug_pool_handler(ngx_http_request_t *r)
{
    ngx_int_t    rc;
    ngx_buf_t   *b;
    ngx_chain_t  out;

    if (r->method != NGX_HTTP_GET) {
        return NGX_HTTP_NOT_ALLOWED;
    }

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

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if (ngx_http_debug_pool_buf(r->pool, b) == NGX_ERROR) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = b->last - b->pos;

    rc = ngx_http_send_header(r);

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

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

    return ngx_http_output_filter(r, &out);
}
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 ngx_int_t
ngx_http_image_cache_handler(ngx_http_request_t *r)
{
    ngx_chain_t                    out;
    ngx_int_t                     status;

    if(!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))){
        return NGX_HTTP_NOT_ALLOWED;
    }

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

    ngx_http_image_cache_conf_t  *conf;
    conf = ngx_http_get_module_loc_conf(r, ngx_http_image_cache_module);

    if(!(conf->lookup_cache)){
        return NGX_DECLINED;
    }

    status = ngx_http_lookup_and_send_cache_file(r,&out);

    if(status == NGX_OK){
        ngx_str_set(&r->headers_out.content_type, "text/plain");
        r->headers_out.status = NGX_HTTP_OK;
    
        rc = ngx_http_send_header(r);
        if(rc == NGX_ERROR || rc > NGX_OK || r->header_only){
            return rc;
        }
        ngx_http_output_filter(r,&out);
        return NGX_OK;
    }

    return NGX_DECLINED;
}
static ngx_int_t
ngx_http_push_stream_send_buf_response(ngx_http_request_t *r, ngx_buf_t *buf, const ngx_str_t *content_type, ngx_int_t status_code)
{
    ngx_chain_t             *chain;
    ngx_int_t                rc;

    if ((r == NULL) || (buf == NULL) || (content_type == NULL)) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    r->headers_out.content_type.len = content_type->len;
    r->headers_out.content_type.data = content_type->data;
    r->headers_out.content_length_n = ngx_buf_size(buf);

    if ((chain = ngx_pcalloc(r->pool, sizeof(ngx_chain_t))) == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: unable to allocate memory for send buf response");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    chain->buf = buf;
    chain->next = NULL;

    buf->memory = 1;
    buf->last_buf = 1;

    r->keepalive = 0;
    r->headers_out.status = status_code;

    ngx_http_discard_request_body(r);
    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
        return rc;
    }
    rc = ngx_http_output_filter(r, chain);
    return rc;
}
示例#24
0
ngx_int_t ngx_http_valar_handler(ngx_http_request_t *r) {
	
	ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "valar module have been called!");
	if ((r->method & (NGX_HTTP_GET | NGX_HTTP_POST)) == 0) {
		return NGX_HTTP_NOT_ALLOWED;
	}
	ngx_int_t rc = ngx_http_discard_request_body(r);
	if (rc != NGX_OK) {
		return rc;
	}

	ngx_str_t type = ngx_string("text/plain");
	ngx_str_t response = ngx_string("this is valar module, what I want to say is: hello world!");
	r->headers_out.status = NGX_HTTP_OK;
	r->headers_out.content_type = type;
	r->headers_out.content_length_n = response.len;

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

	ngx_buf_t *b;
	b = ngx_create_temp_buf(r->pool, response.len);
	if (b == NULL) {
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}
	ngx_memcpy(b->pos, response.data, response.len);
	b->last = b->pos + response.len;
	// this is the last buffer
	b->last_buf = 1;

	ngx_chain_t out;
	out.buf = b;
	out.next = NULL;	
	return ngx_http_output_filter( r, &out);
}
ngx_int_t
ngx_http_flex_cache_handler(ngx_http_request_t *r)
{
    ngx_int_t                        rc;
    ngx_http_flex_cache_loc_conf_t  *fc;

    fc = ngx_http_get_module_loc_conf(r, ngx_http_flex_cache_module);
    if (!fc->enabled) {
        return NGX_DECLINED;
    }

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    if (r->uri.data[r->uri.len - 1] == '/') {
        return NGX_DECLINED;
    }

#if defined(nginx_version) \
    && ((nginx_version < 7066) \
        || ((nginx_version >= 8000) && (nginx_version < 8038)))
    if (r->zero_in_uri) {
        return NGX_DECLINED;
    }
#endif

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

    rc = ngx_http_flex_cache_send(r);

    return rc;
}
static ngx_int_t
ngx_http_empty_gif_handler(ngx_http_request_t *r)
{
    ngx_int_t     rc;
    ngx_http_complex_value_t  cv;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    rc = ngx_http_discard_request_body(r);

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

    ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));

    cv.value.len = sizeof(ngx_empty_gif);
    cv.value.data = ngx_empty_gif;
    r->headers_out.last_modified_time = 23349600;

    return ngx_http_send_response(r, NGX_HTTP_OK, &ngx_http_gif_type, &cv);
}
//这里是处理每个请求的回调函数
//执行到这一步时,nginx已经把http请求做了初步的解析,整个请求的信息放在ngx_http_request_t中
static ngx_int_t
ngx_http_mmseg_handler(ngx_http_request_t *r) {
	ngx_int_t    rc;
	
    //根据请求类型的不同执行不同动作
	if (r->method & NGX_HTTP_GET) { // get
		return ngx_http_do_get(r);// get
		
    } else if (r->method & NGX_HTTP_POST) { // post
		rc = ngx_http_read_client_request_body(r, ngx_http_do_post); 
			//ngx_http_do_post(&request_info);
		if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
			return rc;
		}
		return NGX_DONE;

	} else {
		//nginx的自带函数,表示抛弃包体,不执行任何操作
		rc = ngx_http_discard_request_body(r);
		return rc;
	}

	return NGX_HTTP_NOT_ALLOWED;
}
static ngx_int_t
ngx_http_notetask_handler(ngx_http_request_t *r)
{ENTER
        ngx_int_t    rc;
        ngx_buf_t   *b;
        ngx_chain_t  out;
        ngx_http_notetask_loc_conf_t* my_conf;

#define LEN_RESPONSE_STRING (1024*1024)
        u_char ngx_notetask_string[LEN_RESPONSE_STRING] = {0};
        ngx_uint_t content_length = 0;

        ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "n1gx_http_notetask_handler is called!");

        my_conf = ngx_http_get_module_loc_conf(r, ngx_http_notetask_module);
        if (my_conf->notetask_string.len == 0 )
        {
                ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "notetask_string is empty!");
                return NGX_DECLINED;
        }

#if 0
        if (my_conf->notetask_counter == NGX_CONF_UNSET
                || my_conf->notetask_counter == 0)
        {
                ngx_sprintf(ngx_notetask_string, "%s", my_conf->notetask_string.data);
        }
        else
        {
                ngx_sprintf(ngx_notetask_string, "%s Visited Times:%d", my_conf->notetask_string.data,
                        ++ngx_notetask_visited_times);
        }
#endif

        setbuf(stdout, NULL);
        //char s[LEN_RESPONSE_STRING]; s[0] = '\0';
        int total_times = 1;//100*1024;
        struct timeval tvs;
        struct timeval tve;
        gettimeofday(&tvs, NULL);
        while(total_times --) {
            static int ktest_times = 0;
            query_result_to_cstring("/", (char*)ngx_notetask_string, LEN_RESPONSE_STRING, r);
            ktest_times ++;
            if(0 == ktest_times % 10240) {
                printf(".%zd %d\n", strlen((char*)ngx_notetask_string), ktest_times);
            }
        }
        gettimeofday(&tve, NULL);
        long usec = tve.tv_usec-tvs.tv_usec;
        if(usec >= 0) {
            PRINTF_DEBUG("%ld.%06ld", tve.tv_sec-tvs.tv_sec, usec);
        }
        else {
            PRINTF_DEBUG("%ld.%06ld", tve.tv_sec-tvs.tv_sec-1, usec+1000000);
        }

        PRINTF_DEBUG("notetask_string:%s", ngx_notetask_string);
        content_length = ngx_strlen(ngx_notetask_string);
        PRINTF_DEBUG("[%zd]\n", content_length);

        /* we response to 'GET' and 'HEAD' requests only */
        if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
                return NGX_HTTP_NOT_ALLOWED;
        }

        /* discard request body, since we don't need it here */
        rc = ngx_http_discard_request_body(r);

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

        /* set the 'Content-type' header */
        /*
         *r->headers_out.content_type.len = sizeof("text/html") - 1;
         *r->headers_out.content_type.data = (u_char *)"text/html";
         */
        ngx_str_set(&r->headers_out.content_type, "text/html");


        /* send the header only, if the request type is http 'HEAD' */
        if (r->method == NGX_HTTP_HEAD) {
                r->headers_out.status = NGX_HTTP_OK;
                r->headers_out.content_length_n = content_length;

                return ngx_http_send_header(r);
        }

        /* allocate a buffer for your response body */
        b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
        if (b == NULL) {
                return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }

        /* attach this buffer to the buffer chain */
        out.buf = b;
        out.next = NULL;

        /* adjust the pointers of the buffer */
        b->pos = ngx_notetask_string;
        b->last = ngx_notetask_string + content_length;
        b->memory = 1;    /* this buffer is in memory */
        b->last_buf = 1;  /* this is the last buffer in the buffer chain */

        /* set the status line */
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_length_n = content_length;

        /* send the headers of your response */
        rc = ngx_http_send_header(r);

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

        /* send the buffer chain of your response */
        return ngx_http_output_filter(r, &out);
}
static ngx_int_t ngx_http_status_code_counter_handler(ngx_http_request_t *r)
{
    size_t             size;
    ngx_int_t          rc, i, j;
    ngx_buf_t         *b;
    ngx_chain_t        out;
    ngx_time_t        *time;
    ngx_http_status_code_counter_conf_t *cf;
        
    if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    rc = ngx_http_discard_request_body(r);

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

    cf = ngx_http_get_module_main_conf(r, ngx_http_status_code_counter_module);
    if (NULL == cf) {
        return NGX_ERROR;
    }

    ngx_str_set(&r->headers_out.content_type, "application/json");

    if (r->method == NGX_HTTP_HEAD) {
        r->headers_out.status = NGX_HTTP_OK;

        rc = ngx_http_send_header(r);

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

    /* count number of seen status codes to determine how much buffer we need */
    j = 0;
    for(i = 0; i < NGX_HTTP_NUM_STATUS_CODES; i++ )
    {
      if(ngx_http_status_code_counts[i] > 0)
      {
        j++;
      }
    }

    size = sizeof("{}") + j * (sizeof("\"XXX\":,") + NGX_ATOMIC_T_LEN);

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

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

    time = ngx_timeofday();

    b->last = ngx_sprintf(b->last, "{\"uptime\":%uA", time->sec - cf->startup);
    for(i = 0; i < NGX_HTTP_NUM_STATUS_CODES; i++ )
    {
      if(ngx_http_status_code_counts[i] > 0)
      {
        b->last = ngx_sprintf(b->last, ",\"%uA\":%uA", i+NGX_HTTP_OK, ngx_http_status_code_counts[i]);
      }
    }

    b->last = ngx_sprintf(b->last, "}");
    
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = b->last - b->pos;

    b->last_buf = 1;

    rc = ngx_http_send_header(r);

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

    return ngx_http_output_filter(r, &out);
}
示例#30
0
ngx_int_t
passenger_static_content_handler(ngx_http_request_t *r, ngx_str_t *filename)
{
    u_char                    *last, *location;
    size_t                     len;
    ngx_int_t                  rc;
    ngx_uint_t                 level;
    ngx_log_t                 *log;
    ngx_buf_t                 *b;
    ngx_chain_t                out;
    ngx_open_file_info_t       of;
    ngx_http_core_loc_conf_t  *clcf;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD|NGX_HTTP_POST))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    if (r->uri.data[r->uri.len - 1] == '/') {
        return NGX_DECLINED;
    }

    log = r->connection->log;

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
                   "http filename: \"%s\"", filename->data);

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

    ngx_memzero(&of, sizeof(ngx_open_file_info_t));
    of.read_ahead = clcf->read_ahead;
    of.directio = clcf->directio;
    of.valid = clcf->open_file_cache_valid;
    of.min_uses = clcf->open_file_cache_min_uses;
    of.errors = clcf->open_file_cache_errors;
    of.events = clcf->open_file_cache_events;

    if (ngx_open_cached_file(clcf->open_file_cache, filename, &of, r->pool)
        != NGX_OK)
    {
        switch (of.err) {

        case 0:
            return NGX_HTTP_INTERNAL_SERVER_ERROR;

        case NGX_ENOENT:
        case NGX_ENOTDIR:
        case NGX_ENAMETOOLONG:

            level = NGX_LOG_ERR;
            rc = NGX_HTTP_NOT_FOUND;
            break;

        case NGX_EACCES:

            level = NGX_LOG_ERR;
            rc = NGX_HTTP_FORBIDDEN;
            break;

        default:

            level = NGX_LOG_CRIT;
            rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
            break;
        }

        if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
            ngx_log_error(level, log, of.err,
                          ngx_open_file_n " \"%s\" failed", filename->data);
        }

        return rc;
    }

    r->root_tested = !r->error_page;

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "http static fd: %d", of.fd);

    if (of.is_dir) {

        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http dir");

        r->headers_out.location = ngx_palloc(r->pool, sizeof(ngx_table_elt_t));
        if (r->headers_out.location == NULL) {
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }

        len = r->uri.len + 1;

        if (!clcf->alias && clcf->root_lengths == NULL && r->args.len == 0) {
            location = filename->data + clcf->root.len;

        } else {
            if (r->args.len) {
                len += r->args.len + 1;
            }

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

            last = ngx_copy(location, r->uri.data, r->uri.len);

            *last = '/';

            if (r->args.len) {
                *++last = '?';
                ngx_memcpy(++last, r->args.data, r->args.len);
            }
        }

        /*
         * we do not need to set the r->headers_out.location->hash and
         * r->headers_out.location->key fields
         */

        r->headers_out.location->value.len = len;
        r->headers_out.location->value.data = location;

        return NGX_HTTP_MOVED_PERMANENTLY;
    }

#if !(NGX_WIN32) /* the not regular files are probably Unix specific */

    if (!of.is_file) {
        ngx_log_error(NGX_LOG_CRIT, log, 0,
                      "\"%s\" is not a regular file", filename->data);

        return NGX_HTTP_NOT_FOUND;
    }

#endif

    if (r->method & NGX_HTTP_POST) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    rc = ngx_http_discard_request_body(r);

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

    log->action = "sending response to client";

    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = of.size;
    r->headers_out.last_modified_time = of.mtime;

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

    if (r != r->main && of.size == 0) {
        return ngx_http_send_header(r);
    }

    r->allow_ranges = 1;

    /* we need to allocate all before the header would be sent */

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
    if (b->file == NULL) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    rc = ngx_http_send_header(r);

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

    b->file_pos = 0;
    b->file_last = of.size;

    b->in_file = b->file_last ? 1: 0;
    b->last_buf = (r == r->main) ? 1: 0;
    b->last_in_chain = 1;

    b->file->fd = of.fd;
    b->file->name = *filename;
    b->file->log = log;
    b->file->directio = of.is_directio;

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

    return ngx_http_output_filter(r, &out);
}