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

« back to all changes in this revision

Viewing changes to sapi/webjames/webjames.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
   | Author: Alex Waugh <alex@alexwaugh.com>                              |
 
16
   +----------------------------------------------------------------------+
 
17
*/
 
18
 
 
19
 
 
20
#include "php.h"
 
21
#include "SAPI.h"
 
22
#include "php_main.h"
 
23
#include "php_variables.h"
 
24
 
 
25
#define WEBJAMES_PHP_ONLY
 
26
#include "php_webjames.h"
 
27
 
 
28
#include <unixlib/local.h>
 
29
 
 
30
#define WEBJAMES_SAPI_VERSION "1.0.0"
 
31
 
 
32
typedef struct {
 
33
        struct connection *conn; /*structure holding all the details of the current request*/
 
34
        int bodyread; /*amount of POST body read*/
 
35
        closefn oldclose; /*function to call to close the connection*/
 
36
} php_webjames_globals;
 
37
 
 
38
static php_webjames_globals webjames_globals;
 
39
 
 
40
#define WG(v) (webjames_globals.v)
 
41
 
 
42
static int sapi_webjames_ub_write(const char *str, uint str_length TSRMLS_DC)
 
43
/*unbuffered write - send data straight out to socket*/
 
44
{
 
45
        int bytes;
 
46
        
 
47
        bytes = webjames_writebuffer(WG(conn),str,str_length);
 
48
        if (bytes<0) {
 
49
                PG(connection_status) = PHP_CONNECTION_ABORTED;
 
50
                if (!PG(ignore_user_abort)) {
 
51
                        zend_bailout();
 
52
                }
 
53
        }
 
54
        return bytes;
 
55
}
 
56
 
 
57
static void sapi_webjames_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC)
 
58
/*send an HTTP header*/
 
59
{
 
60
        if (WG(conn)->flags.outputheaders) {
 
61
                if (sapi_header)
 
62
                        webjames_writebuffer(WG(conn), sapi_header->header, sapi_header->header_len);
 
63
                webjames_writestring(WG(conn), "\r\n");
 
64
        }
 
65
}
 
66
 
 
67
static int sapi_webjames_read_post(char *buffer, uint count_bytes TSRMLS_DC)
 
68
/*read some of the post data*/
 
69
{
 
70
        if (WG(conn)->body==NULL) return 0;
 
71
        if (count_bytes+WG(bodyread)>WG(conn)->bodysize) count_bytes=WG(conn)->bodysize-WG(bodyread);
 
72
        memcpy(buffer, WG(conn)->body+WG(bodyread), count_bytes);
 
73
        WG(bodyread)+=count_bytes;
 
74
        return count_bytes;
 
75
}
 
76
 
 
77
static char *sapi_webjames_read_cookies(TSRMLS_D)
 
78
{
 
79
        return WG(conn)->cookie;
 
80
}
 
81
 
 
82
#define BUF_SIZE 512
 
83
#define ADD_STRING(name,string)\
 
84
        php_register_variable(name, string, track_vars_array TSRMLS_CC)
 
85
 
 
86
#define ADD_NUM(name,field) {\
 
87
        snprintf(buf, BUF_SIZE, "%d", WG(conn)->field);\
 
88
        php_register_variable(name, buf, track_vars_array TSRMLS_CC);\
 
89
}
 
90
 
 
91
#define ADD_FIELD(name, field) \
 
92
        if (WG(conn)->field) { \
 
93
                php_register_variable(name, WG(conn)->field, track_vars_array TSRMLS_CC); \
 
94
        }
 
95
 
 
96
static void sapi_webjames_register_variables(zval *track_vars_array TSRMLS_DC)
 
