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)
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
39 static const char mime_type_xml
[] = "text/xml";
40 static const char mime_type_html
[] = "text/html; charset=us-ascii";
41 static const char mime_type_css
[] = "text/css";
42 static const char mime_type_js
[] = "text/javascript";
43 static const char encoding_identity
[] = "identity";
44 static const char encoding_gzip
[] = "gzip";
46 static const char server
[] = PACKAGE_NAME
"/" PACKAGE_VERSION
;
47 static int idletime
= 60;
48 static int sockin
= -1; /* socket to accept connections from */
49 #define MAX_REQUEST_LENGTH 4000
52 #define min(a,b) (((a) < (b)) ? (a) : (b))
56 LIST_ENTRY(connection
) entries
;
59 struct sockaddr_storage client
;
62 RECV_REQUEST
, /* receiving request */
63 SEND_HEADER_AND_REPLY
, /* try to send header+reply together */
64 SEND_HEADER
, /* sending generated header */
65 SEND_REPLY
, /* sending reply */
66 DONE
/* conn closed, need to remove from queue */
69 /* char request[request_length+1] is null-terminated */
71 size_t request_length
;
75 char *method
, *uri
, *query
; /* query can be NULL */
78 const char *mime_type
, *encoding
, *header_extra
;
79 size_t header_length
, header_sent
;
80 int header_dont_free
, header_only
, http_code
;
84 size_t reply_length
, reply_sent
;
86 unsigned int total_sent
; /* header + body = total, for logging */
89 static LIST_HEAD(conn_list_head
, connection
) connlist
=
90 LIST_HEAD_INITIALIZER(conn_list_head
);
92 /* ---------------------------------------------------------------------------
93 * Decode URL by converting %XX (where XX are hexadecimal digits) to the
94 * character it represents. Don't forget to free the return value.
96 static char *urldecode(const char *url
)
98 size_t i
, len
= strlen(url
);
99 char *out
= xmalloc(len
+1);
102 for (i
=0, pos
=0; i
<len
; i
++)
104 if (url
[i
] == '%' && i
+2 < len
&&
105 isxdigit(url
[i
+1]) && isxdigit(url
[i
+2]))
108 #define HEX_TO_DIGIT(hex) ( \
109 ((hex) >= 'A' && (hex) <= 'F') ? ((hex)-'A'+10): \
110 ((hex) >= 'a' && (hex) <= 'f') ? ((hex)-'a'+10): \
113 out
[pos
++] = HEX_TO_DIGIT(url
[i
+1]) * 16 +
114 HEX_TO_DIGIT(url
[i
+2]);
127 /* don't really need to realloc here - it's probably a performance hit */
128 out
= xrealloc(out
, strlen(out
)+1); /* dealloc what we don't need */
135 /* ---------------------------------------------------------------------------
136 * Consolidate slashes in-place by shifting parts of the string over repeated
139 static void consolidate_slashes(char *s
)
141 size_t left
= 0, right
= 0;
146 while (s
[right
] != '\0')
150 if (s
[right
] == '/') right
++;
154 s
[left
++] = s
[right
++];
159 if (s
[right
] == '/') saw_slash
++;
160 s
[left
++] = s
[right
++];
168 /* ---------------------------------------------------------------------------
169 * Resolve /./ and /../ in a URI, returing a new, safe URI, or NULL if the URI
170 * is invalid/unsafe. Returned buffer needs to be deallocated.
172 static char *make_safe_uri(char *uri
)
175 unsigned int slashes
= 0, elements
= 0;
176 size_t urilen
, i
, j
, pos
;
181 consolidate_slashes(uri
);
182 urilen
= strlen(uri
);
184 /* count the slashes */
185 for (i
=0, slashes
=0; i
<urilen
; i
++)
186 if (uri
[i
] == '/') slashes
++;
188 /* make an array for the URI elements */
189 elem
= xmalloc(sizeof(*elem
) * slashes
);
190 for (i
=0; i
<slashes
; i
++)
193 /* split by slashes and build elem[] array */
196 /* look for the next slash */
197 for (j
=i
; j
<urilen
&& uri
[j
] != '/'; j
++)
200 /* process uri[i,j) */
201 if ((j
== i
+1) && (uri
[i
] == '.'))
203 else if ((j
== i
+2) && (uri
[i
] == '.') && (uri
[i
+1] == '.'))
209 * Unsafe string so free elem[]. All its elements are free
218 free(elem
[elements
]);
221 else elem
[elements
++] = split_string(uri
, i
, j
);
223 i
= j
+ 1; /* uri[j] is a slash - move along one */
227 out
= xmalloc(urilen
+1); /* it won't expand */
229 for (i
=0; i
<elements
; i
++)
231 size_t delta
= strlen(elem
[i
]);
233 assert(pos
<= urilen
);
236 assert(pos
+delta
<= urilen
);
237 memcpy(out
+pos
, elem
[i
], delta
);
243 if ((elements
== 0) || (uri
[urilen
-1] == '/')) out
[pos
++] = '/';
244 assert(pos
<= urilen
);
248 /* don't really need to do this and it's probably a performance hit: */
249 /* shorten buffer if necessary */
250 if (pos
!= urilen
) out
= xrealloc(out
, strlen(out
)+1);
255 /* ---------------------------------------------------------------------------
256 * Allocate and initialize an empty connection.
258 static struct connection
*new_connection(void)
260 struct connection
*conn
= xmalloc(sizeof(*conn
));
263 memset(&conn
->client
, 0, sizeof(conn
->client
));
264 conn
->last_active
= now
;
265 conn
->request
= NULL
;
266 conn
->request_length
= 0;
267 conn
->accept_gzip
= 0;
272 conn
->mime_type
= NULL
;
273 conn
->encoding
= NULL
;
274 conn
->header_extra
= "";
275 conn
->header_length
= 0;
276 conn
->header_sent
= 0;
277 conn
->header_dont_free
= 0;
278 conn
->header_only
= 0;
281 conn
->reply_dont_free
= 0;
282 conn
->reply_length
= 0;
283 conn
->reply_sent
= 0;
284 conn
->total_sent
= 0;
286 /* Make it harmless so it gets garbage-collected if it should, for some
287 * reason, fail to be correctly filled out.
296 /* ---------------------------------------------------------------------------
297 * Accept a connection from sockin and add it to the connection queue.
299 static void accept_connection(void)
301 struct sockaddr_storage addrin
;
303 struct connection
*conn
;
304 char ipaddr
[INET6_ADDRSTRLEN
], portstr
[12];
307 sin_size
= (socklen_t
)sizeof(addrin
);
308 sock
= accept(sockin
, (struct sockaddr
*)&addrin
, &sin_size
);
311 if (errno
== ECONNABORTED
|| errno
== EINTR
)
313 verbosef("accept() failed: %s", strerror(errno
));
316 /* else */ err(1, "accept()");
319 fd_set_nonblock(sock
);
321 /* allocate and initialise struct connection */
322 conn
= new_connection();
324 conn
->state
= RECV_REQUEST
;
325 memcpy(&conn
->client
, &addrin
, sizeof(conn
->client
));
326 LIST_INSERT_HEAD(&connlist
, conn
, entries
);
328 getnameinfo((struct sockaddr
*) &addrin
, sin_size
,
329 ipaddr
, sizeof(ipaddr
), portstr
, sizeof(portstr
),
330 NI_NUMERICHOST
| NI_NUMERICSERV
);
331 verbosef("accepted connection from %s:%s", ipaddr
, portstr
);
336 /* ---------------------------------------------------------------------------
337 * Log a connection, then cleanly deallocate its internals.
339 static void free_connection(struct connection
*conn
)
341 dverbosef("free_connection(%d)", conn
->socket
);
342 if (conn
->socket
!= -1) close(conn
->socket
);
343 if (conn
->request
!= NULL
) free(conn
->request
);
344 if (conn
->method
!= NULL
) free(conn
->method
);
345 if (conn
->uri
!= NULL
) free(conn
->uri
);
346 if (conn
->query
!= NULL
) free(conn
->query
);
347 if (conn
->header
!= NULL
&& !conn
->header_dont_free
) free(conn
->header
);
348 if (conn
->reply
!= NULL
&& !conn
->reply_dont_free
) free(conn
->reply
);
353 /* ---------------------------------------------------------------------------
354 * Format [when] as an RFC1123 date, stored in the specified buffer. The same
355 * buffer is returned for convenience.
357 #define DATE_LEN 30 /* strlen("Fri, 28 Feb 2003 00:02:08 GMT")+1 */
358 static char *rfc1123_date(char *dest
, const time_t when
)
361 if (strftime(dest
, DATE_LEN
,
362 "%a, %d %b %Y %H:%M:%S %Z", gmtime(&tmp
) ) == 0)
363 errx(1, "strftime() failed [%s]", dest
);
367 static void generate_header(struct connection
*conn
,
368 const int code
, const char *text
)
372 assert(conn
->header
== NULL
);
373 assert(conn
->mime_type
!= NULL
);
374 if (conn
->encoding
== NULL
)
375 conn
->encoding
= encoding_identity
;
377 verbosef("http: %d %s (%s: %d bytes)", code
, text
,
378 conn
->encoding
, conn
->reply_length
);
379 conn
->header_length
= xasprintf(&(conn
->header
),
383 "Vary: Accept-Encoding\r\n"
384 "Content-Type: %s\r\n"
385 "Content-Length: %d\r\n"
386 "Content-Encoding: %s\r\n"
387 "X-Robots-Tag: noindex, noarchive\r\n"
392 rfc1123_date(date
, now
), server
,
393 conn
->mime_type
, conn
->reply_length
, conn
->encoding
,
395 conn
->http_code
= code
;
400 /* ---------------------------------------------------------------------------
401 * A default reply for any (erroneous) occasion.
403 static void default_reply(struct connection
*conn
,
404 const int errcode
, const char *errname
, const char *format
, ...)
409 va_start(va
, format
);
410 xvasprintf(&reason
, format
, va
);
413 conn
->reply_length
= xasprintf(&(conn
->reply
),
414 "<html><head><title>%d %s</title></head><body>\n"
415 "<h1>%s</h1>\n" /* errname */
420 errcode
, errname
, errname
, reason
, server
);
423 /* forget any dangling metadata */
424 conn
->mime_type
= mime_type_html
;
425 conn
->encoding
= encoding_identity
;
427 generate_header(conn
, errcode
, errname
);
432 /* ---------------------------------------------------------------------------
433 * Parses a single HTTP request field. Returns string from end of [field] to
434 * first \r, \n or end of request string. Returns NULL if [field] can't be
437 * You need to remember to deallocate the result.
438 * example: parse_field(conn, "Referer: ");
440 static char *parse_field(const struct connection
*conn
, const char *field
)
442 size_t bound1
, bound2
;
446 pos
= strstr(conn
->request
, field
);
449 bound1
= pos
- conn
->request
+ strlen(field
);
452 for (bound2
= bound1
;
453 conn
->request
[bound2
] != '\r' &&
454 bound2
< conn
->request_length
; bound2
++)
458 return (split_string(conn
->request
, bound1
, bound2
));
463 /* ---------------------------------------------------------------------------
464 * Parse an HTTP request like "GET /hosts/?sort=in HTTP/1.1" to get the method
465 * (GET), the uri (/hosts/), the query (sort=in) and whether the UA will
466 * accept gzip encoding. Remember to deallocate all these buffers. Query
467 * can be NULL. The method will be returned in uppercase.
469 static int parse_request(struct connection
*conn
)
471 size_t bound1
, bound2
, mid
;
475 for (bound1
= 0; bound1
< conn
->request_length
&&
476 conn
->request
[bound1
] != ' '; bound1
++)
479 conn
->method
= split_string(conn
->request
, 0, bound1
);
480 strntoupper(conn
->method
, bound1
);
483 for (; bound1
< conn
->request_length
&&
484 conn
->request
[bound1
] == ' '; bound1
++)
487 if (bound1
== conn
->request_length
)
488 return (0); /* fail */
490 for (bound2
=bound1
+1; bound2
< conn
->request_length
&&
491 conn
->request
[bound2
] != ' ' &&
492 conn
->request
[bound2
] != '\r'; bound2
++)
495 /* find query string */
496 for (mid
=bound1
; mid
<bound2
&& conn
->request
[mid
] != '?'; mid
++)
499 if (conn
->request
[mid
] == '?') {
500 conn
->query
= split_string(conn
->request
, mid
+1, bound2
);
504 conn
->uri
= split_string(conn
->request
, bound1
, bound2
);
506 /* parse important fields */
507 accept_enc
= parse_field(conn
, "Accept-Encoding: ");
508 if (accept_enc
!= NULL
) {
509 if (strstr(accept_enc
, "gzip") != NULL
)
510 conn
->accept_gzip
= 1;
516 /* FIXME: maybe we need a smarter way of doing static pages: */
518 /* ---------------------------------------------------------------------------
519 * Web interface: static stylesheet.
522 static_style_css(struct connection
*conn
)
524 #include "stylecss.h"
526 conn
->reply
= style_css
;
527 conn
->reply_length
= style_css_len
;
528 conn
->reply_dont_free
= 1;
529 conn
->mime_type
= mime_type_css
;
532 /* ---------------------------------------------------------------------------
533 * Web interface: static JavaScript.
536 static_graph_js(struct connection
*conn
)
540 conn
->reply
= graph_js
;
541 conn
->reply_length
= graph_js_len
;
542 conn
->reply_dont_free
= 1;
543 conn
->mime_type
= mime_type_js
;
546 /* ---------------------------------------------------------------------------
547 * gzip a reply, if requested and possible. Don't bother with a minimum
548 * length requirement, I've never seen a page fail to compress.
551 process_gzip(struct connection
*conn
)
557 if (!conn
->accept_gzip
)
560 buf
= xmalloc(conn
->reply_length
);
561 len
= conn
->reply_length
;
567 if (deflateInit2(&zs
, Z_BEST_COMPRESSION
, Z_DEFLATED
,
568 15+16, /* 15 = biggest window, 16 = add gzip header+trailer */
570 Z_DEFAULT_STRATEGY
) != Z_OK
)
573 zs
.avail_in
= conn
->reply_length
;
574 zs
.next_in
= (unsigned char *)conn
->reply
;
576 zs
.avail_out
= conn
->reply_length
;
577 zs
.next_out
= (unsigned char *)buf
;
579 if (deflate(&zs
, Z_FINISH
) != Z_STREAM_END
) {
582 verbosef("failed to compress %u bytes", (unsigned int)len
);
586 if (conn
->reply_dont_free
)
587 conn
->reply_dont_free
= 0;
592 conn
->reply_length
-= zs
.avail_out
;
593 conn
->encoding
= encoding_gzip
;
597 /* ---------------------------------------------------------------------------
598 * Process a GET/HEAD request
600 static void process_get(struct connection
*conn
)
602 char *decoded_url
, *safe_url
;
604 verbosef("http: %s \"%s\" %s", conn
->method
, conn
->uri
,
605 (conn
->query
== NULL
)?"":conn
->query
);
607 /* work out path of file being requested */
608 decoded_url
= urldecode(conn
->uri
);
610 /* make sure it's safe */
611 safe_url
= make_safe_uri(decoded_url
);
613 if (safe_url
== NULL
)
615 default_reply(conn
, 400, "Bad Request",
616 "You requested an invalid URI: %s", conn
->uri
);
620 if (strcmp(safe_url
, "/") == 0) {
621 struct str
*buf
= html_front_page();
622 str_extract(buf
, &(conn
->reply_length
), &(conn
->reply
));
623 conn
->mime_type
= mime_type_html
;
625 else if (str_starts_with(safe_url
, "/hosts/")) {
626 /* FIXME here - make this saner */
627 struct str
*buf
= html_hosts(safe_url
, conn
->query
);
629 default_reply(conn
, 404, "Not Found",
630 "The page you requested could not be found.");
634 str_extract(buf
, &(conn
->reply_length
), &(conn
->reply
));
635 conn
->mime_type
= mime_type_html
;
637 else if (str_starts_with(safe_url
, "/graphs.xml")) {
638 struct str
*buf
= xml_graphs();
639 str_extract(buf
, &(conn
->reply_length
), &(conn
->reply
));
640 conn
->mime_type
= mime_type_xml
;
641 /* hack around Opera caching the XML */
642 conn
->header_extra
= "Pragma: no-cache\r\n";
644 else if (strcmp(safe_url
, "/style.css") == 0)
645 static_style_css(conn
);
646 else if (strcmp(safe_url
, "/graph.js") == 0)
647 static_graph_js(conn
);
649 default_reply(conn
, 404, "Not Found",
650 "The page you requested could not be found.");
657 assert(conn
->mime_type
!= NULL
);
658 generate_header(conn
, 200, "OK");
663 /* ---------------------------------------------------------------------------
664 * Process a request: build the header and reply, advance state.
666 static void process_request(struct connection
*conn
)
668 if (!parse_request(conn
))
670 default_reply(conn
, 400, "Bad Request",
671 "You sent a request that the server couldn't understand.");
673 else if (strcmp(conn
->method
, "GET") == 0)
677 else if (strcmp(conn
->method
, "HEAD") == 0)
680 conn
->header_only
= 1;
684 default_reply(conn
, 501, "Not Implemented",
685 "The method you specified (%s) is not implemented.",
690 if (conn
->header_only
)
691 conn
->state
= SEND_HEADER
;
693 conn
->state
= SEND_HEADER_AND_REPLY
;
695 /* request not needed anymore */
697 conn
->request
= NULL
; /* important: don't free it again later */
702 /* ---------------------------------------------------------------------------
705 static void poll_recv_request(struct connection
*conn
)
707 #define BUFSIZE 65536
711 recvd
= recv(conn
->socket
, buf
, BUFSIZE
, 0);
712 dverbosef("poll_recv_request(%d) got %d bytes", conn
->socket
, (int)recvd
);
716 verbosef("recv(%d) error: %s", conn
->socket
, strerror(errno
));
720 conn
->last_active
= now
;
723 /* append to conn->request */
724 conn
->request
= xrealloc(conn
->request
, conn
->request_length
+recvd
+1);
725 memcpy(conn
->request
+conn
->request_length
, buf
, (size_t)recvd
);
726 conn
->request_length
+= recvd
;
727 conn
->request
[conn
->request_length
] = 0;
729 /* process request if we have all of it */
730 if (conn
->request_length
> 4 &&
731 memcmp(conn
->request
+conn
->request_length
-4, "\r\n\r\n", 4) == 0)
732 process_request(conn
);
734 /* die if it's too long */
735 if (conn
->request_length
> MAX_REQUEST_LENGTH
)
737 default_reply(conn
, 413, "Request Entity Too Large",
738 "Your request was dropped because it was too long.");
739 conn
->state
= SEND_HEADER
;
745 /* ---------------------------------------------------------------------------
746 * Try to send header and [a part of the] reply in one packet.
748 static void poll_send_header_and_reply(struct connection
*conn
)
753 assert(!conn
->header_only
);
754 assert(conn
->reply_length
> 0);
755 assert(conn
->header_sent
== 0);
757 assert(conn
->reply_sent
== 0);
760 iov
[0].iov_base
= conn
->header
;
761 iov
[0].iov_len
= conn
->header_length
;
763 iov
[1].iov_base
= conn
->reply
;
764 iov
[1].iov_len
= conn
->reply_length
;
766 sent
= writev(conn
->socket
, iov
, 2);
767 conn
->last_active
= now
;
769 /* handle any errors (-1) or closure (0) in send() */
772 verbosef("writev(%d) error: %s", conn
->socket
, strerror(errno
));
777 /* Figure out what we've sent. */
778 conn
->total_sent
+= (unsigned int)sent
;
779 if (sent
< (ssize_t
)conn
->header_length
) {
780 verbosef("partially sent header");
781 conn
->header_sent
= sent
;
782 conn
->state
= SEND_HEADER
;
786 conn
->header_sent
= conn
->header_length
;
787 sent
-= conn
->header_length
;
789 if (sent
< (ssize_t
)conn
->reply_length
) {
790 verbosef("partially sent reply");
791 conn
->reply_sent
+= sent
;
792 conn
->state
= SEND_REPLY
;
796 conn
->reply_sent
= conn
->reply_length
;
800 /* ---------------------------------------------------------------------------
801 * Sending header. Assumes conn->header is not NULL.
803 static void poll_send_header(struct connection
*conn
)
807 sent
= send(conn
->socket
, conn
->header
+ conn
->header_sent
,
808 conn
->header_length
- conn
->header_sent
, 0);
809 conn
->last_active
= now
;
810 dverbosef("poll_send_header(%d) sent %d bytes", conn
->socket
, (int)sent
);
812 /* handle any errors (-1) or closure (0) in send() */
816 verbosef("send(%d) error: %s", conn
->socket
, strerror(errno
));
820 conn
->header_sent
+= (unsigned int)sent
;
821 conn
->total_sent
+= (unsigned int)sent
;
823 /* check if we're done sending */
824 if (conn
->header_sent
== conn
->header_length
)
826 if (conn
->header_only
)
829 conn
->state
= SEND_REPLY
;
835 /* ---------------------------------------------------------------------------
838 static void poll_send_reply(struct connection
*conn
)
842 sent
= send(conn
->socket
,
843 conn
->reply
+ conn
->reply_sent
,
844 conn
->reply_length
- conn
->reply_sent
, 0);
845 conn
->last_active
= now
;
846 dverbosef("poll_send_reply(%d) sent %d: [%d-%d] of %d",
847 conn
->socket
, (int)sent
,
848 (int)conn
->reply_sent
,
849 (int)(conn
->reply_sent
+ sent
- 1),
850 (int)conn
->reply_length
);
852 /* handle any errors (-1) or closure (0) in send() */
856 verbosef("send(%d) error: %s", conn
->socket
, strerror(errno
));
858 verbosef("send(%d) closure", conn
->socket
);
862 conn
->reply_sent
+= (unsigned int)sent
;
863 conn
->total_sent
+= (unsigned int)sent
;
865 /* check if we're done sending */
866 if (conn
->reply_sent
== conn
->reply_length
) conn
->state
= DONE
;
869 /* Use getaddrinfo to figure out what type of socket to create and
870 * what to bind it to. "bindaddr" can be NULL. Remember to freeaddrinfo()
873 static struct addrinfo
*get_bind_addr(
874 const char *bindaddr
, const unsigned short bindport
)
876 struct addrinfo hints
, *ai
;
880 memset(&hints
, 0, sizeof(hints
));
881 hints
.ai_family
= AF_UNSPEC
;
882 if (bindaddr
== NULL
)
883 hints
.ai_family
= AF_INET6
; /* dual stack socket */
884 hints
.ai_socktype
= SOCK_STREAM
;
885 hints
.ai_flags
= AI_PASSIVE
;
887 hints
.ai_flags
|= AI_ADDRCONFIG
;
889 snprintf(portstr
, sizeof(portstr
), "%u", bindport
);
890 if ((ret
= getaddrinfo(bindaddr
, portstr
, &hints
, &ai
)))
891 err(1, "getaddrinfo(%s,%s) failed: %s",
892 bindaddr
? bindaddr
: "NULL", portstr
, gai_strerror(ret
));
894 err(1, "getaddrinfo() returned NULL pointer");
895 if (ai
->ai_next
!= NULL
)
896 warnx("getaddrinfo() returned multiple addresses");
900 /* --------------------------------------------------------------------------
901 * Initialize the sockin global. This is the socket that we accept
902 * connections from. Pass -1 as max_conn for system limit.
904 void http_init(const char *bindaddr
, const unsigned short bindport
,
908 char ipaddr
[INET6_ADDRSTRLEN
];
911 ai
= get_bind_addr(bindaddr
, bindport
);
913 /* create incoming socket */
914 if ((sockin
= socket(ai
->ai_family
, SOCK_STREAM
, 0)) == -1)
915 err(1, "socket() failed");
919 if (setsockopt(sockin
, SOL_SOCKET
, SO_REUSEADDR
,
920 &sockopt
, sizeof(sockopt
)) == -1)
921 err(1, "can't set SO_REUSEADDR");
923 /* dual stack socket */
924 if (ai
->ai_family
== AF_INET6
) {
926 if (setsockopt(sockin
, IPPROTO_IPV6
, IPV6_V6ONLY
,
927 &sockopt
, sizeof(sockopt
)) == -1)
928 err(1, "can't unset IPV6_V6ONLY");
931 /* format address into ipaddr string */
932 if ((ret
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, ipaddr
,
933 sizeof(ipaddr
), NULL
, 0, NI_NUMERICHOST
)) != 0)
934 err(1, "getnameinfo failed: %s", gai_strerror(ret
));
937 if (bind(sockin
, ai
->ai_addr
, ai
->ai_addrlen
) == -1)
938 err(1, "bind(\"%s\") failed", ipaddr
);
940 verbosef("listening on http://%s%s%s:%u/",
941 (ai
->ai_family
== AF_INET6
) ? "[" : "",
943 (ai
->ai_family
== AF_INET6
) ? "]" : "",
948 /* listen on socket */
949 if (listen(sockin
, max_conn
) == -1)
950 err(1, "listen() failed");
953 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
954 err(1, "can't ignore SIGPIPE");
959 /* ---------------------------------------------------------------------------
960 * Set recv/send fd_sets and calculate timeout length.
963 http_fd_set(fd_set
*recv_set
, fd_set
*send_set
, int *max_fd
,
964 struct timeval
*timeout
, int *need_timeout
)
966 struct connection
*conn
, *next
;
967 int minidle
= idletime
+ 1;
969 #define MAX_FD_SET(sock, fdset) do { \
970 FD_SET(sock, fdset); *max_fd = max(*max_fd, sock); } while(0)
972 MAX_FD_SET(sockin
, recv_set
);
974 LIST_FOREACH_SAFE(conn
, &connlist
, entries
, next
)
976 int idlefor
= now
- conn
->last_active
;
978 /* Time out dead connections. */
979 if (idlefor
>= idletime
) {
980 char ipaddr
[INET6_ADDRSTRLEN
];
981 /* FIXME: this is too late on FreeBSD, socket is invalid */
982 int ret
= getnameinfo((struct sockaddr
*)&conn
->client
,
983 sizeof(conn
->client
), ipaddr
, sizeof(ipaddr
),
984 NULL
, 0, NI_NUMERICHOST
);
986 verbosef("http socket timeout from %s (fd %d)",
987 ipaddr
, conn
->socket
);
989 warn("http socket timeout: getnameinfo error: %s",
994 /* Connections that need a timeout. */
995 if (conn
->state
!= DONE
)
996 minidle
= min(minidle
, (idletime
- idlefor
));
1001 /* clean out stale connection */
1002 LIST_REMOVE(conn
, entries
);
1003 free_connection(conn
);
1008 MAX_FD_SET(conn
->socket
, recv_set
);
1011 case SEND_HEADER_AND_REPLY
:
1014 MAX_FD_SET(conn
->socket
, send_set
);
1017 default: errx(1, "invalid state");
1022 /* Only set timeout if cap hasn't already. */
1023 if ((*need_timeout
== 0) && (minidle
<= idletime
)) {
1025 timeout
->tv_sec
= minidle
;
1026 timeout
->tv_usec
= 0;
1032 /* ---------------------------------------------------------------------------
1033 * poll connections that select() says need attention
1035 void http_poll(fd_set
*recv_set
, fd_set
*send_set
)
1037 struct connection
*conn
;
1039 if (FD_ISSET(sockin
, recv_set
)) accept_connection();
1041 LIST_FOREACH(conn
, &connlist
, entries
)
1042 switch (conn
->state
)
1045 if (FD_ISSET(conn
->socket
, recv_set
)) poll_recv_request(conn
);
1048 case SEND_HEADER_AND_REPLY
:
1049 if (FD_ISSET(conn
->socket
, send_set
)) poll_send_header_and_reply(conn
);
1053 if (FD_ISSET(conn
->socket
, send_set
)) poll_send_header(conn
);
1057 if (FD_ISSET(conn
->socket
, send_set
)) poll_send_reply(conn
);
1060 case DONE
: /* fallthrough */
1061 default: errx(1, "invalid state");
1065 void http_stop(void) {
1069 /* vim:set ts=4 sw=4 et tw=78: */