diff -ur ./libhttpd.c /usr/ports/www/thttpd/work/thttpd-2.20b/libhttpd.c --- ./libhttpd.c Wed Sep 27 11:13:24 2000 +++ /usr/ports/www/thttpd/work/thttpd-2.20b/libhttpd.c Fri Apr 20 03:50:30 2001 @@ -429,6 +429,20 @@ (void) close( listen_fd ); return -1; } +#ifdef HAVE_ACCEPT_FILTERS + { + struct accept_filter_arg a; + int error; + + bzero(&a, sizeof(a)); + strcpy(a.af_name, "httpready"); + error = setsockopt(listen_fd, SOL_SOCKET, + SO_ACCEPTFILTER, &a, sizeof(a)); + if (error == -1) + syslog( LOG_ERR, + "unable to push acceptfilter see accf_http(9) manpage"); + } +#endif /* HAVE_ACCEPT_FILTERS */ return listen_fd; } @@ -3395,6 +3409,9 @@ httpd_send_err( hc, 500, err500title, "", err500form, hc->encodedurl ); return -1; } +#ifdef USE_SENDFILE + hc->file_fd = *((int *) hc->file_address); +#endif hc->bytes = hc->sb.st_size; send_mime( hc, 200, ok200title, hc->encodings, "", hc->type, hc->sb.st_size, diff -ur ./libhttpd.h /usr/ports/www/thttpd/work/thttpd-2.20b/libhttpd.h --- ./libhttpd.h Tue Jun 13 11:48:56 2000 +++ /usr/ports/www/thttpd/work/thttpd-2.20b/libhttpd.h Fri Apr 20 03:50:30 2001 @@ -133,6 +133,9 @@ struct stat sb; int conn_fd; char* file_address; +#ifdef USE_SENDFILE + int file_fd; +#endif } httpd_conn; /* Methods. */ diff -ur ./mmc.c /usr/ports/www/thttpd/work/thttpd-2.20b/mmc.c --- ./mmc.c Wed Sep 13 15:46:11 2000 +++ /usr/ports/www/thttpd/work/thttpd-2.20b/mmc.c Fri Apr 20 03:58:59 2001 @@ -48,6 +48,9 @@ time_t mtime; int refcount; time_t reftime; +#ifdef USE_SENDFILE + int fd; +#endif void* addr; int hash; int hash_idx; @@ -115,7 +118,11 @@ { /* Yep. */ ++m->refcount; +#ifdef USE_SENDFILE + return (&m->fd); +#else return m->addr; +#endif } /* Nope. Open the file. */ @@ -157,7 +164,9 @@ m->addr = (void*) 1; /* arbitrary non-NULL address */ else { -#ifdef HAVE_MMAP +#ifdef USE_SENDFILE + m->fd = fd; +#elif defined(HAVE_MMAP) /* Map the file into memory. */ m->addr = mmap( 0, m->size, PROT_READ, MAP_SHARED, fd, 0 ); if ( m->addr == (void*) -1 ) @@ -186,8 +195,9 @@ } #endif /* HAVE_MMAP */ } +#ifndef USE_SENDFILE (void) close( fd ); - +#endif /* !USE_SENDFILE */ /* Put the Map into the hash table. */ if ( add_hash( m ) < 0 ) { @@ -201,8 +211,12 @@ maps = m; ++map_count; +#ifdef USE_SENDFILE + return (&m->fd); +#else /* And return the address. */ return m->addr; +#endif } @@ -272,7 +286,9 @@ m = *mm; if ( m->size != 0 ) { -#ifdef HAVE_MMAP +#ifdef USE_SENDFILE + close(m->fd); +#elif defined(HAVE_MMAP) if ( munmap( m->addr, m->size ) < 0 ) syslog( LOG_ERR, "munmap - %m" ); #else /* HAVE_MMAP */ diff -ur ./thttpd.c /usr/ports/www/thttpd/work/thttpd-2.20b/thttpd.c --- ./thttpd.c Wed Sep 27 12:31:48 2000 +++ /usr/ports/www/thttpd/work/thttpd-2.20b/thttpd.c Fri Apr 20 03:50:30 2001 @@ -1366,12 +1366,45 @@ if ( hc->responselen == 0 ) { /* No, just write the file. */ +#ifdef USE_SENDFILE + off_t sbytes; + + sz = sendfile( + hc->file_fd, hc->conn_fd, c->bytes_sent, + MIN( c->bytes_to_send - c->bytes_sent, c->limit ), + NULL, &sbytes, 0 ); + if (sz == -1 && errno == EAGAIN) + sz = sbytes > 0 ? sbytes : -1; + else if (sz == 0) + sz = sbytes; +#else sz = write( hc->conn_fd, &(hc->file_address[c->bytes_sent]), MIN( c->bytes_to_send - c->bytes_sent, c->limit ) ); +#endif } else { +#ifdef USE_SENDFILE + struct sf_hdtr sf; + struct iovec iv; + off_t sbytes; + + iv.iov_base = hc->response; + iv.iov_len = hc->responselen; + sf.headers = &iv; + sf.hdr_cnt = 1; + sf.trailers = NULL; + sf.trl_cnt = 0; + sz = sendfile( + hc->file_fd, hc->conn_fd, c->bytes_sent, + MIN( c->bytes_to_send - c->bytes_sent, c->limit ), + &sf, &sbytes, 0 ); + if (sz == -1 && errno == EAGAIN) + sz = sbytes > 0 ? sbytes : -1; + else if (sz == 0) + sz = sbytes; +#else /* Yes. We'll combine headers and file into a single writev(), ** hoping that this generates a single packet. */ @@ -1382,6 +1415,7 @@ iv[1].iov_base = &(hc->file_address[c->bytes_sent]); iv[1].iov_len = MIN( c->bytes_to_send - c->bytes_sent, c->limit ); sz = writev( hc->conn_fd, iv, 2 ); +#endif } if ( sz == 0 || diff -ur ./version.h /usr/ports/www/thttpd/work/thttpd-2.20b/version.h --- ./version.h Tue Oct 10 10:53:03 2000 +++ /usr/ports/www/thttpd/work/thttpd-2.20b/version.h Fri Apr 20 03:50:30 2001 @@ -3,7 +3,14 @@ #ifndef _VERSION_H_ #define _VERSION_H_ -#define SERVER_SOFTWARE "thttpd/2.20b 10oct00" +#define SERVER_SOFTWARE_BASE "thttpd/2.20b 10oct00" + +#ifdef USE_SENDFILE +#define SERVER_SOFTWARE SERVER_SOFTWARE_BASE " (+FreeBSD sendfile)" +#else /* USE_SENDFILE */ +#define SERVER_SOFTWARE SERVER_SOFTWARE_BASE +#endif + #define SERVER_ADDRESS "http://www.acme.com/software/thttpd/" #endif /* _VERSION_H_ */