summaryrefslogtreecommitdiff
path: root/contrib/uwsgi.java
blob: c3eadcbada8bc2a60972d645ae5b95cd8e4af903 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Enumeration;
import org.apache.catalina.util.IOTools;

public class uwsgi extends HttpServlet {
	private String mountpoint = null;

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

		String servletName = getServletConfig().getServletName();
		if (servletName == null)
			servletName = "";
		if (servletName.startsWith("org.apache.catalina.INVOKER."))
			throw new UnavailableException
				("Cannot invoke uWSGIServlet through the invoker");

		if (getServletConfig().getInitParameter("mountpoint") != null) {
			mountpoint = getServletConfig().getInitParameter("mountpoint");
			if (mountpoint.length() <= 1) {
				mountpoint = null;
			}
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		uWSGIHandler(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		uWSGIHandler(request, response);
	}


	private void uWSGIHandler(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {

		Socket client = new Socket("localhost",3017);
		ByteArrayOutputStream uwsgi_request = new ByteArrayOutputStream();
		DataOutputStream wos = new DataOutputStream(uwsgi_request); 

		DataOutputStream os = new DataOutputStream(client.getOutputStream());
		DataInputStream is = new DataInputStream( client.getInputStream() );


		ServletOutputStream out = response.getOutputStream();

		boolean hasBody = false;

		wos.writeShort(swapShort( (short) 14));
		wos.writeBytes("REQUEST_METHOD");
		wos.writeShort(swapShort( (short) request.getMethod().length() ) );
		wos.writeBytes( request.getMethod() );

		wos.writeShort(swapShort( (short) 12));
		wos.writeBytes("QUERY_STRING");
		if (request.getQueryString() != null) {
			wos.writeShort(swapShort( (short) request.getQueryString().length() ) );
			wos.writeBytes( request.getQueryString() );
		}
		else {
			wos.writeShort(0);
		}

		wos.writeShort(swapShort( (short) 11));
		wos.writeBytes("SERVER_NAME");
		wos.writeShort(swapShort( (short) request.getServerName().length() ) );
		wos.writeBytes( request.getServerName() );

		wos.writeShort(swapShort( (short) 11));
		wos.writeBytes("SERVER_PORT");
		wos.writeShort(swapShort( (short) Integer.toString(request.getServerPort()).length() ) );
		wos.writeBytes( Integer.toString(request.getServerPort()) );

		wos.writeShort(swapShort( (short) 15));
		wos.writeBytes("SERVER_PROTOCOL");
		wos.writeShort(swapShort( (short) request.getProtocol().length() ) );
		wos.writeBytes( request.getProtocol() );

		wos.writeShort(swapShort( (short) 11));
		wos.writeBytes("REQUEST_URI");
		if (request.getQueryString() != null) {
			if (request.getQueryString().length() > 0) {
				wos.writeShort(swapShort( (short) (request.getRequestURI().length()+1+request.getQueryString().length()) ) );
				wos.writeBytes( request.getRequestURI()+"?"+request.getQueryString() );
			}
			else {
				wos.writeShort(swapShort( (short) request.getRequestURI().length() ) );
				wos.writeBytes( request.getRequestURI() );
			}
		}
		else {
			wos.writeShort(swapShort( (short) request.getRequestURI().length() ) );
			wos.writeBytes( request.getRequestURI() );
		}


		if (mountpoint != null) {
			wos.writeShort(swapShort( (short) 11));
			wos.writeBytes("SCRIPT_NAME");
			wos.writeShort(swapShort( (short) mountpoint.length() ) );
			wos.writeBytes( mountpoint );
		}


		wos.writeShort(swapShort( (short) 9));
		wos.writeBytes("PATH_INFO");
		if (mountpoint != null) {
			wos.writeShort(swapShort( (short) (request.getRequestURI().length() - mountpoint.length()) ) );
			wos.writeBytes( request.getRequestURI().substring(mountpoint.length())  );
		}
		else {
			wos.writeShort(swapShort( (short) request.getRequestURI().length() ) );
			wos.writeBytes( request.getRequestURI() );
		}

		wos.writeShort(swapShort( (short) 11));
		wos.writeBytes("REMOTE_ADDR");
		wos.writeShort(swapShort( (short) request.getRemoteAddr().length() ) );
		wos.writeBytes( request.getRemoteAddr() );

		if (request.getRemoteUser() != null) {
			wos.writeShort(swapShort( (short) 11));
			wos.writeBytes("REMOTE_USER");
			wos.writeShort(swapShort( (short) request.getRemoteUser().length() ) );
			wos.writeBytes( request.getRemoteUser() );
		}

		if (request.getContentType() != null) {
			wos.writeShort(swapShort( (short) 12));
			wos.writeBytes("CONTENT_TYPE");
			wos.writeShort(swapShort( (short) request.getContentType().length() ) );
			wos.writeBytes( request.getContentType() );
		}

		if (request.getContentLength() > 0) {
			wos.writeShort(swapShort( (short) 14));
			wos.writeBytes("CONTENT_LENGTH");
			String sContentLength = new Integer(request.getContentLength()).toString();
			wos.writeShort(swapShort( (short) sContentLength.length() ) );
			wos.writeBytes( sContentLength );
			hasBody = true;
		}


		// taken from CGI servlet

		Enumeration headers = request.getHeaderNames();
		String header = null;
		while (headers.hasMoreElements()) {
			header = null;
			header = ((String) headers.nextElement()).toUpperCase();
			wos.writeShort(swapShort( (short) ("HTTP_" + header.replace('-', '_')).length() ));
			wos.writeBytes("HTTP_" + header.replace('-', '_'));
			wos.writeShort(swapShort( (short) request.getHeader(header).length() ));
			wos.writeBytes( request.getHeader(header) );
		}


		os.writeByte(0);
		os.writeShort( swapShort( (short) wos.size() ) );
		os.writeByte(0);

		uwsgi_request.writeTo( os );


		if (hasBody) {
			IOTools.flow(request.getInputStream(), os);
		}

		boolean statusParsed = false;
		for(;;) {
			String line = byte_readline(is);
			if (line == "") {
				break;
			}
			if (statusParsed == false) {
				String[] status = line.split(" ",3);
				response.setStatus( Integer.parseInt(status[1]) );
				statusParsed = true;
			}
			else {
				String[] keyval = line.split(": ",2);
				if (keyval.length == 2) {
					response.addHeader(keyval[0], keyval[1]);
				}
				else {
					break;
				}
			}
		}

		byte cb[] = new byte[4096];
		int len = 0;
		for(;;) {
			len = is.read(cb,0,4096);
			if ( len > 0) {
				out.write(cb,0,len);	
			}
			else {
				break;
			}
		}


	}

	private String byte_readline(DataInputStream is) throws IOException {

		ByteArrayOutputStream line = new ByteArrayOutputStream();
		for(;;) {
			byte b = is.readByte();
			if (b == 10) {
				break;
			}
			line.write(b);
		}

		return line.toString("ASCII").replaceAll("\\n","").replaceAll("\\r","");
	}

	private short swapShort(short x) {

		short tmp1 = (short) (x>>8);
		short tmp2 = (short) (x<<8);
		return (short) (tmp1 | tmp2);
	}	

}