1999-11-29 15:36:25 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP version 4.0 |
|
|
|
|
+----------------------------------------------------------------------+
|
2001-02-26 06:11:02 +00:00
|
|
|
| Copyright (c) 1997-2001 The PHP Group |
|
1999-11-29 15:36:25 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2000-07-24 05:41:02 +00:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-11-29 15:36:25 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-07-24 05:41:02 +00:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-11-29 15:36:25 +00:00
|
|
|
| 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) |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2000-07-24 05:41:02 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
1999-11-29 15:36:25 +00:00
|
|
|
package net.php;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2000-03-09 13:07:25 +00:00
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.util.Enumeration;
|
1999-11-29 15:36:25 +00:00
|
|
|
import javax.servlet.*;
|
|
|
|
import javax.servlet.http.*;
|
|
|
|
|
2000-04-01 17:57:42 +00:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
1999-11-29 15:36:25 +00:00
|
|
|
public class servlet extends HttpServlet {
|
|
|
|
|
|
|
|
char slash=System.getProperty("file.separator").charAt(0);
|
|
|
|
HttpServletRequest request;
|
|
|
|
HttpServletResponse response;
|
|
|
|
ServletInputStream stream;
|
|
|
|
|
2000-03-05 21:35:24 +00:00
|
|
|
static int startup_count = 0;
|
|
|
|
|
|
|
|
protected boolean display_source_mode = false;
|
2000-04-01 17:57:42 +00:00
|
|
|
private Method addHeader;
|
2000-03-05 21:35:24 +00:00
|
|
|
|
1999-11-29 15:36:25 +00:00
|
|
|
/******************************************************************/
|
|
|
|
/* native methods */
|
|
|
|
/******************************************************************/
|
|
|
|
|
2000-06-14 01:41:13 +00:00
|
|
|
public native void startup();
|
|
|
|
public native long define(String name);
|
|
|
|
public native void send(String requestMethod, String queryString,
|
1999-11-29 15:36:25 +00:00
|
|
|
String pathInfo, String pathTranslated,
|
2000-03-05 21:35:24 +00:00
|
|
|
String contentType, int contentLength, String authUser,
|
|
|
|
boolean display_source_mode);
|
2000-06-14 01:41:13 +00:00
|
|
|
public native void shutdown();
|
1999-11-29 15:36:25 +00:00
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* sapi callbacks */
|
|
|
|
/******************************************************************/
|
|
|
|
|
2000-06-14 01:41:13 +00:00
|
|
|
public String readPost(int bytes) {
|
2000-03-09 13:07:25 +00:00
|
|
|
String result;
|
|
|
|
if (!request.getMethod().equals("POST")) {
|
|
|
|
result = request.getQueryString();
|
|
|
|
} else {
|
|
|
|
Enumeration e = request.getParameterNames();
|
|
|
|
result="";
|
|
|
|
String concat="";
|
|
|
|
while (e.hasMoreElements()) {
|
|
|
|
String name = (String)e.nextElement();
|
|
|
|
String value = request.getParameter(name);
|
|
|
|
result+=concat+name+"="+URLEncoder.encode(value);
|
|
|
|
concat="&";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == null) return "";
|
|
|
|
return result;
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
|
|
|
|
2000-06-14 01:41:13 +00:00
|
|
|
public String readCookies() {
|
1999-11-29 15:36:25 +00:00
|
|
|
reflect.setResult(define("request"), request);
|
|
|
|
reflect.setResult(define("response"), response);
|
2000-04-05 09:06:05 +00:00
|
|
|
reflect.setResult(define("PHP_SELF"), request.getRequestURI());
|
1999-11-29 15:36:25 +00:00
|
|
|
return request.getHeader("cookie");
|
|
|
|
}
|
|
|
|
|
2000-06-14 01:41:13 +00:00
|
|
|
public void header(String data) {
|
2000-04-01 17:57:42 +00:00
|
|
|
|
|
|
|
// 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).
|
1999-11-29 15:36:25 +00:00
|
|
|
try {
|
2000-03-05 21:35:24 +00:00
|
|
|
if (data.startsWith("Content-type: ")) {
|
1999-11-29 15:36:25 +00:00
|
|
|
response.setContentType(data.substring(data.indexOf(" ")+1));
|
|
|
|
} else if (data.startsWith("Location: ")) {
|
|
|
|
response.sendRedirect(data.substring(data.indexOf(" ")+1));
|
|
|
|
} else {
|
|
|
|
int colon = data.indexOf(": ");
|
|
|
|
if (colon > 0) {
|
2000-04-01 17:57:42 +00:00
|
|
|
try {
|
|
|
|
addHeader.invoke(response, new Object[]
|
|
|
|
{ data.substring(0,colon), data.substring(colon+2) } );
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace(System.err);
|
|
|
|
}
|
1999-11-29 15:36:25 +00:00
|
|
|
} else {
|
2000-07-30 04:50:31 +00:00
|
|
|
write(data);
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2000-04-01 17:57:42 +00:00
|
|
|
e.printStackTrace(System.err);
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
2000-04-01 17:57:42 +00:00
|
|
|
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
|
|
|
|
2000-06-14 01:41:13 +00:00
|
|
|
public void write(String data) {
|
1999-11-29 15:36:25 +00:00
|
|
|
try {
|
|
|
|
response.getWriter().print(data);
|
|
|
|
} catch (IOException e) {
|
2000-04-01 17:57:42 +00:00
|
|
|
e.printStackTrace(System.err);
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* servlet interface */
|
|
|
|
/******************************************************************/
|
|
|
|
|
|
|
|
public void init(ServletConfig config) throws ServletException {
|
2000-03-05 21:35:24 +00:00
|
|
|
super.init(config);
|
2000-06-27 10:37:40 +00:00
|
|
|
|
|
|
|
// first time in, initialize native code
|
|
|
|
if (0 == startup_count++) {
|
|
|
|
reflect.loadLibrary("servlet");
|
|
|
|
startup();
|
|
|
|
}
|
2000-04-01 17:57:42 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void service(HttpServletRequest request,
|
2000-07-30 04:50:31 +00:00
|
|
|
HttpServletResponse response,
|
|
|
|
String contextPath)
|
1999-11-29 15:36:25 +00:00
|
|
|
throws ServletException
|
|
|
|
{
|
|
|
|
this.request=request;
|
|
|
|
this.response=response;
|
2000-01-30 20:48:33 +00:00
|
|
|
|
1999-11-29 15:36:25 +00:00
|
|
|
send(request.getMethod(), request.getQueryString(),
|
2000-07-24 05:29:20 +00:00
|
|
|
request.getRequestURI(), contextPath,
|
1999-11-29 15:36:25 +00:00
|
|
|
request.getContentType(), request.getContentLength(),
|
2000-03-05 21:35:24 +00:00
|
|
|
request.getRemoteUser(), display_source_mode);
|
1999-11-29 15:36:25 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (stream != null) stream.close();
|
|
|
|
} catch (IOException e) {
|
2000-04-01 17:57:42 +00:00
|
|
|
throw new ServletException(e.toString());
|
1999-11-29 15:36:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-30 04:50:31 +00:00
|
|
|
public void service(HttpServletRequest request,
|
|
|
|
HttpServletResponse response)
|
|
|
|
throws ServletException
|
|
|
|
{
|
|
|
|
String servletPath=request.getServletPath();
|
|
|
|
String contextPath=getServletContext().getRealPath(servletPath);
|
|
|
|
service(request, response, contextPath);
|
|
|
|
}
|
|
|
|
|
1999-11-29 15:36:25 +00:00
|
|
|
public void destroy() {
|
2000-03-05 21:35:24 +00:00
|
|
|
if (0 == --startup_count) shutdown();
|
1999-11-29 15:36:25 +00:00
|
|
|
super.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|