summaryrefslogtreecommitdiff
path: root/sapi/servlet/servlet.java
blob: 62be4f1d2c827a9f9413fdaa554285146e113404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999 The PHP Group                         |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_0.txt.                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Sam Ruby (rubys@us.ibm.com)                                  |
   +----------------------------------------------------------------------+
*/

package net.php;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class servlet extends HttpServlet {

    char slash=System.getProperty("file.separator").charAt(0);
    HttpServletRequest request;
    HttpServletResponse response;
    ServletInputStream stream;

    /******************************************************************/
    /*                          native methods                        */ 
    /******************************************************************/

    static { reflect.loadLibrary("servlet"); }
    native void startup();
    native long define(String name);
    native void send(String requestMethod, String queryString,
      String pathInfo, String pathTranslated,
      String contentType, int contentLength, String authUser);
    native void shutdown();

    /******************************************************************/
    /*                         sapi callbacks                         */ 
    /******************************************************************/

    String readPost(int bytes) {
       try {
         if (stream == null) stream=request.getInputStream();
         byte[] data = new byte[bytes];
         int len = stream.read(data, 0, bytes);
         if (len <= 0) return "";
         return new String(data, 0, len);
       } catch (IOException e) {
         return "";
       }
    }

    String readCookies() {
       reflect.setResult(define("request"), request);
       reflect.setResult(define("response"), response);
       return request.getHeader("cookie");
    }

    void header(String data) {
      try {
        if (data.startsWith("Content-Type: ")) {
          response.setContentType(data.substring(data.indexOf(" ")+1));
        } else if (data.startsWith("Location: ")) {
          response.sendRedirect(data.substring(data.indexOf(" ")+1));
        } else {
          int colon = data.indexOf(": ");
          if (colon > 0) {
            response.addHeader(data.substring(0,colon),
              data.substring(colon+2));
          } else {
            response.getWriter().println(data);
          }
        }
      } catch (IOException e) {
        System.err.print(data);
      }
    }

    void write(String data) {
      try {
        response.getWriter().print(data);
      } catch (IOException e) {
        System.err.print(data);
      }
    }

    /******************************************************************/
    /*                        servlet interface                       */ 
    /******************************************************************/

    public void init(ServletConfig config) throws ServletException {
       super.init(config);
       startup();
    }

    public void service(HttpServletRequest request,
                        HttpServletResponse response) 
       throws ServletException
    {
       this.request=request;
       this.response=response;
       send(request.getMethod(), request.getQueryString(),
            request.getPathInfo(), getPathTranslated(), 
            request.getContentType(), request.getContentLength(),
	    request.getRemoteUser());

       try {
         if (stream != null) stream.close();
       } catch (IOException e) {
         throw new ServletException(e);
       }
    }

    public void destroy() {
      shutdown();
      super.destroy();
    }

    /******************************************************************/
    /*                         utility function                       */ 
    /******************************************************************/

    String getPathTranslated() {
       /* I have no idea why this has to be this hard... */
       String servletPath=request.getServletPath();
       String contextPath=getServletContext().getRealPath(servletPath);
       servletPath=servletPath.replace('/',slash);
       contextPath=contextPath.substring(0,contextPath.lastIndexOf(slash));
       return contextPath+servletPath;
    }
}