summaryrefslogtreecommitdiff
path: root/sapi/servlet/servlet.c
blob: aada017b2bef03021165bcc7cf0c14c933b34aea (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
   +----------------------------------------------------------------------+
   | 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.               |
   +----------------------------------------------------------------------+
   | Author: Sam Ruby (rubys@us.ibm.com)                                  |
   +----------------------------------------------------------------------+
*/

#include <jni.h>

#include "dl/phpdl.h"
#include "php.h"
#include "php_globals.h"

#include "SAPI.h"

#include <stdio.h>
#include "php.h"
#if WIN32|WINNT
#include "win32/time.h"
#include "win32/signal.h"
#include <process.h>
#else
#include "build-defs.h"
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
#if HAVE_SETLOCALE
#include <locale.h>
#endif
#include "zend.h"
#include "php_ini.h"
#include "php_globals.h"
#include "main.h"
#include "fopen-wrappers.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/php_dir.h"
#include "snprintf.h"
#if WIN32|WINNT
#include <io.h>
#include <fcntl.h>
#include "win32/syslog.h"
#include "win32/php_registry.h"
#else
#include <syslog.h>
#endif

#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_highlight.h"
#include "zend_indent.h"

#if WIN32|WINNT || !defined(HAVE_GETOPT)
#include "php_getopt.h"
#endif

PHPAPI extern char *php3_ini_path;

JNIEXPORT void JNICALL Java_net_php_reflect_setEnv
  (JNIEnv *newJenv, jclass self);

PHPAPI extern char *optarg;
PHPAPI extern int optind;

typedef struct {
	JNIEnv *jenv;
	jobject servlet;
	char *cookies;
} servlet_request;

/***************************************************************************/

/*
 * JNI convenience utilities
 */

#define SETSTRING(target, source) \
	{ const char *UTFString; \
	if (source) { \
	UTFString = (*jenv)->GetStringUTFChars(jenv, source, 0); \
	target = estrdup(UTFString); \
	(*jenv)->ReleaseStringUTFChars(jenv, source, UTFString); \
	} else { \
	target = 0; \
	} }

#define FREESTRING(target) \
	{ if (target) { efree(target); target=0; } }

void ThrowIOException (JNIEnv *jenv, char *msg) {
	jclass iox = (*jenv)->FindClass (jenv, "java/io/IOException");
	(*jenv)->ThrowNew (jenv, iox, (msg?msg:"null") );
	printf("IOException: %s\n", msg);
}

void ThrowServletException (JNIEnv *jenv, char *msg) {
	jclass sx = (*jenv)->FindClass (jenv, "javax/servlet/ServletException");
	(*jenv)->ThrowNew (jenv, sx, msg);
	printf("ServletException: %s\n", msg);
}

/***************************************************************************/

/*
 * sapi callbacks
 */

static int zend_servlet_ub_write(const char *str, uint str_length)
{
	SLS_FETCH();
	if (!SG(server_context)) {
		fprintf(stderr, str);
		return 0;
	}

	{
	JNIEnv *jenv = ((servlet_request*)SG(server_context))->jenv;
	jobject servlet = ((servlet_request*)SG(server_context))->servlet;

	jclass servletClass = (*jenv)->GetObjectClass(jenv, servlet);
	jmethodID write = (*jenv)->GetMethodID(jenv, servletClass, "write",
		"(Ljava/lang/String;)V");
	jstring arg=(*jenv)->NewStringUTF(jenv, str);
	(*jenv)->CallVoidMethod(jenv, servlet, write, arg);
	(*jenv)->DeleteLocalRef(jenv, arg);
	return str_length;
	}
}


static void sapi_servlet_send_header(sapi_header_struct *sapi_header, void *server_context)
{
	SLS_FETCH();
	if (!sapi_header) return;
	if (!SG(server_context)) return;

	{
	JNIEnv *jenv = ((servlet_request*)SG(server_context))->jenv;
	jobject servlet = ((servlet_request*)SG(server_context))->servlet;

	jclass servletClass = (*jenv)->GetObjectClass(jenv, servlet);
	jmethodID header = (*jenv)->GetMethodID(jenv, servletClass, "header",
		"(Ljava/lang/String;)V");
	jstring arg=(*jenv)->NewStringUTF(jenv, sapi_header->header);
	(*jenv)->CallVoidMethod(jenv, servlet, header, arg);
	(*jenv)->DeleteLocalRef(jenv, arg);
	}
}


static int sapi_servlet_read_post(char *buffer, uint count_bytes SLS_DC)
{
	JNIEnv *jenv = ((servlet_request*)SG(server_context))->jenv;
	jobject servlet = ((servlet_request*)SG(server_context))->servlet;

	jclass servletClass = (*jenv)->GetObjectClass(jenv, servlet);
	jmethodID readPost = (*jenv)->GetMethodID(jenv, servletClass, "readPost",
		"(I)Ljava/lang/String;");
	jstring post = (*jenv)->CallObjectMethod(jenv, servlet, readPost, 
		count_bytes);

	const char *postAsUTF = (*jenv)->GetStringUTFChars(jenv, post, 0);
	uint read_bytes=(*jenv)->GetStringLength(jenv, post);
	if (read_bytes>count_bytes) read_bytes=count_bytes;

	memcpy(buffer, postAsUTF, count_bytes);

	(*jenv)->ReleaseStringUTFChars(jenv, post, postAsUTF);

	return read_bytes;
}


static char *sapi_servlet_read_cookies(SLS_D)
{
	JNIEnv *jenv = ((servlet_request*)SG(server_context))->jenv;
	jobject servlet = ((servlet_request*)SG(server_context))->servlet;

	jclass servletClass = (*jenv)->GetObjectClass(jenv, servlet);
	jmethodID readCookies = (*jenv)->GetMethodID(jenv, servletClass, 
		"readCookies", "()Ljava/lang/String;");
	jstring cookies = (*jenv)->CallObjectMethod(jenv, servlet, readCookies);

	SETSTRING( ((servlet_request*)SG(server_context))->cookies, cookies);

	return ((servlet_request*)SG(server_context))->cookies;
}

/***************************************************************************/

/*
 * sapi maintenance
 */

static sapi_module_struct sapi_module = {
	"PHP Language",					/* name */
									
	php_module_startup,				/* startup */
	php_module_shutdown_wrapper,	/* shutdown */

	zend_servlet_ub_write,			/* unbuffered write */

	php_error,						/* error handler */

	NULL,							/* header handler */
	NULL,							/* send headers handler */
	sapi_servlet_send_header,		/* send header handler */

	sapi_servlet_read_post,			/* read POST data */
	sapi_servlet_read_cookies,		/* read Cookies */

	STANDARD_SAPI_MODULE_PROPERTIES
};


JNIEXPORT void JNICALL Java_net_php_servlet_startup
	(JNIEnv *jenv, jobject self)
{

#ifdef ZTS
	tsrm_startup(1,1,0);
#else
	if (setjmp(EG(bailout))!=0) {
		ThrowServletException(jenv,"bailout");
		return;
	}
#endif

	sapi_startup(&sapi_module);

	if (php_module_startup(&sapi_module)==FAILURE) {
		ThrowServletException(jenv,"module startup failure");
		return;
	}
}


JNIEXPORT void JNICALL Java_net_php_servlet_shutdown
	(JNIEnv *jenv, jobject self)
{
	SLS_FETCH();

	php_module_shutdown();
#ifdef ZTS
	tsrm_shutdown();
#endif
	return;
}

/***************************************************************************/

/*
 * define a Java object to PHP
 */

JNIEXPORT jlong JNICALL Java_net_php_servlet_define
	(JNIEnv *jenv, jobject self, jstring name) 
{
	pval *pzval;
	jlong addr = 0;
	ELS_FETCH();
	const char *nameAsUTF = (*jenv)->GetStringUTFChars(jenv, name, 0);

	MAKE_STD_ZVAL(pzval);
	(pval*)(long)addr = pzval;

	zend_hash_add(&EG(symbol_table), (char*)nameAsUTF, 
		strlen(nameAsUTF)+1, &pzval, sizeof(pval *), NULL);
	(*jenv)->ReleaseStringUTFChars(jenv, name, nameAsUTF);

	return addr;
}

/*
 * execute a script
 */

JNIEXPORT void JNICALL Java_net_php_servlet_send
	(JNIEnv *jenv, jobject self, 
	 jstring requestMethod, jstring queryString,
	 jstring pathInfo, jstring pathTranslated,
	 jstring contentType, jint contentLength, jstring authUser)
{

	zend_file_handle file_handle;
	char cwd[MAXPATHLEN+1];
	jlong addr = 0;
	SLS_FETCH();
	PLS_FETCH();
	CLS_FETCH();
	ELS_FETCH();

#ifdef ZTS
	if (setjmp(EG(bailout))!=0) {
		ThrowServletException(jenv,"bailout");
		return;
	}
#endif

	SG(server_context) = emalloc(sizeof(servlet_request));
	((servlet_request*)SG(server_context))->jenv=jenv;
	((servlet_request*)SG(server_context))->servlet=self;
	((servlet_request*)SG(server_context))->cookies=0;

	CG(extended_info) = 0;

	/*
	 * Initialize the request
	 */
	SETSTRING( SG(request_info).auth_user, authUser );
	SETSTRING( SG(request_info).request_method, requestMethod );
	SETSTRING( SG(request_info).query_string, queryString );
	SETSTRING( SG(request_info).request_uri, pathInfo );
	SETSTRING( SG(request_info).content_type, contentType );
	SG(request_info).content_length = contentLength;
	SG(request_info).auth_password = NULL;
	if (php_request_startup(CLS_C ELS_CC PLS_CC SLS_CC)==FAILURE) {
		ThrowServletException(jenv,"request startup failure");
		return;
	}

	/*
	 * Parse the file
	 */
	SETSTRING( SG(request_info).path_translated, pathTranslated );
	getcwd(cwd,MAXPATHLEN);
	file_handle.handle.fp = php3_fopen_for_parser();
	chdir(cwd);
	file_handle.filename = SG(request_info).path_translated;
	file_handle.free_filename = 0;
	file_handle.type = ZEND_HANDLE_FP;

	if (!file_handle.handle.fp) {
		php_request_shutdown((void *) 0);
		php_module_shutdown();
		ThrowIOException(jenv,file_handle.filename);
		return;
	}

	/*
	 * Execute the request
	 */
	Java_net_php_reflect_setEnv(jenv, 0);
	php_execute_script(&file_handle CLS_CC ELS_CC PLS_CC);

	/*
	 * Clean up
	 */
	php3_header();			/* Make sure headers have been sent */
	
	FREESTRING(SG(request_info).request_method);
	FREESTRING(SG(request_info).query_string);
	FREESTRING(SG(request_info).request_uri);
	FREESTRING(SG(request_info).path_translated);
	FREESTRING(SG(request_info).content_type);
	FREESTRING(SG(request_info).auth_user);
	FREESTRING(((servlet_request*)SG(server_context))->cookies);    
	efree(SG(server_context));
	SG(server_context)=0;

	php_request_shutdown((void *) 0);
}