97
{
 
98
        char buf[BUF_SIZE + 1];
 
99
 
 
100
        buf[BUF_SIZE] = '\0';
 
101
 
 
102
        ADD_STRING("SERVER_SOFTWARE", configuration.server);
 
103
        ADD_STRING("SERVER_NAME", configuration.serverip);
 
104
        ADD_FIELD("SERVER_PROTOCOL", protocol);
 
105
        ADD_NUM("SERVER_PORT", port);
 
106
        ADD_STRING("SERVER_ADMIN",configuration.webmaster);
 
107
        ADD_STRING("GATEWAY_INTERFACE", "CGI/1.1");
 
108
        ADD_STRING("DOCUMENT_ROOT", configuration.site);
 
109
 
 
110
        ADD_FIELD("REQUEST_METHOD", methodstr);
 
111
        ADD_FIELD("REQUEST_URI", requesturi);
 
112
        ADD_STRING("PATH_TRANSLATED", SG(request_info).path_translated);
 
113
        ADD_FIELD("SCRIPT_NAME", uri);
 
114
        ADD_FIELD("PHP_SELF", uri);
 
115
        ADD_FIELD("QUERY_STRING", args);
 
116
 
 
117
        
 
118
        snprintf(buf, BUF_SIZE, "%d.%d.%d.%d", WG(conn)->ipaddr[0], WG(conn)->ipaddr[1], WG(conn)->ipaddr[2], WG(conn)->ipaddr[3]);
 
119
        ADD_STRING("REMOTE_ADDR", buf);
 
120
        if (WG(conn)->dnsstatus == DNS_OK) ADD_FIELD("REMOTE_HOST", host);
 
121
 
 
122
        if ((WG(conn)->method == METHOD_POST) || (WG(conn)->method == METHOD_PUT)) {
 
123
                ADD_NUM("CONTENT_LENGTH", bodysize);
 
124
                ADD_FIELD("CONTENT_TYPE", type);
 
125
        }
 
126
 
 
127
        if ((WG(conn)->method == METHOD_PUT) || (WG(conn)->method == METHOD_DELETE)) ADD_FIELD("ENTITY_PATH", requesturi);
 
128
 
 
129
        if (WG(conn)->pwd) {
 
130
                ADD_STRING("AUTH_TYPE", "basic");
 
131
                ADD_FIELD("REMOTE_USER", authorization);
 
132
        }
 
133
 
 
134
        ADD_FIELD("HTTP_COOKIE", cookie);
 
135
        ADD_FIELD("HTTP_USER_AGENT", useragent);
 
136
        ADD_FIELD("HTTP_REFERER", referer);
 
137
        ADD_FIELD("HTTP_ACCEPT", accept);
 
138
        ADD_FIELD("HTTP_ACCEPT_LANGUAGE", acceptlanguage);
 
139
        ADD_FIELD("HTTP_ACCEPT_CHARSET", acceptcharset);
 
140
        ADD_FIELD("HTTP_ACCEPT_ENCODING", acceptencoding);
 
141
}
 
142
 
 
143
static void webjames_module_main(TSRMLS_D)
 
144
{
 
145
        zend_file_handle file_handle;
 
146
        FILE *fp=NULL;
 
147
        char *path;
 
148
 
 
149
        /* Convert filename to Unix format*/
 
150
        __riscosify_control|=__RISCOSIFY_DONT_CHECK_DIR;
 
151
        path = __unixify(WG(conn)->filename,0,NULL,1024,0);
 
152
        if (path) SG(request_info).path_translated = estrdup(path);
 
153
 
 
154
        SG(request_info).query_string = WG(conn)->args;
 
155
        SG(request_info).request_uri = WG(conn)->requesturi;
 
156
        SG(request_info).request_method = WG(conn)->methodstr;
 
157
        if (WG(conn)->method==METHOD_HEAD) {
 
158
                SG(request_info).headers_only = 1;
 
159
        } else {
 
160
                SG(request_info).headers_only = 0;
 
161
        }
 
162
        SG(sapi_headers).http_response_code = 200;
 
163
        SG(request_info).content_type = WG(conn)->type;
 
164
        SG(request_info).content_length = WG(conn)->bodysize;
 
165
 
 
166
        SG(request_info).auth_user = NULL;
 
167
        SG(request_info).auth_password = NULL;
 
168
        if (WG(conn)->authorization) {
 
169
                char *colon=strchr(WG(conn)->authorization,':');
 
170
                if (colon) {
 
171
                        SG(request_info).auth_user = emalloc(colon-WG(conn)->authorization+1);
 
172
                        if (SG(request_info).auth_user) {
 
173
                                memcpy(SG(request_info).auth_user,WG(conn)->authorization,colon-WG(conn)->authorization);
 
174
                                SG(request_info).auth_user[colon-WG(conn)->authorization]='\0';
 
175
                                SG(request_info).auth_password = estrdup(colon+1);
 
176
                        }
 
177
                }
 
178
        }
 
179
 
 
180
        /*ensure that syslog calls get logged separately from WebJames' main log */
 
181
        openlog("PHP",0,0);
 
182
 
 
183
        file_handle.type = ZEND_HANDLE_FILENAME;
 
184
        file_handle.filename = SG(request_info).path_translated;
 
185
        file_handle.free_filename = 0;
 
186
        file_handle.opened_path = NULL;
 
187
 
 
188
        if (php_request_startup(TSRMLS_C) == FAILURE) {
 
189
                return;
 
190
        }
 
191
        
 
192
        php_execute_script(&file_handle TSRMLS_CC);
 
193
        php_request_shutdown(NULL);
 
194
}
 
