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.

144 lines
5.0 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. public class servlet extends HttpServlet {
  25. char slash=System.getProperty("file.separator").charAt(0);
  26. HttpServletRequest request;
  27. HttpServletResponse response;
  28. ServletInputStream stream;
  29. static int startup_count = 0;
  30. protected boolean display_source_mode = false;
  31. /******************************************************************/
  32. /* native methods */
  33. /******************************************************************/
  34. static { reflect.loadLibrary("servlet"); }
  35. native void startup();
  36. native long define(String name);
  37. native void send(String requestMethod, String queryString,
  38. String pathInfo, String pathTranslated,
  39. String contentType, int contentLength, String authUser,
  40. boolean display_source_mode);
  41. native void shutdown();
  42. /******************************************************************/
  43. /* sapi callbacks */
  44. /******************************************************************/
  45. String readPost(int bytes) {
  46. String result;
  47. if (!request.getMethod().equals("POST")) {
  48. result = request.getQueryString();
  49. } else {
  50. Enumeration e = request.getParameterNames();
  51. result="";
  52. String concat="";
  53. while (e.hasMoreElements()) {
  54. String name = (String)e.nextElement();
  55. String value = request.getParameter(name);
  56. result+=concat+name+"="+URLEncoder.encode(value);
  57. concat="&";
  58. }
  59. }
  60. if (result == null) return "";
  61. return result;
  62. }
  63. String readCookies() {
  64. reflect.setResult(define("request"), request);
  65. reflect.setResult(define("response"), response);
  66. return request.getHeader("cookie");
  67. }
  68. void header(String data) {
  69. try {
  70. if (data.startsWith("Content-type: ")) {
  71. response.setContentType(data.substring(data.indexOf(" ")+1));
  72. } else if (data.startsWith("Location: ")) {
  73. response.sendRedirect(data.substring(data.indexOf(" ")+1));
  74. } else {
  75. int colon = data.indexOf(": ");
  76. if (colon > 0) {
  77. response.addHeader(data.substring(0,colon),
  78. data.substring(colon+2));
  79. } else {
  80. response.getWriter().println(data);
  81. }
  82. }
  83. } catch (IOException e) {
  84. System.err.print(data);
  85. }
  86. }
  87. void write(String data) {
  88. try {
  89. response.getWriter().print(data);
  90. } catch (IOException e) {
  91. System.err.print(data);
  92. }
  93. }
  94. /******************************************************************/
  95. /* servlet interface */
  96. /******************************************************************/
  97. public void init(ServletConfig config) throws ServletException {
  98. super.init(config);
  99. if (0 == startup_count++) startup();
  100. }
  101. public void service(HttpServletRequest request,
  102. HttpServletResponse response)
  103. throws ServletException
  104. {
  105. this.request=request;
  106. this.response=response;
  107. String servletPath=request.getServletPath();
  108. String contextPath=getServletContext().getRealPath(servletPath);
  109. send(request.getMethod(), request.getQueryString(),
  110. request.getPathInfo(), contextPath,
  111. request.getContentType(), request.getContentLength(),
  112. request.getRemoteUser(), display_source_mode);
  113. try {
  114. if (stream != null) stream.close();
  115. } catch (IOException e) {
  116. throw new ServletException(e);
  117. }
  118. }
  119. public void destroy() {
  120. if (0 == --startup_count) shutdown();
  121. super.destroy();
  122. }
  123. }