2 * Copyright (c) 1987-2017 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)
168 StrBuf_ServGetln(WCC->MigrateReadLineBuf);
169 len = StrLength(WCC->MigrateReadLineBuf);
172 memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
173 FlushStrBuf(WCC->MigrateReadLineBuf);
176 syslog(LOG_DEBUG, "%3d<<<%s\n", WCC->serv_sock, strbuf);
182 int StrBuf_ServGetln(StrBuf *buf)
185 const char *ErrStr = NULL;
192 rc = StrBufTCP_read_buffered_line_fast(buf,
200 syslog(LOG_INFO, "StrBuf_ServGetln(): Server connection broken: %s\n",
202 wc_backtrace(LOG_INFO);
203 if (WCC->serv_sock > 0) close(WCC->serv_sock);
204 WCC->serv_sock = (-1);
212 if (WCC->ReadPos != NULL)
213 pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
214 syslog(LOG_DEBUG, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
220 int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
226 rc = StrBufReadBLOBBuffered(buf,
236 syslog(LOG_INFO, "StrBuf_ServGetBLOBBuffered(): Server connection broken: %s\n",
238 wc_backtrace(LOG_INFO);
239 if (WCC->serv_sock > 0) close(WCC->serv_sock);
240 WCC->serv_sock = (-1);
246 syslog(LOG_DEBUG, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
252 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
259 rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
262 syslog(LOG_INFO, "StrBuf_ServGetBLOB(): Server connection broken: %s\n",
264 wc_backtrace(LOG_INFO);
265 if (WCC->serv_sock > 0) close(WCC->serv_sock);
266 WCC->serv_sock = (-1);
272 syslog(LOG_DEBUG, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
279 void FlushReadBuf (void)
286 len = StrLength(WCC->ReadBuf);
288 (WCC->ReadPos != NULL) &&
289 (WCC->ReadPos != StrBufNOTNULL))
292 pch = ChrPtr(WCC->ReadBuf);
294 if (WCC->ReadPos != pche)
297 "ERROR: somebody didn't eat his soup! Remaing Chars: %ld [%s]\n",
298 (long)(pche - WCC->ReadPos),
302 "--------------------------------------------------------------------------------\n"
304 "--------------------------------------------------------------------------------\n",
306 AppendImportantMessage(HKEY("Suppenkasper alert! watch your webcit logfile and get connected to your favourite opensource Crew."));
310 FlushStrBuf(WCC->ReadBuf);
318 * send binary to server
319 * buf the buffer to write to citadel server
320 * nbytes how many bytes to send to citadel server
322 int serv_write(const char *buf, int nbytes)
325 int bytes_written = 0;
329 while (bytes_written < nbytes) {
330 retval = write(WCC->serv_sock, &buf[bytes_written],
331 nbytes - bytes_written);
333 const char *ErrStr = strerror(errno);
334 syslog(LOG_INFO, "serv_write(): Server connection broken: %s\n",
336 if (WCC->serv_sock > 0) close(WCC->serv_sock);
337 WCC->serv_sock = (-1);
342 bytes_written = bytes_written + retval;
349 * send line to server
350 * string the line to send to the citadel server
352 int serv_puts(const char *string)
355 syslog(LOG_DEBUG, "%3d>>>%s\n", WC->serv_sock, string);
359 if (!serv_write(string, strlen(string)))
361 return serv_write("\n", 1);
365 * send line to server
366 * string the line to send to the citadel server
368 int serv_putbuf(const StrBuf *string)
371 syslog(LOG_DEBUG, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
375 if (!serv_write(ChrPtr(string), StrLength(string)))
377 return serv_write("\n", 1);
382 * convenience function to send stuff to the server
383 * format the formatstring
384 * ... the entities to insert into format
386 int serv_printf(const char *format,...)
395 va_start(arg_ptr, format);
396 vsnprintf(buf, sizeof buf, format, arg_ptr);
402 rc = serv_write(buf, len);
404 syslog(LOG_DEBUG, ">>>%s", buf);
411 * Read binary data from server into memory using a series of server READ commands.
412 * returns the read content as StrBuf
414 int serv_read_binary(StrBuf *Ret, size_t total_len, StrBuf *Buf)
417 size_t bytes_read = 0;
418 size_t this_block = 0;
426 while ((bytes_read < total_len) && (ServerRc == 6)) {
428 if (WCC->serv_sock==-1) {
433 serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, bytes_read, total_len-bytes_read);
434 if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
435 (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) )
439 StrBufCutLeft(Buf, 4);
440 this_block = StrTol(Buf);
441 rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
443 syslog(LOG_INFO, "Server connection broken during download\n");
444 wc_backtrace(LOG_INFO);
445 if (WCC->serv_sock > 0) close(WCC->serv_sock);
446 WCC->serv_sock = (-1);
455 return StrLength(Ret);
459 int client_write(StrBuf *ThisBuf)
462 const char *ptr, *eptr;
468 ptr = ChrPtr(ThisBuf);
469 count = StrLength(ThisBuf);
472 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
474 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
475 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
477 FD_SET(WCC->Hdr->http_sock, &wset);
478 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
479 syslog(LOG_INFO, "client_write: Socket select failed (%s)\n", strerror(errno));
484 if ((WCC->Hdr->http_sock == -1) ||
485 ((res = write(WCC->Hdr->http_sock, ptr, count)),
488 syslog(LOG_INFO, "client_write: Socket write failed (%s)\n", strerror(errno));
489 wc_backtrace(LOG_INFO);
511 serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, *bytes_read, total_len-(*bytes_read));
512 if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
513 (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) )
515 size_t this_block = 0;
520 StrBufCutLeft(Buf, 4);
521 this_block = StrTol(Buf);
522 rc = StrBuf_ServGetBLOBBuffered(WCC->WBuf, this_block);
524 syslog(LOG_INFO, "Server connection broken during download\n");
525 wc_backtrace(LOG_INFO);
526 if (WCC->serv_sock > 0) close(WCC->serv_sock);
527 WCC->serv_sock = (-1);
537 static inline int send_http(StrBuf *Buf)
541 return client_write_ssl(Buf);
544 return client_write(Buf);
547 * Read binary data from server into memory using a series of server READ commands.
548 * returns the read content as StrBuf
550 void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static, int detect_mime)
554 size_t bytes_read = 0;
556 int client_con_state = 0;
559 const char *Err = NULL;
560 StrBuf *BufHeader = NULL;
565 IOBuffer WriteBuffer;
570 if (WCC->Hdr->HaveRange)
572 WCC->Hdr->HaveRange++;
573 WCC->Hdr->TotalBytes = total_len;
574 /* open range? or beyound file border? correct the numbers. */
575 if ((WCC->Hdr->RangeTil == -1) || (WCC->Hdr->RangeTil>= total_len))
576 WCC->Hdr->RangeTil = total_len - 1;
577 bytes_read = WCC->Hdr->RangeStart;
578 total_len = WCC->Hdr->RangeTil;
581 chunked = total_len > SIZ * 10; /* TODO: disallow for HTTP / 1.0 */
585 BufHeader = NewStrBuf();
588 if ((detect_mime != 0) && (bytes_read != 0))
590 /* need to read first chunk to detect mime, though the client doesn't care */
591 size_t bytes_read = 0;
594 ServerRc = read_serv_chunk(
601 FreeStrBuf(&BufHeader);
605 CT = GuessMimeType(SKEY(WCC->WBuf));
606 FlushStrBuf(WCC->WBuf);
607 StrBufPlain(MimeType, CT, -1);
608 CheckGZipCompressionAllowed(SKEY(MimeType));
613 memset(&WriteBuffer, 0, sizeof(IOBuffer));
614 if (chunked && !DisableGzip && WCC->Hdr->HR.gzip_ok)
617 SC = StrBufNewStreamContext (eZLibEncode, &Err);
619 syslog(LOG_ERR, "Error while initializing stream context: %s", Err);
624 memset(&ReadBuffer, 0, sizeof(IOBuffer));
625 ReadBuffer.Buf = WCC->WBuf;
627 WriteBuffer.Buf = NewStrBufPlain(NULL, SIZ*2);;
628 pBuf = WriteBuffer.Buf;
637 http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
639 if (send_http(WCC->HBuf) < 0)
642 FreeStrBuf(&WriteBuffer.Buf);
643 FreeStrBuf(&BufHeader);
644 if (StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
645 syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
651 while ((bytes_read < total_len) &&
653 (client_con_state == 0))
656 if (WCC->serv_sock==-1) {
657 FlushStrBuf(WCC->WBuf);
659 FreeStrBuf(&WriteBuffer.Buf);
660 FreeStrBuf(&BufHeader);
661 StrBufDestroyStreamContext(eZLibEncode, &SC, &Err);
662 if (StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
663 syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
668 ServerRc = read_serv_chunk(
680 CT = GuessMimeType(SKEY(WCC->WBuf));
681 StrBufPlain(MimeType, CT, -1);
683 CheckGZipCompressionAllowed(SKEY(MimeType));
684 is_gzip = WCC->Hdr->HR.gzip_ok;
686 http_transmit_headers(ChrPtr(MimeType), is_static, chunked, is_gzip);
688 client_con_state = send_http(WCC->HBuf);
693 int done = (bytes_read == total_len);
694 while ((IOBufferStrLength(&ReadBuffer) > 0) && (client_con_state == 0)) {
698 rc = StrBufStreamTranscode(eZLibEncode, &WriteBuffer, &ReadBuffer, NULL, -1, SC, done, &Err);
700 if (StrLength (pBuf) > 0) {
701 StrBufPrintf(BufHeader, "%s%x\r\n",
705 client_con_state = send_http(BufHeader);
706 if (client_con_state == 0) {
707 client_con_state = send_http(pBuf);
711 } while ((rc == 1) && (StrLength(pBuf) > 0));
713 FlushStrBuf(WCC->WBuf);
716 if ((chunked) && (client_con_state == 0))
718 StrBufPrintf(BufHeader, "%s%x\r\n",
722 client_con_state = send_http(BufHeader);
725 if (client_con_state == 0)
726 client_con_state = send_http(pBuf);
732 if (SC && StrBufDestroyStreamContext(eZLibEncode, &SC, &Err) && Err) {
733 syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
735 FreeStrBuf(&WriteBuffer.Buf);
736 if ((chunked) && (client_con_state == 0))
738 StrBufPlain(BufHeader, HKEY("\r\n0\r\n\r\n"));
739 if (send_http(BufHeader) < 0)
742 FreeStrBuf(&BufHeader);
746 FreeStrBuf(&BufHeader);
750 int ClientGetLine(ParsedHttpHdrs *Hdr, StrBuf *Target)
754 const char *pch, *pchs;
755 int rlen, len, retval = 0;
759 if (StrLength(Hdr->ReadBuf) > 0)
761 pchs = ChrPtr(Hdr->ReadBuf);
762 pch = strchr(pchs, '\n');
766 if (len > 0 && (*(pch - 1) == '\r') )
768 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
769 StrBufCutLeft(Hdr->ReadBuf, len + 1);
774 while (retval == 0) {
776 pchs = ChrPtr(Hdr->ReadBuf);
778 pch = strchr(pchs, '\n');
780 retval = client_read_sslbuffer(Hdr->ReadBuf, SLEEPING);
781 pchs = ChrPtr(Hdr->ReadBuf);
782 pch = strchr(pchs, '\n');
793 if ((retval > 0) && (pch != NULL)) {
796 if (len > 0 && (*(pch - 1) == '\r') )
798 StrBufSub(Target, Hdr->ReadBuf, 0, len - rlen);
799 StrBufCutLeft(Hdr->ReadBuf, len + 1);
808 return StrBufTCP_read_buffered_line_fast(Target,
819 * This is a generic function to set up a master socket for listening on
820 * a TCP port. The server shuts down if the bind fails. (IPv4/IPv6 version)
822 * ip_addr IP address to bind
823 * port_number port number to bind
824 * queue_len number of incoming connections to allow in the queue
826 int webcit_tcp_server(const char *ip_addr, int port_number, int queue_len)
828 const char *ipv4broadcast = "0.0.0.0";
831 struct sockaddr_in6 sin6;
832 struct sockaddr_in sin4;
837 memset(&sin6, 0, sizeof(sin6));
838 memset(&sin4, 0, sizeof(sin4));
839 sin6.sin6_family = AF_INET6;
840 sin4.sin_family = AF_INET;
842 if ( (ip_addr == NULL) /* any IPv6 */
843 || (IsEmptyStr(ip_addr))
844 || (!strcmp(ip_addr, "*"))
848 sin6.sin6_addr = in6addr_any;
850 else if (!strcmp(ip_addr, "0.0.0.0")) /* any IPv4 */
853 sin4.sin_addr.s_addr = INADDR_ANY;
855 else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':'))) /* specific IPv4 */
858 if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
859 syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
860 return (-WC_EXIT_BIND);
863 else /* specific IPv6 */
866 if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
867 syslog(LOG_WARNING, "Error binding to [%s] : %s\n", ip_addr, strerror(errno));
868 return (-WC_EXIT_BIND);
872 if (port_number == 0) {
873 syslog(LOG_WARNING, "Cannot start: no port number specified.\n");
874 return (-WC_EXIT_BIND);
876 sin6.sin6_port = htons((u_short) port_number);
877 sin4.sin_port = htons((u_short) port_number);
879 p = getprotobyname("tcp");
881 s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
883 if (IsDefault && (errno == EAFNOSUPPORT))
886 ip_addr = ipv4broadcast;
889 syslog(LOG_WARNING, "Can't create a listening socket: %s\n", strerror(errno));
890 return (-WC_EXIT_BIND);
892 /* Set some socket options that make sense. */
894 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
896 if (ip_version == 6) {
897 b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
900 b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
904 syslog(LOG_ERR, "Can't bind: %s\n", strerror(errno));
906 return (-WC_EXIT_BIND);
909 if (listen(s, queue_len) < 0) {
910 syslog(LOG_ERR, "Can't listen: %s\n", strerror(errno));
912 return (-WC_EXIT_BIND);
919 * Create a Unix domain socket and listen on it
920 * sockpath - file name of the unix domain socket
921 * queue_len - Number of incoming connections to allow in the queue
923 int webcit_uds_server(char *sockpath, int queue_len)
925 struct sockaddr_un addr;
928 int actual_queue_len;
930 actual_queue_len = queue_len;
931 if (actual_queue_len < 5) actual_queue_len = 5;
933 i = unlink(sockpath);
934 if ((i != 0) && (errno != ENOENT)) {
935 syslog(LOG_WARNING, "webcit: can't unlink %s: %s\n",
936 sockpath, strerror(errno));
937 return (-WC_EXIT_BIND);
940 memset(&addr, 0, sizeof(addr));
941 addr.sun_family = AF_UNIX;
942 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
944 s = socket(AF_UNIX, SOCK_STREAM, 0);
946 syslog(LOG_WARNING, "webcit: Can't create a unix domain socket: %s\n", strerror(errno));
947 return (-WC_EXIT_BIND);
950 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
951 syslog(LOG_WARNING, "webcit: Can't bind: %s\n", strerror(errno));
953 return (-WC_EXIT_BIND);
956 if (listen(s, actual_queue_len) < 0) {
957 syslog(LOG_WARNING, "webcit: Can't listen: %s\n", strerror(errno));
959 return (-WC_EXIT_BIND);
962 chmod(sockpath, 0777);
970 * Read data from the client socket.
972 * sock socket fd to read from
973 * buf buffer to read into
974 * bytes number of bytes to read
975 * timeout Number of seconds to wait before timing out
977 * Possible return values:
978 * 1 Requested number of bytes has been read.
979 * 0 Request timed out.
980 * -1 Connection is broken, or other error.
982 int client_read_to(ParsedHttpHdrs *Hdr, StrBuf *Target, int bytes, int timeout)
992 baselen = StrLength(Target);
994 if (Hdr->Pos == NULL)
995 Hdr->Pos = ChrPtr(Hdr->ReadBuf);
997 if (StrLength(Hdr->ReadBuf) > 0)
999 bufremain = StrLength(Hdr->ReadBuf) - (Hdr->Pos - ChrPtr(Hdr->ReadBuf));
1001 if (bytes < bufremain)
1003 StrBufAppendBufPlain(Target, Hdr->Pos, bufremain, 0);
1004 StrBufCutLeft(Hdr->ReadBuf, bufremain);
1007 if (bytes > bufremain)
1009 while ((StrLength(Hdr->ReadBuf) + StrLength(Target) < bytes + baselen) &&
1011 retval = client_read_sslbuffer(Hdr->ReadBuf, timeout);
1013 StrBufAppendBuf(Target, Hdr->ReadBuf, 0); /* todo: Buf > bytes? */
1017 syslog(LOG_INFO, "client_read_ssl() failed\n");
1026 retval = StrBufReadBLOBBuffered(Target,
1035 syslog(LOG_INFO, "client_read() failed: %s\n",
1037 wc_backtrace(LOG_DEBUG);
1046 * Begin buffering HTTP output so we can transmit it all in one write operation later.
1048 void begin_burst(void)
1050 if (WC->WBuf == NULL) {
1051 WC->WBuf = NewStrBufPlain(NULL, 32768);
1057 * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
1059 long end_burst(void)
1061 wcsession *WCC = WC;
1062 const char *ptr, *eptr;
1068 if (!DisableGzip && (WCC->Hdr->HR.gzip_ok))
1070 if (CompressBuffer(WCC->WBuf) > 0)
1071 hprintf("Content-encoding: gzip\r\n");
1073 syslog(LOG_ALERT, "Compression failed: %d [%s] sending uncompressed\n", errno, strerror(errno));
1074 wc_backtrace(LOG_INFO);
1078 if (WCC->WFBuf != NULL) {
1079 WildFireSerializePayload(WCC->WFBuf, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
1080 FreeStrBuf(&WCC->WFBuf);
1083 if (WCC->Hdr->HR.prohibit_caching)
1084 hprintf("Pragma: no-cache\r\nCache-Control: no-store\r\nExpires:-1\r\n");
1085 hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
1087 ptr = ChrPtr(WCC->HBuf);
1088 count = StrLength(WCC->HBuf);
1093 client_write_ssl(WCC->HBuf);
1094 client_write_ssl(WCC->WBuf);
1099 if (WCC->Hdr->http_sock == -1)
1101 fdflags = fcntl(WC->Hdr->http_sock, F_GETFL);
1103 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)){
1104 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
1106 FD_SET(WCC->Hdr->http_sock, &wset);
1107 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
1108 syslog(LOG_DEBUG, "client_write: Socket select failed (%s)\n", strerror(errno));
1113 if ((WCC->Hdr->http_sock == -1) ||
1114 (res = write(WCC->Hdr->http_sock,
1117 syslog(LOG_DEBUG, "client_write: Socket write failed (%s)\n", strerror(errno));
1118 wc_backtrace(LOG_INFO);
1125 ptr = ChrPtr(WCC->WBuf);
1126 count = StrLength(WCC->WBuf);
1129 while ((ptr < eptr) && (WCC->Hdr->http_sock != -1)) {
1130 if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
1132 FD_SET(WCC->Hdr->http_sock, &wset);
1133 if (select(WCC->Hdr->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
1134 syslog(LOG_INFO, "client_write: Socket select failed (%s)\n", strerror(errno));
1139 if ((WCC->Hdr->http_sock == -1) ||
1140 (res = write(WCC->Hdr->http_sock,
1143 syslog(LOG_INFO, "client_write: Socket write failed (%s)\n", strerror(errno));
1144 wc_backtrace(LOG_INFO);
1151 return StrLength(WCC->WBuf);
1156 * lingering_close() a`la Apache. see
1157 * http://httpd.apache.org/docs/2.0/misc/fin_wait_2.html for rationale
1159 int lingering_close(int fd)
1164 struct timeval tv, start;
1166 gettimeofday(&start, NULL);
1172 gettimeofday(&tv, NULL);
1173 tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
1174 tv.tv_usec = start.tv_usec - tv.tv_usec;
1175 if (tv.tv_usec < 0) {
1177 tv.tv_usec += 1000000;
1181 i = select(fd + 1, &set, NULL, NULL, &tv);
1182 } while (i == -1 && errno == EINTR);
1187 i = read(fd, buf, sizeof buf);
1188 } while (i != 0 && (i != -1 || errno == EINTR));
1194 HttpNewModule_TCPSOCKETS
1195 (ParsedHttpHdrs *httpreq)
1198 httpreq->ReadBuf = NewStrBufPlain(NULL, SIZ * 4);
1202 HttpDetachModule_TCPSOCKETS
1203 (ParsedHttpHdrs *httpreq)
1206 FlushStrBuf(httpreq->ReadBuf);
1207 ReAdjustEmptyBuf(httpreq->ReadBuf, 4 * SIZ, SIZ);
1211 HttpDestroyModule_TCPSOCKETS
1212 (ParsedHttpHdrs *httpreq)
1215 FreeStrBuf(&httpreq->ReadBuf);
1220 SessionNewModule_TCPSOCKETS
1223 sess->CLineBuf = NewStrBuf();
1224 sess->MigrateReadLineBuf = NewStrBuf();
1228 SessionDestroyModule_TCPSOCKETS
1231 FreeStrBuf(&sess->CLineBuf);
1232 FreeStrBuf(&sess->ReadBuf);
1233 sess->connected = 0;
1234 sess->ReadPos = NULL;
1235 FreeStrBuf(&sess->MigrateReadLineBuf);
1236 if (sess->serv_sock > 0) {
1237 syslog(LOG_DEBUG, "Closing socket %d", sess->serv_sock);
1238 close(sess->serv_sock);
1240 sess->serv_sock = -1;