2008-08-05 15:08:32 +00:00
|
|
|
|
2008-11-16 20:33:53 +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) {
|
2009-07-08 19:06:07 +00:00
|
|
|
liNetworkStatus res;
|
2008-08-09 11:51:06 +00:00
|
|
|
#ifdef TCP_CORK
|
|
|
|
int corked = 0;
|
|
|
|
#endif
|
2009-04-16 15:02:53 +00:00
|
|
|
goffset write_bytes, wrote;
|
2008-08-09 11:51:06 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
*/
|
2009-11-08 10:12:12 +00:00
|
|
|
if (cq->queue.length > 1) {
|
2008-08-09 11:51:06 +00:00
|
|
|
corked = 1;
|
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-11-17 18:26:41 +00:00
|
|
|
write_bytes = write_max;
|
2008-11-17 19:23:16 +00:00
|
|
|
/* 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);
|
2008-11-17 19:23:16 +00:00
|
|
|
#else
|
2009-07-09 20:17:24 +00:00
|
|
|
res = li_network_write_writev(vr, fd, cq, &write_bytes);
|
2008-11-17 19:23:16 +00:00
|
|
|
#endif
|
2008-11-18 10:14:57 +00:00
|
|
|
wrote = write_max - write_bytes;
|
2008-08-09 11:51:06 +00:00
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
if (corked) {
|
|
|
|
corked = 0;
|
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_CORK, &corked, sizeof(corked));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-02-10 21:25:48 +00:00
|
|
|
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 */
|
2009-03-01 23:49:02 +00:00
|
|
|
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
|
|
|
|
2009-03-01 23:49:02 +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");
|
2009-03-01 23:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-05 15:08:32 +00:00
|
|
|
do {
|
2010-02-10 21:25:48 +00:00
|
|
|
liBuffer *buf = NULL;
|
2010-01-23 19:24:21 +00:00
|
|
|
gboolean cq_buf_append;
|
|
|
|
|
|
|
|
buf = li_chunkqueue_get_last_buffer(cq, 1024);
|
2010-02-10 21:25:48 +00:00
|
|
|
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);
|
2010-01-23 19:24:21 +00:00
|
|
|
} else {
|
2010-02-10 21:25:48 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
buf = li_buffer_new(blocksize);
|
|
|
|
}
|
2010-01-23 19:24:21 +00:00
|
|
|
}
|
2010-02-10 21:25:48 +00:00
|
|
|
|
2010-01-23 19:24:21 +00:00
|
|
|
if (-1 == (r = li_net_read(fd, buf->addr + buf->used, buf->alloc_size - buf->used))) {
|
2010-02-10 21:25:48 +00:00
|
|
|
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
|
2010-08-02 14:32:20 +00:00
|
|
|
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:
|
2009-07-08 19:06:07 +00:00
|
|
|
return LI_NETWORK_STATUS_CONNECTION_CLOSE;
|
2008-08-05 15:08:32 +00:00
|
|
|
default:
|
2008-10-25 12:53:57 +00:00
|
|
|
VR_ERROR(vr, "oops, read from fd=%d failed: %s", fd, g_strerror(errno) );
|
2009-07-08 19:06:07 +00:00
|
|
|
return LI_NETWORK_STATUS_FATAL_ERROR;
|
2008-08-05 15:08:32 +00:00
|
|
|
}
|
|
|
|
} else if (0 == r) {
|
2010-02-10 21:25:48 +00:00
|
|
|
if (buffer == NULL && !cq_buf_append) li_buffer_release(buf);
|
2010-08-02 14:32:20 +00:00
|
|
|
return LI_NETWORK_STATUS_CONNECTION_CLOSE;
|
2008-08-05 15:08:32 +00:00
|
|
|
}
|
2010-01-23 19:24:21 +00:00
|
|
|
if (cq_buf_append) {
|
|
|
|
li_chunkqueue_update_last_buffer_size(cq, r);
|
|
|
|
} else {
|
2010-02-10 21:25:48 +00:00
|
|
|
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;
|
|
|
|
}
|
2010-01-23 19:24:21 +00:00
|
|
|
}
|
2008-08-05 15:08:32 +00:00
|
|
|
len += r;
|
|
|
|
} while (r == blocksize && len < max_read);
|
|
|
|
|
2009-07-08 19:06:07 +00:00
|
|
|
return LI_NETWORK_STATUS_SUCCESS;
|
2008-08-05 15:08:32 +00:00
|
|
|
}
|