static ngx_int_t ngx_selective_cache_purge_send_response_text(ngx_http_request_t *r, const u_char *text, uint len, ngx_flag_t last_buffer) { ngx_buf_t *b; ngx_chain_t out; if ((text == NULL) || (r->connection->error)) { return NGX_ERROR; } b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->last = ngx_copy(b->pos, text, len); b->memory = len ? 1 : 0; b->last_buf = (r == r->main) ? last_buffer : 0; b->last_in_chain = 1; b->flush = 1; out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out); }
/* Handler function */ static ngx_int_t ngx_http_bo_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; char *content=NULL; int content_len=0; if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST))) { return NGX_HTTP_NOT_ALLOWED; } r->headers_out.content_type.len = g_content_type.len; r->headers_out.content_type.data = (u_char *) g_content_type.str; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = 0; if(r->method == NGX_HTTP_HEAD) { rc = ngx_http_send_header(r); if(rc != NGX_OK) { return rc; } } rc = BOProcess( (const char*)r->uri.data, r->uri.len, (const char*)r->args.data, r->args.len, &content, &content_len ); if( rc != 0 || content == NULL) return NGX_HTTP_INTERNAL_SERVER_ERROR; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if(b == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer."); return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->pos = (u_char *)ngx_palloc(r->pool, content_len); if(b->pos == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate buffer for response content."); return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->last = b->pos + content_len; memcpy(b->pos, (void *)content, content_len); BOFree(&content); b->memory = 1; b->last_buf = 1; out.buf = b; out.next = NULL; r->headers_out.content_length_n = content_len; rc = ngx_http_send_header(r); if(rc != NGX_OK) return rc; return ngx_http_output_filter(r, &out); }
/** * Content handler. * * @param r * Pointer to the request structure. See http_request.h. * @return * The status of the response generation. */ static ngx_int_t ngx_http_helloworld_handler(ngx_http_request_t *r) { ngx_buf_t *b; ngx_chain_t out; /* 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"; /* Allocate a new buffer for sending out the reply. */ b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); /* Insertion in the buffer chain. */ out.buf = b; out.next = NULL; /* just one buffer */ b->pos = ngx_helloworld; /* first position in memory of the data */ b->last = ngx_helloworld + sizeof(ngx_helloworld); /* last position in memory of the data */ b->memory = 1; /* content is in read-only memory */ b->last_buf = 1; /* there will be no more buffers in the request */ /* Sending the headers for the reply. */ r->headers_out.status = NGX_HTTP_OK; /* 200 status code */ /* Get the content length of the body. */ r->headers_out.content_length_n = sizeof(ngx_helloworld); ngx_http_send_header(r); /* Send the headers */ /* Send the body, and return the status code of the output filter chain. */ return ngx_http_output_filter(r, &out); } /* ngx_http_helloworld_handler */
//请求处理器,由ngx_http_hello方法向nginx登记 static ngx_int_t ngx_diskone_handler(ngx_http_request_t *r){ //ngx_int_t rc; size_t size; ngx_chain_t out; ngx_buf_t *b; ngx_str_set(&r->headers_out.content_type, "text/plain");//设置响应的类型为纯文本 size = sizeof("It is OK"); b = ngx_create_temp_buf(r->pool, size); //获取内存空间 if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->last = ngx_sprintf(b->last, "It is OK!"); //写入内容到缓存 out.buf = b; //将响应体的内容指向b缓存 out.next = NULL; r->headers_out.status = NGX_HTTP_OK; //响应码,200 r->headers_out.content_length_n = b->last - b->pos;//设置响应体的长度 b->last_buf = 1; ngx_http_send_header(r); //发送响应头,如Content-Type text/plain return ngx_http_output_filter(r, &out);//继续其他过滤器 };
//allocates nothing static ngx_int_t ngx_http_push_prepare_response_to_subscriber_request(ngx_http_request_t *r, ngx_chain_t *chain, ngx_str_t *content_type, ngx_str_t *etag, time_t last_modified) { ngx_int_t res; if (content_type!=NULL) { r->headers_out.content_type.len=content_type->len; r->headers_out.content_type.data = content_type->data; } if(last_modified) { //if-modified-since header r->headers_out.last_modified_time=last_modified; } if(etag!=NULL) { //etag, if we need one if ((r->headers_out.etag=ngx_http_push_add_response_header(r, &NGX_HTTP_PUSH_HEADER_ETAG, etag))==NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } } //Vary header needed for proper HTTP caching. ngx_http_push_add_response_header(r, &NGX_HTTP_PUSH_HEADER_VARY, &NGX_HTTP_PUSH_VARY_HEADER_VALUE); r->headers_out.status=NGX_HTTP_OK; //we know the entity length, and we're using just one buffer. so no chunking please. r->headers_out.content_length_n=ngx_buf_size(chain->buf); if((res = ngx_http_send_header(r)) >= NGX_HTTP_SPECIAL_RESPONSE) { return res; } return ngx_http_output_filter(r, chain); }
static ngx_int_t ngx_http_ntimes_echo_handler(ngx_http_request_t*r) { ngx_http_ntimes_echo_loc_conf_t *nelcf; ngx_buf_t *buf; ngx_chain_t out; ngx_int_t rc; ngx_str_t type=ngx_string("text/plain"); size_t len; if(!(r->method&(NGX_HTTP_GET|NGX_HTTP_HEAD))){ return NGX_HTTP_NOT_ALLOWED; } nelcf=ngx_http_get_module_loc_conf(r,ngx_http_ntimes_echo_module); len=(nelcf->echo_str.len)*(nelcf->echo_time); r->headers_out.content_type=type; r->headers_out.content_length_n=len; 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; buf=ngx_create_temp_buf(r->pool,len); if(buf==NULL){ return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_memset(buf->pos,0,len); ngx_int_t n=nelcf->echo_time; /* when use sprintf,be careful about the lensize,don't overbuffer, * eg: add "%s\n",so "\n" maybe overbuffer in somecase */ while(n){ buf->last=ngx_sprintf(buf->last,"%s",nelcf->echo_str.data); n--; } ngx_log_error(NGX_LOG_INFO,r->connection->log,0,"buf is :%d:%*s", (buf->last-buf->pos),(size_t)(buf->last-buf->pos),buf->pos); buf->last=buf->pos+len; buf->last_buf=1; buf->memory=1; out.next=NULL; out.buf=buf; return ngx_http_output_filter(r,&out); }
static int command_sendcontent(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { ngx_buf_t *b; ngx_chain_t out; Tcl_Obj *content; int rc; int len; ngx_http_request_t *r = getrequest(clientData); if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "content"); return TCL_ERROR; } content = objv[1]; b = ngx_calloc_buf(r->pool); if (b == NULL) { return TCL_ERROR; } out.buf = b; out.next = NULL; b->start = (u_char*)Tcl_GetByteArrayFromObj(content, &len); b->pos = b->start; b->end = b->last = b->start + len; b->memory = 1; b->last_buf = 1; b->last_in_chain = 1; rc = ngx_tcl_cleanup_add_Tcl_Obj(r->pool, content); if (rc != TCL_OK) { return rc; } if (!r->header_sent) { r->headers_out.content_length_n = len; if (r->headers_out.status == 0) { r->headers_out.status = 200; } ngx_http_send_header(r); /* TODO: CHECK RETURN */ } rc = ngx_http_output_filter(r, &out); if (rc != NGX_OK) { ngx_tcl_set_error_code(interp, rc); return TCL_ERROR; } printf("ngx_http_output_filter returns %i\n", rc); fflush(stdout); return TCL_OK; }
ngx_int_t ngx_http_echo_send_chain_link(ngx_http_request_t *r, ngx_http_echo_ctx_t *ctx, ngx_chain_t *in) { ngx_int_t rc; rc = ngx_http_echo_send_header_if_needed(r, ctx); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } if (in == NULL) { #if defined(nginx_version) && nginx_version <= 8004 /* earlier versions of nginx does not allow subrequests to send last_buf themselves */ if (r != r->main) { return NGX_OK; } #endif rc = ngx_http_send_special(r, NGX_HTTP_LAST); if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { return rc; } return NGX_OK; } /* FIXME we should udpate chains to recycle chain links and bufs */ return ngx_http_output_filter(r, in); }
static int ngx_send_thumbnail(void *arg, unsigned char *buf, size_t size) { ngx_http_request_t *r; ngx_buf_t *b; ngx_chain_t out; ngx_int_t rc; r = (ngx_http_request_t *) arg; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_pcalloc fail"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; b->pos = (u_char *) buf; b->last = (u_char *) buf + size; b->memory = 1; b->last_in_chain = 1; b->last_buf = 1; rc = ngx_http_output_filter(r, &out); if (rc == NGX_OK || rc == NGX_AGAIN) { return 0; } else { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_output_filter fail, return code: %d", rc); return rc; } }
static ngx_int_t ngx_http_esi_stub_output(ngx_http_request_t *r, void *data, ngx_int_t rc) { ngx_chain_t *out; if (rc == NGX_ERROR || r->connection->error || r->request_output) { return rc; } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ssi stub output: \"%V?%V\"", &r->uri, &r->args); out = data; if (!r->header_sent) { if (ngx_http_set_content_type(r) == NGX_ERROR) { return NGX_ERROR; } if (ngx_http_send_header(r) == NGX_ERROR) { return NGX_ERROR; } } return ngx_http_output_filter(r, out); }
static mrb_value ngx_mrb_send_header(mrb_state *mrb, mrb_value self) { rputs_chain_list_t *chain; ngx_mruby_ctx_t *ctx; ngx_http_request_t *r = ngx_mrb_get_request(); mrb_int status = NGX_HTTP_OK; mrb_get_args(mrb, "i", &status); r->headers_out.status = status; ctx = ngx_http_get_module_ctx(r, ngx_http_mruby_module); if (ctx == NULL) { ngx_log_error(NGX_LOG_ERR , r->connection->log , 0 , "get mruby context failed." ); } chain = ctx->rputs_chain; (*chain->last)->buf->last_buf = 1; if (r->headers_out.status == NGX_HTTP_OK) { ngx_http_send_header(r); ngx_http_output_filter(r, chain->out); ngx_http_set_ctx(r, NULL, ngx_http_mruby_module); } return self; }
static ngx_int_t ngx_http_perl_output(ngx_http_request_t *r, ngx_buf_t *b) { ngx_chain_t out; #if (NGX_HTTP_SSI) ngx_chain_t *cl; ngx_http_perl_ctx_t *ctx; ctx = ngx_http_get_module_ctx(r, ngx_http_perl_module); if (ctx->ssi) { cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = b; cl->next = NULL; *ctx->ssi->last_out = cl; ctx->ssi->last_out = &cl->next; return NGX_OK; } #endif out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out); }
static ngx_int_t ngx_http_remote_ip_handler(ngx_http_request_t *r) { ngx_buf_t *b; ngx_chain_t out; u_char *ngx_remote_ip = r->connection->addr_text.data; size_t ip_len = r->connection->addr_text.len; r->headers_out.content_type.len = sizeof("text/html") - 1; r->headers_out.content_type.data = (u_char *) "text/html"; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); out.buf = b; out.next = NULL; b->pos = ngx_remote_ip; b->last = ngx_remote_ip + ip_len; b->memory = 1; b->last_buf = 1; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = ip_len; ngx_http_send_header(r); return ngx_http_output_filter(r, &out); }
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); }
ngx_int_t ngx_http_proxy_connect_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_buf_t *b; ngx_http_proxy_connect_ctx_t *ctx; ngx_http_proxy_connect_loc_conf_t *pccf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_connect_module); if (!(r->method & (NGX_HTTP_CONNECT)) \ || ngx_http_proxy_connect_test_block_ports(r)) { return NGX_HTTP_NOT_ALLOWED; } if (pccf->black_hosts != NGX_CONF_UNSET_PTR) { if (NGX_OK == ngx_http_proxy_connect_test_black_hosts(r)) { return NGX_HTTP_NOT_ALLOWED; } } ngx_http_proxy_connect_buf.pos = ngx_http_proxy_connect_success.data; ngx_http_proxy_connect_buf.last = ngx_http_proxy_connect_success.data \ + ngx_http_proxy_connect_success.len; ngx_http_proxy_connect_buf.start = ngx_http_proxy_connect_success.data; ngx_http_proxy_connect_buf.end = ngx_http_proxy_connect_success.data \ + ngx_http_proxy_connect_success.len; ngx_http_proxy_connect_buf.last_buf = 1; ngx_http_proxy_connect_buf.memory = 1; rc = ngx_http_output_filter(r, &ngx_http_proxy_connect_chain); if (rc != NGX_OK) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } ctx = ngx_palloc(r->pool, sizeof(ngx_http_proxy_connect_ctx_t)); if (ctx == NULL) { return NGX_ERROR; } ngx_http_set_ctx(r, ctx, ngx_http_proxy_connect_module); b = ngx_calloc_buf(r->pool); if (b == NULL) { return NGX_ERROR; } b->start = ngx_palloc(r->pool, 4096); if (b->start == NULL) { return NGX_ERROR; } b->end = b->start + 4096; b->pos = b->start; b->last = b->pos; b->last_buf = 1; b->memory = 1; ctx->client_hello = b; r->main->count ++; r->read_event_handler = ngx_http_proxy_connect_read_hello_data; ngx_http_proxy_connect_read_hello_data(r); return NGX_DONE; }
ngx_int_t ngx_postgres_output_chain(ngx_http_request_t *r, ngx_chain_t *cl) { ngx_http_upstream_t *u = r->upstream; ngx_http_core_loc_conf_t *clcf; ngx_postgres_loc_conf_t *pglcf; ngx_postgres_ctx_t *pgctx; ngx_int_t rc; dd("entering"); if (!r->header_sent) { ngx_http_clear_content_length(r); pglcf = ngx_http_get_module_loc_conf(r, ngx_postgres_module); pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module); r->headers_out.status = pgctx->status ? abs(pgctx->status) : NGX_HTTP_OK; if (pglcf->output_handler == &ngx_postgres_output_rds) { /* RDS for output rds */ r->headers_out.content_type.data = (u_char *) rds_content_type; r->headers_out.content_type.len = rds_content_type_len; r->headers_out.content_type_len = rds_content_type_len; } else if (pglcf->output_handler != NULL) { /* default type for output value|row */ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); r->headers_out.content_type = clcf->default_type; r->headers_out.content_type_len = clcf->default_type.len; } r->headers_out.content_type_lowcase = NULL; rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { dd("returning rc:%d", (int) rc); return rc; } } if (cl == NULL) { dd("returning NGX_DONE"); return NGX_DONE; } rc = ngx_http_output_filter(r, cl); if (rc == NGX_ERROR || rc > NGX_OK) { dd("returning rc:%d", (int) rc); return rc; } ngx_chain_update_chains(&u->free_bufs, &u->busy_bufs, &cl, u->output.tag); dd("returning rc:%d", (int) rc); 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); }
ngx_int_t _ngx_http_303_handler(ngx_http_request_t *r, ngx_str_t *p_path){ ngx_str_t contentType; ngx_buf_t *b; ngx_chain_t out; ngx_log_t *log = r->connection->log; /*ngx_http_udsproxy_conf_t *ulcf;*/ ngx_str_t path = *p_path; /*ulcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);*/ /*if ( ulcf->uds_prefix.len > 0 ){*/ /*path.len = ulcf->uds_prefix.len + p_path->len;*/ /*path.data = (u_char*)ngx_pnalloc(r->pool, path.len);*/ /*if ( path.data == NULL ) {*/ /*return NGX_HTTP_INTERNAL_SERVER_ERROR;*/ /*}*/ /*ngx_sprintf(path.data, "%V%V", &ulcf->uds_prefix, p_path);*/ /*} else {*/ /*path = *p_path;*/ /*}*/ ngx_log_error(NGX_LOG_ERR, log, NGX_EACCES, "==xx== Enter _ngx_http_303_handler(), path:%V", &path); r->headers_out.status = NGX_HTTP_SEE_OTHER; /*r->headers_out.content_length_n = 0;*/ ngx_str_set(&contentType, "text/html" ); r->headers_out.content_type_len = contentType.len; r->headers_out.content_type = contentType; ngx_http_clear_location(r); r->headers_out.location = ngx_list_push(&r->headers_out.headers); r->headers_out.location->hash = 1; ngx_str_set(&r->headers_out.location->key, "Location"); r->headers_out.location->value = path; 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)); b->file_pos = 0; b->file_last = 0; b->in_file = 0; b->last_buf = 1; b->last_in_chain = 1; out.buf = b; out.next = NULL; ngx_log_error(NGX_LOG_ERR, log, NGX_EACCES, "==xx== Leave _ngx_http_303_handler(), path:%V", &path); /*return NGX_OK;*/ return ngx_http_output_filter(r, &out); }
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 void ngx_http_dav_ext_flush(ngx_http_request_t *r, ngx_chain_t **ll) { ngx_chain_t *cl; cl = (*ll)->next; (*ll)->next = NULL; ngx_http_output_filter(r, cl); *ll = NULL; }
ngx_int_t ngx_http_cache_purge_send_response(ngx_http_request_t *r) { ngx_chain_t out; ngx_buf_t *b; ngx_str_t *key; ngx_int_t rc; size_t len; key = r->cache->keys.elts; len = sizeof(ngx_http_cache_purge_success_page_top) - 1 + sizeof(ngx_http_cache_purge_success_page_tail) - 1 + sizeof("<br>Key : ") - 1 + sizeof(CRLF "<br>Path: ") - 1 + key[0].len + r->cache->file.name.len; r->headers_out.content_type.len = sizeof("text/html") - 1; r->headers_out.content_type.data = (u_char *) "text/html"; r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = len; if (r->method == NGX_HTTP_HEAD) { rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } } b = ngx_create_temp_buf(r->pool, len); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; b->last = ngx_cpymem(b->last, ngx_http_cache_purge_success_page_top, sizeof(ngx_http_cache_purge_success_page_top) - 1); b->last = ngx_cpymem(b->last, "<br>Key : ", sizeof("<br>Key : ") - 1); b->last = ngx_cpymem(b->last, key[0].data, key[0].len); b->last = ngx_cpymem(b->last, CRLF "<br>Path: ", sizeof(CRLF "<br>Path: ") - 1); b->last = ngx_cpymem(b->last, r->cache->file.name.data, r->cache->file.name.len); b->last = ngx_cpymem(b->last, ngx_http_cache_purge_success_page_tail, sizeof(ngx_http_cache_purge_success_page_tail) - 1); 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); }
static void echo_reading_callback(ngx_event_t *wev) { ngx_http_request_t *r; ngx_http_echo_ctx_t *ctx; ngx_buf_t *b; ngx_chain_t out; ctx = (ngx_http_echo_ctx_t *) wev->data; r = ctx->request; printf("In callvback, cunks to send: %u\n", ctx->chunks_to_send); if(!r->header_sent) { printf("Sending headers\n"); /* Set the Content-Type header. */ r->headers_out.content_type.len = sizeof("text/plain") - 1; r->headers_out.content_type.data = (u_char *) "text/plain"; /* Sending the headers for the reply. */ r->headers_out.status = NGX_HTTP_OK; /* 200 status code */ /* Get the content length of the body. */ r->headers_out.content_length_n = sizeof(ngx_hello_world) * ctx->chunks_to_send; ngx_http_send_header(r); /* Send the headers */ } ctx->chunks_to_send--; /* Allocate a new buffer for sending out the reply. */ b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); /* Insertion in the buffer chain. */ out.buf = b; out.next = NULL; /* just one buffer */ b->pos = ngx_hello_world; /* first position in memory of the data */ b->last = ngx_hello_world + sizeof(ngx_hello_world); /* last position in memory of the data */ b->memory = 1; b->flush = 1; b->last_buf = ctx->chunks_to_send == 0; /* there will be no more buffers in the request */ /* Send the body, and return the status code of the output filter chain. */ ngx_http_output_filter(r, &out); if(ctx->chunks_to_send > 0 && !ctx->request->connection->write->error) { printf("Adding timer\n"); delete_ev(&ctx->wev); create_ev(&ctx->wev, (ngx_msec_t)1000); } else { // ctx->request->main->count--; ngx_http_finalize_request(r, NGX_DONE); } }
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_shmtest_counter_inc_int(ngx_http_request_t *r) { ngx_int_t rc = NGX_HTTP_OK; ngx_str_t key = ngx_null_string; ngx_str_t szn = ngx_null_string; ngx_int_t n = 1; int64_t cur = 0; if(ngx_http_arg(r, (u_char*)"key", 3, &key)!=NGX_OK){ NLOG_ERROR("get arg 'key' failed!"); return NGX_HTTP_BAD_REQUEST; } if(ngx_http_arg(r, (u_char*)"n", 1, &szn)==NGX_OK){ n = ngx_atoi(szn.data, szn.len); } shmtest_main_conf_t* smcf; smcf = ngx_http_get_module_main_conf(r, ngx_http_shmtest_module); if(smcf == NULL){ NLOG_ERROR("get module ngx_http_shmtest_module's main conf failed!"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_shm_zone_t* zone = smcf->shmap; u_char* rsp = ngx_pcalloc(r->connection->pool, 256); int rsp_len = 0; rc = ngx_shmap_inc_int(zone, &key, n,0, &cur); if(rc == 0){ rsp_len = ngx_sprintf(rsp, "inc_int(key=%V,n=%l)=%l\n", &key,n, cur)-rsp; }else{ rsp_len = ngx_sprintf(rsp, "inc_int(key=%V,n=%l) failed!\n", &key,n)-rsp; } ngx_chain_t* chain = ngx_http_shmtest_resp(r, (char*)rsp, rsp_len); if(chain != NULL){ r->headers_out.content_length_n = rsp_len; }else{ r->headers_out.content_length_n = 0; } rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { }else{ rc = ngx_http_output_filter(r, chain); } return rc; }
static ngx_int_t ngx_http_write_back_request_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_buf_t *b = NULL; ngx_chain_t out; ngx_http_write_back_request_loc_conf_t *wbrcf; wbrcf = ngx_http_get_module_loc_conf(r, ngx_http_write_back_request_module); if(wbrcf->enable != 1) { return NGX_DECLINED; } if(!(r->method & (NGX_HTTP_HEAD | NGX_HTTP_GET| NGX_HTTP_POST))) { return NGX_HTTP_NOT_ALLOWED; } r->headers_out.content_type.len = sizeof("text/html") - 1; r->headers_out.content_type.data = (u_char *)"text/html"; r->headers_out.status = NGX_HTTP_OK; if(r->method == NGX_HTTP_HEAD) { rc = ngx_http_send_header(r); if(rc != NGX_OK) { return rc; } } b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if(b == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer."); return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; if(wbrcf->enable == 1) { b->pos = r->request_start; b->last = r->request_end; b->memory = 1; b->last_buf = 1; } rc = ngx_http_send_header(r); if(rc != NGX_OK) { return rc; } return ngx_http_output_filter(r, &out); }
ngx_int_t ngx_http_mruby_run(ngx_http_request_t *r, ngx_http_mruby_state_t *state, ngx_http_mruby_code_t *code, ngx_flag_t cached) { ngx_http_mruby_ctx_t *ctx; ngx_http_mruby_rputs_chain_list_t *chain; ctx = ngx_http_get_module_ctx(r, ngx_http_mruby_module); ngx_http_mruby_push_request(r); state->ai = mrb_gc_arena_save(state->mrb); mrb_run(state->mrb, code->proc, mrb_top_self(state->mrb)); if (state->mrb->exc) { ngx_http_mruby_raise_error(state, code, r); } mrb_gc_arena_restore(state->mrb, state->ai); //mrb_gc_protect(state->mrb, ctx->table); if (!cached) { ngx_http_mruby_irep_clean(state, code); } if (ctx->exited) { return ctx->exit_code; } chain = ctx->rputs_chain; if (chain == NULL) { if (state->mrb->exc) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (r->headers_out.status >= NGX_HTTP_OK) { if (ctx->phase < NGX_HTTP_MRUBY_PHASE_LOG) { return r->headers_out.status; } } return NGX_DECLINED; } if (r->headers_out.status == NGX_HTTP_OK || !(*chain->last)->buf->last_buf) { r->headers_out.status = NGX_HTTP_OK; (*chain->last)->buf->last_buf = 1; ngx_http_send_header(r); ngx_http_output_filter(r, chain->out); return NGX_OK; } return r->headers_out.status; }
static void ngx_http_xrlt_wev_handler(ngx_http_request_t *r) { dd("XRLT end (main: %p)", r); if (r == r->connection->data && r->postponed) { ngx_http_output_filter(r, NULL); } ngx_http_finalize_request(r, NGX_OK); }
ngx_int_t ngx_http_echo_flush_postponed_outputs(ngx_http_request_t *r) { if (r == r->connection->data && r->postponed) { /* notify the downstream postpone filter to flush the postponed * outputs of the current request */ return ngx_http_output_filter(r, NULL); } /* do nothing */ return NGX_OK; }
static void ngx_http_parallel_wev_handler(ngx_http_request_t *r) { ngx_http_parallel_fiber_ctx_t* fiber; ngx_http_parallel_ctx_t *ctx; ngx_int_t rc; ctx = ngx_http_get_module_ctx(r, ngx_http_parallel_module); // restore the write event handler r->write_event_handler = ctx->original_write_event_handler; ctx->original_write_event_handler = NULL; // get the fiber fiber = ctx->completed_fiber; ctx->completed_fiber = NULL; if (fiber == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "ngx_http_parallel_wev_handler: unexpected, fiber is null"); return; } // code taken from echo-nginx-module to work around nginx subrequest issues if (r == r->connection->data && r->postponed) { if (r->postponed->request) { r->connection->data = r->postponed->request; #if defined(nginx_version) && nginx_version >= 8012 ngx_http_post_request(r->postponed->request, NULL); #else ngx_http_post_request(r->postponed->request); #endif } else { ngx_http_output_filter(r, NULL); } } rc = ngx_http_parallel_handle_request_complete(ctx, fiber->sr, fiber); if (rc != NGX_OK && ctx->error_code == NGX_AGAIN) { ctx->error_code = rc; } if (ctx->error_code != NGX_AGAIN && ctx->active_fibers == 0) { ngx_http_finalize_request(r, ctx->error_code); } }
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); }