#define _NGINX_H_INCLUDED_
-#define NGINX_VER "nginx/0.0.1"
+#define NGINX_VER "ng:nx/0.0.1"
#define NGINX_CONF "nginx.conf"
ngx_test_null(ce, ngx_alloc_chain_entry(pool), NGX_ERROR);
ce->hunk = in->hunk;
- ce->next = NULL;
*le = ce;
le = &ce->next;
in = in->next;
}
+ *le = NULL;
+
return NGX_OK;
}
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_event.h>
#include <ngx_event_connect.h>
int ngx_event_connect_peer(ngx_peer_connection_t *pc)
{
+ int rc, instance;
time_t now;
- ngx_peer_r *peer;
+ ngx_err_t err;
+ ngx_peer_t *peer;
ngx_socket_t s;
- struct sockaddr_in *addr;
+ ngx_event_t *rev, *wev;
+ ngx_connection_t *c;
+ struct sockaddr_in addr;
now = ngx_time();
pc->cached = 0;
pc->connection = NULL;
+ peer = &pc->peers->peers[0];
+
if (pc->peers->number > 1) {
/* there are several peers */
pc->cur_peer = pc->peers->current++;
- if (cp->peers->current >= cp->peers->number) {
+ if (pc->peers->current >= pc->peers->number) {
pc->peers->current = 0;
}
}
if (pc->tries == 0) {
/* ngx_unlock_mutex(pc->peers->mutex); */
+
return NGX_ERROR;
}
}
/* ngx_unlock_mutex(pc->peers->mutex); */
+#if 0
pc->addr_port_text = peer->addr_port_text;
+#endif
s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
}
}
- addr = p->sockaddr;
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = peer->addr;
+ addr.sin_port = htons(peer->port);
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = peer->addr;
- addr->sin_port = htons(peer->port);
-
- rc = connect(s, p->sockaddr, sizeof(struct sockaddr_in));
+ rc = connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
if (rc == -1) {
err = ngx_socket_errno;
}
}
- c->data = ???;
-
-
+ return NGX_OK;
}
#include <ngx_event.h>
+#define NGX_CONNECT_ERROR -10
+
+
typedef struct {
u_int32_t addr;
ngx_str_t host;
typedef struct {
- int current;
- int number;
- int max_fails;
- int fail_timeout;
+ int current;
+ int number;
+ int max_fails;
+ int fail_timeout;
+ int last_cached;
- /* ngx_mutex_t *mutex; */
- ngx_connection_t *cached;
+ /* ngx_mutex_t *mutex; */
+ ngx_connection_t **cached;
- ngx_peer_t peers[1];
+ ngx_peer_t peers[1];
} ngx_peers_t;
} ngx_peer_connection_t;
+int ngx_event_connect_peer(ngx_peer_connection_t *pc);
+void ngx_event_connect_peer_failed(ngx_peer_connection_t *pc);
+
+
#endif /* _NGX_EVENT_CONNECT_H_INCLUDED_ */
static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
- int rc, zin, zout;
+ int rc, wbits, mem_level, zin, zout;
struct gztrailer *trailer;
ngx_hunk_t *h;
ngx_chain_t *ce;
conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
if (ctx->alloc == NULL) {
+ wbits = MAX_WBITS;
+ mem_level = MAX_MEM_LEVEL - 1;
+
+ if (ctx->length > 0) {
+
+ /* the actual zlib window size is smaller by 262 bytes */
+
+ while (ctx->length < ((1 << (wbits - 1)) - 262)) {
+ wbits--;
+ mem_level--;
+ }
+ }
+
#if 0
ngx_test_null(ctx->alloc, ngx_alloc(200K, r->log), NGX_ERROR);
#else
ctx->alloc = (void *) ~NULL;
#endif
+
rc = deflateInit2(&ctx->zstream, /**/ 1, Z_DEFLATED,
- /**/ -MAX_WBITS, /**/ MAX_MEM_LEVEL - 1,
- Z_DEFAULT_STRATEGY);
+ -wbits, mem_level, Z_DEFAULT_STRATEGY);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
ctx->in_hunk = ctx->in->hunk;
ctx->in = ctx->in->next;
- ctx->zstream.next_in = ctx->in_hunk->pos;
+ ctx->zstream.next_in = (unsigned char *) ctx->in_hunk->pos;
ctx->zstream.avail_in = ctx->in_hunk->last - ctx->in_hunk->pos;
if (ctx->in_hunk->type & NGX_HUNK_LAST) {
break;
}
- ctx->zstream.next_out = ctx->out_hunk->pos;
+ ctx->zstream.next_out = (unsigned char *) ctx->out_hunk->pos;
ctx->zstream.avail_out = conf->hunk_size;
}
ctx->zstream.next_in _ ctx->zstream.next_out _
ctx->zstream.avail_in _ ctx->zstream.avail_out _ rc);
- ctx->in_hunk->pos = ctx->zstream.next_in;
+ ctx->in_hunk->pos = (char *) ctx->zstream.next_in;
if (ctx->zstream.avail_out == 0) {
ctx->out_hunk->last += conf->hunk_size;
ctx->redo = 1;
} else {
- ctx->out_hunk->last = ctx->zstream.next_out;
+ ctx->out_hunk->last = (char *) ctx->zstream.next_out;
ctx->redo = 0;
if (ctx->flush == Z_SYNC_FLUSH) {
ctx->zstream.avail_in = 0;
ctx->zstream.avail_out = 0;
- ngx_http_delete_ctx(r, ngx_http_gzip_filter_module);
#if 0
- ngx_free();
+ ngx_free(ctx->alloc);
#endif
+ ngx_http_delete_ctx(r, ngx_http_gzip_filter_module);
+
break;
} else if (conf->no_buffer && ctx->in == NULL) {
r->headers_out.content_length = -1;
r->headers_out.content_type->key.len = 0;
r->headers_out.content_type = NULL;
-
- /* TODO: delete "Accept-Ranges" header
+ r->headers_out.accept_ranges->key.len = 0;
}
return next_header_filter(r);
int rc, boundary, len, i;
char *p;
off_t start, end;
- ngx_table_elt_t *accept_ranges;
ngx_http_range_t *range;
ngx_http_range_filter_ctx_t *ctx;
|| r->headers_in.range->value.len < 7
|| ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
{
- ngx_test_null(accept_ranges,
+ ngx_test_null(r->headers_out.accept_ranges,
ngx_push_table(r->headers_out.headers),
NGX_ERROR);
- accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
- accept_ranges->key.data = "Accept-Ranges";
- accept_ranges->value.len = sizeof("bytes") - 1;
- accept_ranges->value.data = "bytes";
+ r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
+ r->headers_out.accept_ranges->key.data = "Accept-Ranges";
+ r->headers_out.accept_ranges->value.len = sizeof("bytes") - 1;
+ r->headers_out.accept_ranges->value.data = "bytes";
return next_header_filter(r);
}
static void ngx_http_proxy_send_request(ngx_event_t *wev);
+static void ngx_http_proxy_close_connection(ngx_connection_t *c);
+static ngx_chain_t *ngx_http_proxy_copy_request_hunks(ngx_http_proxy_ctx_t *p);
static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
p->action = "connecting to upstream";
p->request = r;
p->upstream.peers = p->lcf->peers;
+ p->upstream.tries = p->lcf->peers->number;
- /* TODO: change log->data, how to restore log->data ? */
+ /* TODO: log->data would be changed, how to restore log->data ? */
p->upstream.log = r->connection->log;
- do {
+ for ( ;; ) {
rc = ngx_event_connect_peer(&p->upstream);
if (rc == NGX_ERROR) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
- if (rc == NGX_OK) {
- ngx_http_proxy_send_request(p->upstream.connection->write);
- return NGX_OK;
+ if (rc == NGX_CONNECT_ERROR) {
+ ngx_event_connect_peer_failed(&p->upstream);
+
+ if (p->upstream.tries == 0) {
+ return NGX_HTTP_BAD_GATEWAY;
+ }
}
- if (rc == NGX_AGAIN) {
- /* TODO */ return NGX_OK;
+ p->upstream.connection->data = p;
+ p->upstream.connection->write->event_handler =
+ ngx_http_proxy_send_request;
+ p->upstream.connection->read->event_handler = /* STUB */ NULL;
+
+ if (p->upstream.tries > 1) {
+ ngx_test_null(p->work_request_hunks,
+ ngx_http_proxy_copy_request_hunks(p),
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+ } else {
+ p->work_request_hunks = p->request_hunks;
}
- /* rc == NGX_CONNECT_FAILED */
+ if (rc == NGX_OK) {
+ ngx_http_proxy_send_request(p->upstream.connection->write);
+ return NGX_OK;
+ }
- ngx_event_connect_peer_failed(&p->upstream);
+ /* rc == NGX_AGAIN */
- } while (p->upstream.tries);
+ /* timer */
- return NGX_HTTP_BAD_GATEWAY;
+ /* TODO */ return NGX_OK;
+ }
}
if (chain == (ngx_chain_t *) -1) {
ngx_http_proxy_close_connection(c);
- do {
+ for ( ;; ) {
rc = ngx_event_connect_peer(&p->upstream);
- if (rc == NGX_OK) {
+ if (rc == NGX_ERROR) {
+ ngx_http_finalize_request(p->request,
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
- /* copy chain and hunks p->request_hunks
- from p->initial_request_hunks */
+ if (rc == NGX_CONNECT_ERROR) {
+ ngx_event_connect_peer_failed(&p->upstream);
- p->request_hunks = NULL;
- if (ngx_chain_add_copy(r->pool, p->request_hunks,
- p->initial_request_hunks) == NGX_ERROR)
- {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
- }
-
- for (ce = p->request_hunks; ce; ce = ce->next) {
- ce->hunk->pos = ce->hunk->start;
+ if (p->upstream.tries == 0) {
+ return;
}
+ }
+ if (p->upstream.tries > 1) {
+ ngx_test_null(p->work_request_hunks,
+ ngx_http_proxy_copy_request_hunks(p),
+ /* void */);
+ } else {
+ p->work_request_hunks = p->request_hunks;
+ }
+ if (rc == NGX_OK) {
c = p->connection;
wev = c->write;
break;
}
- if (rc == NGX_ERROR) {
- ngx_http_finalize_request(p->request,
- NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- if (rc == NGX_AGAIN) {
- return;
- }
-
- /* rc == NGX_CONNECT_FAILED */
+ /* rc == NGX_AGAIN */
+ return;
- ngx_event_connect_peer_failed(&p->upstream);
-
- } while (p->upstream.tries);
-
- return;
+ }
} else {
- p->request_hunks = chain;
+ p->work_request_hunks = chain;
ngx_del_timer(wev);
}
}
+static void ngx_http_proxy_close_connection(ngx_connection_t *c)
+{
+ return;
+}
+
+static ngx_chain_t *ngx_http_proxy_copy_request_hunks(ngx_http_proxy_ctx_t *p)
+{
+ ngx_chain_t *ce, *te, *fe, **le;
+
+#if (NGX_SUPPRESS_WARN)
+ le = NULL;
+#endif
+
+ ngx_test_null(fe, ngx_alloc_chain_entry(p->request->pool), NULL);
+
+ te = fe;
+
+ for (ce = p->request_hunks; ce; ce = ce->next) {
+ te->hunk = ce->hunk;
+ *le = te;
+ le = &te->next;
+ ce->hunk->pos = ce->hunk->start;
+
+ ngx_test_null(te, ngx_alloc_chain_entry(p->request->pool), NULL);
+ }
+
+ *le = NULL;
+
+ return fe;
+}
+
static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len)
{
ngx_http_proxy_loc_conf_t *lcf;
+ ngx_chain_t *work_request_hunks;
ngx_chain_t *request_hunks;
char *action;
ngx_table_elt_t *location;
ngx_table_elt_t *last_modified;
ngx_table_elt_t *content_range;
+ ngx_table_elt_t *accept_ranges;
ngx_str_t charset;
ngx_array_t ranges;