195
 
 
196
static void webjames_php_close(struct connection *conn, int force)
 
197
/*called by webjames if it wants to close the connection*/
 
198
{
 
199
        TSRMLS_FETCH();
 
200
 
 
201
        php_request_shutdown(NULL);
 
202
        WG(oldclose)(conn,force);
 
203
}
 
204
 
 
205
void webjames_php_request(struct connection *conn)
 
206
/*called by WebJames to start handler*/
 
207
{
 
208
        TSRMLS_FETCH();
 
209
 
 
210
        WG(conn) = conn;
 
211
        WG(bodyread) = 0;
 
212
        WG(oldclose) = conn->close;
 
213
        conn->close=webjames_php_close;
 
214
 
 
215
        webjames_module_main(TSRMLS_C);
 
216
 
 
217
        WG(oldclose)(WG(conn), 0);
 
218
}
 
219
 
 
220
static void php_info_webjames(ZEND_MODULE_INFO_FUNC_ARGS)
 
221
{
 
222
        php_info_print_table_start();
 
223
        php_info_print_table_row(2, "SAPI module version", WEBJAMES_SAPI_VERSION);
 
224
        php_info_print_table_row(2, "WebJames version", WEBJAMES_VERSION " (" WEBJAMES_DATE ")");
 
225
        php_info_print_table_end();
 
226
}
 
227
 
 
228
static zend_module_entry php_webjames_module = {
 
229
#if ZEND_MODULE_API_NO >= 20010901
 
230
    STANDARD_MODULE_HEADER,
 
231
#endif
 
232
  "WebJames",
 
233
  NULL,
 
234
  NULL,
 
235
  NULL,
 
236
  NULL,
 
237
  NULL,
 
238
  php_info_webjames,
 
239
#if ZEND_MODULE_API_NO >= 20010901
 
240
  WEBJAMES_SAPI_VERSION,          
 
241
#endif
 
242
  STANDARD_MODULE_PROPERTIES
 
243
};
 
244
 
 
245
 
 
246
static int php_webjames_startup(sapi_module_struct *sapi_module)
 
247
{
 
248
  if(php_module_startup(sapi_module, &php_webjames_module, 1) == FAILURE) {
 
249
    return FAILURE;
 
250
  } else {
 
251
    return SUCCESS;
 
252
  }
 
253
}
 
254
 
 
255
static sapi_module_struct sapi_module = {
 
256
        "webjames",                             /* name */
 
257
        "WebJames",                             /* pretty name */
 
258
 
 
259
        php_webjames_startup,                   /* startup */
 
260
        php_module_shutdown_wrapper,            /* shutdown */
 
261
 
 
262
        NULL,                                                                   /* activate */
 
263
        NULL,                                                                   /* deactivate */
 
264
 
 
265
        sapi_webjames_ub_write,                 /* unbuffered write */
 
266
        NULL,                                   /* flush */
 
267
        NULL,                                                                   /* get uid */
 
268
        NULL,                                                                   /* getenv */
 
269
 
 
270
        php_error,                              /* error handler */
 
271
 
 
272
        NULL,                                   /* header handler */
 
273
        NULL,                                                           /* send headers handler */
 
274
        sapi_webjames_send_header,              /* send header handler */
 
275
        sapi_webjames_read_post,                /* read POST data */
 
276
        sapi_webjames_read_cookies,             /* read Cookies */
 
277
 
 
278
        sapi_webjames_register_variables,       /* register server variables */
 
279
        NULL,                                                                   /* Log message */
 
280
 
 
281
        STANDARD_SAPI_MODULE_PROPERTIES
 
282
};
 
283
 
 
284
int webjames_php_init(void)
 
285
/*called when WebJames initialises*/
 
286
{
 
287
        TSRMLS_FETCH();
 
288
        if (strcmp(configuration.webjames_h_revision,WEBJAMES_H_REVISION)!=0) {
 
289
                /*This file was compiled against a different revision of
 
290
                  webjames.h than webjames was, which could be bad news*/
 
291
                webjames_writelog(0,"PHP module is compiled for WebJames (%s) and was linked with a different version (%s)",WEBJAMES_H_REVISION,configuration.webjames_h_revision);
 
292
                return 0; /*failed to initialise*/
 
293
        }
 
294
        sapi_startup(&sapi_module);
 
295
        sapi_module.startup(&sapi_module);
 
296
        SG(server_context) = (void *) 1;
 
297
        return 1; /*initialised correctly*/
 
298
}
 
299
 
 
300
void webjames_php_shutdown(void)
 
301
/*called when WebJames is about to quit*/
 
302
{
 
303
        sapi_module.shutdown(&sapi_module);
 
304
        sapi_shutdown();
 
305
}