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.

219 lines
6.0 KiB

25 years ago
25 years ago
25 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2002 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. // Native methods
  35. public native void startup();
  36. public native long define(String name);
  37. public native void send(String requestMethod,
  38. String queryString,
  39. String pathInfo,
  40. String pathTranslated,
  41. String contentType,
  42. int contentLength,
  43. String authUser,
  44. boolean display_source_mode
  45. );
  46. public native void shutdown();
  47. // SAPI Callbacks
  48. public String readPost(int bytes) {
  49. String result;
  50. if (!request.getMethod().equals("POST")) {
  51. result = request.getQueryString();
  52. } else {
  53. Enumeration enum = request.getParameterNames();
  54. String concat = "";
  55. result = "";
  56. while (enum.hasMoreElements()) {
  57. String name = (String)enum.nextElement();
  58. String value = request.getParameter(name);
  59. try {
  60. result += concat + name + "=" + URLEncoder.encode(value, "UTF-8");
  61. }
  62. catch (Exception e) {
  63. e.printStackTrace(System.err);
  64. }
  65. concat = "&";
  66. }
  67. }
  68. if (result == null) return "";
  69. return result;
  70. }
  71. public String readCookies() {
  72. reflect.setResult(define("request"), request);
  73. reflect.setResult(define("response"), response);
  74. reflect.setResult(define("PHP_SELF"), request.getRequestURI());
  75. return request.getHeader("cookie");
  76. }
  77. public void header(String data) {
  78. // try to send the header using the most specific servlet API
  79. // as possible (some servlet engines will add a content type
  80. // header unless the setContentType method is called).
  81. try {
  82. if (data.startsWith("Content-type: ")) {
  83. response.setContentType(data.substring(data.indexOf(" ") + 1));
  84. }
  85. else if (data.startsWith("Location: ")) {
  86. response.sendRedirect(data.substring(data.indexOf(" ") + 1));
  87. }
  88. else {
  89. int colon = data.indexOf(": ");
  90. if (colon > 0) {
  91. try {
  92. addHeader.invoke(response, new Object[]
  93. { data.substring(0, colon), data.substring(colon + 2) } );
  94. }
  95. catch (Exception e) {
  96. e.printStackTrace(System.err);
  97. }
  98. }
  99. else {
  100. write(data);
  101. }
  102. }
  103. }
  104. catch (IOException e) {
  105. e.printStackTrace(System.err);
  106. }
  107. }
  108. public void write(String data) {
  109. try {
  110. response.getWriter().print(data);
  111. }
  112. catch (IOException e) {
  113. e.printStackTrace(System.err);
  114. }
  115. }
  116. // Servlet interface
  117. public void init(ServletConfig config)
  118. throws ServletException {
  119. super.init(config);
  120. // first time in, initialize native code
  121. if (0 == startup_count++) {
  122. reflect.loadLibrary("servlet");
  123. startup();
  124. }
  125. // try to find the addHeader method (added in the servlet API 2.2)
  126. // otherwise settle for the setHeader method
  127. try {
  128. Class c = Class.forName("javax.servlet.http.HttpServletResponse");
  129. Method method[] = c.getDeclaredMethods();
  130. for (int i = 0; i < method.length; i++) {
  131. if (method[i].getName().equals("addHeader")) {
  132. addHeader = method[i];
  133. break;
  134. }
  135. if (method[i].getName().equals("setHeader")) {
  136. addHeader = method[i];
  137. }
  138. }
  139. }
  140. catch (Exception e) {
  141. e.printStackTrace(System.err);
  142. }
  143. }
  144. public void service(HttpServletRequest request, HttpServletResponse response, String contextPath)
  145. throws ServletException {
  146. this.request = request;
  147. this.response = response;
  148. send(request.getMethod(),
  149. request.getQueryString(),
  150. request.getRequestURI(),
  151. contextPath,
  152. request.getContentType(),
  153. request.getContentLength(),
  154. request.getRemoteUser(),
  155. display_source_mode
  156. );
  157. try {
  158. if (stream != null) stream.close();
  159. }
  160. catch (IOException e) {
  161. throw new ServletException(e.toString());
  162. }
  163. }
  164. public void service(HttpServletRequest request, HttpServletResponse response)
  165. throws ServletException {
  166. String servletPath = request.getServletPath();
  167. String contextPath = getServletContext().getRealPath(servletPath);
  168. service(request, response, contextPath);
  169. }
  170. public void destroy() {
  171. if (0 == --startup_count) shutdown();
  172. super.destroy();
  173. }
  174. }