[PHP-DEV] CVS update: php3 From: cschneid (php-dev <email protected>)
Date: 10/25/98

Date: Sunday October 25, 1998 @ 16:34
Author: cschneid

Update of /repository/php3
In directory asf:/u2/tmp/cvs-serv16428

Modified Files:
        configure.in main.c mod_php3.c mod_php3.h php.h
Log Message:
Added zlib compression to output functions of Apache module. This means
(currently for Apache module only) PHP supports the HTTP header line
Accept-encoding: *gzip* and sends Content-encoding: gzip as well as
compressing the output. Added the configuration flag compress_output [on|off]
which can be set for the Apache module on a per directory basis.

Index: php3/configure.in
diff -c php3/configure.in:1.266 php3/configure.in:1.267
*** php3/configure.in:1.266 Tue Oct 20 20:13:41 1998
--- php3/configure.in Sun Oct 25 16:34:47 1998
***************
*** 1,4 ****
! dnl $Id: configure.in,v 1.266 1998/10/21 00:13:41 ssb Exp $
  dnl Process this file with autoconf to produce a configure script.
  
  AC_INIT(main.c)
--- 1,4 ----
! dnl $Id: configure.in,v 1.267 1998/10/25 21:34:47 cschneid Exp $
  dnl Process this file with autoconf to produce a configure script.
  
  AC_INIT(main.c)
***************
*** 1296,1303 ****
--- 1296,1306 ----
        test -f $withval/include/zlib.h && ZLIB_INCLUDE="-I$withval/include"
        if test -n "$ZLIB_INCLUDE" ; then
          AC_MSG_RESULT(yes)
+ old_LIBS=$LIBS
+ LIBS="$LIBS -L$withval/lib"
          AC_CHECK_LIB(z, gzgets, [AC_DEFINE(HAVE_ZLIB) ZLIB_LIBS="-L$withval/lib -lz"],
            [AC_MSG_ERROR(Zlib module requires zlib >= 1.0.9.)])
+ LIBS=$old_LIBS
        else
          AC_MSG_RESULT(no)
        fi ;;
Index: php3/main.c
diff -c php3/main.c:1.476 php3/main.c:1.477
*** php3/main.c:1.476 Sat Oct 17 15:32:50 1998
--- php3/main.c Sun Oct 25 16:34:48 1998
***************
*** 29,35 ****
     +----------------------------------------------------------------------+
   */
  
! /* $Id: main.c,v 1.476 1998/10/17 19:32:50 shane Exp $ */
  
  /* #define CRASH_DETECTION */
  
--- 29,35 ----
     +----------------------------------------------------------------------+
   */
  
! /* $Id: main.c,v 1.477 1998/10/25 21:34:48 cschneid Exp $ */
  
  /* #define CRASH_DETECTION */
  
***************
*** 84,89 ****
--- 84,103 ----
  #include <syslog.h>
  #endif
  
+ #if HAVE_ZLIB
+ #include <zlib.h>
+
+ /* Copied definitions from zutil.h */
+ #ifndef OS_CODE
+ # define OS_CODE 0x03 /* assume Unix */
+ #endif
+ #if MAX_MEM_LEVEL >= 8
+ # define DEF_MEM_LEVEL 8
+ #else
+ # define DEF_MEM_LEVEL MAX_MEM_LEVEL
+ #endif
+ #endif
+
  #if USE_SAPI
  #include "serverapi/sapi.h"
  void *gLock;
***************
*** 108,114 ****
--- 122,135 ----
  unsigned int max_execution_time = 0;
  #if APACHE
  request_rec *php3_rqst = NULL; /* request record pointer for apache module version */
+ #if HAVE_ZLIB
+ z_stream php3_zstream; /* zlib stream for Content-encoding: gzip */
+ z_streamp php3_zstreamp; /* pointer to zstream above */
+ Bytef *php3_zoutbuf; /* output buffer for zstream */
+ uLong php3_zcrc; /* output CRC checksum for gzip file */
+ #define ZOUTBUF_SIZE 1024*8
  #endif
