unsigned pipeline:1;
unsigned unexpected_eof:1;
unsigned tcp_nopush:1;
- unsigned tcp_nopush_enabled:1;
#if (HAVE_IOCP)
unsigned accept_context_updated:1;
#endif
#define NGX_OK 0
#define NGX_ERROR -1
#define NGX_AGAIN -2
-#define NGX_DONE -3
-/*
-#define NGX_BUSY -3
-*/
#define NGX_DECLINED -4
+#define NGX_ABORT -5
+
/*
+#define NGX_DONE -3
+#define NGX_BUSY -3
#define NGX_ALERT -5
*/
#include <ngx_iocp_module.h>
#endif
+
+
+ngx_inline static int ngx_handle_read_event(ngx_event_t *rev)
+{
+ if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) {
+
+ /* aio, iocp, epoll */
+
+ return NGX_OK;
+ }
+
+ if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
+
+ /* kqueue */
+
+ if (!rev->active && !rev->ready) {
+ if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT)
+ == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+ }
+
+ return NGX_OK;
+ }
+
+ /* select, poll, /dev/poll */
+
+ if (!rev->active && !rev->ready) {
+ if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT) == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ return NGX_OK;
+ }
+
+ if (rev->active && rev->ready) {
+ if (ngx_del_event(rev, NGX_READ_EVENT, 0) == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ return NGX_OK;
+ }
+
+ return NGX_OK;
+}
+
+
+ngx_inline static int ngx_handle_write_event(ngx_event_t *wev, int lowat)
+{
+ if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) {
+
+ /* aio, iocp, epoll */
+
+ return NGX_OK;
+ }
+
+ if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
+
+ /* kqueue */
+
+#if (HAVE_LOWAT_EVENT) /* kqueue's NOTE_LOWAT */
+
+ if (ngx_event_flags & NGX_HAVE_LOWAT_EVENT) {
+ wev->lowat = lowat;
+ }
+
+#endif
+ if (!wev->active && !wev->ready) {
+ if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT)
+ == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+ }
+
+ return NGX_OK;
+ }
+
+ /* select, poll, /dev/poll */
+
+ if (!wev->active && !wev->ready) {
+ if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT) == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ return NGX_OK;
+ }
+
+ if (wev->active && wev->ready) {
+ if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ return NGX_OK;
+ }
+
+ return NGX_OK;
+}
+
+
+
/* ***************************** */
} else if (p->allocated < p->max_block_size) {
h = ngx_create_temp_hunk(p->pool, p->block_size, 20, 20);
if (h == NULL) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
p->allocated += p->block_size;
temp = ngx_alloc_chain_entry(p->pool);
if (temp == NULL) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
temp->hunk = h;
if (p->upstream->read->blocked) {
if (ngx_add_event(p->upstream->read, NGX_READ_EVENT,
NGX_LEVEL_EVENT) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
p->block_upstream = 0;
p->upstream->read->blocked = 0;
h = ngx_alloc_hunk(p->pool);
if (h == NULL) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
ngx_memcpy(h, entry->hunk, sizeof(ngx_hunk_t));
temp = ngx_alloc_chain_entry(p->pool);
if (temp == NULL) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
temp->hunk = h;
if (p->input_filter) {
if (p->input_filter(p, chain) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
}
#if (NGX_EVENT_COPY_FILTER)
if (p->input_filter(p, NULL) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
#else
if (p->input_filter) {
if (p->input_filter(p, NULL) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
} else {
}
if (p->out_hunks && p->downstream->write->ready) {
- if (ngx_event_proxy_write_to_downstream(p) == NGX_ERROR
- && p->fatal_error)
- {
- return NGX_ERROR;
+ if (ngx_event_proxy_write_to_downstream(p) == NGX_ABORT) {
+ return NGX_ABORT;
}
}
} else if ((p->out_hunks || p->in_hunks) && p->downstream->write->ready) {
- if (ngx_event_proxy_write_to_downstream(p) == NGX_ERROR
- && p->fatal_error)
- {
- return NGX_ERROR;
+ if (ngx_event_proxy_write_to_downstream(p) == NGX_ABORT) {
+ return NGX_ABORT;
}
}
&& ngx_event_flags & NGX_USE_LEVEL_EVENT)
{
if (ngx_del_event(p->upstream->read, NGX_READ_EVENT, 0) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
p->upstream->read->blocked = 1;
{
if (ngx_del_event(p->downstream->write, NGX_WRITE_EVENT, 0)
== NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
p->downstream->write->blocked = 1;
if (p->downstream->write->blocked) {
if (ngx_add_event(p->downstream->write, NGX_WRITE_EVENT,
NGX_LEVEL_EVENT) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
p->downstream->write->blocked = 0;
}
}
if (p->upstream->read->ready) {
- if (ngx_event_proxy_read_upstream(p) == NGX_ERROR && p->fatal_error) {
- return NGX_ERROR;
+ if (ngx_event_proxy_read_upstream(p) == NGX_ERROR) {
+ return NGX_ABORT;
}
}
p->cachable);
if (rc == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
if (rc == NGX_AGAIN) {
if (ngx_write_chain_to_file(p->temp_file, p->in_hunks, p->temp_offset,
p->pool) == NGX_ERROR) {
- p->fatal_error = 1;
- return NGX_ERROR;
+ return NGX_ABORT;
}
for (entry = p->in_hunks; entry; entry = next) {
void *output_data;
unsigned cachable:1;
- unsigned fatal_error:1;
unsigned block_upstream:1;
unsigned upstream_eof:1;
unsigned upstream_error:1;
rc = ngx_http_send_header(r);
- if (rc == NGX_ERROR || rc > NGX_OK) {
- ngx_http_finalize_request(r, rc);
- return NGX_OK;
- }
-
- if (r->header_only) {
- ngx_http_finalize_request(r, rc);
-
-#if 0
- if (rc == NGX_AGAIN) {
- ngx_http_set_write_handler(r);
- } else {
- ngx_http_finalize_request(r, 0);
- }
-#endif
-
- return NGX_OK;
+ if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
+ return rc;
}
-
-#if 0
h->type = r->main ? NGX_HUNK_FILE : NGX_HUNK_FILE|NGX_HUNK_LAST;
-#else
- h->type = NGX_HUNK_FILE;
-#endif
-
h->file_pos = 0;
h->file_last = ngx_file_size(r->file.info);
h->file->fd = r->file.fd;
h->file->log = r->connection->log;
- rc = ngx_http_output_filter(r, h);
-
- ngx_http_finalize_request(r, rc);
-
-#if 0
- if (r->main == NULL) {
- if (rc == NGX_AGAIN) {
- ngx_http_set_write_handler(r);
-
- } else {
- ngx_http_finalize_request(r, 0);
- }
- }
-#endif
-
- return NGX_OK;
+ return ngx_http_output_filter(r, h);
}
ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
- ngx_test_null(h, ngx_push_array(&cmcf->translate_handlers), NGX_ERROR);
+ ngx_test_null(h, ngx_push_array(
+ &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers),
+ NGX_ERROR);
*h = ngx_http_proxy_translate_handler;
/* init list of the handlers */
- ngx_init_array(cmcf->translate_handlers, cf->cycle->pool,
- 10, sizeof(ngx_http_handler_pt), NGX_CONF_ERROR);
+ ngx_init_array(cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers,
+ cf->cycle->pool, 10, sizeof(ngx_http_handler_pt),
+ NGX_CONF_ERROR);
+
+ cmcf->phases[NGX_HTTP_REWRITE_PHASE].type = NGX_OK;
+ cmcf->phases[NGX_HTTP_REWRITE_PHASE].post_handler =
+ ngx_http_find_location_config;
+
+
+ ngx_init_array(cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers,
+ cf->cycle->pool, 10, sizeof(ngx_http_handler_pt),
+ NGX_CONF_ERROR);
+
+ cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].type = NGX_OK;
+
ngx_init_array(cmcf->index_handlers, cf->cycle->pool,
3, sizeof(ngx_http_handler_pt), NGX_CONF_ERROR);
} ngx_http_log_ctx_t;
-typedef int (*ngx_http_handler_pt)(ngx_http_request_t *r);
-
typedef int (*ngx_http_output_header_filter_p)(ngx_http_request_t *r);
typedef int (*ngx_http_output_body_filter_p)
int ngx_http_find_server_conf(ngx_http_request_t *r);
void ngx_http_handler(ngx_http_request_t *r);
void ngx_http_finalize_request(ngx_http_request_t *r, int error);
-void ngx_http_set_write_handler(ngx_http_request_t *r);
+void ngx_http_writer(ngx_event_t *wev);
int ngx_http_send_last(ngx_http_request_t *r);
#include <ngx_config.h>
#include <ngx_core.h>
+#include <ngx_event.h>
#include <ngx_http.h>
#include <nginx.h>
/* STUB */
int ngx_http_static_handler(ngx_http_request_t *r);
+static void ngx_http_phase_event_handler(ngx_event_t *rev);
+static void ngx_http_run_phases(ngx_http_request_t *r);
static int ngx_http_core_index_handler(ngx_http_request_t *r);
void ngx_http_handler(ngx_http_request_t *r)
{
- int rc, i;
- ngx_http_log_ctx_t *lcx;
- ngx_http_handler_pt *h;
- ngx_http_core_loc_conf_t *clcf, **clcfp;
- ngx_http_core_srv_conf_t *cscf;
- ngx_http_core_main_conf_t *cmcf;
+ ngx_http_log_ctx_t *lcx;
r->connection->unexpected_eof = 0;
/* TEST STUB */ r->lingering_close = 1;
+ r->connection->write->event_handler = ngx_http_phase_event_handler;
+
+ r->phase = 0;
+ r->phase_handler = 0;
+
+ ngx_http_run_phases(r);
+
+ return;
+}
+
+
+static void ngx_http_phase_event_handler(ngx_event_t *ev)
+{
+ ngx_connection_t *c;
+ ngx_http_request_t *r;
+
+ c = ev->data;
+ r = c->data;
+
+ ngx_http_run_phases(r);
+
+ return;
+}
+
+
+static void ngx_http_run_phases(ngx_http_request_t *r)
+{
+ int rc;
+ ngx_http_handler_pt *h;
+ ngx_http_core_main_conf_t *cmcf;
+
+ cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
+
+ rc = NGX_DECLINED;
+
+ for (/* void */; r->phase < NGX_HTTP_LAST_PHASE; r->phase++) {
+
+ h = cmcf->phases[r->phase].handlers.elts;
+ for (r->phase_handler = cmcf->phases[r->phase].handlers.nelts - 1;
+ r->phase_handler >= 0;
+ r->phase_handler--)
+ {
+ rc = h[r->phase_handler](r);
+
+ if (rc == NGX_DECLINED) {
+ continue;
+ }
+
+ if (rc == NGX_AGAIN) {
+ return;
+ }
+
+ if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
+ ngx_http_finalize_request(r, rc);
+ return;
+ }
+
+ if (rc == NGX_OK && cmcf->phases[r->phase].type == NGX_OK) {
+ break;
+ }
+ }
+
+ if (cmcf->phases[r->phase].post_handler) {
+ rc = cmcf->phases[r->phase].post_handler(r);
- /* TODO: run rewrite url phase */
+ if (rc == NGX_AGAIN) {
+ return;
+ }
+ if (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE) {
+ ngx_http_finalize_request(r, rc);
+ return;
+ }
+ }
+ }
- /* find location config */
+ if (r->content_handler) {
+ r->connection->write->event_handler = ngx_http_writer;
+ rc = r->content_handler(r);
+ ngx_http_finalize_request(r, rc);
+ return;
+ }
+
+ /* TODO: no handlers found ? */
+ ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+}
+
+
+int ngx_http_find_location_config(ngx_http_request_t *r)
+{
+ int i, rc;
+ ngx_http_core_loc_conf_t *clcf, **clcfp;
+ ngx_http_core_srv_conf_t *cscf;
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY;
}
- cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
-
- /* run translation phase */
-
- h = cmcf->translate_handlers.elts;
- for (i = cmcf->translate_handlers.nelts - 1; i >= 0; i--) {
-
- rc = h[i](r);
-
- if (rc == NGX_DECLINED) {
- continue;
- }
-
- if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
- ngx_http_finalize_request(r, rc);
- return;
- }
-
- if (rc == NGX_OK) {
- rc = r->handler(r);
- if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
- ngx_http_finalize_request(r, rc);
- }
- return;
- }
- }
-
- /* TODO: no handlers found ? */
- ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
+ return NGX_OK;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (clcf->handler) {
- r->handler = clcf->handler;
+ r->content_handler = clcf->handler;
return NGX_OK;
}
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
if (r->uri.data[r->uri.len - 1] == '/') {
- r->handler = ngx_http_core_index_handler;
+ r->content_handler = ngx_http_core_index_handler;
return NGX_OK;
}
return NGX_HTTP_MOVED_PERMANENTLY;
}
- r->handler = ngx_http_static_handler;
+ r->content_handler = ngx_http_static_handler;
return NGX_OK;
}
ctx = (ngx_http_conf_ctx_t *) cycle->conf_ctx[ngx_http_module.index];
cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
- ngx_test_null(h, ngx_push_array(&cmcf->translate_handlers), NGX_ERROR);
+ ngx_test_null(h, ngx_push_array(
+ &cmcf->phases[NGX_HTTP_TRANSLATE_PHASE].handlers),
+ NGX_ERROR);
*h = ngx_http_core_translate_handler;
ngx_http_core_main_conf_t *cmcf;
ngx_test_null(cmcf,
- ngx_palloc(cf->pool, sizeof(ngx_http_core_main_conf_t)),
+ ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t)),
NGX_CONF_ERROR);
ngx_init_array(cmcf->servers, cf->pool,
typedef struct {
- ngx_array_t servers; /* array of ngx_http_core_srv_conf_t */
- ngx_array_t translate_handlers;
- ngx_array_t index_handlers;
+ ngx_array_t handlers;
+ int type; /* NGX_OK, NGX_DECLINED */
+ ngx_http_handler_pt post_handler;
+} ngx_http_phase_t;
+
+#define NGX_HTTP_REWRITE_PHASE 0
+#define NGX_HTTP_TRANSLATE_PHASE 1
+#define NGX_HTTP_LAST_PHASE 2
+
+typedef struct {
+ ngx_array_t servers; /* array of ngx_http_core_srv_conf_t */
+
+ ngx_http_phase_t phases[NGX_HTTP_LAST_PHASE];
+ ngx_array_t index_handlers;
} ngx_http_core_main_conf_t;
+int ngx_http_find_location_config(ngx_http_request_t *r);
int ngx_http_core_translate_handler(ngx_http_request_t *r);
int ngx_http_internal_redirect(ngx_http_request_t *r,
#include <ngx_http.h>
#include <nginx.h>
-/* STUB probably, needed for ngx_freebsd_tcp_nopush_flush */
-#ifdef __FreeBSD__
-#include <ngx_freebsd_init.h>
-#endif
-
-
static int ngx_http_header_filter_init(ngx_cycle_t *cycle);
static int ngx_http_header_filter(ngx_http_request_t *r);
ngx_chain_t *ch;
ngx_table_elt_t *header;
-#ifdef __FreeBSD__
-
- if (r->keepalive) {
- if (ngx_freebsd_tcp_nopush_flush) {
- r->connection->tcp_nopush_enabled = 1;
- }
-
- } else {
- r->connection->tcp_nopush_enabled = 1;
- }
-
-#else
-
- r->connection->tcp_nopush_enabled = 1;
-
-#endif
-
if (r->http_version < NGX_HTTP_VERSION_10) {
return NGX_OK;
}
ctx->last_out = &ce->next;
ctx->hunk = NULL;
- break;
+ if (ctx->free == NULL) {
+ break;
+ }
}
if (ctx->out == NULL && last != NGX_NONE) {
static void ngx_http_process_request_headers(ngx_event_t *rev);
static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
-static void ngx_http_writer(ngx_event_t *ev);
+static void ngx_http_set_write_handler(ngx_http_request_t *r);
static void ngx_http_block_read(ngx_event_t *ev);
static void ngx_http_read_discarded_body_event(ngx_event_t *rev);
return;
}
+ if (ngx_handle_read_event(rev) == NGX_ERROR) {
+ ngx_http_close_connection(c);
+ return;
+ }
+
+#if 0
+
if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
/* kqueue */
event = NGX_CLEAR_EVENT;
if (ngx_add_event(rev, NGX_READ_EVENT, event) == NGX_ERROR) {
ngx_http_close_connection(c);
}
+
+#endif
+
+ return;
}
}
rev->event_handler = ngx_http_block_read;
+ c->write->event_handler = ngx_http_writer;
ngx_http_handler(r);
return;
r->header_timeout_set = 1;
}
+ if (ngx_handle_read_event(rev) == NGX_ERROR) {
+ ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ ngx_http_close_connection(r->connection);
+ return NGX_ERROR;
+ }
+
+#if 0
if (!rev->active) {
if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) {
/* kqueue */
return NGX_ERROR;
}
}
+#endif
return NGX_AGAIN;
}
}
-void ngx_http_finalize_request(ngx_http_request_t *r, int error)
+void ngx_http_finalize_request(ngx_http_request_t *r, int rc)
{
- int rc;
ngx_event_t *rev, *wev;
- if (r->main) {
+ if (r->main || r->closed) {
return;
}
- rc = error;
-
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
rev = r->connection->read;
wev->timer_set = 0;
}
- rc = ngx_http_special_response_handler(r, rc);
-
- if (rc == NGX_AGAIN) {
- return;
- }
+ ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
- if (rc == NGX_ERROR) {
- ngx_http_close_request(r, 0);
- ngx_http_close_connection(r->connection);
- return;
- }
-
-#if 1
return;
-#endif
} else if (rc == NGX_ERROR) {
- r->keepalive = 0;
- r->lingering_close = 0;
-
- } else {
- if (ngx_http_send_last(r) == NGX_ERROR) {
- ngx_http_close_request(r, 0);
- ngx_http_close_connection(r->connection);
- return;
- }
+ ngx_http_close_request(r, 0);
+ ngx_http_close_connection(r->connection);
+ return;
- if (rc == NGX_AGAIN) {
- ngx_http_set_write_handler(r);
- return;
- }
+ } else if (rc == NGX_AGAIN) {
+ ngx_http_set_write_handler(r);
+ return;
}
rev = r->connection->read;
}
-void ngx_http_set_write_handler(ngx_http_request_t *r)
+static void ngx_http_set_write_handler(ngx_http_request_t *r)
{
int event;
ngx_event_t *wev;
ngx_add_timer(wev, clcf->send_timeout);
wev->timer_set = 1;
+ if (ngx_handle_write_event(wev, clcf->send_lowat) == NGX_ERROR) {
+ ngx_http_close_request(r, 0);
+ ngx_http_close_connection(r->connection);
+ }
+
+#if 0
+
if (ngx_event_flags & (NGX_HAVE_AIO_EVENT|NGX_HAVE_EDGE_EVENT)) {
/* aio, iocp, epoll */
return;
ngx_http_close_connection(r->connection);
}
+#endif
+
return;
}
-static void ngx_http_writer(ngx_event_t *wev)
+void ngx_http_writer(ngx_event_t *wev)
{
int rc;
ngx_event_t *rev;
ctx->url = NULL;
ngx_destroy_pool(r->pool);
+
+ r->closed = 1;
+
+ return;
}
c->fd = -1;
ngx_destroy_pool(c->pool);
+
+ return;
}
typedef struct ngx_http_request_s ngx_http_request_t;
+typedef int (*ngx_http_handler_pt)(ngx_http_request_t *r);
+
struct ngx_http_request_s {
ngx_connection_t *connection;
ngx_http_headers_in_t headers_in;
ngx_http_headers_out_t headers_out;
- int (*handler)(ngx_http_request_t *r);
-
time_t lingering_time;
int method;
ngx_str_t *server_name;
ngx_array_t *virtual_names;
+ int phase;
+ int phase_handler;
+ ngx_http_handler_pt content_handler;
char *discarded_buffer;
unsigned header_only:1;
unsigned keepalive:1;
unsigned lingering_close:1;
+ unsigned closed:1;
/* TODO: use filter or bits ???? */
int filter;
rc = ngx_http_send_header(r);
- if (rc == NGX_ERROR) {
- return NGX_ERROR;
- }
-
- if (r->header_only) {
- ngx_http_finalize_request(r, rc);
-#if 0
- if (rc == NGX_AGAIN) {
- ngx_http_set_write_handler(r);
- return NGX_AGAIN;
- }
-#endif
-
- return NGX_OK;
+ if (rc == NGX_ERROR || r->header_only) {
+ return rc;
}
if (error_pages[err].len == 0) {
h->pos = error_tail;
h->last = error_tail + sizeof(error_tail) - 1;
- if (1) {
+ if (/* STUB: "msie_padding on/off" */ 1) {
if (ngx_http_output_filter(r, h) == NGX_ERROR) {
return NGX_ERROR;
}
h->type |= NGX_HUNK_LAST;
- rc = ngx_http_output_filter(r, h);
-
- ngx_http_finalize_request(r, rc);
-
-#if 0
- if (r->main == NULL) {
- if (rc == NGX_AGAIN) {
- ngx_http_set_write_handler(r);
- return NGX_AGAIN;
- }
- }
-#endif
-
- return NGX_OK;
-
+ return ngx_http_output_filter(r, h);
}
return NGX_AGAIN;
}
+ if (size == 0) {
+ return NGX_OK;
+ }
+
chain = ngx_write_chain(r->connection, ctx->out);
#if (NGX_DEBUG_WRITE_FILTER)
int ngx_freebsd_hw_ncpu;
int ngx_freebsd_net_inet_tcp_sendspace;
int ngx_freebsd_sendfile_nbytes_bug;
-int ngx_freebsd_tcp_nopush_flush;
+int ngx_freebsd_use_tcp_nopush;
/* FreeBSD 5.0 */
int ngx_freebsd_kern_ipc_zero_copy_send;
if ((version < 500000 && version >= 440003) || version >= 500017) {
- ngx_freebsd_tcp_nopush_flush = 1;
+ ngx_freebsd_use_tcp_nopush = 1;
}
extern int ngx_freebsd_hw_ncpu;
extern int ngx_freebsd_net_inet_tcp_sendspace;
extern int ngx_freebsd_sendfile_nbytes_bug;
-extern int ngx_freebsd_tcp_nopush_flush;
extern int ngx_freebsd_kern_ipc_zero_copy_send;
-extern int ngx_freebsd_tcp_nopush_flush;
+extern int ngx_freebsd_use_tcp_nopush;
#endif /* _NGX_FREEBSD_INIT_H_INCLUDED_ */
* and the first part of the file in one packet but also sends 4K pages
* in the full packets.
*
- * Until FreeBSD 4.5 the turning TCP_NOPUSH off does not not flush
- * the pending data that less than MSS and the data sent with 5 second delay.
- * So we use TCP_NOPUSH on FreeBSD prior to 4.5 only if the connection
- * is not needed to be keepalive.
+ * Until FreeBSD 4.5 the turning TCP_NOPUSH off does not flush
+ * the pending data that less than MSS so the data is sent with 5 second delay.
+ * We do not use TCP_NOPUSH on FreeBSD prior to 4.5 although it can be used
+ * for non-keepalive HTTP connections.
*/
ngx_chain_t *ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in)
{
- int rc, eintr, tcp_nopush;
+ int rc, eintr;
char *prev;
ssize_t hsize, size;
off_t sent;
ngx_hunk_t *file;
ngx_chain_t *ce, *tail;
- tcp_nopush = 0;
-
do {
ce = in;
file = NULL;
ngx_init_array(trailer, c->pool, 10, sizeof(struct iovec),
NGX_CHAIN_ERROR);
- /* create the header iovec */
-
-#if 0
- if (ngx_hunk_in_memory_only(ce->hunk) || ngx_hunk_special(ce->hunk)) {
-#endif
- prev = NULL;
- iov = NULL;
-
- /* create the iovec and coalesce the neighbouring chain entries */
+ /* create the iovec and coalesce the neighbouring chain entries */
- for ( /* void */; ce; ce = ce->next) {
- if (ngx_hunk_special(ce->hunk)) {
- continue;
- }
+ prev = NULL;
+ iov = NULL;
- if (!ngx_hunk_in_memory_only(ce->hunk)) {
- break;
- }
+ for (ce = in; ce; ce = ce->next) {
+ if (ngx_hunk_special(ce->hunk)) {
+ continue;
+ }
- if (prev == ce->hunk->pos) {
- iov->iov_len += ce->hunk->last - ce->hunk->pos;
- prev = ce->hunk->last;
+ if (!ngx_hunk_in_memory_only(ce->hunk)) {
+ break;
+ }
- } else {
- ngx_test_null(iov, ngx_push_array(&header),
- NGX_CHAIN_ERROR);
- iov->iov_base = ce->hunk->pos;
- iov->iov_len = ce->hunk->last - ce->hunk->pos;
- prev = ce->hunk->last;
- }
+ if (prev == ce->hunk->pos) {
+ iov->iov_len += ce->hunk->last - ce->hunk->pos;
+ prev = ce->hunk->last;
- hsize += ce->hunk->last - ce->hunk->pos;
+ } else {
+ ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
+ iov->iov_base = ce->hunk->pos;
+ iov->iov_len = ce->hunk->last - ce->hunk->pos;
+ prev = ce->hunk->last;
}
-#if 0
+
+ hsize += ce->hunk->last - ce->hunk->pos;
}
-#endif
/* TODO: coalesce the neighbouring file hunks */
ce = ce->next;
}
- /* create the trailer iovec */
-
-#if 0
- if (ce
- && (ngx_hunk_in_memory_only(ce->hunk)
- || ngx_hunk_special(ce->hunk)))
- {
-#endif
- prev = NULL;
- iov = NULL;
+ /* create the iovec and coalesce the neighbouring chain entries */
- /* create the iovec and coalesce the neighbouring chain entries */
+ prev = NULL;
+ iov = NULL;
- for ( /* void */; ce; ce = ce->next) {
- if (ngx_hunk_special(ce->hunk)) {
- continue;
- }
+ for ( /* void */; ce; ce = ce->next) {
+ if (ngx_hunk_special(ce->hunk)) {
+ continue;
+ }
- if (!ngx_hunk_in_memory_only(ce->hunk)) {
- break;
- }
+ if (!ngx_hunk_in_memory_only(ce->hunk)) {
+ break;
+ }
- if (prev == ce->hunk->pos) {
- iov->iov_len += ce->hunk->last - ce->hunk->pos;
- prev = ce->hunk->last;
+ if (prev == ce->hunk->pos) {
+ iov->iov_len += ce->hunk->last - ce->hunk->pos;
+ prev = ce->hunk->last;
- } else {
- ngx_test_null(iov, ngx_push_array(&trailer),
- NGX_CHAIN_ERROR);
- iov->iov_base = ce->hunk->pos;
- iov->iov_len = ce->hunk->last - ce->hunk->pos;
- prev = ce->hunk->last;
- }
+ } else {
+ ngx_test_null(iov, ngx_push_array(&trailer), NGX_CHAIN_ERROR);
+ iov->iov_base = ce->hunk->pos;
+ iov->iov_len = ce->hunk->last - ce->hunk->pos;
+ prev = ce->hunk->last;
}
-#if 0
}
-#endif
tail = ce;
if (file) {
- if (!c->tcp_nopush && c->tcp_nopush_enabled) {
+ if (ngx_freebsd_use_tcp_nopush && !c->tcp_nopush) {
c->tcp_nopush = 1;
- tcp_nopush = 1;
+
+ngx_log_debug(c->log, "NOPUSH");
+
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
ngx_log_error(NGX_LOG_CRIT, c->log, ngx_socket_errno,
ngx_tcp_nopush_n " failed");
return NGX_CHAIN_ERROR;
}
-ngx_log_debug(c->log, "NOPUSH");
}
hdtr.headers = (struct iovec *) header.elts;
#endif
} else {
- if (hsize) {
- rc = writev(c->fd, (struct iovec *) header.elts, header.nelts);
-
- if (rc == -1) {
- err = ngx_errno;
- if (err == NGX_EAGAIN) {
- ngx_log_error(NGX_LOG_INFO, c->log, err,
- "writev() EAGAIN");
-
- } else if (err == NGX_EINTR) {
- eintr = 1;
- ngx_log_error(NGX_LOG_INFO, c->log, err,
- "writev() EINTR");
-
- } else {
- ngx_log_error(NGX_LOG_CRIT, c->log, err,
- "writev() failed");
- return NGX_CHAIN_ERROR;
- }
+ rc = writev(c->fd, (struct iovec *) header.elts, header.nelts);
+
+ if (rc == -1) {
+ err = ngx_errno;
+ if (err == NGX_EAGAIN) {
+ ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EAGAIN");
+
+ } else if (err == NGX_EINTR) {
+ eintr = 1;
+ ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EINTR");
+
+ } else {
+ ngx_log_error(NGX_LOG_CRIT, c->log, err, "writev() failed");
+ return NGX_CHAIN_ERROR;
}
+ }
- sent = rc > 0 ? rc : 0;
+ sent = rc > 0 ? rc : 0;
#if (NGX_DEBUG_WRITE_CHAIN)
- ngx_log_debug(c->log, "writev: %qd" _ sent);
+ ngx_log_debug(c->log, "writev: %qd" _ sent);
#endif
-
- } else {
- sent = 0;
- }
}
c->sent += sent;