Browse Source

[core] collect ioctl FIONREAD code

include <sys/ioctl.h> in files which use ioctl()
  instead of exposing header in local header "sys-socket.h"
personal/stbuehler/mod-csrf
Glenn Strauss 5 years ago
parent
commit
4796313efc
  1. 10
      src/connections.c
  2. 24
      src/fdevent.c
  3. 2
      src/fdevent.h
  4. 1
      src/fdevent_solaris_devpoll.c
  5. 7
      src/http-header-glue.c
  6. 1
      src/mod_cgi.c
  7. 5
      src/mod_fastcgi.c
  8. 1
      src/mod_proxy.c
  9. 1
      src/mod_scgi.c
  10. 1
      src/response.h
  11. 6
      src/sys-socket.h

10
src/connections.c

@ -968,12 +968,7 @@ static int connection_read_cq(server *srv, connection *con, chunkqueue *cq, off_
* if FIONREAD doesn't signal a big chunk we fill the previous buffer
* if it has >= 1kb free
*/
#if defined(__WIN32)
chunkqueue_get_memory(con->read_queue, &mem, &mem_len, 0, 4096);
len = recv(con->fd, mem, mem_len, 0);
#else /* __WIN32 */
if (ioctl(con->fd, FIONREAD, &toread) || toread <= 4*1024) {
if (0 != fdevent_ioctl_fionread(con->fd, S_IFSOCK, &toread) || toread <= 4096) {
toread = 4096;
}
else if (toread > MAX_READ_LIMIT) {
@ -981,6 +976,9 @@ static int connection_read_cq(server *srv, connection *con, chunkqueue *cq, off_
}
chunkqueue_get_memory(con->read_queue, &mem, &mem_len, 0, toread);
#if defined(__WIN32)
len = recv(con->fd, mem, mem_len, 0);
#else
len = read(con->fd, mem, mem_len);
#endif /* __WIN32 */

24
src/fdevent.c

@ -12,7 +12,6 @@
#include <errno.h>
#include <fcntl.h>
fdevents *fdevent_init(server *srv, size_t maxfds, int type) {
fdevents *ev;
@ -356,6 +355,29 @@ int fdevent_event_next_fdndx(fdevents *ev, int ndx) {
}
#include <sys/ioctl.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h> /* FIONREAD (for illumos (OpenIndiana)) */
#endif
#ifdef _WIN32
#include <winsock2.h>
#endif
int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread) {
#ifdef _WIN32
if (fdfmt != S_IFSOCK) { errno = ENOTSOCK; return -1; }
return ioctlsocket(fd, FIONREAD, toread);
#else
#ifdef __CYGWIN__
/*(cygwin supports FIONREAD on pipes, not sockets)*/
if (fdfmt != S_IFIFO) { errno = EOPNOTSUPP; return -1; }
#else
UNUSED(fdfmt);
#endif
return ioctl(fd, FIONREAD, toread);
#endif
}
#include <netinet/tcp.h>
#if (defined(__APPLE__) && defined(__MACH__)) \
|| defined(__FreeBSD__) || defined(__NetBSD__) \

2
src/fdevent.h

@ -223,6 +223,8 @@ int fdevent_solaris_port_init(fdevents *ev);
int fdevent_freebsd_kqueue_init(fdevents *ev);
int fdevent_libev_init(fdevents *ev);
int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread);
/* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */
int fdevent_is_tcp_half_closed(int fd);

1
src/fdevent_solaris_devpoll.c

@ -15,6 +15,7 @@
#ifdef USE_SOLARIS_DEVPOLL
# include <sys/devpoll.h>
# include <sys/ioctl.h>
static void fdevent_solaris_devpoll_free(fdevents *ev) {
free(ev->devpollfds);

7
src/http-header-glue.c

@ -1264,8 +1264,7 @@ handler_t http_response_read(server *srv, connection *con, http_response_opts *o
size_t avail = buffer_string_space(b);
unsigned int toread = 4096;
#if !defined(_WIN32) && !defined(__CYGWIN__)
if (0 == ioctl(fd, FIONREAD, (int *)&toread)) {
if (0 == fdevent_ioctl_fionread(fd, opts->fdfmt, (int *)&toread)) {
if (avail < toread) {
if (toread < 4096)
toread = 4096;
@ -1284,7 +1283,6 @@ handler_t http_response_read(server *srv, connection *con, http_response_opts *o
#endif
}
}
#endif
if (con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN) {
off_t cqlen = chunkqueue_length(con->write_queue);
@ -1303,7 +1301,8 @@ handler_t http_response_read(server *srv, connection *con, http_response_opts *o
}
if (avail < toread) {
avail = toread;
/*(add avail+toread to reduce allocations when ioctl EOPNOTSUPP)*/
avail = avail ? avail - 1 + toread : toread;
buffer_string_prepare_append(b, avail);
}

1
src/mod_cgi.c

@ -1006,6 +1006,7 @@ URIHANDLER_FUNC(cgi_is_handled) {
hctx->plugin_data = p;
hctx->cgi_handler = cgi_handler;
memcpy(&hctx->conf, &p->conf, sizeof(plugin_config));
hctx->opts.fdfmt = S_IFIFO;
hctx->opts.backend = BACKEND_CGI;
hctx->opts.authorizer = 0;
hctx->opts.local_redir = hctx->conf.local_redir;

5
src/mod_fastcgi.c

@ -2151,8 +2151,8 @@ static int fcgi_demux_response(server *srv, handler_ctx *hctx) {
/*
* check how much we have to read
*/
#if !defined(_WIN32) && !defined(__CYGWIN__)
if (ioctl(hctx->fd, FIONREAD, &toread)) {
#ifndef __CYGWIN__ /*(cygwin does not support FIONREAD on sockets)*/
if (0 != fdevent_ioctl_fionread(hctx->fd, S_IFSOCK, &toread)) {
if (errno == EAGAIN) {
return 0;
}
@ -3125,6 +3125,7 @@ static handler_t fcgi_check_extension(server *srv, connection *con, void *p_d, i
/*hctx->conf.ext_mapping = p->conf.ext_mapping;*/
hctx->conf.debug = p->conf.debug;
hctx->opts.fdfmt = S_IFSOCK;
hctx->opts.backend = BACKEND_FASTCGI;
hctx->opts.authorizer = (fcgi_mode == FCGI_AUTHORIZER);
hctx->opts.local_redir = 0;

1
src/mod_proxy.c

@ -1128,6 +1128,7 @@ static handler_t mod_proxy_check_extension(server *srv, connection *con, void *p
hctx->conf.debug = p->conf.debug;
hctx->conf.replace_http_host = p->conf.replace_http_host;
hctx->opts.fdfmt = S_IFSOCK;
hctx->opts.backend = BACKEND_PROXY;
hctx->opts.authorizer = 0;
hctx->opts.local_redir = 0;

1
src/mod_scgi.c

@ -2466,6 +2466,7 @@ static handler_t scgi_check_extension(server *srv, connection *con, void *p_d, i
hctx->conf.proto = p->conf.proto;
hctx->conf.debug = p->conf.debug;
hctx->opts.fdfmt = S_IFSOCK;
hctx->opts.backend = BACKEND_SCGI;
hctx->opts.authorizer = 0;
hctx->opts.local_redir = 0;

1
src/response.h

@ -29,6 +29,7 @@ enum {
};
typedef struct http_response_opts_t {
int fdfmt;
int backend;
int authorizer;
unsigned short local_redir;

6
src/sys-socket.h

@ -10,10 +10,8 @@
#define EINPROGRESS WSAEINPROGRESS
#define EALREADY WSAEALREADY
#define ECONNABORTED WSAECONNABORTED
#define ioctl ioctlsocket
#else
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/un.h>
@ -21,10 +19,6 @@
#include <netdb.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h> /* FIONREAD (for illumos (OpenIndiana)) */
#endif
#endif
#endif

Loading…
Cancel
Save