~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to ext/standard/php_fopen_wrapper.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
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
   +----------------------------------------------------------------------+
 
19
 */
 
20
/* $Id: php_fopen_wrapper.c,v 1.44 2004/04/19 17:41:39 wez Exp $ */
 
21
 
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#if HAVE_UNISTD_H
 
25
#include <unistd.h>
 
26
#endif
 
27
 
 
28
#include "php.h"
 
29
#include "php_globals.h"
 
30
#include "php_standard.h"
 
31
#include "php_fopen_wrappers.h"
 
32
#include "SAPI.h"
 
33
 
 
34
static size_t php_stream_output_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
 
35
{
 
36
        PHPWRITE(buf, count);
 
37
        return count;
 
38
}
 
39
 
 
40
static size_t php_stream_output_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
 
41
{
 
42
        stream->eof = 1;
 
43
        return 0;
 
44
}
 
45
 
 
46
static int php_stream_output_close(php_stream *stream, int close_handle TSRMLS_DC)
 
47
{
 
48
        return 0;
 
49
}
 
50
 
 
51
static int php_stream_output_flush(php_stream *stream TSRMLS_DC)
 
52
{
 
53
        sapi_flush(TSRMLS_C);
 
54
        return 0;
 
55
}
 
56
 
 
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,
 
62
        "Output",
 
63
        NULL, /* seek */
 
64
        NULL, /* cast */
 
65
        NULL, /* stat */
 
66
        NULL  /* set_option */
 
67
};
 
68
 
 
69
static size_t php_stream_input_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
 
70
{
 
71
        return -1;
 
72
}
 
73
 
 
74
static size_t php_stream_input_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
 
75
{
 
76
        size_t read_bytes = 0;
 
77
        if(!stream->eof) {
 
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) {
 
81
                                stream->eof = 1;
 
82
                        } else {
 
83
                                read_bytes = count;
 
84
                        }
 
85
                        if(read_bytes) {
 
86
                                memcpy(buf, SG(request_info).raw_post_data + stream->position, read_bytes);
 
87
                        }
 
88
                } else if(sapi_module.read_post) {
 
89
                        read_bytes = sapi_module.read_post(buf, count TSRMLS_CC);
 
90
                        if(read_bytes <= 0){
 
91
                                stream->eof = 1;
 
92
                                read_bytes = 0;
 
93
                        }
 
94
                } else {
 
95
                        stream->eof = 1;
 
96
                }
 
97
        }
 
98
 
 
99
        SG(read_post_bytes) += read_bytes;
 
100
    return read_bytes;
 
101
}
 
102
 
 
103
static int php_stream_input_close(php_stream *stream, int close_handle TSRMLS_DC)
 
104
{
 
105
        return 0;
 
106
}
 
107
 
 
108
static int php_stream_input_flush(php_stream *stream TSRMLS_DC)
 
109
{
 
110
        return -1;
 
111
}
 
112
 
 
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,
 
118
        "Input",
 
119
        NULL, /* seek */
 
120
        NULL, /* cast */
 
121
        NULL, /* stat */
 
122
        NULL  /* set_option */
 
123
};
 
124
 
 
125
static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, int read_chain, int write_chain TSRMLS_DC) {
 
126
        char *p, *token;
 
127
        php_stream_filter *temp_filter;
 
128
 
 
129
        p = php_strtok_r(filterlist, "|", &token);
 
130
        while (p) {
 
131
                if (read_chain) {
 
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);
 
134
                        } else {
 
135
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create filter (%s)\n", p);
 
136
                        }
 
137
                }
 
138
                if (write_chain) {
 
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);
 
141
                        } else {
 
142
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create filter (%s)\n", p);
 
143
                        }
 
144
                }
 
145
                p = php_strtok_r(NULL, "|", &token);
 
146
        }
 
147
}
 
148
 
 
149
 
 
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)
 
151
{
 
152
        int fd = -1;
 
153
        int mode_rw = 0;
 
154
        php_stream * stream = NULL;
 
155
        char *p, *token, *pathdup;
 
156
 
 
157
        if (!strncasecmp(path, "php://", 6))
 
158
                path += 6;
 
159
        
 
160
        if (!strcasecmp(path, "output")) {
 
161
                return php_stream_alloc(&php_stream_output_ops, NULL, 0, "wb");
 
162
        }
 
163
        
 
164
        if (!strcasecmp(path, "input")) {
 
165
                return php_stream_alloc(&php_stream_input_ops, NULL, 0, "rb");
 
166
        }  
 
167
        
 
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;
 
178
                }
 
179
                if (strchr(mode, 'w') || strchr(mode, '+') || strchr(mode, 'a')) {
 
180
                        mode_rw |= PHP_STREAM_FILTER_WRITE;
 
181
                }
 
182
                pathdup = estrndup(path + 6, strlen(path + 6));
 
183
                p = strstr(pathdup, "/resource=");
 
184
                if (!p) {
 
185
                        php_error_docref(NULL TSRMLS_CC, E_ERROR, "No URL resource specified.");
 
186
                        efree(pathdup);
 
187
                        return NULL;
 
188
                }
 
189
                if (!(stream = php_stream_open_wrapper(p + 10, mode, options, opened_path))) {
 
190
                        efree(pathdup);
 
191
                        return NULL;
 
192
                }
 
193
 
 
194
                *p = '\0';
 
195
 
 
196
                p = php_strtok_r(pathdup + 1, "/", &token);
 
197
                while (p) {
 
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);
 
202
                        } else {
 
203
                                php_stream_apply_filter_list(stream, p, mode_rw & PHP_STREAM_FILTER_READ, mode_rw & PHP_STREAM_FILTER_WRITE TSRMLS_CC);
 
204
                        }
 
205
                        p = php_strtok_r(NULL, "/", &token);
 
206
                }
 
207
                efree(pathdup);
 
208
 
 
209
                return stream;
 
210
        } else {
 
211
                /* invalid php://thingy */
 
212
                return NULL;
 
213
        }
 
214
        
 
215
        /* must be stdin, stderr or stdout */
 
216
        if (fd == -1)   {
 
217
                /* failed to dup */
 
218
                return NULL;
 
219
        }
 
220
        
 
221
        stream = php_stream_fopen_from_fd(fd, mode, NULL);
 
222
        if (stream == NULL) {
 
223
                close(fd);
 
224
        }
 
225
 
 
226
        return stream;
 
227
}
 
228
 
 
229
static php_stream_wrapper_ops php_stdio_wops = {
 
230
        php_stream_url_wrap_php,
 
231
        NULL, /* close */
 
232
        NULL, /* fstat */
 
233
        NULL, /* stat */
 
234
        NULL, /* opendir */
 
235
        "PHP",
 
236
        NULL, /* unlink */
 
237
        NULL, /* rename */
 
238
        NULL, /* mkdir */
 
239
        NULL  /* rmdir */
 
240
};
 
241
 
 
242
php_stream_wrapper php_stream_php_wrapper =     {
 
243
        &php_stdio_wops,
 
244
        NULL,
 
245
        0, /* is_url */
 
246
};
 
247
 
 
248
 
 
249
/*
 
250
 * Local variables:
 
251
 * tab-width: 4
 
252
 * c-basic-offset: 4
 
253
 * End:
 
254
 * vim600: sw=4 ts=4 fdm=marker
 
255
 * vim<600: sw=4 ts=4
 
256
 */