2 * copyright (c) 2001-2008 Emil Mikulic.
4 * http.c: embedded webserver.
5 * This borrows a lot of code from darkhttpd.
7 * You may use, modify and redistribute this file under the terms of the
8 * GNU General Public License version 2. (see COPYING.GPL)
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
37 static const char mime_type_xml
[] = "text/xml";
38 static const char mime_type_html
[] = "text/html; charset=us-ascii";
39 static const char mime_type_css
[] = "text/css";
40 static const char mime_type_js
[] = "text/javascript";
41 static const char encoding_gzip
[] =
42 "Vary: Accept-Encoding\r\n"
43 "Content-Encoding: gzip\r\n";
45 static const char server
[] = PACKAGE_NAME
"/" PACKAGE_VERSION
;
46 static int idletime
= 60;
47 static int sockin
= -1; /* socket to accept connections from */
48 #define MAX_REQUEST_LENGTH 4000
51 #define min(a,b) (((a) < (b)) ? (a) : (b))
55 LIST_ENTRY(connection
) entries
;
58 struct sockaddr_storage client
;
61 RECV_REQUEST
, /* receiving request */
62 SEND_HEADER_AND_REPLY
, /* try to send header+reply together */
63 SEND_HEADER
, /* sending generated header */
64 SEND_REPLY
, /* sending reply */
65 DONE
/* conn closed, need to remove from queue */
68 /* char request[request_length+1] is null-terminated */
70 size_t request_length
;
74 char *method
, *uri
, *query
; /* query can be NULL */
77 const char *mime_type
, *encoding
, *header_extra
;
78 size_t header_length
, header_sent
;
79 int header_dont_free
, header_only
, http_code
;
83 size_t reply_length
, reply_sent
;
85 unsigned int total_sent
; /* header + body = total, for logging */
88 static LIST_HEAD(conn_list_head
, connection
) connlist
=
89 LIST_HEAD_INITIALIZER(conn_list_head
);
91 /* ---------------------------------------------------------------------------
92 * Decode URL by converting %XX (where XX are hexadecimal digits) to the
93 * character it represents. Don't forget to free the return value.
95 static char *urldecode(const char *url
)
97 size_t i
, len
= strlen(url
);
98 char *out
= xmalloc(len
+1);
101 for (i
=0, pos
=0; i
<len
; i
++)
103 if (url
[i
] == '%' && i
+2 < len
&&
104 isxdigit(url
[i
+1]) && isxdigit(url
[i
+2]))
107 #define HEX_TO_DIGIT(hex) ( \
108 ((hex) >= 'A' && (hex) <= 'F') ? ((hex)-'A'+10): \
109 ((hex) >= 'a' && (hex) <= 'f') ? ((hex)-'a'+10): \
112 out
[pos
++] = HEX_TO_DIGIT(url
[i
+1]) * 16 +
113 HEX_TO_DIGIT(url
[i
+2]);
126 /* don't really need to realloc here - it's probably a performance hit */
127 out
= xrealloc(out
, strlen(out
)+1); /* dealloc what we don't need */
134 /* ---------------------------------------------------------------------------
135 * Consolidate slashes in-place by shifting parts of the string over repeated
138 static void consolidate_slashes(char *s
)
140 size_t left
= 0, right
= 0;
145 while (s
[right
] != '\0')
149 if (s
[right
] == '/') right
++;
153 s
[left
++] = s
[right
++];
158 if (s
[right
] == '/') saw_slash
++;
159 s
[left
++] = s
[right
++];
167 /* ---------------------------------------------------------------------------
168 * Resolve /./ and /../ in a URI, returing a new, safe URI, or NULL if the URI
169 * is invalid/unsafe. Returned buffer needs to be deallocated.
171 static char *make_safe_uri(char *uri
)
174 unsigned int slashes
= 0, elements
= 0;
175 size_t urilen
, i
, j
, pos
;
180 consolidate_slashes(uri
);
181 urilen
= strlen(uri
);
183 /* count the slashes */
184 for (i
=0, slashes
=0; i
<urilen
; i
++)
185 if (uri
[i
] == '/') slashes
++;
187 /* make an array for the URI elements */
188 elem
= xmalloc(sizeof(*elem
) * slashes
);
189 for (i
=0; i
<slashes
; i
++)
192 /* split by slashes and build elem[] array */
195 /* look for the next slash */
196 for (j
=i
; j
<urilen
&& uri
[j
] != '/'; j
++)
199 /* process uri[i,j) */
200 if ((j
== i
+1) && (uri
[i
] == '.'))
202 else if ((j
== i
+2) && (uri
[i
] == '.') && (uri
[i
+1] == '.'))
208 * Unsafe string so free elem[]. All its elements are free
217 free(elem
[elements
]);
220 else elem
[elements
++] = split_string(uri
, i
, j
);
222 i
= j
+ 1; /* uri[j] is a slash - move along one */
226 out
= xmalloc(urilen
+1); /* it won't expand */
228 for (i
=0; i
<elements
; i
++)
230 size_t delta
= strlen(elem
[i
]);
232 assert(pos
<= urilen
);
235 assert(pos
+delta
<= urilen
);
236 memcpy(out
+pos
, elem
[i
], delta
);
242 if ((elements
== 0) || (uri
[urilen
-1] == '/')) out
[pos
++] = '/';
243 assert(pos
<= urilen
);
247 /* don't really need to do this and it's probably a performance hit: */
248 /* shorten buffer if necessary */
249 if (pos
!= urilen
) out
= xrealloc(out
, strlen(out
)+1);
254 /* ---------------------------------------------------------------------------
255 * Allocate and initialize an empty connection.
257 static struct connection
*new_connection(void)
259 struct connection
*conn
= xmalloc(sizeof(*conn
));
262 memset(&conn
->client
, 0, sizeof(conn
->client
));
263 conn
->last_active
= now
;
264 conn
->request
= NULL
;
265 conn
->request_length
= 0;
266 conn
->accept_gzip
= 0;
271 conn
->mime_type
= NULL
;
273 conn
->header_extra
= "";
274 conn
->header_length
= 0;
275 conn
->header_sent
= 0;
276 conn
->header_dont_free
= 0;
277 conn
->header_only
= 0;
280 conn
->reply_dont_free
= 0;
281 conn
->reply_length
= 0;
282 conn
->reply_sent
= 0;
283 conn
->total_sent
= 0;
285 /* Make it harmless so it gets garbage-collected if it should, for some
286 * reason, fail to be correctly filled out.
295 /* ---------------------------------------------------------------------------
296 * Accept a connection from sockin and add it to the connection queue.
298 static void accept_connection(void)
300 struct sockaddr_storage addrin
;
302 struct connection
*conn
;
303 char ipaddr
[INET6_ADDRSTRLEN
], portstr
[12];
306 sin_size
= (socklen_t
)sizeof(addrin
);
307 sock
= accept(sockin
, &addrin
, &sin_size
);
310 if (errno
== ECONNABORTED
|| errno
== EINTR
)
312 verbosef("accept() failed: %s", strerror(errno
));
315 /* else */ err(1, "accept()");
318 fd_set_nonblock(sock
);
320 /* allocate and initialise struct connection */
321 conn
= new_connection();
323 conn
->state
= RECV_REQUEST
;
324 memcpy(&conn
->client
, &addrin
, sizeof(conn
->client
));
325 LIST_INSERT_HEAD(&connlist
, conn
, entries
);
327 getnameinfo((struct sockaddr
*) &addrin
, sin_size
,
328 ipaddr
, sizeof(ipaddr
), portstr
, sizeof(portstr
),
329 NI_NUMERICHOST
| NI_NUMERICSERV
);
330 verbosef("accepted connection from %s:%s", ipaddr
, portstr
);
335 /* ---------------------------------------------------------------------------
336 * Log a connection, then cleanly deallocate its internals.
338 static void free_connection(struct connection
*conn
)
340 dverbosef("free_connection(%d)", conn
->socket
);
341 if (conn
->socket
!= -1) close(conn
->socket
);
342 if (conn
->request
!= NULL
) free(conn
->request
);
343 if (conn
->method
!= NULL
) free(conn
->method
);
344 if (conn
->uri
!= NULL
) free(conn
->uri
);
345 if (conn
->query
!= NULL
) free(conn
->query
);
346 if (conn
->header
!= NULL
&& !conn
->header_dont_free
) free(conn
->header
);
347 if (conn
->reply
!= NULL
&& !conn
->reply_dont_free
) free(conn
->reply
);
352 /* ---------------------------------------------------------------------------
353 * Format [when] as an RFC1123 date, stored in the specified buffer. The same
354 * buffer is returned for convenience.
356 #define DATE_LEN 30 /* strlen("Fri, 28 Feb 2003 00:02:08 GMT")+1 */
357 static char *rfc1123_date(char *dest
, const time_t when
)
360 if (strftime(dest
, DATE_LEN
,
361 "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tmp
) ) == 0)
362 errx(1, "strftime() failed [%s]", dest
);
368 /* ---------------------------------------------------------------------------
369 * A default reply for any (erroneous) occasion.
371 static void default_reply(struct connection
*conn
,
372 const int errcode
, const char *errname
, const char *format
, ...)
374 char *reason
, date
[DATE_LEN
];
377 va_start(va
, format
);
378 xvasprintf(&reason
, format
, va
);
381 /* Only really need to calculate the date once. */
382 (void)rfc1123_date(date
, now
);
384 conn
->reply_length
= xasprintf(&(conn
->reply
),
385 "<html><head><title>%d %s</title></head><body>\n"
386 "<h1>%s</h1>\n" /* errname */
389 "Generated by %s on %s\n"
391 errcode
, errname
, errname
, reason
, server
, date
);
394 conn
->header_length
= xasprintf(&(conn
->header
),
398 "Content-Length: %d\r\n"
399 "Content-Type: text/html\r\n"
401 errcode
, errname
, date
, server
, conn
->reply_length
);
403 conn
->http_code
= errcode
;
408 /* ---------------------------------------------------------------------------
409 * Parses a single HTTP request field. Returns string from end of [field] to
410 * first \r, \n or end of request string. Returns NULL if [field] can't be
413 * You need to remember to deallocate the result.
414 * example: parse_field(conn, "Referer: ");
416 static char *parse_field(const struct connection
*conn
, const char *field
)
418 size_t bound1
, bound2
;
422 pos
= strstr(conn
->request
, field
);
425 bound1
= pos
- conn
->request
+ strlen(field
);
428 for (bound2
= bound1
;
429 conn
->request
[bound2
] != '\r' &&
430 bound2
< conn
->request_length
; bound2
++)
434 return (split_string(conn
->request
, bound1
, bound2
));
439 /* ---------------------------------------------------------------------------
440 * Parse an HTTP request like "GET /hosts/?sort=in HTTP/1.1" to get the method
441 * (GET), the uri (/hosts/), the query (sort=in) and whether the UA will
442 * accept gzip encoding. Remember to deallocate all these buffers. Query
443 * can be NULL. The method will be returned in uppercase.
445 static int parse_request(struct connection
*conn
)
447 size_t bound1
, bound2
, mid
;
451 for (bound1
= 0; bound1
< conn
->request_length
&&
452 conn
->request
[bound1
] != ' '; bound1
++)
455 conn
->method
= split_string(conn
->request
, 0, bound1
);
456 strntoupper(conn
->method
, bound1
);
459 for (; bound1
< conn
->request_length
&&
460 conn
->request
[bound1
] == ' '; bound1
++)
463 if (bound1
== conn
->request_length
)
464 return (0); /* fail */
466 for (bound2
=bound1
+1; bound2
< conn
->request_length
&&
467 conn
->request
[bound2
] != ' ' &&
468 conn
->request
[bound2
] != '\r'; bound2
++)
471 /* find query string */
472 for (mid
=bound1
; mid
<bound2
&& conn
->request
[mid
] != '?'; mid
++)
475 if (conn
->request
[mid
] == '?') {
476 conn
->query
= split_string(conn
->request
, mid
+1, bound2
);
480 conn
->uri
= split_string(conn
->request
, bound1
, bound2
);
482 /* parse important fields */
483 accept_enc
= parse_field(conn
, "Accept-Encoding: ");
484 if (accept_enc
!= NULL
) {
485 if (strstr(accept_enc
, "gzip") != NULL
)
486 conn
->accept_gzip
= 1;
492 /* FIXME: maybe we need a smarter way of doing static pages: */
494 /* ---------------------------------------------------------------------------
495 * Web interface: static stylesheet.
498 static_style_css(struct connection
*conn
)
500 #include "stylecss.h"
502 conn
->reply
= style_css
;
503 conn
->reply_length
= style_css_len
;
504 conn
->reply_dont_free
= 1;
505 conn
->mime_type
= mime_type_css
;
508 /* ---------------------------------------------------------------------------
509 * Web interface: static JavaScript.
512 static_graph_js(struct connection
*conn
)
516 conn
->reply
= graph_js
;
517 conn
->reply_length
= graph_js_len
;
518 conn
->reply_dont_free
= 1;
519 conn
->mime_type
= mime_type_js
;
522 /* ---------------------------------------------------------------------------
523 * gzip a reply, if requested and possible. Don't bother with a minimum
524 * length requirement, I've never seen a page fail to compress.
527 process_gzip(struct connection
*conn
)
533 if (!conn
->accept_gzip
)
536 buf
= xmalloc(conn
->reply_length
);
537 len
= conn
->reply_length
;
543 if (deflateInit2(&zs
, Z_BEST_COMPRESSION
, Z_DEFLATED
,
544 15+16, /* 15 = biggest window, 16 = add gzip header+trailer */
546 Z_DEFAULT_STRATEGY
) != Z_OK
)
549 zs
.avail_in
= conn
->reply_length
;
550 zs
.next_in
= (unsigned char *)conn
->reply
;
552 zs
.avail_out
= conn
->reply_length
;
553 zs
.next_out
= (unsigned char *)buf
;
555 if (deflate(&zs
, Z_FINISH
) != Z_STREAM_END
) {
558 verbosef("failed to compress %u bytes", (unsigned int)len
);
562 if (conn
->reply_dont_free
)
563 conn
->reply_dont_free
= 0;
568 conn
->reply_length
-= zs
.avail_out
;
569 conn
->encoding
= encoding_gzip
;
573 /* ---------------------------------------------------------------------------
574 * Process a GET/HEAD request
576 static void process_get(struct connection
*conn
)
578 char *decoded_url
, *safe_url
;
581 verbosef("http: %s \"%s\" %s", conn
->method
, conn
->uri
,
582 (conn
->query
== NULL
)?"":conn
->query
);
584 /* work out path of file being requested */
585 decoded_url
= urldecode(conn
->uri
);
587 /* make sure it's safe */
588 safe_url
= make_safe_uri(decoded_url
);
590 if (safe_url
== NULL
)
592 default_reply(conn
, 400, "Bad Request",
593 "You requested an invalid URI: %s", conn
->uri
);
597 if (strcmp(safe_url
, "/") == 0) {
598 struct str
*buf
= html_front_page();
599 str_extract(buf
, &(conn
->reply_length
), &(conn
->reply
));
600 conn
->mime_type
= mime_type_html
;
602 else if (str_starts_with(safe_url
, "/hosts/")) {
603 /* FIXME here - make this saner */
604 struct str
*buf
= html_hosts(safe_url
, conn
->query
);
606 default_reply(conn
, 404, "Not Found",
607 "The page you requested could not be found.");
610 str_extract(buf
, &(conn
->reply_length
), &(conn
->reply
));
611 conn
->mime_type
= mime_type_html
;
613 else if (str_starts_with(safe_url
, "/graphs.xml")) {
614 struct str
*buf
= xml_graphs();
615 str_extract(buf
, &(conn
->reply_length
), &(conn
->reply
));
616 conn
->mime_type
= mime_type_xml
;
617 /* hack around Opera caching the XML */
618 conn
->header_extra
= "Pragma: no-cache\r\n";
620 else if (strcmp(safe_url
, "/style.css") == 0)
621 static_style_css(conn
);
622 else if (strcmp(safe_url
, "/graph.js") == 0)
623 static_graph_js(conn
);
625 default_reply(conn
, 404, "Not Found",
626 "The page you requested could not be found.");
632 assert(conn
->mime_type
!= NULL
);
633 conn
->header_length
= xasprintf(&(conn
->header
),
634 "HTTP/1.1 200 OK\r\n"
637 "Content-Length: %d\r\n"
638 "Content-Type: %s\r\n"
643 rfc1123_date(date
, now
), server
,
644 conn
->reply_length
, conn
->mime_type
, conn
->encoding
,
646 conn
->http_code
= 200;
651 /* ---------------------------------------------------------------------------
652 * Process a request: build the header and reply, advance state.
654 static void process_request(struct connection
*conn
)
656 if (!parse_request(conn
))
658 default_reply(conn
, 400, "Bad Request",
659 "You sent a request that the server couldn't understand.");
661 else if (strcmp(conn
->method
, "GET") == 0)
665 else if (strcmp(conn
->method
, "HEAD") == 0)
668 conn
->header_only
= 1;
672 default_reply(conn
, 501, "Not Implemented",
673 "The method you specified (%s) is not implemented.",
678 if (conn
->header_only
)
679 conn
->state
= SEND_HEADER
;
681 conn
->state
= SEND_HEADER_AND_REPLY
;
683 /* request not needed anymore */
685 conn
->request
= NULL
; /* important: don't free it again later */
690 /* ---------------------------------------------------------------------------
693 static void poll_recv_request(struct connection
*conn
)
695 #define BUFSIZE 65536
699 recvd
= recv(conn
->socket
, buf
, BUFSIZE
, 0);
700 dverbosef("poll_recv_request(%d) got %d bytes", conn
->socket
, (int)recvd
);
704 verbosef("recv(%d) error: %s", conn
->socket
, strerror(errno
));
708 conn
->last_active
= now
;
711 /* append to conn->request */
712 conn
->request
= xrealloc(conn
->request
, conn
->request_length
+recvd
+1);
713 memcpy(conn
->request
+conn
->request_length
, buf
, (size_t)recvd
);
714 conn
->request_length
+= recvd
;
715 conn
->request
[conn
->request_length
] = 0;
717 /* process request if we have all of it */
718 if (conn
->request_length
> 4 &&
719 memcmp(conn
->request
+conn
->request_length
-4, "\r\n\r\n", 4) == 0)
720 process_request(conn
);
722 /* die if it's too long */
723 if (conn
->request_length
> MAX_REQUEST_LENGTH
)
725 default_reply(conn
, 413, "Request Entity Too Large",
726 "Your request was dropped because it was too long.");
727 conn
->state
= SEND_HEADER
;
733 /* ---------------------------------------------------------------------------
734 * Try to send header and [a part of the] reply in one packet.
736 static void poll_send_header_and_reply(struct connection
*conn
)
741 assert(!conn
->header_only
);
742 assert(conn
->reply_length
> 0);
743 assert(conn
->header_sent
== 0);
745 assert(conn
->reply_sent
== 0);
748 iov
[0].iov_base
= conn
->header
;
749 iov
[0].iov_len
= conn
->header_length
;
751 iov
[1].iov_base
= conn
->reply
+ conn
->reply_sent
;
752 iov
[1].iov_len
= conn
->reply_length
- conn
->reply_sent
;
754 sent
= writev(conn
->socket
, iov
, 2);
755 conn
->last_active
= now
;
756 verbosef("poll_send_header_and_reply(%d) sent %d bytes",
757 conn
->socket
, (int)sent
);
759 /* handle any errors (-1) or closure (0) in send() */
762 verbosef("writev(%d) error: %s", conn
->socket
, strerror(errno
));
767 /* Figure out what we've sent. */
768 conn
->total_sent
+= (unsigned int)sent
;
769 if (sent
< (ssize_t
)conn
->header_length
) {
770 verbosef("partially sent header");
771 conn
->header_sent
= sent
;
772 conn
->state
= SEND_HEADER
;
776 conn
->header_sent
= conn
->header_length
;
777 sent
-= conn
->header_length
;
779 if (conn
->reply_sent
+ sent
< conn
->reply_length
) {
780 verbosef("partially sent reply");
781 conn
->reply_sent
+= sent
;
782 conn
->state
= SEND_REPLY
;
786 conn
->reply_sent
= conn
->reply_length
;
790 /* ---------------------------------------------------------------------------
791 * Sending header. Assumes conn->header is not NULL.
793 static void poll_send_header(struct connection
*conn
)
797 sent
= send(conn
->socket
, conn
->header
+ conn
->header_sent
,
798 conn
->header_length
- conn
->header_sent
, 0);
799 conn
->last_active
= now
;
800 dverbosef("poll_send_header(%d) sent %d bytes", conn
->socket
, (int)sent
);
802 /* handle any errors (-1) or closure (0) in send() */
806 verbosef("send(%d) error: %s", conn
->socket
, strerror(errno
));
810 conn
->header_sent
+= (unsigned int)sent
;
811 conn
->total_sent
+= (unsigned int)sent
;
813 /* check if we're done sending */
814 if (conn
->header_sent
== conn
->header_length
)
816 if (conn
->header_only
)
819 conn
->state
= SEND_REPLY
;
825 /* ---------------------------------------------------------------------------
828 static void poll_send_reply(struct connection
*conn
)
832 sent
= send(conn
->socket
,
833 conn
->reply
+ conn
->reply_sent
,
834 conn
->reply_length
- conn
->reply_sent
, 0);
835 conn
->last_active
= now
;
836 dverbosef("poll_send_reply(%d) sent %d: [%d-%d] of %d",
837 conn
->socket
, (int)sent
,
838 (int)conn
->reply_sent
,
839 (int)(conn
->reply_sent
+ sent
- 1),
840 (int)conn
->reply_length
);
842 /* handle any errors (-1) or closure (0) in send() */
846 verbosef("send(%d) error: %s", conn
->socket
, strerror(errno
));
848 verbosef("send(%d) closure", conn
->socket
);
852 conn
->reply_sent
+= (unsigned int)sent
;
853 conn
->total_sent
+= (unsigned int)sent
;
855 /* check if we're done sending */
856 if (conn
->reply_sent
== conn
->reply_length
) conn
->state
= DONE
;
861 /* ---------------------------------------------------------------------------
862 * Initialize the sockin global. This is the socket that we accept
863 * connections from. Pass -1 as max_conn for system limit.
865 void http_init(const char *bindaddr
, const unsigned short bindport
,
868 struct sockaddr_storage addrin
;
869 struct addrinfo hints
, *ai
, *aiptr
;
870 char ipaddr
[INET6_ADDRSTRLEN
], portstr
[12];
873 memset(&hints
, 0, sizeof(hints
));
874 hints
.ai_family
= AF_UNSPEC
;
875 hints
.ai_socktype
= SOCK_STREAM
;
876 hints
.ai_flags
= AI_PASSIVE
;
878 hints
.ai_flags
|= AI_ADDRCONFIG
;
880 snprintf(portstr
, sizeof(portstr
), "%u", bindport
);
882 if ((ret
= getaddrinfo(bindaddr
, portstr
, &hints
, &aiptr
)))
883 err(1, "getaddrinfo(): %s", gai_strerror(ret
));
885 for (ai
= aiptr
; ai
; ai
= ai
->ai_next
) {
886 /* create incoming socket */
887 sockin
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
893 if (setsockopt(sockin
, SOL_SOCKET
, SO_REUSEADDR
,
894 &sockopt
, sizeof(sockopt
)) == -1) {
899 /* Recover address and port strings. */
900 getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, ipaddr
, sizeof(ipaddr
),
901 NULL
, 0, NI_NUMERICHOST
);
904 memcpy(&addrin
, ai
->ai_addr
, ai
->ai_addrlen
);
905 if (bind(sockin
, (struct sockaddr
*)&addrin
, ai
->ai_addrlen
) == -1) {
910 verbosef("listening on %s:%u", ipaddr
, bindport
);
912 /* listen on socket */
913 if (listen(sockin
, max_conn
) >= 0)
914 /* Successfully bound and now listening. */
917 /* Next candidate. */
924 err(1, "getaddrinfo() unable to locate address");
927 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
928 err(1, "can't ignore SIGPIPE");
933 /* ---------------------------------------------------------------------------
934 * Set recv/send fd_sets and calculate timeout length.
937 http_fd_set(fd_set
*recv_set
, fd_set
*send_set
, int *max_fd
,
938 struct timeval
*timeout
, int *need_timeout
)
940 struct connection
*conn
, *next
;
941 int minidle
= idletime
+ 1;
943 #define MAX_FD_SET(sock, fdset) do { \
944 FD_SET(sock, fdset); *max_fd = max(*max_fd, sock); } while(0)
946 MAX_FD_SET(sockin
, recv_set
);
948 LIST_FOREACH_SAFE(conn
, &connlist
, entries
, next
)
950 int idlefor
= now
- conn
->last_active
;
952 /* Time out dead connections. */
953 if (idlefor
>= idletime
)
955 char ipaddr
[INET6_ADDRSTRLEN
];
956 getnameinfo((struct sockaddr
*) &conn
->client
, sizeof(conn
->client
),
957 ipaddr
, sizeof(ipaddr
), NULL
, 0, NI_NUMERICHOST
);
958 verbosef("http socket timeout from %s (fd %d)",
959 ipaddr
, conn
->socket
);
963 /* Connections that need a timeout. */
964 if (conn
->state
!= DONE
)
965 minidle
= min(minidle
, (idletime
- idlefor
));
970 /* clean out stale connection */
971 LIST_REMOVE(conn
, entries
);
972 free_connection(conn
);
977 MAX_FD_SET(conn
->socket
, recv_set
);
980 case SEND_HEADER_AND_REPLY
:
983 MAX_FD_SET(conn
->socket
, send_set
);
986 default: errx(1, "invalid state");
991 /* Only set timeout if cap hasn't already. */
992 if ((*need_timeout
== 0) && (minidle
<= idletime
)) {
994 timeout
->tv_sec
= minidle
;
995 timeout
->tv_usec
= 0;
1001 /* ---------------------------------------------------------------------------
1002 * poll connections that select() says need attention
1004 void http_poll(fd_set
*recv_set
, fd_set
*send_set
)
1006 struct connection
*conn
;
1008 if (FD_ISSET(sockin
, recv_set
)) accept_connection();
1010 LIST_FOREACH(conn
, &connlist
, entries
)
1011 switch (conn
->state
)
1014 if (FD_ISSET(conn
->socket
, recv_set
)) poll_recv_request(conn
);
1017 case SEND_HEADER_AND_REPLY
:
1018 if (FD_ISSET(conn
->socket
, send_set
)) poll_send_header_and_reply(conn
);
1022 if (FD_ISSET(conn
->socket
, send_set
)) poll_send_header(conn
);
1026 if (FD_ISSET(conn
->socket
, send_set
)) poll_send_reply(conn
);
1029 case DONE
: /* fallthrough */
1030 default: errx(1, "invalid state");
1034 /* vim:set ts=4 sw=4 et tw=78: */