2004-06-23 23:18:17 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-06-23 23:18:17 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_channel.h>
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_write_channel(ngx_socket_t s, ngx_channel_t *ch, size_t size,
|
|
|
|
ngx_log_t *log)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
ngx_err_t err;
|
|
|
|
struct iovec iov[1];
|
|
|
|
struct msghdr msg;
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
|
2004-06-23 23:18:17 +08:00
|
|
|
|
|
|
|
union {
|
|
|
|
struct cmsghdr cm;
|
|
|
|
char space[CMSG_SPACE(sizeof(int))];
|
|
|
|
} cmsg;
|
|
|
|
|
|
|
|
if (ch->fd == -1) {
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
msg.msg_control = (caddr_t) &cmsg;
|
|
|
|
msg.msg_controllen = sizeof(cmsg);
|
|
|
|
|
|
|
|
cmsg.cm.cmsg_len = sizeof(cmsg);
|
|
|
|
cmsg.cm.cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg.cm.cmsg_type = SCM_RIGHTS;
|
|
|
|
*(int *) CMSG_DATA(&cmsg.cm) = ch->fd;
|
|
|
|
}
|
|
|
|
|
2005-01-25 20:27:35 +08:00
|
|
|
msg.msg_flags = 0;
|
|
|
|
|
2004-06-23 23:18:17 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
if (ch->fd == -1) {
|
|
|
|
msg.msg_accrights = NULL;
|
|
|
|
msg.msg_accrightslen = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
msg.msg_accrights = (caddr_t) &ch->fd;
|
|
|
|
msg.msg_accrightslen = sizeof(int);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
iov[0].iov_base = (char *) ch;
|
|
|
|
iov[0].iov_len = size;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
|
|
n = sendmsg(s, &msg, 0);
|
|
|
|
|
|
|
|
if (n == -1) {
|
|
|
|
err = ngx_errno;
|
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, err, "sendmsg() failed");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_read_channel(ngx_socket_t s, ngx_channel_t *ch, size_t size,
|
|
|
|
ngx_log_t *log)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
ngx_err_t err;
|
|
|
|
struct iovec iov[1];
|
|
|
|
struct msghdr msg;
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
|
2004-06-23 23:18:17 +08:00
|
|
|
union {
|
|
|
|
struct cmsghdr cm;
|
|
|
|
char space[CMSG_SPACE(sizeof(int))];
|
|
|
|
} cmsg;
|
|
|
|
#else
|
|
|
|
int fd;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
iov[0].iov_base = (char *) ch;
|
|
|
|
iov[0].iov_len = size;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
|
2004-06-23 23:18:17 +08:00
|
|
|
msg.msg_control = (caddr_t) &cmsg;
|
|
|
|
msg.msg_controllen = sizeof(cmsg);
|
|
|
|
#else
|
|
|
|
msg.msg_accrights = (caddr_t) &fd;
|
|
|
|
msg.msg_accrightslen = sizeof(int);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
n = recvmsg(s, &msg, 0);
|
|
|
|
|
|
|
|
if (n == -1) {
|
|
|
|
err = ngx_errno;
|
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, err, "recvmsg() failed");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-11-11 22:07:14 +08:00
|
|
|
if (n == 0) {
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "recvmsg() returned zero");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-06-23 23:18:17 +08:00
|
|
|
if ((size_t) n < sizeof(ngx_channel_t)) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"recvmsg() returned not enough data: %uz", n);
|
2004-06-23 23:18:17 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-11-26 00:17:31 +08:00
|
|
|
#if (NGX_HAVE_MSGHDR_MSG_CONTROL)
|
2004-06-23 23:18:17 +08:00
|
|
|
|
|
|
|
if (ch->command == NGX_CMD_OPEN_CHANNEL) {
|
|
|
|
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
if (cmsg.cm.cmsg_len < (socklen_t) sizeof(cmsg)) {
|
2004-06-23 23:18:17 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
|
|
"recvmsg() returned too small ancillary data");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmsg.cm.cmsg_level != SOL_SOCKET || cmsg.cm.cmsg_type != SCM_RIGHTS)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
|
|
"recvmsg() returned invalid ancillary data "
|
|
|
|
"level %d or type %d",
|
|
|
|
cmsg.cm.cmsg_level, cmsg.cm.cmsg_type);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch->fd = *(int *) CMSG_DATA(&cmsg.cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
|
|
"recvmsg() truncated data");
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (ch->command == NGX_CMD_OPEN_CHANNEL) {
|
|
|
|
if (msg.msg_accrightslen != sizeof(int)) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, 0,
|
|
|
|
"recvmsg() returned no ancillary data");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch->fd = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_add_channel_event(ngx_cycle_t *cycle, ngx_fd_t fd,
|
|
|
|
ngx_int_t event, ngx_event_handler_pt handler)
|
|
|
|
{
|
|
|
|
ngx_event_t *ev, *rev, *wev;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
|
|
|
|
c = &cycle->connections[fd];
|
|
|
|
rev = &cycle->read_events[fd];
|
|
|
|
wev = &cycle->write_events[fd];
|
|
|
|
|
|
|
|
ngx_memzero(c, sizeof(ngx_connection_t));
|
|
|
|
ngx_memzero(rev, sizeof(ngx_event_t));
|
|
|
|
ngx_memzero(wev, sizeof(ngx_event_t));
|
|
|
|
|
|
|
|
c->fd = fd;
|
|
|
|
c->pool = cycle->pool;
|
|
|
|
|
|
|
|
c->read = rev;
|
|
|
|
c->write = wev;
|
|
|
|
|
|
|
|
c->log = cycle->log;
|
|
|
|
rev->log = cycle->log;
|
|
|
|
wev->log = cycle->log;
|
|
|
|
rev->index = NGX_INVALID_INDEX;
|
|
|
|
wev->index = NGX_INVALID_INDEX;
|
|
|
|
rev->data = c;
|
|
|
|
wev->data = c;
|
|
|
|
|
2004-07-07 23:01:00 +08:00
|
|
|
#if (NGX_THREADS)
|
|
|
|
rev->lock = &c->lock;
|
|
|
|
wev->lock = &c->lock;
|
|
|
|
rev->own_lock = &c->lock;
|
|
|
|
wev->own_lock = &c->lock;
|
|
|
|
#endif
|
|
|
|
|
2004-06-23 23:18:17 +08:00
|
|
|
ev = (event == NGX_READ_EVENT) ? rev : wev;
|
|
|
|
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
ev->handler = handler;
|
2004-06-23 23:18:17 +08:00
|
|
|
|
2004-07-15 00:01:42 +08:00
|
|
|
if (ngx_add_conn && (ngx_event_flags & NGX_USE_EPOLL_EVENT) == 0) {
|
2004-06-23 23:18:17 +08:00
|
|
|
if (ngx_add_conn(c) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ngx_add_event(ev, event, 0) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2004-07-15 00:01:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
void ngx_close_channel(ngx_fd_t *fd, ngx_log_t *log)
|
|
|
|
{
|
|
|
|
if (close(fd[0]) == -1) {
|
2004-11-12 22:35:09 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() channel failed");
|
2004-07-15 00:01:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd[1]) == -1) {
|
2004-11-12 22:35:09 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() channel failed");
|
2004-07-15 00:01:42 +08:00
|
|
|
}
|
|
|
|
}
|