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.

221 lines
6.1 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. } else if (data.startsWith("HTTP/1")) {
  88. return; // this one is added from servlet container (Tomcat 4.1), we have to check for others
  89. }
  90. else {
  91. int colon = data.indexOf(": ");
  92. if (colon > 0) {
  93. try {
  94. addHeader.invoke(response, new Object[]
  95. { data.substring(0, colon), data.substring(colon + 2) } );
  96. }
  97. catch (Exception e) {
  98. e.printStackTrace(System.err);
  99. }
  100. }
  101. else {
  102. write(data);
  103. }
  104. }
  105. }
  106. catch (IOException e) {
  107. e.printStackTrace(System.err);
  108. }
  109. }
  110. public void write(String data) {
  111. try {
  112. response.getWriter().print(data);
  113. }
  114. catch (IOException e) {
  115. e.printStackTrace(System.err);
  116. }
  117. }
  118. // Servlet interface
  119. public void init(ServletConfig config)
  120. throws ServletException {
  121. super.init(config);
  122. // first time in, initialize native code
  123. if (0 == startup_count++) {
  124. reflect.loadLibrary("servlet");
  125. startup();
  126. }
  127. // try to find the addHeader method (added in the servlet API 2.2)
  128. // otherwise settle for the setHeader method
  129. try {
  130. Class c = Class.forName("javax.servlet.http.HttpServletResponse");
  131. Method method[] = c.getDeclaredMethods();
  132. for (int i = 0; i < method.length; i++) {
  133. if (method[i].getName().equals("addHeader")) {
  134. addHeader = method[i];
  135. break;
  136. }
  137. if (method[i].getName().equals("setHeader")) {
  138. addHeader = method[i];
  139. }
  140. }
  141. }
  142. catch (Exception e) {
  143. e.printStackTrace(System.err);
  144. }
  145. }
  146. public void service(HttpServletRequest request, HttpServletResponse response, String contextPath)
  147. throws ServletException {
  148. this.request = request;
  149. this.response = response;
  150. send(request.getMethod(),
  151. request.getQueryString(),
  152. request.getRequestURI(),
  153. contextPath,
  154. request.getContentType(),
  155. request.getContentLength(),
  156. request.getRemoteUser(),
  157. display_source_mode
  158. );
  159. try {
  160. if (stream != null) stream.close();
  161. }
  162. catch (IOException e) {
  163. throw new ServletException(e.toString());
  164. }
  165. }
  166. public void service(HttpServletRequest request, HttpServletResponse response)
  167. throws ServletException {
  168. String servletPath = request.getServletPath();
  169. String contextPath = getServletContext().getRealPath(servletPath);
  170. service(request, response, contextPath);
  171. }
  172. public void destroy() {
  173. if (0 == --startup_count) shutdown();
  174. super.destroy();
  175. }
  176. }