summaryrefslogtreecommitdiff
path: root/sapi/servlet/servlet.java
diff options
context:
space:
mode:
authorSam Ruby <rubys@php.net>1999-11-29 15:36:25 +0000
committerSam Ruby <rubys@php.net>1999-11-29 15:36:25 +0000
commitfd9265362c9d4f32256ec8f23c36ebbfa650dfb9 (patch)
treec13ffa00cc1f3bbd2cf283244a7c0a3f9e1870b4 /sapi/servlet/servlet.java
parenta76c133df16455dd7a29f8921f0a3609f6133734 (diff)
downloadphp-git-fd9265362c9d4f32256ec8f23c36ebbfa650dfb9.tar.gz
Initial alpha-level of sapi/servlet. See README for details.
Diffstat (limited to 'sapi/servlet/servlet.java')
-rw-r--r--sapi/servlet/servlet.java138
1 files changed, 138 insertions, 0 deletions
diff --git a/sapi/servlet/servlet.java b/sapi/servlet/servlet.java
new file mode 100644
index 0000000000..62be4f1d2c
--- /dev/null
+++ b/sapi/servlet/servlet.java
@@ -0,0 +1,138 @@
+/*
+ +----------------------------------------------------------------------+
+ | 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) |
+ +----------------------------------------------------------------------+
+*/
+
+package net.php;
+
+import java.io.IOException;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class servlet extends HttpServlet {
+
+ char slash=System.getProperty("file.separator").charAt(0);
+ HttpServletRequest request;
+ HttpServletResponse response;
+ ServletInputStream stream;
+
+ /******************************************************************/
+ /* native methods */
+ /******************************************************************/
+
+ static { reflect.loadLibrary("servlet"); }
+ native void startup();
+ native long define(String name);
+ native void send(String requestMethod, String queryString,
+ String pathInfo, String pathTranslated,
+ String contentType, int contentLength, String authUser);
+ native void shutdown();
+
+ /******************************************************************/
+ /* sapi callbacks */
+ /******************************************************************/
+
+ String readPost(int bytes) {
+ try {
+ if (stream == null) stream=request.getInputStream();
+ byte[] data = new byte[bytes];
+ int len = stream.read(data, 0, bytes);
+ if (len <= 0) return "";
+ return new String(data, 0, len);
+ } catch (IOException e) {
+ return "";
+ }
+ }
+
+ String readCookies() {
+ reflect.setResult(define("request"), request);
+ reflect.setResult(define("response"), response);
+ return request.getHeader("cookie");
+ }
+
+ void header(String data) {
+ 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 {
+ int colon = data.indexOf(": ");
+ if (colon > 0) {
+ response.addHeader(data.substring(0,colon),
+ data.substring(colon+2));
+ } else {
+ response.getWriter().println(data);
+ }
+ }
+ } catch (IOException e) {
+ System.err.print(data);
+ }
+ }
+
+ void write(String data) {
+ try {
+ response.getWriter().print(data);
+ } catch (IOException e) {
+ System.err.print(data);
+ }
+ }
+
+ /******************************************************************/
+ /* servlet interface */
+ /******************************************************************/
+
+ public void init(ServletConfig config) throws ServletException {
+ super.init(config);
+ startup();
+ }
+
+ public void service(HttpServletRequest request,
+ HttpServletResponse response)
+ throws ServletException
+ {
+ this.request=request;
+ this.response=response;
+ send(request.getMethod(), request.getQueryString(),
+ request.getPathInfo(), getPathTranslated(),
+ request.getContentType(), request.getContentLength(),
+ request.getRemoteUser());
+
+ try {
+ if (stream != null) stream.close();
+ } catch (IOException e) {
+ throw new ServletException(e);
+ }
+ }
+
+ public void destroy() {
+ shutdown();
+ super.destroy();
+ }
+
+ /******************************************************************/
+ /* utility function */
+ /******************************************************************/
+
+ String getPathTranslated() {
+ /* I have no idea why this has to be this hard... */
+ String servletPath=request.getServletPath();
+ String contextPath=getServletContext().getRealPath(servletPath);
+ servletPath=servletPath.replace('/',slash);
+ contextPath=contextPath.substring(0,contextPath.lastIndexOf(slash));
+ return contextPath+servletPath;
+ }
+}