You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.4 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2001 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sam Ruby (rubys@us.ibm.com) |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. package net.php;
  20. import java.io.IOException;
  21. import java.net.URLEncoder;
  22. import java.util.Enumeration;
  23. import javax.servlet.*;
  24. import javax.servlet.http.*;
  25. import java.lang.reflect.Method;
  26. public class servlet extends HttpServlet {
  27. char slash=System.getProperty("file.separator").charAt(0);
  28. HttpServletRequest request;
  29. HttpServletResponse response;
  30. ServletInputStream stream;
  31. static int startup_count = 0;
  32. protected boolean display_source_mode = false;
  33. private Method addHeader;
  34. /******************************************************************/
  35. /* native methods */
  36. /******************************************************************/
  37. public native void startup();
  38. public native long define(String name);
  39. public native void send(String requestMethod, String queryString,
  40. String pathInfo, String pathTranslated,
  41. String contentType, int contentLength, String authUser,
  42. boolean display_source_mode);
  43. public native void shutdown();
  44. /******************************************************************/
  45. /* sapi callbacks */
  46. /******************************************************************/
  47. public String readPost(int bytes) {
  48. String result;
  49. if (!request.getMethod().equals("POST")) {
  50. result = request.getQueryString();
  51. } else {
  52. Enumeration e = request.getParameterNames();
  53. result="";
  54. String concat="";
  55. while (e.hasMoreElements()) {
  56. String name = (String)e.nextElement();
  57. String value = request.getParameter(name);
  58. result+=concat+name+"="+URLEncoder.encode(value);
  59. concat="&";
  60. }
  61. }
  62. if (result == null) return "";
  63. return result;
  64. }
  65. public String readCookies() {
  66. reflect.setResult(define("request"), request);
  67. reflect.setResult(define("response"), response);
  68. reflect.setResult(define("PHP_SELF"), request.getRequestURI());
  69. return request.getHeader("cookie");
  70. }
  71. public void header(String data) {
  72. // try to send the header using the most specific servlet API
  73. // as possible (some servlet engines will add a content type
  74. // header unless the setContentType method is called).
  75. try {
  76. if (data.startsWith("Content-type: ")) {
  77. response.setContentType(data.substring(data.indexOf(" ")+1));
  78. } else if (data.startsWith("Location: ")) {
  79. response.sendRedirect(data.substring(data.indexOf(" ")+1));
  80. } else {
  81. int colon = data.indexOf(": ");
  82. if (colon > 0) {
  83. try {
  84. addHeader.invoke(response, new Object[]
  85. { data.substring(0,colon), data.substring(colon+2) } );
  86. } catch (Exception e) {
  87. e.printStackTrace(System.err);
  88. }
  89. } else {
  90. write(data);
  91. }
  92. }
  93. } catch (IOException e) {
  94. e.printStackTrace(System.err);
  95. }
  96. }
  97. public void write(String data) {
  98. try {
  99. response.getWriter().print(data);
  100. } catch (IOException e) {
  101. e.printStackTrace(System.err);
  102. }
  103. }
  104. /******************************************************************/
  105. /* servlet interface */
  106. /******************************************************************/
  107. public void init(ServletConfig config) throws ServletException {
  108. super.init(config);
  109. // first time in, initialize native code
  110. if (0 == startup_count++) {
  111. reflect.loadLibrary("servlet");
  112. startup();
  113. }
  114. // try to find the addHeader method (added in the servlet API 2.2)
  115. // otherwise settle for the setHeader method
  116. try {
  117. Class c = Class.forName("javax.servlet.http.HttpServletResponse");
  118. Method method[] = c.getDeclaredMethods();
  119. for (int i=0; i<method.length; i++) {
  120. if (method[i].getName().equals("addHeader")) {
  121. addHeader = method[i];
  122. break;
  123. }
  124. if (method[i].getName().equals("setHeader")) {
  125. addHeader = method[i];
  126. }
  127. }
  128. } catch (Exception e) {
  129. e.printStackTrace(System.err);
  130. }
  131. }
  132. public void service(HttpServletRequest request,
  133. HttpServletResponse response,
  134. String contextPath)
  135. throws ServletException
  136. {
  137. this.request=request;
  138. this.response=response;
  139. send(request.getMethod(), request.getQueryString(),
  140. request.getRequestURI(), contextPath,
  141. request.getContentType(), request.getContentLength(),
  142. request.getRemoteUser(), display_source_mode);
  143. try {
  144. if (stream != null) stream.close();
  145. } catch (IOException e) {
  146. throw new ServletException(e.toString());
  147. }
  148. }
  149. public void service(HttpServletRequest request,
  150. HttpServletResponse response)
  151. throws ServletException
  152. {
  153. String servletPath=request.getServletPath();
  154. String contextPath=getServletContext().getRealPath(servletPath);
  155. service(request, response, contextPath);
  156. }
  157. public void destroy() {
  158. if (0 == --startup_count) shutdown();
  159. super.destroy();
  160. }
  161. }