php-src/output.c

233 lines
5.2 KiB
C
Raw Normal View History

1999-04-07 21:05:13 +00:00
/*
+----------------------------------------------------------------------+
1999-07-16 13:13:16 +00:00
| PHP version 4.0 |
1999-04-07 21:05:13 +00:00
+----------------------------------------------------------------------+
1999-07-16 13:13:16 +00:00
| Copyright (c) 1997, 1998, 1999 The PHP Group |
1999-04-07 21:05:13 +00:00
+----------------------------------------------------------------------+
1999-07-16 13:13:16 +00:00
| This source file is subject to version 2.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
1999-04-07 21:05:13 +00:00
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "ext/standard/head.h"
#include "ext/session/php_session.h"
#include "SAPI.h"
1999-04-07 21:05:13 +00:00
/* output functions */
PHPAPI int (*php_body_write)(const char *str, uint str_length); /* string output */
PHPAPI int (*php_header_write)(const char *str, uint str_length); /* unbuffer string output */
static int php_ub_body_write(const char *str, uint str_length);
static int php_ub_body_write_no_header(const char *str, uint str_length);
static int php_b_body_write(const char *str, uint str_length);
1999-04-07 21:05:13 +00:00
/* output buffering */
static char *ob_buffer;
static uint ob_buffer_size;
static uint ob_block_size;
static uint ob_text_length;
static void php_ob_init(uint initial_size, uint block_size);
static void php_ob_destroy();
static void php_ob_append(const char *text, uint text_length);
static void php_ob_prepend(const char *text, uint text_length);
static inline void php_ob_send();
1999-04-07 21:05:13 +00:00
/*
* Main
*/
1999-08-24 23:12:50 +00:00
/* Start output layer */
PHPAPI void php_output_startup()
1999-04-07 21:05:13 +00:00
{
ob_buffer = NULL;
php_body_write = php_ub_body_write;
php_header_write = sapi_module.ub_write;
1999-04-07 21:05:13 +00:00
}
1999-08-24 23:12:50 +00:00
/* Start output buffering */
void php_start_ob_buffering()
1999-04-07 21:05:13 +00:00
{
php_ob_init(4096, 1024);
php_body_write = php_b_body_write;
1999-04-07 21:05:13 +00:00
}
1999-08-24 23:12:50 +00:00
/* End output buffering */
void php_end_ob_buffering(int send_buffer)
1999-04-07 21:05:13 +00:00
{
1999-05-05 19:53:15 +00:00
SLS_FETCH();
1999-04-07 21:05:13 +00:00
if (!ob_buffer) {
return;
}
if (SG(headers_sent) && !SG(request_info).headers_only) {
php_body_write = php_ub_body_write_no_header;
1999-05-05 19:53:15 +00:00
} else {
php_body_write = php_ub_body_write;
1999-05-05 19:53:15 +00:00
}
1999-04-07 21:05:13 +00:00
if (send_buffer) {
php_ob_send();
1999-04-07 21:05:13 +00:00
}
php_ob_destroy();
1999-04-07 21:05:13 +00:00
}
/*
* Output buffering - implementation
*/
static inline void php_ob_allocate()
1999-04-07 21:05:13 +00:00
{
if (ob_buffer_size<ob_text_length) {
while ((ob_buffer_size+=ob_block_size) < ob_text_length);
ob_buffer = (char *) erealloc(ob_buffer, ob_buffer_size+1);
}
}
static void php_ob_init(uint initial_size, uint block_size)
1999-04-07 21:05:13 +00:00
{
if (ob_buffer) {
return;
}
ob_block_size = block_size;
ob_buffer_size = initial_size;
ob_buffer = (char *) emalloc(initial_size+1);
ob_text_length = 0;
}
static void php_ob_destroy()
1999-04-07 21:05:13 +00:00
{
if (ob_buffer) {
efree(ob_buffer);
ob_buffer = NULL;
}
}
static void php_ob_append(const char *text, uint text_length)
1999-04-07 21:05:13 +00:00
{
char *target;
int original_ob_text_length=ob_text_length;
ob_text_length += text_length;
php_ob_allocate();
1999-04-07 21:05:13 +00:00
target = ob_buffer+original_ob_text_length;
memcpy(target, text, text_length);
target[text_length]=0;
}
static void php_ob_prepend(const char *text, uint text_length)
1999-04-07 21:05:13 +00:00
{
char *p, *start;
ob_text_length += text_length;
php_ob_allocate();
1999-04-07 21:05:13 +00:00
/* php_ob_allocate() may change ob_buffer, so we can't initialize p&start earlier */
1999-04-07 21:05:13 +00:00
p = ob_buffer+ob_text_length;
start = ob_buffer;
while (--p>=start) {
p[text_length] = *p;
}
memcpy(ob_buffer, text, text_length);
ob_buffer[ob_text_length]=0;
}
static inline void php_ob_send()
1999-04-07 21:05:13 +00:00
{
/* header_write is a simple, unbuffered output function */
php_body_write(ob_buffer, ob_text_length);
1999-04-07 21:05:13 +00:00
}
1999-08-24 23:12:50 +00:00
/* Return the current output buffer */
int php_ob_get_buffer(pval *p)
1999-04-07 21:05:13 +00:00
{
if (!ob_buffer) {
return FAILURE;
}
p->type = IS_STRING;
p->value.str.val = estrndup(ob_buffer, ob_text_length);
p->value.str.len = ob_text_length;
return SUCCESS;
}
/*
* Wrapper functions - implementation
*/
/* buffered output function */
static int php_b_body_write(const char *str, uint str_length)
1999-04-07 21:05:13 +00:00
{
php_ob_append(str, str_length);
1999-04-07 21:05:13 +00:00
return str_length;
}
static int php_ub_body_write_no_header(const char *str, uint str_length)
1999-05-05 19:53:15 +00:00
{
char *newstr = NULL;
1999-09-13 20:03:56 +00:00
uint new_length=0;
int result;
session_adapt_uris(str, str_length, &newstr, &new_length);
if (newstr) {
str = newstr;
str_length = new_length;
}
result = php_header_write(str, str_length);
if (newstr) {
free(newstr);
}
return result;
1999-05-05 19:53:15 +00:00
}
static int php_ub_body_write(const char *str, uint str_length)
1999-04-07 21:05:13 +00:00
{
int result = 0;
SLS_FETCH();
if (SG(request_info).headers_only) {
1999-04-07 21:05:13 +00:00
zend_bailout();
}
if (php3_header()) {
php_body_write = php_ub_body_write_no_header;
result = php_ub_body_write_no_header(str, str_length);
1999-04-07 21:05:13 +00:00
}
return result;
1999-04-07 21:05:13 +00:00
}
/*
* HEAD support
*/
void set_header_request(int value)
{
/* deprecated */
1999-04-07 21:05:13 +00:00
}