summaryrefslogtreecommitdiff
path: root/output.c
blob: a748ed5cbbcd36162cb331bcc66b9f7362726335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* 
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999 The PHP Group                         |
   +----------------------------------------------------------------------+
   | 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.               |
   +----------------------------------------------------------------------+
   | 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"

/* 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);

/* 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();


/*
 * Main
 */

/* Start output layer */
PHPAPI void php_output_startup()
{
	ob_buffer = NULL;
	php_body_write = php_ub_body_write;
	php_header_write = sapi_module.ub_write;
}


/* Start output buffering */
void php_start_ob_buffering()
{
	php_ob_init(4096, 1024);
	php_body_write = php_b_body_write;
}


/* End output buffering */
void php_end_ob_buffering(int send_buffer)
{
	SLS_FETCH();

	if (!ob_buffer) {
		return;
	}
	if (SG(headers_sent) && !SG(request_info).headers_only) {
		php_body_write = php_ub_body_write_no_header;
	} else {
		php_body_write = php_ub_body_write;
	}
	if (send_buffer) {
		php_ob_send();
	}
	php_ob_destroy();
}


/*
 * Output buffering - implementation
 */

static inline void php_ob_allocate()
{
	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)
{
	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()
{
	if (ob_buffer) {
		efree(ob_buffer);
		ob_buffer = NULL;
	}
}


static void php_ob_append(const char *text, uint text_length)
{
	char *target;
	int original_ob_text_length=ob_text_length;

	ob_text_length += text_length;
	php_ob_allocate();
	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)
{
	char *p, *start;

	ob_text_length += text_length;
	php_ob_allocate();

	/* php_ob_allocate() may change ob_buffer, so we can't initialize p&start earlier */
	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()
{
	/* header_write is a simple, unbuffered output function */
	php_body_write(ob_buffer, ob_text_length);
}


/* Return the current output buffer */
int php_ob_get_buffer(pval *p)
{
	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)
{
	php_ob_append(str, str_length);
	return str_length;
}


static int php_ub_body_write_no_header(const char *str, uint str_length)
{
	char *newstr = NULL;
	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;
}


static int php_ub_body_write(const char *str, uint str_length)
{
	int result = 0;
	SLS_FETCH();

	if (SG(request_info).headers_only) {
		zend_bailout();
	}
	if (php3_header()) {
		php_body_write = php_ub_body_write_no_header;
		result = php_ub_body_write_no_header(str, str_length);
	}

	return result;
}


/*
 * HEAD support
 */

void set_header_request(int value)
{
	/* deprecated */
}