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.

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