+ #endif
  
  /* This one doesn't exists on QNX */
  #ifndef SIGPROF
***************
*** 175,181 ****
--- 196,206 ----
          TLS_VARS;
  
          if (GLOBAL(php3_rqst)) {
+ #if HAVE_ZLIB
+ php3_apache_phpwrite((void *)s, strlen(s));
+ #else
                  rputs(s, GLOBAL(php3_rqst));
+ #endif
          } else {
                  fputs(s, stdout);
          }
***************
*** 186,198 ****
--- 211,417 ----
          TLS_VARS;
  
          if (GLOBAL(php3_rqst)) {
+ #if HAVE_ZLIB
+ php3_apache_phpwrite(&c, 1);
+ #else
                  rputc(c, GLOBAL(php3_rqst));
+ #endif
          } else {
                  fputc(c, stdout);
          }
  }
+
+ PHPAPI int php3_apache_phpwrite(void *buf, int size)
+ {
+ int written;
+
+ #if HAVE_ZLIB
+ TLS_VARS;
+
+ if (GLOBAL(php3_zstreamp))
+ {
+ int zerr = Z_OK;
+
+ php3_zstream.next_in = (Bytef*)buf;
+ php3_zstream.avail_in = size;
+
+ /* Basic compression loop adapted from gzwrite in zlib gzio.c */
+ while (GLOBAL(php3_zstreamp)->avail_in > 0)
+ {
+ if (GLOBAL(php3_zstreamp)->avail_out == 0)
+ {
+ GLOBAL(php3_zstreamp)->next_out = GLOBAL(php3_zoutbuf);
+
+ if (rwrite(GLOBAL(php3_zoutbuf),
+ ZOUTBUF_SIZE, GLOBAL(php3_rqst)) != ZOUTBUF_SIZE)
+ {
+ zerr = Z_ERRNO;
+ break;
+ }
+
+ GLOBAL(php3_zstreamp)->avail_out = ZOUTBUF_SIZE;
+ }
+
+ zerr = deflate(GLOBAL(php3_zstreamp), Z_NO_FLUSH);
+
+ if (zerr != Z_OK)
+ break;
+ }
+
+ GLOBAL(php3_zcrc) = crc32(GLOBAL(php3_zcrc), (const Bytef *)buf, size);
+
+ written = (zerr == Z_OK) ? size : -1;
+ }
+ else
+ written = rwrite(buf, size, GLOBAL(php3_rqst));
+ #else
+ written = rwrite(buf, size, GLOBAL(php3_rqst));
  #endif
  
+ return written;
+ }
+
+ /* Utility function adapted from putLong in zlib gzio.c */
+ static void php3_putLong (uLong x)
+ {
+ int n;
+
+ for (n = 0; n < 4; n++)
+ {
+ rputc((int)(x & 0xff), GLOBAL(php3_rqst));
+ x >>= 8;
+ }
+ }
+
+ #if HAVE_ZLIB
+ voidpf php3_zalloc(voidpf opaque, uInt items, uInt size)
+ {
+ return emalloc(items * size);
+ }
+
+ void php3_zfree(voidpf opaque, voidpf address)
+ {
+ efree(address);
+ }
+
+ static void php3_apache_initzlib()
+ {
+ TLS_VARS;
+
+ GLOBAL(php3_zstreamp) = NULL;
+
+ if (php3_ini.compress_output &&
+ GLOBAL(php3_rqst) && GLOBAL(php3_rqst)->headers_in)
+ {
+ #if MODULE_MAGIC_NUMBER > 19961007
+ const char *s = NULL;
+ #else
+ char *s = NULL;
+ #endif
+
+ s = table_get(GLOBAL(php3_rqst)->headers_in, "Accept-encoding");
+
+ if (s && strstr(s, "gzip"))
+ {
+ GLOBAL(php3_zoutbuf) = emalloc(ZOUTBUF_SIZE);
+
+ if (GLOBAL(php3_zoutbuf))
+ {
+ GLOBAL(php3_zstream).zalloc = php3_zalloc;
+ GLOBAL(php3_zstream).zfree = php3_zfree;
+ GLOBAL(php3_zstream).opaque = Z_NULL;
+
+ /* windowBits < 0 to suppress zlib header if gzip mode */
+ if (deflateInit2(&(GLOBAL(php3_zstream)),
+ Z_DEFAULT_COMPRESSION,
+ Z_DEFLATED,
+ -MAX_WBITS,
+ DEF_MEM_LEVEL,
+ Z_DEFAULT_STRATEGY) == Z_OK)
+ {
+ /* Prefill output buffer with gzip file header */
+ static Bytef gz_header[] = { 0x1f, 0x8b, Z_DEFLATED,
+ 0 /*flags*/,
+ 0,0,0,0 /*time*/,
+ 0 /*xflags*/, OS_CODE };
+
+ GLOBAL(php3_zstream).next_out = GLOBAL(php3_zoutbuf);
+ GLOBAL(php3_zstream).avail_out = ZOUTBUF_SIZE;
+
+ memcpy(GLOBAL(php3_zoutbuf), gz_header,
+ sizeof(gz_header));
+ GLOBAL(php3_zstream).next_out += sizeof(gz_header);
+ GLOBAL(php3_zstream).avail_out -= sizeof(gz_header);
+ GLOBAL(php3_zcrc) = 0;
+ php3_addheaderline("Content-encoding: gzip");
+
+ GLOBAL(php3_zstreamp) = &GLOBAL(php3_zstream);
+ }
+ }
+ }
+ }
+ }
+
+ static void php3_apache_exitzlib()
+ {
+ TLS_VARS;
+
+ if (GLOBAL(php3_zstreamp) && GLOBAL(php3_zoutbuf))
+ {
+ uInt len;
+ int done = 0;
+ int zerr = Z_OK;
+
+ /* Flush loop adapted from do_flush in zlib gzio.c */
+ for (;;)
+ {
+ len = ZOUTBUF_SIZE - GLOBAL(php3_zstreamp)->avail_out;
+
+ if (len != 0)
+ {
+ if (rwrite(GLOBAL(php3_zoutbuf), len, GLOBAL(php3_rqst)) != len)
+ {
+ zerr = Z_ERRNO;
+ break;
+ }
+ GLOBAL(php3_zstreamp)->next_out = GLOBAL(php3_zoutbuf);
+ GLOBAL(php3_zstreamp)->avail_out = ZOUTBUF_SIZE;
+ }
+
+ if (done)
+ break;
+
+ zerr = deflate(GLOBAL(php3_zstreamp), Z_FINISH);
+
+ /* deflate has finished flushing only when it hasn't used up
+ * all the available space in the output buffer:
+ */
+ done = (GLOBAL(php3_zstreamp)->avail_out != 0 ||
+ zerr == Z_STREAM_END);
+
+ if (zerr != Z_OK && zerr != Z_STREAM_END)
+ break;
+ }
+
+ php3_putLong(GLOBAL(php3_zcrc));
+ php3_putLong(GLOBAL(php3_zstreamp)->total_in);
+ }
+
+ if (GLOBAL(php3_zoutbuf))
+ {
+ efree(GLOBAL(php3_zoutbuf));
+ GLOBAL(php3_zoutbuf) = NULL;
+ }
+
+ if (GLOBAL(php3_zstreamp))
+ {
+ deflateEnd(GLOBAL(php3_zstreamp));
+ GLOBAL(php3_zstreamp) = NULL;
+ }
+ }
+ #endif
+ #endif
+
  void php3_log_err(char *log_message)
  {
          FILE *log_file;
***************
*** 259,272 ****
  {
          va_list args;
          int ret;
! #if WIN32_SERVER_MOD || USE_SAPI || FHTTPD
          char buffer[PRINTF_BUFFER_SIZE];
          int size;
  #endif
          TLS_VARS;
  
          va_start(args, format);
! #if APACHE
          if (GLOBAL(php3_rqst)) {
  #if USE_TRANSFER_TABLES
                  ret = charset_vbprintf(GLOBAL(php3_rqst)->connection->client, GLOBAL(php3_rqst), format, args);
--- 478,491 ----
  {
          va_list args;
          int ret;
! #if WIN32_SERVER_MOD || USE_SAPI || FHTTPD || (APACHE && HAVE_ZLIB)
          char buffer[PRINTF_BUFFER_SIZE];
          int size;
  #endif
          TLS_VARS;
  
          va_start(args, format);
! #if APACHE && !(HAVE_ZLIB)
          if (GLOBAL(php3_rqst)) {
  #if USE_TRANSFER_TABLES
                  ret = charset_vbprintf(GLOBAL(php3_rqst)->connection->client, GLOBAL(php3_rqst), format, args);
***************
*** 282,288 ****
          ret = vfprintf(stdout, format, args);
  #endif
  
! #if FHTTPD
          size = vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, args);
          ret = PHPWRITE(buffer, size);
  #endif
--- 501,507 ----
          ret = vfprintf(stdout, format, args);
  #endif
  
! #if FHTTPD || (APACHE && HAVE_ZLIB)
          size = vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, args);
          ret = PHPWRITE(buffer, size);
  #endif
***************
*** 1071,1076 ****
--- 1290,1298 ----
                  if (cfg_get_long("enable_dl", &php3_ini.enable_dl) == FAILURE) {
                          php3_ini.enable_dl = 1;
                  }
+ if (cfg_get_long("compress_output", &php3_ini.compress_output) == FAILURE) {
+ php3_ini.compress_output = 0;
+ }
                  /* THREADX Will have to look into this on windows
                   * Make a master copy to use as a basis for every per-dir config.
                   * Without two copies we would have a previous requst's per-dir
***************
*** 1814,1819 ****
--- 2036,2044 ----
          if (php3_request_startup(_INLINE_TLS_VOID) == FAILURE) {
                  return FAILURE;
          }
+ #if HAVE_ZLIB
+ php3_apache_initzlib();
+ #endif
          php3_TreatHeaders();
          in = fdopen(fd, "r");
          if (in) {
***************
*** 1851,1856 ****
--- 2076,2084 ----
          if (GLOBAL(initialized)) {
                  php3_header(); /* Make sure headers have been sent */
          }
+ #if HAVE_ZLIB
+ php3_apache_exitzlib();
+ #endif
          return (OK);
  }
  #endif /* APACHE */
Index: php3/mod_php3.c
diff -c php3/mod_php3.c:1.81 php3/mod_php3.c:1.82
*** php3/mod_php3.c:1.81 Tue Aug 25 15:56:27 1998
--- php3/mod_php3.c Sun Oct 25 16:34:48 1998
***************
*** 27,33 ****
     | (with helpful hints from Dean Gaudet <dgaudet <email protected>> |
     +----------------------------------------------------------------------+
   */
! /* $Id: mod_php3.c,v 1.81 1998/08/25 19:56:27 rasmus Exp $ */
  
  #ifdef THREAD_SAFE
  #include "tls.h"
--- 27,33 ----
     | (with helpful hints from Dean Gaudet <dgaudet <email protected>> |
     +----------------------------------------------------------------------+
   */
! /* $Id: mod_php3.c,v 1.82 1998/10/25 21:34:48 cschneid Exp $ */
  
  #ifdef THREAD_SAFE
  #include "tls.h"
***************
*** 322,327 ****
--- 322,328 ----
          if (add->error_append_string != orig.error_append_string) new->error_append_string = add->error_append_string;
          if (add->open_basedir != orig.open_basedir) new->open_basedir = add->open_basedir;
          if (add->enable_dl != orig.enable_dl) new->enable_dl = add->enable_dl;
+ if (add->compress_output != orig.compress_output) new->compress_output = add->compress_output;
          
          return new;
  }
***************
*** 378,383 ****
--- 379,387 ----
                  case 13:
                          conf->enable_dl = val;
                          break;
+ case 14:
+ conf->compress_output = val;
+ break;
          }
          return NULL;
  }
