2
0
Fork 0
lighttpd2/src/main/network.c

173 lines
4.0 KiB
C
Raw Normal View History

2008-08-05 15:08:32 +00:00
#include <lighttpd/base.h>
2008-11-17 18:26:41 +00:00
#include <lighttpd/plugin_core.h>
2008-08-05 15:08:32 +00:00
/** repeats write after EINTR */
2009-07-09 20:17:24 +00:00
ssize_t li_net_write(int fd, void *buf, ssize_t nbyte) {
2008-08-05 15:08:32 +00:00
ssize_t r;
while (-1 == (r = write(fd, buf, nbyte))) {
switch (errno) {
case EINTR:
/* Try again */
break;
default:
/* report error */
return r;
}
}
/* return bytes written */
return r;
}
/** repeats read after EINTR */
2009-07-09 20:17:24 +00:00
ssize_t li_net_read(int fd, void *buf, ssize_t nbyte) {
2008-08-05 15:08:32 +00:00
ssize_t r;
while (-1 == (r = read(fd, buf, nbyte))) {
switch (errno) {
case EINTR:
/* Try again */
break;
default:
/* report error */
return r;
}
}
/* return bytes read */
return r;
}
2009-07-09 20:17:24 +00:00
liNetworkStatus li_network_write(liVRequest *vr, int fd, liChunkQueue *cq, goffset write_max) {
liNetworkStatus res;
#ifdef TCP_CORK
int corked = 0;
#endif
goffset write_bytes, wrote;
#ifdef TCP_CORK
/* Linux: put a cork into the socket as we want to combine the write() calls
* but only if we really have multiple chunks
*/
if (cq->queue.length > 1) {
corked = 1;
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
}
#endif
2008-11-17 18:26:41 +00:00
write_bytes = write_max;
/* TODO: add setup-option to select the backend */
#ifdef USE_SENDFILE
2009-07-09 20:17:24 +00:00
res = li_network_write_sendfile(vr, fd, cq, &write_bytes);
#else
2009-07-09 20:17:24 +00:00
res = li_network_write_writev(vr, fd, cq, &write_bytes);
#endif
wrote = write_max - write_bytes;
#ifdef TCP_CORK
if (corked) {
corked = 0;
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
}
#endif
return res;
}
liNetworkStatus li_network_read(liVRequest *vr, int fd, liChunkQueue *cq, liBuffer **buffer) {
2008-08-05 15:08:32 +00:00
const ssize_t blocksize = 16*1024; /* 16k */
off_t max_read = 16 * blocksize; /* 256k */
2008-08-05 15:08:32 +00:00
ssize_t r;
2008-08-06 18:46:42 +00:00
off_t len = 0;
2008-08-05 15:08:32 +00:00
if (cq->limit && cq->limit->limit > 0) {
if (max_read > cq->limit->limit - cq->limit->current) {
max_read = cq->limit->limit - cq->limit->current;
if (max_read <= 0) {
max_read = 0; /* we still have to read something */
2009-07-09 20:17:24 +00:00
VR_ERROR(vr, "%s", "li_network_read: fd should be disabled as chunkqueue is already full");
}
}
}
2008-08-05 15:08:32 +00:00
do {
liBuffer *buf = NULL;
gboolean cq_buf_append;
buf = li_chunkqueue_get_last_buffer(cq, 1024);
cq_buf_append = (buf != NULL);
if (NULL != buffer) {
if (buf != NULL) {
/* use last buffer as *buffer; they should be the same anyway */
if (G_UNLIKELY(buf != *buffer)) {
li_buffer_acquire(buf);
li_buffer_release(*buffer);
*buffer = buf;
}
} else {
buf = *buffer;
if (buf != NULL) {
/* if *buffer is the only reference, we can reset the buffer */
if (g_atomic_int_get(&buf->refcount) == 1) {
buf->used = 0;
}
if (buf->alloc_size - buf->used < 1024) {
/* release *buffer */
li_buffer_release(buf);
*buffer = buf = NULL;
}
}
if (buf == NULL) {
*buffer = buf = li_buffer_new(blocksize);
}
}
assert(*buffer == buf);
} else {
if (buf == NULL) {
buf = li_buffer_new(blocksize);
}
}
if (-1 == (r = li_net_read(fd, buf->addr + buf->used, buf->alloc_size - buf->used))) {
if (buffer == NULL && !cq_buf_append) li_buffer_release(buf);
2008-08-05 15:08:32 +00:00
switch (errno) {
case EAGAIN:
#if EWOULDBLOCK != EAGAIN
2008-09-23 11:09:07 +00:00
case EWOULDBLOCK:
2008-08-05 15:08:32 +00:00
#endif
return LI_NETWORK_STATUS_WAIT_FOR_EVENT;
2008-08-05 15:08:32 +00:00
case ECONNRESET:
2010-01-30 22:52:48 +00:00
case ETIMEDOUT:
return LI_NETWORK_STATUS_CONNECTION_CLOSE;
2008-08-05 15:08:32 +00:00
default:
VR_ERROR(vr, "oops, read from fd=%d failed: %s", fd, g_strerror(errno) );
return LI_NETWORK_STATUS_FATAL_ERROR;
2008-08-05 15:08:32 +00:00
}
} else if (0 == r) {
if (buffer == NULL && !cq_buf_append) li_buffer_release(buf);
return LI_NETWORK_STATUS_CONNECTION_CLOSE;
2008-08-05 15:08:32 +00:00
}
if (cq_buf_append) {
li_chunkqueue_update_last_buffer_size(cq, r);
} else {
gsize offset;
if (buffer != NULL) li_buffer_acquire(buf);
offset = buf->used;
buf->used += r;
li_chunkqueue_append_buffer2(cq, buf, offset, r);
}
if (NULL != buffer) {
if (buf->alloc_size - buf->used < 1024) {
/* release *buffer */
li_buffer_release(buf);
*buffer = buf = NULL;
}
}
2008-08-05 15:08:32 +00:00
len += r;
} while (r == blocksize && len < max_read);
return LI_NETWORK_STATUS_SUCCESS;
2008-08-05 15:08:32 +00:00
}