HttpTunneling – Proof of Concept

Here is the code for the HttpTunnelServer in project tunnel in package tunnel residing on the web server. (Tomcat 4.1\webapps\tunnel\WEB-INF\classes\tunnel\HttpTunnelServer.class) Note that HttpTunnelServer is simply a java servlet.


package tunnel;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpTunnelServer extends HttpServlet {

	public void doPost(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {

		// get the data the client sent

		ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
				request.getInputStream()));
		String messageFromRJ = null;
		try {
			messageFromRJ = (String) in.readObject();
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
		System.out.println("Got from the client [" + messageFromRJ + "]");
		in.close();
		// send reply to client

		ObjectOutputStream out = new ObjectOutputStream(
				new BufferedOutputStream(response.getOutputStream()));
		out.writeObject("Hi RJ, this is the server");

		out.flush();
		out.close();

	}

	public void doGet(javax.servlet.http.HttpServletRequest request,
			javax.servlet.http.HttpServletResponse response)
			throws javax.servlet.ServletException, java.io.IOException {

		throw new ServletException("I expect only doPosts!!");

	}

}

web.xml (Tomcat 4.1\webapps\tunnel\WEB-INF\web.xml) looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>fopwork</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>HttpTunnelServer</servlet-name>
        <servlet-class>tunnel.HttpTunnelServer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HttpTunnelServer</servlet-name>
        <url-pattern>/HttpTunnelServer</url-pattern>
    </servlet-mapping>
</web-app>

The Client, which calls the HttpTunnelServer, looks as below. Start web server and run the Client class as a standalone application from its location anywhere on the machine
Viz. java Client


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Client {

	public static void main(String[] args) {

		try {
			URL servletToCall = new URL(
					"http://localhost:8080/tunnel/HttpTunnelServer");
			URLConnection servletConn = servletToCall.openConnection();
			servletConn.setUseCaches(false);

			// send data to server

			servletConn.setDoOutput(true);
			servletConn.setRequestProperty("Content-Type",
					"application/octet-stream");
			ObjectOutputStream out = new ObjectOutputStream(
					new BufferedOutputStream(servletConn.getOutputStream()));
			String sendMeToServer = "Hey Server, this is R.J. - how's it going?";
			out.writeObject(sendMeToServer);
			out.flush();

			// get reply from server

			// use same connection we got earlier, no need to open another one

			ObjectInputStream in = new ObjectInputStream(
					new BufferedInputStream(servletConn.getInputStream()));
			String replyFromServer = (String) in.readObject();
			System.out.println("Got reply from server [" + replyFromServer
					+ "]");

		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}
}

Output on the client side

D:\m2m\tunnel_ex>java Client
Got reply from server [Hi RJ, this is the server]

Corresponding output on the web server

INFO: Jk running ID=0 time=0/31 config=C:\:Program Files\Apache Group\Tomcat 4.1\conf\jk2.properties
Got from the client [Hey Server, this is R.J. – how’s it going?]

Note: Do not call the HttpTunnelServer directly on a browser: it will simply throw an exception.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: