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.

179 lines
6.2 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.0 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_0.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. package net.php;
  19. import java.io.IOException;
  20. import java.net.URLEncoder;
  21. import java.util.Enumeration;
  22. import javax.servlet.*;
  23. import javax.servlet.http.*;
  24. import java.lang.reflect.Method;
  25. public class servlet extends HttpServlet {
  26. char slash=System.getProperty("file.separator").charAt(0);
  27. HttpServletRequest request;
  28. HttpServletResponse response;
  29. ServletInputStream stream;
  30. static int startup_count = 0;
  31. protected boolean display_source_mode = false;
  32. private Method addHeader;
  33. /******************************************************************/
  34. /* native methods */
  35. /******************************************************************/
  36. public native void startup();
  37. public native long define(String name);
  38. public native void send(String requestMethod, String queryString,
  39. String pathInfo, String pathTranslated,
  40. String contentType, int contentLength, String authUser,
  41. boolean display_source_mode);
  42. public native void shutdown();
  43. /******************************************************************/
  44. /* sapi callbacks */
  45. /******************************************************************/
  46. public String readPost(int bytes) {
  47. String result;
  48. if (!request.getMethod().equals("POST")) {
  49. result = request.getQueryString();
  50. } else {
  51. Enumeration e = request.getParameterNames();
  52. result="";
  53. String concat="";
  54. while (e.hasMoreElements()) {
  55. String name = (String)e.nextElement();
  56. String value = request.getParameter(name);
  57. result+=concat+name+"="+URLEncoder.encode(value);
  58. concat="&";
  59. }
  60. }
  61. if (result == null) return "";
  62. return result;
  63. }
  64. public String readCookies() {
  65. reflect.setResult(define("request"), request);
  66. reflect.setResult(define("response"), response);
  67. reflect.setResult(define("PHP_SELF"), request.getRequestURI());
  68. return request.getHeader("cookie");
  69. }
  70. public void header(String data) {
  71. // try to send the header using the most specific servlet API
  72. // as possible (some servlet engines will add a content type
  73. // header unless the setContentType method is called).
  74. try {
  75. if (data.startsWith("Content-type: ")) {
  76. response.setContentType(data.substring(data.indexOf(" ")+1));
  77. } else if (data.startsWith("Location: ")) {
  78. response.sendRedirect(data.substring(data.indexOf(" ")+1));
  79. } else {
  80. int colon = data.indexOf(": ");
  81. if (colon > 0) {
  82. try {
  83. addHeader.invoke(response, new Object[]
  84. { data.substring(0,colon), data.substring(colon+2) } );
  85. } catch (Exception e) {
  86. e.printStackTrace(System.err);
  87. }
  88. } else {
  89. response.getWriter().println(data);
  90. }
  91. }
  92. } catch (IOException e) {
  93. e.printStackTrace(System.err);
  94. }
  95. }
  96. public void write(String data) {
  97. try {
  98. response.getWriter().print(data);
  99. } catch (IOException e) {
  100. e.printStackTrace(System.err);
  101. }
  102. }
  103. /******************************************************************/
  104. /* servlet interface */
  105. /******************************************************************/
  106. public void init(ServletConfig config) throws ServletException {
  107. super.init(config);
  108. // first time in, initialize native code
  109. if (0 == startup_count++) {
  110. reflect.loadLibrary("servlet");
  111. startup();
  112. }
  113. // try to find the addHeader method (added in the servlet API 2.2)
  114. // otherwise settle for the setHeader method
  115. try {
  116. Class c = Class.forName("javax.servlet.http.HttpServletResponse");
  117. Method method[] = c.getDeclaredMethods();
  118. for (int i=0; i<method.length; i++) {
  119. if (method[i].getName().equals("addHeader")) {
  120. addHeader = method[i];
  121. break;
  122. }
  123. if (method[i].getName().equals("setHeader")) {
  124. addHeader = method[i];
  125. }
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace(System.err);
  129. }
  130. }
  131. public void service(HttpServletRequest request,
  132. HttpServletResponse response)
  133. throws ServletException
  134. {
  135. this.request=request;
  136. this.response=response;
  137. String servletPath=request.getServletPath();
  138. String contextPath=getServletContext().getRealPath(servletPath);
  139. send(request.getMethod(), request.getQueryString(),
  140. request.getPathInfo(), 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 destroy() {
  150. if (0 == --startup_count) shutdown();
  151. super.destroy();
  152. }
  153. }