📄 sendrecv.c
字号:
* partial byte count; this is a non-blocking socket. */ if (sock->timeout > 0) { sock->options |= APR_INCOMPLETE_WRITE; } return arv; } else { /* If the file got smaller mid-request, eventually the offset * becomes equal to the new file size and the kernel returns 0. * Make this an error so the caller knows to log something and * exit. */ return APR_EOF; } } /* Now write the footers */ if (hdtr->numtrailers > 0) { apr_size_t trbytes; arv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &trbytes); nbytes += trbytes; if (arv != APR_SUCCESS) { *len = nbytes; rv = errno; apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); return rv; } } apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0); (*len) = nbytes; return rv < 0 ? errno : APR_SUCCESS;}#elif defined(DARWIN)/* OS/X Release 10.5 or greater */apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file, apr_hdtr_t *hdtr, apr_off_t *offset, apr_size_t *len, apr_int32_t flags){ apr_off_t nbytes = 0; apr_off_t bytes_to_send = *len; apr_off_t bytes_sent = 0; apr_status_t arv; int rv = 0; /* Ignore flags for now. */ flags = 0; if (!hdtr) { hdtr = &no_hdtr; } /* OS X can send the headers/footers as part of the system call, * but how it counts bytes isn't documented properly. We use * apr_socket_sendv() instead. */ if (hdtr->numheaders > 0) { apr_size_t hbytes; int i; /* Now write the headers */ arv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders, &hbytes); if (arv != APR_SUCCESS) { *len = 0; return errno; } bytes_sent = hbytes; hbytes = 0; for (i = 0; i < hdtr->numheaders; i++) { hbytes += hdtr->headers[i].iov_len; } if (bytes_sent < hbytes) { *len = bytes_sent; return APR_SUCCESS; } } do { if (!bytes_to_send) { break; } if (sock->options & APR_INCOMPLETE_WRITE) { apr_status_t arv; sock->options &= ~APR_INCOMPLETE_WRITE; arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } } nbytes = bytes_to_send; rv = sendfile(file->filedes, /* file to be sent */ sock->socketdes, /* socket */ *offset, /* where in the file to start */ &nbytes, /* number of bytes to write/written */ NULL, /* Headers/footers */ flags); /* undefined, set to 0 */ if (rv == -1) { if (errno == EAGAIN) { if (sock->timeout > 0) { sock->options |= APR_INCOMPLETE_WRITE; } /* BSD's sendfile can return -1/EAGAIN even if it * sent bytes. Sanitize the result so we get normal EAGAIN * semantics w.r.t. bytes sent. */ if (nbytes) { bytes_sent += nbytes; /* normal exit for a big file & non-blocking io */ (*len) = bytes_sent; return APR_SUCCESS; } } } else { /* rv == 0 (or the kernel is broken) */ bytes_sent += nbytes; if (nbytes == 0) { /* Most likely the file got smaller after the stat. * Return an error so the caller can do the Right Thing. */ (*len) = bytes_sent; return APR_EOF; } } } while (rv == -1 && (errno == EINTR || errno == EAGAIN)); /* Now write the footers */ if (hdtr->numtrailers > 0) { apr_size_t tbytes; arv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers, &tbytes); bytes_sent += tbytes; if (arv != APR_SUCCESS) { *len = bytes_sent; rv = errno; return rv; } } (*len) = bytes_sent; if (rv == -1) { return errno; } return APR_SUCCESS;}#elif defined(__FreeBSD__) || defined(__DragonFly__)/* Release 3.1 or greater */apr_status_t apr_socket_sendfile(apr_socket_t * sock, apr_file_t * file, apr_hdtr_t * hdtr, apr_off_t * offset, apr_size_t * len, apr_int32_t flags){ off_t nbytes = 0; int rv;#if defined(__FreeBSD_version) && __FreeBSD_version < 460001 int i;#endif struct sf_hdtr headerstruct; apr_size_t bytes_to_send = *len; /* Ignore flags for now. */ flags = 0; if (!hdtr) { hdtr = &no_hdtr; }#if defined(__FreeBSD_version) && __FreeBSD_version < 460001 else if (hdtr->numheaders) { /* On early versions of FreeBSD sendfile, the number of bytes to send * must include the length of the headers. Don't look at the man page * for this :( Instead, look at the the logic in * src/sys/kern/uipc_syscalls::sendfile(). * * This was fixed in the middle of 4.6-STABLE */ for (i = 0; i < hdtr->numheaders; i++) { bytes_to_send += hdtr->headers[i].iov_len; } }#endif headerstruct.headers = hdtr->headers; headerstruct.hdr_cnt = hdtr->numheaders; headerstruct.trailers = hdtr->trailers; headerstruct.trl_cnt = hdtr->numtrailers; /* FreeBSD can send the headers/footers as part of the system call */ do { if (sock->options & APR_INCOMPLETE_WRITE) { apr_status_t arv; sock->options &= ~APR_INCOMPLETE_WRITE; arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } } if (bytes_to_send) { /* We won't dare call sendfile() if we don't have * header or file bytes to send because bytes_to_send == 0 * means send the whole file. */ rv = sendfile(file->filedes, /* file to be sent */ sock->socketdes, /* socket */ *offset, /* where in the file to start */ bytes_to_send, /* number of bytes to send */ &headerstruct, /* Headers/footers */ &nbytes, /* number of bytes written */ flags); /* undefined, set to 0 */ if (rv == -1) { if (errno == EAGAIN) { if (sock->timeout > 0) { sock->options |= APR_INCOMPLETE_WRITE; } /* FreeBSD's sendfile can return -1/EAGAIN even if it * sent bytes. Sanitize the result so we get normal EAGAIN * semantics w.r.t. bytes sent. */ if (nbytes) { /* normal exit for a big file & non-blocking io */ (*len) = nbytes; return APR_SUCCESS; } } } else { /* rv == 0 (or the kernel is broken) */ if (nbytes == 0) { /* Most likely the file got smaller after the stat. * Return an error so the caller can do the Right Thing. */ (*len) = nbytes; return APR_EOF; } } } else { /* just trailer bytes... use writev() */ rv = writev(sock->socketdes, hdtr->trailers, hdtr->numtrailers); if (rv > 0) { nbytes = rv; rv = 0; } else { nbytes = 0; } } if ((rv == -1) && (errno == EAGAIN) && (sock->timeout > 0)) { apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 0); if (arv != APR_SUCCESS) { *len = 0; return arv; } } } while (rv == -1 && (errno == EINTR || errno == EAGAIN)); (*len) = nbytes; if (rv == -1) { return errno; } return APR_SUCCESS;}#elif defined(__hpux) || defined(__hpux__)/* HP cc in ANSI mode defines __hpux; gcc defines __hpux__ *//* HP-UX Version 10.30 or greater * (no worries, because we only get here if autoconfiguration found sendfile) *//* ssize_t sendfile(int s, int fd, off_t offset, size_t nbytes, * const struct iovec *hdtrl, int flags); * * nbytes is the number of bytes to send just from the file; as with FreeBSD, * if nbytes == 0, the rest of the file (from offset) is sent */apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file, apr_hdtr_t *hdtr, apr_off_t *offset, apr_size_t *len, apr_int32_t flags){ int i; apr_ssize_t rc; apr_size_t nbytes = *len, headerlen, trailerlen; struct iovec hdtrarray[2]; char *headerbuf, *trailerbuf;#if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILE64) /* later HP-UXes have a sendfile64() */#define sendfile sendfile64 apr_off_t off = *offset;#elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4 /* HP-UX 11.00 doesn't have a sendfile64(): fail if trying to send * past the 2Gb limit */ off_t off; if ((apr_int64_t)*offset + *len > INT_MAX) { return EINVAL; } off = *offset;#else apr_off_t off = *offset;#endif if (!hdtr) { hdtr = &no_hdtr; } /* Ignore flags for now. */ flags = 0; /* HP-UX can only send one header iovec and one footer iovec; try to * only allocate storage to combine input iovecs when we really have to */ switch(hdtr->numheaders) { case 0: hdtrarray[0].iov_base = NULL; hdtrarray[0].iov_len = 0; break; case 1: hdtrarray[0] = hdtr->headers[0]; break; default: headerlen = 0; for (i = 0; i < hdtr->numheaders; i++) { headerlen += hdtr->headers[i].iov_len; } /* XXX: BUHHH? wow, what a memory leak! */ headerbuf = hdtrarray[0].iov_base = apr_palloc(sock->pool, headerlen); hdtrarray[0].iov_len = headerlen; for (i = 0; i < hdtr->numheaders; i++) { memcpy(headerbuf, hdtr->headers[i].iov_base, hdtr->headers[i].iov_len); headerbuf += hdtr->headers[i].iov_len; } } switch(hdtr->numtrailers) { case 0: hdtrarray[1].iov_base = NULL; hdtrarray[1].iov_len = 0; break; case 1: hdtrarray[1] = hdtr->trailers[0]; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -