[mod_cgi] skip local-redir handling if to self (fixes #2779, #2108)
[lighttpd.git] / src / network_write_no_mmap.c
blob80426548b84028f9c815760a23c84a6b043d489b
1 #include "first.h"
3 #include "network_backends.h"
5 #include "network.h"
6 #include "fdevent.h"
7 #include "log.h"
8 #include "stat_cache.h"
10 #include "sys-socket.h"
12 #include <sys/time.h>
13 #include <stdlib.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
19 #include <errno.h>
20 #include <string.h>
22 int network_open_file_chunk(server *srv, connection *con, chunkqueue *cq) {
23 chunk* const c = cq->first;
24 off_t offset, toSend;
25 struct stat st;
26 UNUSED(con);
28 force_assert(NULL != c);
29 force_assert(FILE_CHUNK == c->type);
30 force_assert(c->offset >= 0 && c->offset <= c->file.length);
32 offset = c->file.start + c->offset;
33 toSend = c->file.length - c->offset;
35 if (-1 == c->file.fd) {
36 if (-1 == (c->file.fd = fdevent_open_cloexec(c->file.name->ptr, O_RDONLY, 0))) {
37 log_error_write(srv, __FILE__, __LINE__, "ssb", "open failed:", strerror(errno), c->file.name);
38 return -1;
42 /*(skip file size checks if file is temp file created by lighttpd)*/
43 if (c->file.is_temp) return 0;
45 if (-1 == fstat(c->file.fd, &st)) {
46 log_error_write(srv, __FILE__, __LINE__, "ss", "fstat failed:", strerror(errno));
47 return -1;
50 if (offset > st.st_size || toSend > st.st_size || offset > st.st_size - toSend) {
51 log_error_write(srv, __FILE__, __LINE__, "sb", "file shrunk:", c->file.name);
52 return -1;
55 return 0;
58 int network_write_file_chunk_no_mmap(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
59 chunk* const c = cq->first;
60 off_t offset, toSend;
61 ssize_t r;
63 force_assert(NULL != c);
64 force_assert(FILE_CHUNK == c->type);
65 force_assert(c->offset >= 0 && c->offset <= c->file.length);
67 offset = c->file.start + c->offset;
68 toSend = c->file.length - c->offset;
69 if (toSend > 64*1024) toSend = 64*1024; /* max read 64kb in one step */
70 if (toSend > *p_max_bytes) toSend = *p_max_bytes;
72 if (0 == toSend) {
73 chunkqueue_remove_finished_chunks(cq);
74 return 0;
77 if (0 != network_open_file_chunk(srv, con, cq)) return -1;
79 buffer_string_prepare_copy(srv->tmp_buf, toSend);
81 if (-1 == lseek(c->file.fd, offset, SEEK_SET)) {
82 log_error_write(srv, __FILE__, __LINE__, "ss", "lseek: ", strerror(errno));
83 return -1;
85 if (-1 == (toSend = read(c->file.fd, srv->tmp_buf->ptr, toSend))) {
86 log_error_write(srv, __FILE__, __LINE__, "ss", "read: ", strerror(errno));
87 return -1;
90 #if defined(__WIN32)
91 if ((r = send(fd, srv->tmp_buf->ptr, toSend, 0)) < 0) {
92 int lastError = WSAGetLastError();
93 switch (lastError) {
94 case WSAEINTR:
95 case WSAEWOULDBLOCK:
96 break;
97 case WSAECONNRESET:
98 case WSAETIMEDOUT:
99 case WSAECONNABORTED:
100 return -2;
101 default:
102 log_error_write(srv, __FILE__, __LINE__, "sdd",
103 "send failed: ", lastError, fd);
104 return -1;
107 #else /* __WIN32 */
108 if ((r = write(fd, srv->tmp_buf->ptr, toSend)) < 0) {
109 switch (errno) {
110 case EAGAIN:
111 case EINTR:
112 break;
113 case EPIPE:
114 case ECONNRESET:
115 return -2;
116 default:
117 log_error_write(srv, __FILE__, __LINE__, "ssd",
118 "write failed:", strerror(errno), fd);
119 return -1;
122 #endif /* __WIN32 */
124 if (r >= 0) {
125 *p_max_bytes -= r;
126 chunkqueue_mark_written(cq, r);
129 return (r > 0 && r == toSend) ? 0 : -3;