- }
-#endif
-
- if (StrLength(Buf) > 0) {/*/// todo: what if Buf > bytes?*/
- StrBufAppendBuf(Target, Buf, 0);
- }
- retval = StrBufReadBLOB(Target,
- sock,
- (StrLength(Target) > 0),
- bytes - StrLength(Target),
- &Error);
- if (retval < 0) {
- lprintf(2, "client_read() failed: %s\n",
- Error);
- return retval;
- }
-
-#ifdef HTTP_TRACING
- write(2, "\033[32m", 5);
- write(2, buf, bytes);
- write(2, "\033[30m", 5);
-#endif
- return 1;
-}
-
-
-/*
- * Begin buffering HTTP output so we can transmit it all in one write operation later.
- */
-void begin_burst(void)
-{
- if (WC->WBuf == NULL) {
- WC->WBuf = NewStrBufPlain(NULL, 32768);
- }
-}
-
-
-/*
- * Finish buffering HTTP output. [Compress using zlib and] output with a Content-Length: header.
- */
-long end_burst(void)
-{
- wcsession *WCC = WC;
- const char *ptr, *eptr;
- long count;
- ssize_t res;
- fd_set wset;
- int fdflags;
-
- //#ifdef HAVE_ZLIB
- /* Perform gzip compression, if enabled and supported by client */
- if (!DisableGzip && (WCC->gzip_ok) && CompressBuffer(WCC->WBuf))
- {
- hprintf("Content-encoding: gzip\r\n");
- }
- //#endif /* HAVE_ZLIB */
-
- hprintf("Content-length: %d\r\n\r\n", StrLength(WCC->WBuf));
-
- ptr = ChrPtr(WCC->HBuf);
- count = StrLength(WCC->HBuf);
- eptr = ptr + count;
-
-#ifdef HAVE_OPENSSL
- if (is_https) {
- client_write_ssl(WCC->HBuf);
- client_write_ssl(WCC->WBuf);
- return (count);
- }
-#endif
-
-
-#ifdef HTTP_TRACING
-
- write(2, "\033[34m", 5);
- write(2, ptr, StrLength(WCC->WBuf));
- write(2, "\033[30m", 5);
-#endif
- fdflags = fcntl(WC->http_sock, F_GETFL);
-
- while (ptr < eptr) {
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
- FD_ZERO(&wset);
- FD_SET(WCC->http_sock, &wset);
- if (select(WCC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
- lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
- return -1;
- }
- }
-
- if ((res = write(WCC->http_sock,
- ptr,
- count)) == -1) {
- lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
- wc_backtrace();
- return res;
- }
- count -= res;
- ptr += res;
- }
-
- ptr = ChrPtr(WCC->WBuf);
- count = StrLength(WCC->WBuf);
- eptr = ptr + count;
-
-#ifdef HTTP_TRACING
-
- write(2, "\033[34m", 5);
- write(2, ptr, StrLength(WCC->WBuf));
- write(2, "\033[30m", 5);
-#endif
-
- while (ptr < eptr) {
- if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
- FD_ZERO(&wset);
- FD_SET(WCC->http_sock, &wset);
- if (select(WCC->http_sock + 1, NULL, &wset, NULL, NULL) == -1) {
- lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
- return -1;
- }
- }
-
- if ((res = write(WCC->http_sock,
- ptr,
- count)) == -1) {
- lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
- wc_backtrace();
- return res;
- }
- count -= res;
- ptr += res;
- }
-
- return StrLength(WCC->WBuf);
-}
-
-
-
-/*
- * Read data from the client socket with default timeout.
- * (This is implemented in terms of client_read_to() and could be
- * justifiably moved out of sysdep.c)
- *
- * sock the socket fd to read from
- * buf the buffer to write to
- * bytes Number of bytes to read
- */
-int client_read(int *sock, StrBuf *Target, StrBuf *buf, int bytes)
-{
- return (client_read_to(sock, Target, buf, bytes, SLEEPING));
-}
-
-
-
-/*
- * Shut us down the regular way.
- * signum is the signal we want to forward
- */
-pid_t current_child;
-void graceful_shutdown_watcher(int signum) {
- lprintf (1, "bye; shutting down watcher.");
- kill(current_child, signum);
- if (signum != SIGHUP)
- exit(0);
-}
-
-
-int ClientGetLine(int *sock, StrBuf *Target, StrBuf *CLineBuf)
-{
- const char *Error, *pch, *pchs;
- int rlen, len, retval = 0;
-
- if (is_https) {
- int ntries = 0;
- if (StrLength(CLineBuf) > 0) {
- pchs = ChrPtr(CLineBuf);
- pch = strchr(pchs, '\n');
- if (pch != NULL) {
- rlen = 0;
- len = pch - pchs;
- if (len > 0 && (*(pch - 1) == '\r') )
- rlen ++;
- StrBufSub(Target, CLineBuf, 0, len - rlen);
- StrBufCutLeft(CLineBuf, len + 1);
- return len - rlen;