***************
*** 535,540 ****
--- 539,545 ----
          {"php3_display_errors", php3flaghandler, (void *)11, OR_OPTIONS, FLAG, "on|off"},
          {"php3_magic_quotes_sybase", php3flaghandler, (void *)12, OR_OPTIONS, FLAG, "on|off"},
          {"php3_enable_dl", php3flaghandler, (void *)13, RSRC_CONF|ACCESS_CONF, FLAG, "on|off"},
+ {"php3_compress_output", php3flaghandler, (void *)14, OR_OPTIONS, FLAG, "on|off"},
          {NULL}
  };
  
Index: php3/mod_php3.h
diff -c php3/mod_php3.h:1.45 php3/mod_php3.h:1.46
*** php3/mod_php3.h:1.45 Mon Sep 21 12:41:50 1998
--- php3/mod_php3.h Sun Oct 25 16:34:49 1998
***************
*** 26,32 ****
     | Authors: Rasmus Lerdorf <rasmus <email protected>> |
     +----------------------------------------------------------------------+
   */
! /* $Id: mod_php3.h,v 1.45 1998/09/21 16:41:50 zeev Exp $ */
  
  #ifndef _MOD_PHP3_H
  #define _MOD_PHP3_H
--- 26,32 ----
     | Authors: Rasmus Lerdorf <rasmus <email protected>> |
     +----------------------------------------------------------------------+
   */
