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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2006 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.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 "php.h"
#include "php_globals.h"
#include "SAPI.h"
#include <stdio.h>
#include "php.h"
#ifdef PHP_WIN32
#include "win32/time.h"
#include "win32/signal.h"
#include <process.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 "php_main.h"
#include "fopen_wrappers.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/php_dir.h"
#ifdef PHP_WIN32
#include <io.h>
#include <fcntl.h>
#include "win32/php_registry.h"
#endif
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_highlight.h"
#include "zend_indent.h"
JNIEXPORT void JNICALL Java_net_php_reflect_setEnv
(JNIEnv *newJenv, jclass self TSRMLS_DC);
typedef struct {
JNIEnv *jenv;
jobject servlet;
char *cookies;
} servlet_request;
extern zend_module_entry java_module_entry;
/***************************************************************************/
/*
* 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") );
}
void ThrowServletException (JNIEnv *jenv, char *msg) {
jclass sx = (*jenv)->FindClass (jenv, "javax/servlet/ServletException");
(*jenv)->ThrowNew (jenv, sx, msg);
}
/***************************************************************************/
/*
* sapi callbacks
*/
static int sapi_servlet_ub_write(const char *str, uint str_length TSRMLS_DC)
{
if (!SG(server_context)) {
fprintf(stderr, "%s", 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");
char *copy = malloc(str_length+1);
jstring arg;
memcpy(copy, str, str_length);
copy[str_length] = 0;
arg=(*jenv)->NewStringUTF(jenv, copy);
free(copy);
(*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 TSRMLS_DC)
{
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 TSRMLS_DC)
{
if (count_bytes == 0) {
return 0;
} else {
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, read_bytes);
if (read_bytes<count_bytes) buffer[read_bytes]=0;
(*jenv)->ReleaseStringUTFChars(jenv, post, postAsUTF);
return read_bytes;
}
}
static char *sapi_servlet_read_cookies(TSRMLS_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 int php_servlet_startup(sapi_module_struct *sapi_module)
{
if (php_module_startup(sapi_module, NULL, 0)==FAILURE) {
return FAILURE;
} else {
return SUCCESS;
}
}
static sapi_module_struct servlet_sapi_module = {
"java_servlet", /* name */
"Java Servlet", /* pretty name */
php_servlet_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
NULL, /* deactivate */
sapi_servlet_ub_write, /* unbuffered write */
NULL, /* flush */
NULL, /* get uid */
NULL, /* getenv */
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 */
NULL, /* register server variables */
NULL, /* Log message */
NULL, /* Block interruptions */
NULL, /* Unblock interruptions */
STANDARD_SAPI_MODULE_PROPERTIES
};
JNIEXPORT void JNICALL Java_net_php_servlet_startup
(JNIEnv *jenv, jobject self)
{
#ifdef ZTS
tsrm_startup(1, 1, 0, NULL);
#endif
sapi_startup(&servlet_sapi_module);
if (php_module_startup(&servlet_sapi_module, &java_module_entry, 1)==FAILURE) {
ThrowServletException(jenv,"module startup failure");
return;
}
}
JNIEXPORT void JNICALL Java_net_php_servlet_shutdown
(JNIEnv *jenv, jobject self)
{
TSRMLS_FETCH();
php_module_shutdown(TSRMLS_C);
#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;
const char *nameAsUTF = (*jenv)->GetStringUTFChars(jenv, name, 0);
TSRMLS_FETCH();
MAKE_STD_ZVAL(pzval);
addr = (jlong)(long) 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 requestURI, jstring pathTranslated,
jstring contentType, jint contentLength,
jstring authUser, jboolean display_source_mode)
{
zend_file_handle file_handle = {0};
int retval;
#ifndef VIRTUAL_DIR
char cwd[MAXPATHLEN];
#endif
TSRMLS_FETCH();
zend_first_try {
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;
/*
* 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, requestURI );
SETSTRING( SG(request_info).content_type, contentType );
SG(sapi_headers).http_response_code = 200;
SG(request_info).content_length = contentLength;
SG(request_info).auth_password = NULL;
if (php_request_startup(TSRMLS_C)==FAILURE) {
ThrowServletException(jenv,"request startup failure");
return;
}
/*
* Parse the file
*/
SETSTRING( SG(request_info).path_translated, pathTranslated );
#ifdef VIRTUAL_DIR
retval = php_fopen_primary_script(&file_handle TSRMLS_CC);
#else
/*
* The java runtime doesn't like the working directory to be
* changed, so save it and change it back as quickly as possible
* in the hopes that Java doesn't notice.
*/
getcwd(cwd, MAXPATHLEN);
retval = php_fopen_primary_script(&file_handle TSRMLS_CC);
chdir(cwd);
#endif
if (retval == FAILURE) {
php_request_shutdown((void *) 0);
php_module_shutdown(TSRMLS_C);
ThrowIOException(jenv, file_handle.filename);
return;
}
/*
* Execute the request
*/
Java_net_php_reflect_setEnv(jenv, 0 TSRMLS_CC);
if (display_source_mode) {
zend_syntax_highlighter_ini syntax_highlighter_ini;
if (open_file_for_scanning(&file_handle TSRMLS_CC)==SUCCESS) {
php_get_highlight_struct(&syntax_highlighter_ini);
sapi_send_headers(TSRMLS_C);
zend_highlight(&syntax_highlighter_ini TSRMLS_CC);
}
} else {
php_execute_script(&file_handle TSRMLS_CC);
php_header(); /* Make sure headers have been sent */
}
/*
* Clean up
*/
FREESTRING(SG(request_info).query_string);
FREESTRING(SG(request_info).request_uri);
FREESTRING(SG(request_info).path_translated);
FREESTRING(((servlet_request*)SG(server_context))->cookies);
efree(SG(server_context));
SG(server_context)=0;
if (!display_source_mode) php_request_shutdown((void *) 0);
} zend_catch {
ThrowServletException(jenv,"bailout");
} zend_end_try();
}
|