camille dot gauthier at neuf dot fr
2007-02-22 09:30:29 UTC
From: camille dot gauthier at neuf dot fr
Operating system: windows
Package version:
PHP version: 5.2.1
Package: Bug System
Bug Type: Bug
Bug description: php5servlet support for java 1.5 or 1.6
Description:
------------
the last version of servlet.java in php5servlet.jar is not java5/6
compatible; it is linked to the fact that servlet.java uses enum (new
reserved java word) for a local variable name; to fix that issue, it is
sufficient to replace enum by en for example; i have recompiled it,
rebuilt php5servlet.jar with it and now php over tomcat works fine. the
complete new code for servlet.java is pasted below:
/*
+----------------------------------------------------------------------+
| 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
|
| ***@php.net so we can mail you a copy immediately.
|
+----------------------------------------------------------------------+
| Author: Sam Ruby (***@us.ibm.com)
|
+----------------------------------------------------------------------+
*/
/* $Id: servlet.java,v 1.19.8.1.8.1 2006/01/01 13:47:02 sniper Exp $ */
package net.php;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.reflect.Method;
public class servlet extends HttpServlet {
char slash = System.getProperty("file.separator").charAt(0);
HttpServletRequest request;
HttpServletResponse response;
ServletInputStream stream;
static int startup_count = 0;
protected boolean display_source_mode = false;
private Method addHeader;
// Native methods
public native void startup();
public native long define(String name);
public native void send(String requestMethod,
String queryString,
String pathInfo,
String pathTranslated,
String contentType,
int contentLength,
String authUser,
boolean display_source_mode
);
public native void shutdown();
// SAPI Callbacks
public String readPost(int bytes) {
String result;
if (!request.getMethod().equals("POST")) {
result = request.getQueryString();
} else {
Enumeration enum = request.getParameterNames();
String concat = "";
result = "";
while (enum.hasMoreElements()) {
String name = (String)enum.nextElement();
String value = request.getParameter(name);
try {
result += concat + name + "=" + URLEncoder.encode(value,
"UTF-8");
}
catch (Exception e) {
e.printStackTrace(System.err);
}
concat = "&";
}
}
if (result == null) return "";
return result;
}
public String readCookies() {
reflect.setResult(define("request"), request);
reflect.setResult(define("response"), response);
reflect.setResult(define("PHP_SELF"), request.getRequestURI());
return request.getHeader("cookie");
}
public void header(String data) {
// try to send the header using the most specific servlet API
// as possible (some servlet engines will add a content type
// header unless the setContentType method is called).
try {
if (data.startsWith("Content-type: ")) {
response.setContentType(data.substring(data.indexOf(" ") + 1));
}
else if (data.startsWith("Location: ")) {
response.sendRedirect(data.substring(data.indexOf(" ") + 1));
} else if (data.startsWith("HTTP/1")) {
return; // this one is added from servlet container (Tomcat 4.1),
we have to check for others
}
else {
int colon = data.indexOf(": ");
if (colon > 0) {
try {
addHeader.invoke(response, new Object[]
{ data.substring(0, colon), data.substring(colon + 2) } );
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
else {
write(data);
}
}
}
catch (IOException e) {
e.printStackTrace(System.err);
}
}
public void write(String data) {
try {
response.getWriter().print(data);
}
catch (IOException e) {
e.printStackTrace(System.err);
}
}
// Servlet interface
public void init(ServletConfig config)
throws ServletException {
super.init(config);
// first time in, initialize native code
if (0 == startup_count++) {
reflect.loadLibrary("servlet");
startup();
}
// try to find the addHeader method (added in the servlet API 2.2)
// otherwise settle for the setHeader method
try {
Class c = Class.forName("javax.servlet.http.HttpServletResponse");
Method method[] = c.getDeclaredMethods();
for (int i = 0; i < method.length; i++) {
if (method[i].getName().equals("addHeader")) {
addHeader = method[i];
break;
}
if (method[i].getName().equals("setHeader")) {
addHeader = method[i];
}
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
public void service(HttpServletRequest request, HttpServletResponse
response, String contextPath)
throws ServletException {
this.request = request;
this.response = response;
send(request.getMethod(),
request.getQueryString(),
request.getRequestURI(),
contextPath,
request.getContentType(),
request.getContentLength(),
request.getRemoteUser(),
display_source_mode
);
try {
if (stream != null) stream.close();
}
catch (IOException e) {
throw new ServletException(e.toString());
}
}
public void service(HttpServletRequest request, HttpServletResponse
response)
throws ServletException {
String servletPath = request.getServletPath();
String contextPath = getServletContext().getRealPath(servletPath);
service(request, response, contextPath);
}
public void destroy() {
if (0 == --startup_count) shutdown();
super.destroy();
}
}
Operating system: windows
Package version:
PHP version: 5.2.1
Package: Bug System
Bug Type: Bug
Bug description: php5servlet support for java 1.5 or 1.6
Description:
------------
the last version of servlet.java in php5servlet.jar is not java5/6
compatible; it is linked to the fact that servlet.java uses enum (new
reserved java word) for a local variable name; to fix that issue, it is
sufficient to replace enum by en for example; i have recompiled it,
rebuilt php5servlet.jar with it and now php over tomcat works fine. the
complete new code for servlet.java is pasted below:
/*
+----------------------------------------------------------------------+
| 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
|
| ***@php.net so we can mail you a copy immediately.
|
+----------------------------------------------------------------------+
| Author: Sam Ruby (***@us.ibm.com)
|
+----------------------------------------------------------------------+
*/
/* $Id: servlet.java,v 1.19.8.1.8.1 2006/01/01 13:47:02 sniper Exp $ */
package net.php;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.reflect.Method;
public class servlet extends HttpServlet {
char slash = System.getProperty("file.separator").charAt(0);
HttpServletRequest request;
HttpServletResponse response;
ServletInputStream stream;
static int startup_count = 0;
protected boolean display_source_mode = false;
private Method addHeader;
// Native methods
public native void startup();
public native long define(String name);
public native void send(String requestMethod,
String queryString,
String pathInfo,
String pathTranslated,
String contentType,
int contentLength,
String authUser,
boolean display_source_mode
);
public native void shutdown();
// SAPI Callbacks
public String readPost(int bytes) {
String result;
if (!request.getMethod().equals("POST")) {
result = request.getQueryString();
} else {
Enumeration enum = request.getParameterNames();
String concat = "";
result = "";
while (enum.hasMoreElements()) {
String name = (String)enum.nextElement();
String value = request.getParameter(name);
try {
result += concat + name + "=" + URLEncoder.encode(value,
"UTF-8");
}
catch (Exception e) {
e.printStackTrace(System.err);
}
concat = "&";
}
}
if (result == null) return "";
return result;
}
public String readCookies() {
reflect.setResult(define("request"), request);
reflect.setResult(define("response"), response);
reflect.setResult(define("PHP_SELF"), request.getRequestURI());
return request.getHeader("cookie");
}
public void header(String data) {
// try to send the header using the most specific servlet API
// as possible (some servlet engines will add a content type
// header unless the setContentType method is called).
try {
if (data.startsWith("Content-type: ")) {
response.setContentType(data.substring(data.indexOf(" ") + 1));
}
else if (data.startsWith("Location: ")) {
response.sendRedirect(data.substring(data.indexOf(" ") + 1));
} else if (data.startsWith("HTTP/1")) {
return; // this one is added from servlet container (Tomcat 4.1),
we have to check for others
}
else {
int colon = data.indexOf(": ");
if (colon > 0) {
try {
addHeader.invoke(response, new Object[]
{ data.substring(0, colon), data.substring(colon + 2) } );
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
else {
write(data);
}
}
}
catch (IOException e) {
e.printStackTrace(System.err);
}
}
public void write(String data) {
try {
response.getWriter().print(data);
}
catch (IOException e) {
e.printStackTrace(System.err);
}
}
// Servlet interface
public void init(ServletConfig config)
throws ServletException {
super.init(config);
// first time in, initialize native code
if (0 == startup_count++) {
reflect.loadLibrary("servlet");
startup();
}
// try to find the addHeader method (added in the servlet API 2.2)
// otherwise settle for the setHeader method
try {
Class c = Class.forName("javax.servlet.http.HttpServletResponse");
Method method[] = c.getDeclaredMethods();
for (int i = 0; i < method.length; i++) {
if (method[i].getName().equals("addHeader")) {
addHeader = method[i];
break;
}
if (method[i].getName().equals("setHeader")) {
addHeader = method[i];
}
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
public void service(HttpServletRequest request, HttpServletResponse
response, String contextPath)
throws ServletException {
this.request = request;
this.response = response;
send(request.getMethod(),
request.getQueryString(),
request.getRequestURI(),
contextPath,
request.getContentType(),
request.getContentLength(),
request.getRemoteUser(),
display_source_mode
);
try {
if (stream != null) stream.close();
}
catch (IOException e) {
throw new ServletException(e.toString());
}
}
public void service(HttpServletRequest request, HttpServletResponse
response)
throws ServletException {
String servletPath = request.getServletPath();
String contextPath = getServletContext().getRealPath(servletPath);
service(request, response, contextPath);
}
public void destroy() {
if (0 == --startup_count) shutdown();
super.destroy();
}
}
--
Edit bug report at http://pecl.php.net/bugs/bug.php?id=10155&edit=1
--
Edit bug report at http://pecl.php.net/bugs/bug.php?id=10155&edit=1
--