HTTP-Based Anonymous Communication Channels
by Marc Waldman and Stefan Kopsell 


Listing One

SocketChannel japConnection;
try{
   japConnection=SocketChannel.open();
   japConnection.connect(new
      InetSocketAddress("localhost",4000));
}
catch(Exception e){
   System.err.println("Unable to connect to JAP - exiting");
   System.exit(1);
}

Listing Two

public class HTTPMessageHeader{
   private static final String CONTENT_LENGTH="Content-Length: ";
   private static final String MESSAGE_ID="Anon-Message-ID: ";
   private static final String CRLF="\r\n";
   private static final String OCTET_CONTENT="Content-Type: octet/stream";
   private static final String HTTP_OK_RESPONSE="HTTP/1.0 200 OK";
   
   public static ByteBuffer prepareRequestMessageHeader(String recipient, 
                                            int messageID, int contentLength){
      StringBuffer sb=new StringBuffer("POST http://");
      sb.append(recipient); 
                 // recipient string should include a colon and port number
      sb.append(" HTTP/1.0"+CRLF);
      sb.append(CONTENT_LENGTH+contentLength+CRLF);
      sb.append(OCTET_CONTENT+CRLF);
      sb.append(MESSAGE_ID+messageID);
      sb.append(CRLF+CRLF);
      try{
         return ByteBuffer.wrap((sb.toString()).getBytes("US-ASCII"));
      }
      catch(Exception e){
         return null;
     }
}

public static ByteBuffer prepareReplyMessageHeader(int messageID, int contentLength){
   StringBuffer sb=new StringBuffer(HTTP_OK_RESPONSE+CRLF);
   sb.append(OCTET_CONTENT+CRLF);
   sb.append(MESSAGE_ID);
   sb.append(messageID+CRLF);
   sb.append(CONTENT_LENGTH);
   sb.append(contentLength);
   sb.append(CRLF+CRLF);
   try{
      return ByteBuffer.wrap((sb.toString()).getBytes("US-ASCII"));
   }
   catch(UnsupportedEncodingException e){
      return null;
   }
 }
}

Listing Three

public void sendMessage(Object sendObject) throws Exception{
   String recipient="courseserver.edu:1765";
   SocketChannel japConnection=null;
   int messageID=1;
   // connect to JAP
   try{
      japConnection=SocketChannel.open();
      japConnection.connect(new InetSocketAddress("localhost",4000));
   }
   catch(Exception e){
      System.err.println("Unable to connect to JAP - exiting");
      System.exit(1);
   }
   // serialize sendObject
   ByteArrayOutputStream baos=new ByteArrayOutputStream();
   ObjectOutputStream oos=new ObjectOutputStream(baos);
   oos.writeObject(sendObject);
   oos.close();
   ByteBuffer bb=HTTPMessageHeader.prepareRequestMessageHeader(recipient,
                                                     messageID, baos.size());
   if (bb==null){
      System.err.println("Unable to create HTTP Header");
      System.exit(1);
   }
   ByteBuffer objectBB=ByteBuffer.wrap(baos.toByteArray());
   ByteBuffer bbArray[]=new ByteBuffer[2];
   bbArray[0]=bb;
   bbArray[1]=objectBB;
   // send the header and object to JAP
   japConnection.write(bbArray);
}





2

