Skip to content

Instantly share code, notes, and snippets.

@akx
Created July 20, 2014 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akx/7a362c04a76722a6a055 to your computer and use it in GitHub Desktop.
Save akx/7a362c04a76722a6a055 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.util.IOTools;
public class uwsgi
extends HttpServlet
{
private String mountpoint = null;
public void init(ServletConfig paramServletConfig)
throws ServletException
{
super.init(paramServletConfig);
String str = getServletConfig().getServletName();
if (str == null) {
str = "";
}
if (str.startsWith("org.apache.catalina.INVOKER.")) {
throw new UnavailableException("Cannot invoke uWSGIServlet through the invoker");
}
if (getServletConfig().getInitParameter("mountpoint") != null)
{
this.mountpoint = getServletConfig().getInitParameter("mountpoint");
if (this.mountpoint.length() <= 1) {
this.mountpoint = null;
}
}
}
public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws IOException, ServletException
{
uWSGIHandler(paramHttpServletRequest, paramHttpServletResponse);
}
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws IOException, ServletException
{
uWSGIHandler(paramHttpServletRequest, paramHttpServletResponse);
}
private void uWSGIHandler(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws IOException, ServletException
{
Socket localSocket = new Socket("localhost", 3017);
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream localDataOutputStream1 = new DataOutputStream(localByteArrayOutputStream);
DataOutputStream localDataOutputStream2 = new DataOutputStream(localSocket.getOutputStream());
DataInputStream localDataInputStream = new DataInputStream(localSocket.getInputStream());
ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
int i = 0;
localDataOutputStream1.writeShort(swapShort((short)14));
localDataOutputStream1.writeBytes("REQUEST_METHOD");
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getMethod().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getMethod());
localDataOutputStream1.writeShort(swapShort((short)12));
localDataOutputStream1.writeBytes("QUERY_STRING");
if (paramHttpServletRequest.getQueryString() != null)
{
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getQueryString().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getQueryString());
}
else
{
localDataOutputStream1.writeShort(0);
}
localDataOutputStream1.writeShort(swapShort((short)11));
localDataOutputStream1.writeBytes("SERVER_NAME");
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getServerName().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getServerName());
localDataOutputStream1.writeShort(swapShort((short)11));
localDataOutputStream1.writeBytes("SERVER_PORT");
localDataOutputStream1.writeShort(swapShort((short)Integer.toString(paramHttpServletRequest.getServerPort()).length()));
localDataOutputStream1.writeBytes(Integer.toString(paramHttpServletRequest.getServerPort()));
localDataOutputStream1.writeShort(swapShort((short)15));
localDataOutputStream1.writeBytes("SERVER_PROTOCOL");
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getProtocol().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getProtocol());
localDataOutputStream1.writeShort(swapShort((short)11));
localDataOutputStream1.writeBytes("REQUEST_URI");
if (paramHttpServletRequest.getQueryString() != null)
{
if (paramHttpServletRequest.getQueryString().length() > 0)
{
localDataOutputStream1.writeShort(swapShort((short)(paramHttpServletRequest.getRequestURI().length() + 1 + paramHttpServletRequest.getQueryString().length())));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRequestURI() + "?" + paramHttpServletRequest.getQueryString());
}
else
{
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getRequestURI().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRequestURI());
}
}
else
{
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getRequestURI().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRequestURI());
}
if (this.mountpoint != null)
{
localDataOutputStream1.writeShort(swapShort((short)11));
localDataOutputStream1.writeBytes("SCRIPT_NAME");
localDataOutputStream1.writeShort(swapShort((short)this.mountpoint.length()));
localDataOutputStream1.writeBytes(this.mountpoint);
}
localDataOutputStream1.writeShort(swapShort((short)9));
localDataOutputStream1.writeBytes("PATH_INFO");
if (this.mountpoint != null)
{
localDataOutputStream1.writeShort(swapShort((short)(paramHttpServletRequest.getRequestURI().length() - this.mountpoint.length())));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRequestURI().substring(this.mountpoint.length()));
}
else
{
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getRequestURI().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRequestURI());
}
localDataOutputStream1.writeShort(swapShort((short)11));
localDataOutputStream1.writeBytes("REMOTE_ADDR");
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getRemoteAddr().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRemoteAddr());
if (paramHttpServletRequest.getRemoteUser() != null)
{
localDataOutputStream1.writeShort(swapShort((short)11));
localDataOutputStream1.writeBytes("REMOTE_USER");
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getRemoteUser().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getRemoteUser());
}
if (paramHttpServletRequest.getContentType() != null)
{
localDataOutputStream1.writeShort(swapShort((short)12));
localDataOutputStream1.writeBytes("CONTENT_TYPE");
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getContentType().length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getContentType());
}
if (paramHttpServletRequest.getContentLength() > 0)
{
localDataOutputStream1.writeShort(swapShort((short)14));
localDataOutputStream1.writeBytes("CONTENT_LENGTH");
localObject1 = new Integer(paramHttpServletRequest.getContentLength()).toString();
localDataOutputStream1.writeShort(swapShort((short)((String)localObject1).length()));
localDataOutputStream1.writeBytes((String)localObject1);
i = 1;
}
Object localObject1 = paramHttpServletRequest.getHeaderNames();
String str = null;
while (((Enumeration)localObject1).hasMoreElements())
{
str = null;
str = ((String)((Enumeration)localObject1).nextElement()).toUpperCase();
localDataOutputStream1.writeShort(swapShort((short)("HTTP_" + str.replace('-', '_')).length()));
localDataOutputStream1.writeBytes("HTTP_" + str.replace('-', '_'));
localDataOutputStream1.writeShort(swapShort((short)paramHttpServletRequest.getHeader(str).length()));
localDataOutputStream1.writeBytes(paramHttpServletRequest.getHeader(str));
}
localDataOutputStream2.writeByte(0);
localDataOutputStream2.writeShort(swapShort((short)localDataOutputStream1.size()));
localDataOutputStream2.writeByte(0);
localByteArrayOutputStream.writeTo(localDataOutputStream2);
if (i != 0) {
IOTools.flow(paramHttpServletRequest.getInputStream(), localDataOutputStream2);
}
int j = 0;
for (;;)
{
localObject2 = byte_readline(localDataInputStream);
if (localObject2 == "") {
break;
}
String[] arrayOfString;
if (j == 0)
{
arrayOfString = ((String)localObject2).split(" ", 3);
paramHttpServletResponse.setStatus(Integer.parseInt(arrayOfString[1]));
j = 1;
}
else
{
arrayOfString = ((String)localObject2).split(": ", 2);
if (arrayOfString.length != 2) {
break;
}
paramHttpServletResponse.addHeader(arrayOfString[0], arrayOfString[1]);
}
}
Object localObject2 = new byte[4096];
int k = 0;
for (;;)
{
k = localDataInputStream.read((byte[])localObject2, 0, 4096);
if (k <= 0) {
break;
}
localServletOutputStream.write((byte[])localObject2, 0, k);
}
}
private String byte_readline(DataInputStream paramDataInputStream)
throws IOException
{
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
for (;;)
{
int i = paramDataInputStream.readByte();
if (i == 10) {
break;
}
localByteArrayOutputStream.write(i);
}
return localByteArrayOutputStream.toString("ASCII").replaceAll("\\n", "").replaceAll("\\r", "");
}
private short swapShort(short paramShort)
{
int i = (short)(paramShort >> 8);
int j = (short)(paramShort << 8);
return (short)(i | j);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment