2 * Copyright (c) 1987-2021 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 3.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 * Uncomment this to log all communications with the Citadel server
19 #include "webserver.h"
21 long MaxRead = -1; /* should we do READ scattered or all at once? */
24 * register the timeout
26 RETSIGTYPE timeout(int signum)
28 syslog(LOG_WARNING, "Connection timed out; unable to reach citserver\n");
29 /* no exit here, since we need to server the connection unreachable thing. exit(3); */
34 * Client side - connect to a unix domain socket
36 int uds_connectsock(char *sockpath)
38 struct sockaddr_un addr;
41 memset(&addr, 0, sizeof(addr));
42 addr.sun_family = AF_UNIX;
43 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
45 s = socket(AF_UNIX, SOCK_STREAM, 0);
47 syslog(LOG_WARNING, "Can't create socket [%s]: %s\n", sockpath, strerror(errno));
51 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
52 syslog(LOG_WARNING, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
61 * TCP client - connect to a host/port
63 int tcp_connectsock(char *host, char *service)
65 struct in6_addr serveraddr;
66 struct addrinfo hints;
67 struct addrinfo *res = NULL;
68 struct addrinfo *ai = NULL;
72 if ((host == NULL) || IsEmptyStr(host))
74 if ((service == NULL) || IsEmptyStr(service))
77 syslog(LOG_DEBUG, "tcp_connectsock(%s,%s)\n", host, service);
79 memset(&hints, 0x00, sizeof(hints));
80 hints.ai_flags = AI_NUMERICSERV;
81 hints.ai_family = AF_UNSPEC;
82 hints.ai_socktype = SOCK_STREAM;
85 * Handle numeric IPv4 and IPv6 addresses
87 rc = inet_pton(AF_INET, host, &serveraddr);
88 if (rc == 1) { /* dotted quad */
89 hints.ai_family = AF_INET;
90 hints.ai_flags |= AI_NUMERICHOST;
92 rc = inet_pton(AF_INET6, host, &serveraddr);
93 if (rc == 1) { /* IPv6 address */
94 hints.ai_family = AF_INET6;
95 hints.ai_flags |= AI_NUMERICHOST;
99 /* Begin the connection process */
101 rc = getaddrinfo(host, service, &hints, &res);
103 syslog(LOG_DEBUG, "%s: %s\n", host, gai_strerror(rc));
109 * Try all available addresses until we connect to one or until we run out.
111 for (ai = res; ai != NULL; ai = ai->ai_next) {
113 if (ai->ai_family == AF_INET) syslog(LOG_DEBUG, "Trying IPv4\n");
114 else if (ai->ai_family == AF_INET6) syslog(LOG_DEBUG, "Trying IPv6\n");
115 else syslog(LOG_WARNING, "This is going to fail.\n");
117 s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
119 syslog(LOG_WARNING, "socket() failed: %s\n", strerror(errno));
123 rc = connect(s, ai->ai_addr, ai->ai_addrlen);
128 fdflags = fcntl(rc, F_GETFL);
131 "unable to get socket %d flags! %s \n",
137 fdflags = fdflags | O_NONBLOCK;
138 if (fcntl(rc, F_SETFL, fdflags) < 0) {
140 "unable to set socket %d nonblocking flags! %s \n",
150 syslog(LOG_WARNING, "connect() failed: %s\n", strerror(errno));
160 * input string from pipe
162 int serv_getln(char *strbuf, int bufsize)
167 StrBuf_ServGetln(WC->MigrateReadLineBuf);
168 len = StrLength(WC->MigrateReadLineBuf);
171 memcpy(strbuf, ChrPtr(WC->MigrateReadLineBuf), len);
172 FlushStrBuf(WC->MigrateReadLineBuf);
175 syslog(LOG_DEBUG, "%3d<<<%s\n", WC->serv_sock, strbuf);
181 int StrBuf_ServGetln(StrBuf *buf)
183 const char *ErrStr = NULL;
190 rc = StrBufTCP_read_buffered_line_fast(buf,
198 syslog(LOG_INFO, "StrBuf_ServGetln(): Server connection broken: %s\n",
200 wc_backtrace(LOG_INFO);
201 if (WC->serv_sock > 0) close(WC->serv_sock);
202 WC->serv_sock = (-1);
210 if (WC->ReadPos != NULL)
211 pos = WC->ReadPos - ChrPtr(WC->ReadBuf);
212 syslog(LOG_DEBUG, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
218 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
223 rc = StrBufReadBLOBBuffered(buf,
233 syslog(LOG_INFO, "StrBuf_ServGetBLOBBuffered(): Server connection broken: %s\n",
235 wc_backtrace(LOG_INFO);
236 if (WC->serv_sock > 0) close(WC->serv_sock);
237 WC->serv_sock = (-1);
243 syslog(LOG_DEBUG, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
249 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
255 rc = StrBufReadBLOB(buf, &WC->serv_sock, 1, BlobSize, &ErrStr);
258 syslog(LOG_INFO, "StrBuf_ServGetBLOB(): Server connection broken: %s\n",
260 wc_backtrace(LOG_INFO);
261 if (WC->serv_sock > 0) close(WC->serv_sock);
262 WC->serv_sock = (-1);
268 syslog(LOG_DEBUG, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
275 void FlushReadBuf (void)
281 len = StrLength(WC->ReadBuf);
283 (WC->ReadPos != NULL) &&
284 (WC->ReadPos != StrBufNOTNULL))
287 pch = ChrPtr(WC->ReadBuf);
289 if (WC->ReadPos != pche)
292 "ERROR: somebody didn't eat his soup! Remaing Chars: %ld [%s]\n",
293 (long)(pche - WC->ReadPos),
297 "--------------------------------------------------------------------------------\n"
299 "--------------------------------------------------------------------------------\n",
301 AppendImportantMessage(HKEY("Suppenkasper alert! watch your webcit logfile and get connected to your favourite opensource Crew."));
305 FlushStrBuf(WC->ReadBuf);
313 * send binary to server
314 * buf the buffer to write to citadel server
315 * nbytes how many bytes to send to citadel server
317 int serv_write(const char *buf, int nbytes)
319 int bytes_written = 0;
323 while (bytes_written < nbytes) {
324 retval = write(WC->serv_sock, &buf[bytes_written],
325 nbytes - bytes_written);
327 const char *ErrStr = strerror(errno);
328 syslog(LOG_INFO, "serv_write(): Server connection broken: %s\n",
330 if (WC->serv_sock > 0) close(WC->serv_sock);
331 WC->serv_sock = (-1);
336 bytes_written = bytes_written + retval;
343 * send line to server
344 * string the line to send to the citadel server
346 int serv_puts(const char *string)
349 syslog(LOG_DEBUG, "%3d>>>%s\n", WC->serv_sock, string);
353 if (!serv_write(string, strlen(string)))
355 return serv_write("\n", 1);
359 * send line to server
360 * string the line to send to the citadel server
362 int serv_putbuf(const StrBuf *string)
365 syslog(LOG_DEBUG, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
369 if (!serv_write(ChrPtr(string), StrLength(string)))
371 return serv_write("\n", 1);
376 * convenience function to send stuff to the server
377 * format the formatstring
378 * ... the entities to insert into format
380 int serv_printf(const char *format,...)
389 va_start(arg_ptr, format);
390 vsnprintf(buf, sizeof buf, format, arg_ptr);
396 rc = serv_write(buf, len);
398 syslog(LOG_DEBUG, ">>>%s", buf);
405 * Read binary data from server into memory using a series of server READ commands.
406 * returns the read content as StrBuf
408 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
410 size_t bytes_read = 0;
411 size_t this_block = 0;
419 while ((bytes_read < total_len) && (ServerRc == 6)) {
421 if (WC->serv_sock==-1) {
426 serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, bytes_read, total_len-bytes_read);
427 if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
428 (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) )
432 StrBufCutLeft(Buf, 4);
433 this_block = StrTol(Buf);
434 rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
436 syslog(LOG_INFO, "Server connection broken during download\n");
437 wc_backtrace(LOG_INFO);
438 if (WC->serv_sock > 0) close(WC->serv_sock);
439 WC->serv_sock = (-1);
448 return StrLength(Ret);
452 int client_write(StrBuf *ThisBuf)
454 const char *ptr, *eptr;
460 ptr = ChrPtr(ThisBuf);
461 count = StrLength(ThisBuf);
464 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
466 while ((ptr < eptr) && (WC->Hdr->http_sock != -1)) {
467 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
469 FD_SET(WC->Hdr->http_sock, &wset);
470 if (select(WC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
471 syslog(LOG_INFO, "client_write: Socket select failed (%s)\n", strerror(errno));
476 if ((WC->Hdr->http_sock == -1) ||
477 ((res = write(WC->Hdr->http_sock, ptr, count)),
480 syslog(LOG_INFO, "client_write: Socket write failed (%s)\n", strerror(errno));
481 wc_backtrace(LOG_INFO);
502 serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, *bytes_read, total_len-(*bytes_read));
503 if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
504 (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) )
506 size_t this_block = 0;
511 StrBufCutLeft(Buf, 4);
512 this_block = StrTol(Buf);
513 rc = StrBuf_ServGetBLOBBuffered(WC->WBuf, this_block);
515 syslog(LOG_INFO, "Server connection broken during download\n");
516 wc_backtrace(LOG_INFO);
517 if (WC->serv_sock > 0) close(WC->serv_sock);
518 WC->serv_sock = (-1);
528 static inline int send_http(StrBuf *Buf)
532 return client_write_ssl(Buf);
535 return client_write(Buf);
538 * Read binary data from server into memory using a series of server READ commands.
539 * returns the read content as StrBuf
541 void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static, int detect_mime)
544 size_t bytes_read = 0;
546 int client_con_state = 0;
549 const char *Err = NULL;
550 StrBuf *BufHeader = NULL;
555 IOBuffer WriteBuffer;
560 if (WC->Hdr->HaveRange)
562 WC->Hdr->HaveRange++;
563 WC->Hdr->TotalBytes = total_len;
564 /* open range? or beyound file border? correct the numbers. */
565 if ((WC->Hdr->RangeTil == -1) || (WC->Hdr->RangeTil>= total_len))
566 WC->Hdr->RangeTil = total_len - 1;
567 bytes_read = WC->Hdr->RangeStart;
568 total_len = WC->Hdr->RangeTil;
571 chunked = total_len > SIZ * 10; /* TODO: disallow for HTTP / 1.0 */
575 BufHeader = NewStrBuf();
578 if ((detect_mime != 0) && (bytes_read != 0))
580 /* need to read first chunk to detect mime, though the client doesn't care */
581 size_t bytes_read = 0;
584 ServerRc = read_serv_chunk(
591 FreeStrBuf(&BufHeader);
595 CT = GuessMimeType(SKEY(WC->WBuf));
596 FlushStrBuf(WC->WBuf);
597 StrBufPlain(MimeType, CT, -1);
598 CheckGZipCompressionAllowed(SKEY(MimeType));
603 memset(&WriteBuffer, 0, sizeof(IOBuffer));
604 if (chunked && !DisableGzip && WC->Hdr->HR.gzip_ok)
607 SC = StrBufNewStreamContext (eZLibEncode, &Err);
609 syslog(LOG_ERR, "Error while initializing stream context: %s", Err);
614 memset(&ReadBuffer, 0, sizeof(IOBuffer));
615 ReadBuffer.Buf = WC->WBuf;
617 WriteBuffer.Buf = NewStrBufPlain(NULL, SIZ*2);;
618 pBuf = WriteBuffer.Buf;
627 http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
629 if (send_http(WC->HBuf) < 0)
632 FreeStrBuf(&WriteBuffer.Buf);
633 FreeStrBuf(&BufHeader);
634 if (StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
635 syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
641 while ((bytes_read < total_len) &&
643 (client_con_state == 0))
646 if (WC->serv_sock==-1) {
647 FlushStrBuf(WC->WBuf);
649 FreeStrBuf(&WriteBuffer.Buf);
650 FreeStrBuf(&BufHeader);
651 StrBufDestroyStreamContext(eZLibEncode, &SC, &Err);
652 if (StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
653 syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
658 ServerRc = read_serv_chunk(
670 CT = GuessMimeType(SKEY(WC->WBuf));
671 StrBufPlain(MimeType, CT, -1);
673 CheckGZipCompressionAllowed(SKEY(MimeType));
674 is_gzip = WC->Hdr->HR.gzip_ok;
676 http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
678 client_con_state = send_http(WC->HBuf);
683 int done = (bytes_read == total_len);
684 while ((IOBufferStrLength(&ReadBuffer) > 0) && (client_con_state == 0)) {
688 rc = StrBufStreamTranscode(eZLibEncode, &WriteBuffer, &ReadBuffer, NULL, -1, SC, done, &Err);
690 if (StrLength (pBuf) > 0) {
691 StrBufPrintf(BufHeader, "%s%x\r\n",
695 client_con_state = send_http(BufHeader);
696 if (client_con_state == 0) {
697 client_con_state = send_http(pBuf);
701 } while ((rc == 1) && (StrLength(pBuf) > 0));
703 FlushStrBuf(WC->WBuf);
706 if ((chunked) && (client_con_state == 0))
708 StrBufPrintf(BufHeader, "%s%x\r\n",
712 client_con_state = send_http(BufHeader);
715 if (client_con_state == 0)
716 client_con_state = send_http(pBuf);
722 if (SC && StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
723 syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
725 FreeStrBuf(&WriteBuffer.Buf);
726 if ((chunked) && (client_con_state == 0))
728 StrBufPlain(BufHeader, HKEY("\r\n0\r\n\r\n"));
729 if (send_http(BufHeader) < 0)
732 FreeStrBuf(&BufHeader);
736 FreeStrBuf(&BufHeader);
740 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
744 const char *pch, *pchs;
745 int rlen, len, retval = 0;
749 if (StrLength(Hdr->ReadBuf) > 0)
751 pchs = ChrPtr(Hdr->ReadBuf);
752 pch = strchr(pchs, '\n');
756 if (len > 0 && (*(pch - 1) == '\r') )
758 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
759 StrBufCutLeft(Hdr->ReadBuf, len + 1);
764 while (retval == 0) {
766 pchs = ChrPtr(Hdr->ReadBuf);
768 pch = strchr(pchs, '\n');
770 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
771 pchs = ChrPtr(Hdr->ReadBuf);
772 pch = strchr(pchs, '\n');
783 if ((retval > 0) && (pch != NULL)) {
786 if (len > 0 && (*(pch - 1) == '\r') )
788 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
789 StrBufCutLeft(Hdr->ReadBuf, len + 1);
798 return StrBufTCP_read_buffered_line_fast(Target,
809 * This is a generic function to set up a master socket for listening on
810 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
812 * ip_addr IP address to bind
813 * port_number port number to bind
814 * queue_len number of incoming connections to allow in the queue
816 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len)
818 const char *ipv4broadcast = "0.0.0.0";
821 struct sockaddr_in6 sin6;
822 struct sockaddr_in sin4;
827 memset(&sin6, 0, sizeof(sin6));
828 memset(&sin4, 0, sizeof(sin4));
829 sin6.sin6_family = AF_INET6;
830 sin4.sin_family = AF_INET;
832 if ( (ip_addr == NULL) /* any IPv6 */
833 || (IsEmptyStr(ip_addr))
834 || (!strcmp(ip_addr, "*"))
838 sin6.sin6_addr = in6addr_any;
840 else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */
843 sin4.sin_addr.s_addr = INADDR_ANY;
845 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */
848 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
849 syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
850 return (-WC_EXIT_BIND);
853 else /* specific IPv6 */
856 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
857 syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
858 return (-WC_EXIT_BIND);
862 if (port_number == 0) {
863 syslog(LOG_WARNING, "Cannot start: no port number specified.\n");
864 return (-WC_EXIT_BIND);
866 sin6.sin6_port = htons((u_short) port_number);
867 sin4.sin_port = htons((u_short) port_number);
869 p = getprotobyname("tcp");
871 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
873 if (IsDefault && (errno == EAFNOSUPPORT))
876 ip_addr = ipv4broadcast;
879 syslog(LOG_WARNING, "Can't create a listening socket: %s\n", strerror(errno));
880 return (-WC_EXIT_BIND);
882 /* Set some socket options that make sense. */
884 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
886 if (ip_version == 6) {
887 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
890 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
894 syslog(LOG_ERR, "Can't bind: %s\n", strerror(errno));
896 return (-WC_EXIT_BIND);
899 if (listen(s, queue_len) < 0) {
900 syslog(LOG_ERR, "Can't listen: %s\n", strerror(errno));
902 return (-WC_EXIT_BIND);
909 * Create a Unix domain socket and listen on it
910 * sockpath - file name of the unix domain socket
911 * queue_len - Number of incoming connections to allow in the queue
913 int webcit_uds_server(char *sockpath, int queue_len) {
914 struct sockaddr_un addr;
917 int actual_queue_len;
919 actual_queue_len = queue_len;
920 if (actual_queue_len < 5) actual_queue_len = 5;
922 i = unlink(sockpath);
923 if ((i != 0) && (errno != ENOENT)) {
924 syslog(LOG_WARNING, "webcit: can't unlink %s: %s\n",
925 sockpath, strerror(errno));
926 return (-WC_EXIT_BIND);
929 memset(&addr, 0, sizeof(addr));
930 addr.sun_family = AF_UNIX;
931 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
933 s = socket(AF_UNIX, SOCK_STREAM, 0);
935 syslog(LOG_WARNING, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
936 return (-WC_EXIT_BIND);
939 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
940 syslog(LOG_WARNING, "webcit: Can't bind: %s\n", strerror(errno));
942 return (-WC_EXIT_BIND);
945 if (listen(s, actual_queue_len) < 0) {
946 syslog(LOG_WARNING, "webcit: Can't listen: %s\n", strerror(errno));
948 return (-WC_EXIT_BIND);
951 chmod(sockpath, 0777);
957 * Read data from the client socket.
959 * sock socket fd to read from
960 * buf buffer to read into
961 * bytes number of bytes to read
962 * timeout Number of seconds to wait before timing out
964 * Possible return values:
965 * 1 Requested number of bytes has been read.
966 * 0 Request timed out.
967 * -1 Connection is broken, or other error.
969 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout) {
978 baselen = StrLength(Target);
980 if (Hdr->Pos == NULL) {
981 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
984 if (StrLength(Hdr->ReadBuf) > 0) {
985 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
987 if (bytes < bufremain)
989 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
990 StrBufCutLeft(Hdr->ReadBuf, bufremain);
993 if (bytes > bufremain) {
994 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
996 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
998 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
1002 syslog(LOG_INFO, "client_read_ssl() failed\n");
1010 retval = StrBufReadBLOBBuffered(Target,
1019 syslog(LOG_INFO, "client_read() failed: %s\n", Error);
1020 wc_backtrace(LOG_DEBUG);
1029 * Begin buffering HTTP output so we can transmit it all in one write operation later.
1031 void begin_burst(void)
1033 if (WC->WBuf == NULL) {
1034 WC->WBuf = NewStrBufPlain(NULL, 32768);
1040 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
1042 long end_burst(void)
1044 const char *ptr, *eptr;
1050 if (!DisableGzip && (WC->Hdr->HR.gzip_ok))
1052 if (CompressBuffer(WC->WBuf) > 0)
1053 hprintf("Content-encoding: gzip\r\n");
1055 syslog(LOG_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
1056 wc_backtrace(LOG_INFO);
1060 if (WC->WFBuf != NULL) {
1061 WildFireSerializePayload(WC->WFBuf, WC->HBuf, &WC->Hdr->nWildfireHeaders, NULL);
1062 FreeStrBuf(&WC->WFBuf);
1065 if (WC->Hdr->HR.prohibit_caching)
1066 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1067 hprintf("Content-length: %d\r\n\r\n", StrLength(WC->WBuf));
1069 ptr = ChrPtr(WC->HBuf);
1070 count = StrLength(WC->HBuf);
1075 client_write_ssl(WC->HBuf);
1076 client_write_ssl(WC->WBuf);
1081 if (WC->Hdr->http_sock == -1) {
1084 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
1086 while ((ptr < eptr) && (WC->Hdr->http_sock != -1)) {
1087 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
1089 FD_SET(WC->Hdr->http_sock, &wset);
1090 if (select(WC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
1091 syslog(LOG_DEBUG, "client_write: Socket select failed (%s)\n", strerror(errno));
1096 if ((WC->Hdr->http_sock == -1) ||
1097 (res = write(WC->Hdr->http_sock,
1100 syslog(LOG_DEBUG, "client_write: Socket write failed (%s)\n", strerror(errno));
1101 wc_backtrace(LOG_INFO);
1108 ptr = ChrPtr(WC->WBuf);
1109 count = StrLength(WC->WBuf);
1112 while ((ptr < eptr) && (WC->Hdr->http_sock != -1)) {
1113 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
1115 FD_SET(WC->Hdr->http_sock, &wset);
1116 if (select(WC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
1117 syslog(LOG_INFO, "client_write: Socket select failed (%s)\n", strerror(errno));
1122 if ((WC->Hdr->http_sock == -1) ||
1123 (res = write(WC->Hdr->http_sock,
1126 syslog(LOG_INFO, "client_write: Socket write failed (%s)\n", strerror(errno));
1127 wc_backtrace(LOG_INFO);
1134 return StrLength(WC->WBuf);
1139 * lingering_close() a`la Apache. see
1140 * http://httpd.apache.org/docs/2.0/misc/fin_wait_2.html for rationale
1142 int lingering_close(int fd)
1147 struct timeval tv, start;
1149 gettimeofday(&start, NULL);
1155 gettimeofday(&tv, NULL);
1156 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
1157 tv.tv_usec = start.tv_usec - tv.tv_usec;
1158 if (tv.tv_usec < 0) {
1160 tv.tv_usec += 1000000;
1164 i = select(fd + 1, &set, NULL, NULL, &tv);
1165 } while (i == -1 && errno == EINTR);
1170 i = read(fd, buf, sizeof buf);
1171 } while (i != 0 && (i != -1 || errno == EINTR));
1177 HttpNewModule_TCPSOCKETS
1178 (ParsedHttpHdrs *httpreq)
1181 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
1185 HttpDetachModule_TCPSOCKETS
1186 (ParsedHttpHdrs *httpreq)
1189 FlushStrBuf(httpreq->ReadBuf);
1190 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
1194 HttpDestroyModule_TCPSOCKETS
1195 (ParsedHttpHdrs *httpreq)
1198 FreeStrBuf(&httpreq->ReadBuf);
1203 SessionNewModule_TCPSOCKETS
1206 sess->CLineBuf = NewStrBuf();
1207 sess->MigrateReadLineBuf = NewStrBuf();
1211 SessionDestroyModule_TCPSOCKETS
1214 FreeStrBuf(&sess->CLineBuf);
1215 FreeStrBuf(&sess->ReadBuf);
1216 sess->connected = 0;
1217 sess->ReadPos = NULL;
1218 FreeStrBuf(&sess->MigrateReadLineBuf);
1219 if (sess->serv_sock > 0) {
1220 syslog(LOG_DEBUG, "Closing socket %d", sess->serv_sock);
1221 close(sess->serv_sock);
1223 sess->serv_sock = -1;