! /* $Id: mod_php3.h,v 1.46 1998/10/25 21:34:49 cschneid Exp $ */
  
  #ifndef _MOD_PHP3_H
  #define _MOD_PHP3_H
***************
*** 90,95 ****
--- 90,96 ----
          char *error_append_string;
          char *open_basedir;
          long enable_dl;
+ long compress_output;
  } php3_ini_structure;
  
  #if MSVC5
Index: php3/php.h
diff -c php3/php.h:1.35 php3/php.h:1.36
*** php3/php.h:1.35 Thu Sep 10 19:56:56 1998
--- php3/php.h Sun Oct 25 16:34:49 1998
***************
*** 28,34 ****
     +----------------------------------------------------------------------+
   */
  
! /* $Id: php.h,v 1.35 1998/09/10 23:56:56 zeev Exp $ */
  
  #ifndef _PHP_H
  #define _PHP_H
--- 28,34 ----
     +----------------------------------------------------------------------+
   */
  
! /* $Id: php.h,v 1.36 1998/10/25 21:34:49 cschneid Exp $ */
  
  #ifndef _PHP_H
  #define _PHP_H
***************
*** 248,257 ****
  #if APACHE
  extern PHPAPI void php3_apache_puts(const char *s);
  extern PHPAPI void php3_apache_putc(char c);
  # if !defined(COMPILE_DL)
  # define PUTS(s) php3_apache_puts(s)
  # define PUTC(c) php3_apache_putc(c)
! # define PHPWRITE(a,n) rwrite((a),(n),GLOBAL(php3_rqst))
  # endif
  #endif
  
--- 248,258 ----
  #if APACHE
  extern PHPAPI void php3_apache_puts(const char *s);
  extern PHPAPI void php3_apache_putc(char c);
+ extern PHPAPI int php3_apache_phpwrite(void *buf, int size);
  # if !defined(COMPILE_DL)
  # define PUTS(s) php3_apache_puts(s)
  # define PUTC(c) php3_apache_putc(c)
! # define PHPWRITE(a,n) php3_apache_phpwrite((a),(n))
  # endif
  #endif
  

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>