2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 of the PHP license, |
8
| that is bundled with this package in the file LICENSE, and is |
9
| available through the world-wide-web at the following url: |
10
| http://www.php.net/license/3_0.txt. |
11
| If you did not receive a copy of the PHP license and are unable to |
12
| obtain it through the world-wide-web, please send a note to |
13
| license@php.net so we can mail you a copy immediately. |
14
+----------------------------------------------------------------------+
15
| Authors: Rasmus Lerdorf <rasmus@php.net> |
16
| Jim Winstead <jimw@php.net> |
17
| Hartmut Holzgraefe <hholzgra@php.net> |
18
+----------------------------------------------------------------------+
20
/* $Id: php_fopen_wrapper.c,v 1.44 2004/04/19 17:41:39 wez Exp $ */
29
#include "php_globals.h"
30
#include "php_standard.h"
31
#include "php_fopen_wrappers.h"
34
static size_t php_stream_output_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
40
static size_t php_stream_output_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
46
static int php_stream_output_close(php_stream *stream, int close_handle TSRMLS_DC)
51
static int php_stream_output_flush(php_stream *stream TSRMLS_DC)
57
php_stream_ops php_stream_output_ops = {
58
php_stream_output_write,
59
php_stream_output_read,
60
php_stream_output_close,
61
php_stream_output_flush,
69
static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
74
static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
76
size_t read_bytes = 0;
78
if(SG(request_info).raw_post_data) { /* data has already been read by a post handler */
79
read_bytes = SG(request_info).raw_post_data_length - stream->position;
80
if(read_bytes <= count) {
86
memcpy(buf, SG(request_info).raw_post_data + stream->position, read_bytes);
88
} else if(sapi_module.read_post) {
89
read_bytes = sapi_module.read_post(buf, count TSRMLS_CC);
99
SG(read_post_bytes) += read_bytes;
103
static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC)
108
static int php_stream_input_flush(php_stream *stream TSRMLS_DC)
113
php_stream_ops php_stream_input_ops = {
114
php_stream_input_write,
115
php_stream_input_read,
116
php_stream_input_close,
117
php_stream_input_flush,
122
NULL /* set_option */
125
static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, int read_chain, int write_chain TSRMLS_DC) {
127
php_stream_filter *temp_filter;
129
p = php_strtok_r(filterlist, "|", &token);
132
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream) TSRMLS_CC))) {
133
php_stream_filter_append(&stream->readfilters, temp_filter);
135
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create filter (%s)\n", p);
139
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream) TSRMLS_CC))) {
140
php_stream_filter_append(&stream->writefilters, temp_filter);
142
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create filter (%s)\n", p);
145
p = php_strtok_r(NULL, "|", &token);
150
php_stream * php_stream_url_wrap_php(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC)
154
php_stream * stream = NULL;
155
char *p, *token, *pathdup;
157
if (!strncasecmp(path, "php://", 6))
160
if (!strcasecmp(path, "output")) {
161
return php_stream_alloc(&php_stream_output_ops, NULL, 0, "wb");
164
if (!strcasecmp(path, "input")) {
165
return php_stream_alloc(&php_stream_input_ops, NULL, 0, "rb");
168
if (!strcasecmp(path, "stdin")) {
169
fd = !strcmp(sapi_module.name, "cli") ? STDIN_FILENO : dup(STDIN_FILENO);
170
} else if (!strcasecmp(path, "stdout")) {
171
fd = !strcmp(sapi_module.name, "cli") ? STDOUT_FILENO : dup(STDOUT_FILENO);
172
} else if (!strcasecmp(path, "stderr")) {
173
fd = !strcmp(sapi_module.name, "cli") ? STDERR_FILENO : dup(STDERR_FILENO);
174
} else if (!strncasecmp(path, "filter/", 7)) {
175
/* Save time/memory when chain isn't specified */
176
if (strchr(mode, 'r') || strchr(mode, '+')) {
177
mode_rw |= PHP_STREAM_FILTER_READ;
179
if (strchr(mode, 'w') || strchr(mode, '+') || strchr(mode, 'a')) {
180
mode_rw |= PHP_STREAM_FILTER_WRITE;
182
pathdup = estrndup(path + 6, strlen(path + 6));
183
p = strstr(pathdup, "/resource=");
185
php_error_docref(NULL TSRMLS_CC, E_ERROR, "No URL resource specified.");
189
if (!(stream = php_stream_open_wrapper(p + 10, mode, options, opened_path))) {
196
p = php_strtok_r(pathdup + 1, "/", &token);
198
if (!strncasecmp(p, "read=", 5)) {
199
php_stream_apply_filter_list(stream, p + 5, 1, 0 TSRMLS_CC);
200
} else if (!strncasecmp(p, "write=", 6)) {
201
php_stream_apply_filter_list(stream, p + 6, 0, 1 TSRMLS_CC);
203
php_stream_apply_filter_list(stream, p, mode_rw & PHP_STREAM_FILTER_READ, mode_rw & PHP_STREAM_FILTER_WRITE TSRMLS_CC);
205
p = php_strtok_r(NULL, "/", &token);
211
/* invalid php://thingy */
215
/* must be stdin, stderr or stdout */
221
stream = php_stream_fopen_from_fd(fd, mode, NULL);
222
if (stream == NULL) {
229
static php_stream_wrapper_ops php_stdio_wops = {
230
php_stream_url_wrap_php,
242
php_stream_wrapper php_stream_php_wrapper = {
254
* vim600: sw=4 ts=4 fdm=marker