beginning of format, updated imports. (shendaras)

This commit is contained in:
shendaras
2004-04-10 11:45:02 +00:00
committed by zzz
parent 8a8e68146f
commit 17a1b11f66
37 changed files with 3546 additions and 3511 deletions

View File

@ -27,54 +27,51 @@ public class HTTPListener extends Thread {
* @param port A port, to connect to.
* @param listenHost A host, to connect to.
*/
public HTTPListener(SocketManagerProducer smp, int port,
String listenHost) {
this.smp = smp;
this.port = port;
start();
public HTTPListener(SocketManagerProducer smp, int port, String listenHost) {
this.smp = smp;
this.port = port;
start();
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
try {
InetAddress lh = listenHost == null
? null
: InetAddress.getByName(listenHost);
ServerSocket ss = new ServerSocket(port, 0, lh);
while(true) {
Socket s = ss.accept();
new HTTPSocketHandler(this, s);
}
} catch (IOException ex) {
_log.error("Error while accepting connections", ex);
}
try {
InetAddress lh = listenHost == null ? null : InetAddress.getByName(listenHost);
ServerSocket ss = new ServerSocket(port, 0, lh);
while (true) {
Socket s = ss.accept();
new HTTPSocketHandler(this, s);
}
} catch (IOException ex) {
_log.error("Error while accepting connections", ex);
}
}
private boolean proxyUsed=false;
private boolean proxyUsed = false;
/**
* Query whether this is the first use of the proxy or not . . .
* @return Whether this is the first proxy use, no doubt.
*/
public boolean firstProxyUse() {
// FIXME: check a config option here
if (true) return false;
if (proxyUsed) {
return false;
} else {
proxyUsed=true;
return true;
}
// FIXME: check a config option here
if (true) return false;
if (proxyUsed) {
return false;
} else {
proxyUsed = true;
return true;
}
}
/**
* @return The SocketManagerProducer being used.
*/
public SocketManagerProducer getSMP() {
return smp;
return smp;
}
/**
@ -84,8 +81,7 @@ public class HTTPListener extends Thread {
* @throws IOException
*/
public void handleNotImplemented(OutputStream out) throws IOException {
out.write(("HTTP/1.1 200 Document following\n\n"+
"<h1>Feature not implemented</h1>").getBytes("ISO-8859-1"));
out.flush();
out.write(("HTTP/1.1 200 Document following\n\n" + "<h1>Feature not implemented</h1>").getBytes("ISO-8859-1"));
out.flush();
}
}
}

View File

@ -27,37 +27,36 @@ public class HTTPSocketHandler extends Thread {
* @param s A socket.
*/
public HTTPSocketHandler(HTTPListener httpl, Socket s) {
this.httpl = httpl;
this.s=s;
h = RootHandler.getInstance();
start();
this.httpl = httpl;
this.s = s;
h = RootHandler.getInstance();
start();
}
/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(s.getInputStream());
out = new BufferedOutputStream(s.getOutputStream());
Request req = new Request(in);
h.handle(req, httpl, out);
} catch (IOException ex) {
_log.error("Error while handling data", ex);
} finally {
try {
if (in != null) in.close();
if (out != null) {
out.flush();
out.close();
}
s.close();
} catch (IOException ex) {
_log.error("IOException in finalizer", ex);
}
}
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(s.getInputStream());
out = new BufferedOutputStream(s.getOutputStream());
Request req = new Request(in);
h.handle(req, httpl, out);
} catch (IOException ex) {
_log.error("Error while handling data", ex);
} finally {
try {
if (in != null) in.close();
if (out != null) {
out.flush();
out.close();
}
s.close();
} catch (IOException ex) {
_log.error("IOException in finalizer", ex);
}
}
}
}
}

View File

@ -44,10 +44,9 @@ public class HTTPTunnel {
* @param shouldThrowAwayManagers whether to throw away a manager after use
* @param listenPort which port to listen on
*/
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers,
boolean shouldThrowAwayManagers, int listenPort) {
this(initialManagers, maxManagers, shouldThrowAwayManagers, listenPort,
"127.0.0.1", 7654);
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers, boolean shouldThrowAwayManagers,
int listenPort) {
this(initialManagers, maxManagers, shouldThrowAwayManagers, listenPort, "127.0.0.1", 7654);
}
/**
@ -60,56 +59,50 @@ public class HTTPTunnel {
* @param i2cpAddress the I2CP address
* @param i2cpPort the I2CP port
*/
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers,
boolean shouldThrowAwayManagers, int listenPort,
String i2cpAddress, int i2cpPort) {
SocketManagerProducer smp =
new SocketManagerProducer(initialManagers, maxManagers,
shouldThrowAwayManagers, i2cpAddress, i2cpPort);
new HTTPListener(smp, listenPort, "127.0.0.1");
public HTTPTunnel(I2PSocketManager[] initialManagers, int maxManagers, boolean shouldThrowAwayManagers,
int listenPort, String i2cpAddress, int i2cpPort) {
SocketManagerProducer smp = new SocketManagerProducer(initialManagers, maxManagers, shouldThrowAwayManagers,
i2cpAddress, i2cpPort);
new HTTPListener(smp, listenPort, "127.0.0.1");
}
/**
* The all important main function, allowing HTTPTunnel to be
* stand-alone, a program in it's own right, and all that jazz.
* @param args A list of String passed to the program
*/
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 7654, max = 1;
boolean throwAwayManagers = false;
if (args.length >1) {
if (args.length == 4) {
host = args[2];
port = Integer.parseInt(args[3]);
} else if (args.length != 2) {
showInfo(); return;
}
max = Integer.parseInt(args[1]);
} else if (args.length != 1) {
showInfo(); return;
}
if (max == 0) {
max = 1;
} else if (max <0) {
max = -max;
throwAwayManagers = true;
}
new HTTPTunnel(null, max, throwAwayManagers, Integer.parseInt(args[0]), host, port);
String host = "127.0.0.1";
int port = 7654, max = 1;
boolean throwAwayManagers = false;
if (args.length > 1) {
if (args.length == 4) {
host = args[2];
port = Integer.parseInt(args[3]);
} else if (args.length != 2) {
showInfo();
return;
}
max = Integer.parseInt(args[1]);
} else if (args.length != 1) {
showInfo();
return;
}
if (max == 0) {
max = 1;
} else if (max < 0) {
max = -max;
throwAwayManagers = true;
}
new HTTPTunnel(null, max, throwAwayManagers, Integer.parseInt(args[0]), host, port);
}
private static void showInfo() {
System.out.println
("Usage: java HTTPTunnel <listenPort> [<max> "+
"[<i2cphost> <i2cpport>]]\n"+
" <listenPort> port to listen for browsers\n"+
" <max> max number of SocketMangers in pool, "+
"use neg. number\n"+
" to use each SocketManager only once "+
"(default: 1)\n"+
" <i2cphost> host to connect to the router "+
"(default: 127.0.0.1)\n"+
" <i2cpport> port to connect to the router "+
"(default: 7654)");
System.out.println("Usage: java HTTPTunnel <listenPort> [<max> " + "[<i2cphost> <i2cpport>]]\n"
+ " <listenPort> port to listen for browsers\n"
+ " <max> max number of SocketMangers in pool, " + "use neg. number\n"
+ " to use each SocketManager only once " + "(default: 1)\n"
+ " <i2cphost> host to connect to the router " + "(default: 127.0.0.1)\n"
+ " <i2cpport> port to connect to the router " + "(default: 7654)");
}
}
}

View File

@ -22,57 +22,56 @@ public class Request {
private String proto;
private String params;
private String postData;
/**
* A constructor, creating a request from an InputStream
* @param in InputStream from which we "read-in" a Request
* @throws IOException
*/
public Request(InputStream in) throws IOException {
BufferedReader br = new BufferedReader
(new InputStreamReader(in, "ISO-8859-1"));
String line = br.readLine();
if (line == null) { // no data at all
method = null;
_log.error("Connection but no data");
return;
}
int pos = line.indexOf(" ");
if (pos == -1) {
method = line;
url="";
_log.error("Malformed HTTP request: "+line);
} else {
method = line.substring(0,pos);
url=line.substring(pos+1);
}
proto="";
pos = url.indexOf(" ");
if (pos != -1) {
proto=url.substring(pos); // leading space intended
url = url.substring(0,pos);
}
StringBuffer sb = new StringBuffer(512);
while((line=br.readLine()) != null) {
if (line.length() == 0) break;
sb.append(line).append("\r\n");
}
params = sb.toString(); // no leading empty line!
sb = new StringBuffer();
// hack for POST requests, ripped from HttpClient
// this won't work for large POSTDATA
// FIXME: do this better, please.
if (!method.equals("GET")) {
while (br.ready()) { // empty the buffer (POST requests)
int i=br.read();
if (i != -1) {
sb.append((char)i);
}
}
postData = sb.toString();
} else {
postData="";
}
BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1"));
String line = br.readLine();
if (line == null) { // no data at all
method = null;
_log.error("Connection but no data");
return;
}
int pos = line.indexOf(" ");
if (pos == -1) {
method = line;
url = "";
_log.error("Malformed HTTP request: " + line);
} else {
method = line.substring(0, pos);
url = line.substring(pos + 1);
}
proto = "";
pos = url.indexOf(" ");
if (pos != -1) {
proto = url.substring(pos); // leading space intended
url = url.substring(0, pos);
}
StringBuffer sb = new StringBuffer(512);
while ((line = br.readLine()) != null) {
if (line.length() == 0) break;
sb.append(line).append("\r\n");
}
params = sb.toString(); // no leading empty line!
sb = new StringBuffer();
// hack for POST requests, ripped from HttpClient
// this won't work for large POSTDATA
// FIXME: do this better, please.
if (!method.equals("GET")) {
while (br.ready()) { // empty the buffer (POST requests)
int i = br.read();
if (i != -1) {
sb.append((char) i);
}
}
postData = sb.toString();
} else {
postData = "";
}
}
/**
@ -80,21 +79,21 @@ public class Request {
* @throws IOException
*/
public byte[] toByteArray() throws IOException {
if (method == null) return null;
return toISO8859_1String().getBytes("ISO-8859-1");
if (method == null) return null;
return toISO8859_1String().getBytes("ISO-8859-1");
}
private String toISO8859_1String() throws IOException {
if (method == null) return null;
return method+" "+url+proto+"\r\n"+params+"\r\n"+postData;
if (method == null) return null;
return method + " " + url + proto + "\r\n" + params + "\r\n" + postData;
}
/**
* @return the URL of the request
*/
public String getURL() {
return url;
return url;
}
/**
@ -102,7 +101,7 @@ public class Request {
* @param newURL the new URL
*/
public void setURL(String newURL) {
url=newURL;
url = newURL;
}
/**
@ -111,19 +110,17 @@ public class Request {
* @return The value of the param, or null
*/
public String getParam(String name) {
try {
BufferedReader br= new BufferedReader(new StringReader(params));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith(name)) {
return line.substring(name.length());
}
}
return null;
} catch (IOException ex) {
_log.error("Error getting parameter", ex);
return null;
}
try {
BufferedReader br = new BufferedReader(new StringReader(params));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith(name)) { return line.substring(name.length()); }
}
return null;
} catch (IOException ex) {
_log.error("Error getting parameter", ex);
return null;
}
}
/**
@ -132,25 +129,25 @@ public class Request {
* @param value the value to be set
*/
public void setParam(String name, String value) {
try {
StringBuffer sb = new StringBuffer(params.length()+value.length());
BufferedReader br= new BufferedReader(new StringReader(params));
String line;
boolean replaced = false;
while((line=br.readLine()) != null) {
if (line.startsWith(name)) {
replaced=true;
if (value == null) continue; // kill param
line = name+value;
}
sb.append(line).append("\r\n");
}
if (!replaced && value != null) {
sb.append(name).append(value).append("\r\n");
}
params=sb.toString();
} catch (IOException ex) {
_log.error("Error getting parameter", ex);
}
try {
StringBuffer sb = new StringBuffer(params.length() + value.length());
BufferedReader br = new BufferedReader(new StringReader(params));
String line;
boolean replaced = false;
while ((line = br.readLine()) != null) {
if (line.startsWith(name)) {
replaced = true;
if (value == null) continue; // kill param
line = name + value;
}
sb.append(line).append("\r\n");
}
if (!replaced && value != null) {
sb.append(name).append(value).append("\r\n");
}
params = sb.toString();
} catch (IOException ex) {
_log.error("Error getting parameter", ex);
}
}
}
}

View File

@ -29,55 +29,48 @@ public class SocketManagerProducer extends Thread {
* @param host which host to listen on
* @param port which port to listen on
*/
public SocketManagerProducer(I2PSocketManager[] initialManagers,
int maxManagers,
boolean shouldThrowAwayManagers,
String host, int port) {
if (maxManagers < 1) {
throw new IllegalArgumentException("maxManagers < 1");
}
this.host=host;
this.port=port;
this.shouldThrowAwayManagers=shouldThrowAwayManagers;
if (initialManagers != null) {
myManagers.addAll(Arrays.asList(initialManagers));
}
this.maxManagers=maxManagers;
this.shouldThrowAwayManagers=shouldThrowAwayManagers;
setDaemon(true);
start();
public SocketManagerProducer(I2PSocketManager[] initialManagers, int maxManagers, boolean shouldThrowAwayManagers,
String host, int port) {
if (maxManagers < 1) { throw new IllegalArgumentException("maxManagers < 1"); }
this.host = host;
this.port = port;
this.shouldThrowAwayManagers = shouldThrowAwayManagers;
if (initialManagers != null) {
myManagers.addAll(Arrays.asList(initialManagers));
}
this.maxManagers = maxManagers;
this.shouldThrowAwayManagers = shouldThrowAwayManagers;
setDaemon(true);
start();
}
/**
* Thread producing new SocketManagers.
*/
public void run() {
while (true) {
synchronized(this) {
// without mcDonalds mode, we most probably need no
// new managers.
while (!shouldThrowAwayManagers && myManagers.size() == maxManagers) {
myWait();
}
}
// produce a new manager, regardless whether it is needed
// or not. Do not synchronized this part, since it can be
// quite time-consuming.
I2PSocketManager newManager =
I2PSocketManagerFactory.createManager(host, port,
new Properties());
// when done, check if it is needed.
synchronized(this) {
while(myManagers.size() == maxManagers) {
myWait();
}
myManagers.add(newManager);
notifyAll();
}
}
while (true) {
synchronized (this) {
// without mcDonalds mode, we most probably need no
// new managers.
while (!shouldThrowAwayManagers && myManagers.size() == maxManagers) {
myWait();
}
}
// produce a new manager, regardless whether it is needed
// or not. Do not synchronized this part, since it can be
// quite time-consuming.
I2PSocketManager newManager = I2PSocketManagerFactory.createManager(host, port, new Properties());
// when done, check if it is needed.
synchronized (this) {
while (myManagers.size() == maxManagers) {
myWait();
}
myManagers.add(newManager);
notifyAll();
}
}
}
/**
* Get a manager for connecting to a given destination. Each
* destination will always get the same manager.
@ -86,12 +79,12 @@ public class SocketManagerProducer extends Thread {
* @return the SocketManager to use
*/
public synchronized I2PSocketManager getManager(String dest) {
I2PSocketManager result = (I2PSocketManager) usedManagers.get(dest);
if (result == null) {
result = getManager();
usedManagers.put(dest,result);
}
return result;
I2PSocketManager result = (I2PSocketManager) usedManagers.get(dest);
if (result == null) {
result = getManager();
usedManagers.put(dest, result);
}
return result;
}
/**
@ -102,26 +95,26 @@ public class SocketManagerProducer extends Thread {
* @return the SocketManager to use
*/
public synchronized I2PSocketManager getManager() {
while (myManagers.size() == 0) {
myWait(); // no manager here, so wait until one is produced
}
int which = (int)(Math.random()*myManagers.size());
I2PSocketManager result = (I2PSocketManager) myManagers.get(which);
if (shouldThrowAwayManagers) {
myManagers.remove(which);
notifyAll();
}
return result;
while (myManagers.size() == 0) {
myWait(); // no manager here, so wait until one is produced
}
int which = (int) (Math.random() * myManagers.size());
I2PSocketManager result = (I2PSocketManager) myManagers.get(which);
if (shouldThrowAwayManagers) {
myManagers.remove(which);
notifyAll();
}
return result;
}
/**
* Wait until InterruptedException
*/
public void myWait() {
try {
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
try {
wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}

View File

@ -13,50 +13,49 @@ import net.i2p.util.Log;
public class ChainFilter implements Filter {
private static final Log _log = new Log(ChainFilter.class);
private Collection filters; // perhaps protected?
private Collection filters; // perhaps protected?
/**
* @param filters A collection (list) of filters to chain to
*/
public ChainFilter(Collection filters) {
this.filters=filters;
this.filters = filters;
}
/* (non-Javadoc)
* @see net.i2p.httptunnel.filter.Filter#filter(byte[])
*/
public byte[] filter(byte[] toFilter) {
byte[] buf = toFilter;
for (Iterator it = filters.iterator(); it.hasNext();) {
Filter f = (Filter) it.next();
buf = f.filter(buf);
}
return buf;
byte[] buf = toFilter;
for (Iterator it = filters.iterator(); it.hasNext();) {
Filter f = (Filter) it.next();
buf = f.filter(buf);
}
return buf;
}
/* (non-Javadoc)
* @see net.i2p.httptunnel.filter.Filter#finish()
*/
public byte[] finish() {
// this is a bit complicated. Think about it...
try {
byte[] buf = EMPTY;
for (Iterator it = filters.iterator(); it.hasNext();) {
Filter f = (Filter) it.next();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (buf.length != 0) {
baos.write(f.filter(buf));
}
baos.write(f.finish());
buf = baos.toByteArray();
}
return buf;
} catch (IOException ex) {
_log.error("Error chaining filters", ex);
return EMPTY;
}
// this is a bit complicated. Think about it...
try {
byte[] buf = EMPTY;
for (Iterator it = filters.iterator(); it.hasNext();) {
Filter f = (Filter) it.next();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (buf.length != 0) {
baos.write(f.filter(buf));
}
baos.write(f.finish());
buf = baos.toByteArray();
}
return buf;
} catch (IOException ex) {
_log.error("Error chaining filters", ex);
return EMPTY;
}
}
}
}

View File

@ -22,4 +22,4 @@ public interface Filter {
* @return the rest of the data
*/
public byte[] finish();
}
}

View File

@ -9,13 +9,13 @@ public class NullFilter implements Filter {
* @see net.i2p.httptunnel.filter.Filter#filter(byte[])
*/
public byte[] filter(byte[] toFilter) {
return toFilter;
return toFilter;
}
/* (non-Javadoc)
* @see net.i2p.httptunnel.filter.Filter#finish()
*/
public byte[] finish() {
return EMPTY;
return EMPTY;
}
}
}

View File

@ -1,4 +1,5 @@
package net.i2p.httptunnel.handler;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
@ -26,11 +27,11 @@ public class EepHandler {
private static final Log _log = new Log(EepHandler.class);
protected ErrorHandler errorHandler;
/* package private */ EepHandler(ErrorHandler eh) {
errorHandler=eh;
/* package private */EepHandler(ErrorHandler eh) {
errorHandler = eh;
}
/**
* @param req the Request
* @param httpl an HTTPListener
@ -40,21 +41,19 @@ public class EepHandler {
* @throws IOException
*/
public void handle(Request req, HTTPListener httpl, OutputStream out,
/* boolean fromProxy, */ String destination)
throws IOException {
SocketManagerProducer smp = httpl.getSMP();
Destination dest = NamingService.getInstance().lookup(destination);
if (dest == null) {
errorHandler.handle(req, httpl, out,
"Could not lookup host: "+destination);
return;
}
I2PSocketManager sm = smp.getManager(destination);
Filter f = new NullFilter(); //FIXME: use other filter
req.setParam("Host: ", dest.toBase64());
if (!handle(req, f, out, dest, sm)) {
errorHandler.handle(req, httpl, out, "Unable to reach peer");
}
/* boolean fromProxy, */String destination) throws IOException {
SocketManagerProducer smp = httpl.getSMP();
Destination dest = NamingService.getInstance().lookup(destination);
if (dest == null) {
errorHandler.handle(req, httpl, out, "Could not lookup host: " + destination);
return;
}
I2PSocketManager sm = smp.getManager(destination);
Filter f = new NullFilter(); //FIXME: use other filter
req.setParam("Host: ", dest.toBase64());
if (!handle(req, f, out, dest, sm)) {
errorHandler.handle(req, httpl, out, "Unable to reach peer");
}
}
/**
@ -66,45 +65,44 @@ public class EepHandler {
* @return boolean, true if something was written, false otherwise.
* @throws IOException
*/
public boolean handle(Request req, Filter f, OutputStream out,
Destination dest, I2PSocketManager sm)
throws IOException {
I2PSocket s = null;
boolean written = false;
try {
synchronized(sm) {
s = sm.connect(dest, new I2PSocketOptions());
}
InputStream in = new BufferedInputStream(s.getInputStream());
OutputStream sout = new BufferedOutputStream(s.getOutputStream());
sout.write(req.toByteArray());
sout.flush();
byte[] buffer = new byte[16384], filtered;
int len;
while ((len=in.read(buffer)) != -1) {
if (len != buffer.length) {
byte[] b2 = new byte[len];
System.arraycopy(buffer, 0, b2, 0, len);
filtered=f.filter(b2);
} else {
filtered=f.filter(buffer);
}
written=true;
out.write(filtered);
}
filtered=f.finish();
written=true;
out.write(filtered);
out.flush();
} catch (IOException ex) {
_log.error("Error while handling eepsite request");
return written;
} catch (I2PException ex) {
_log.error("Error while handling eepsite request");
return written;
} finally {
if (s != null) s.close();
}
return true;
public boolean handle(Request req, Filter f, OutputStream out, Destination dest, I2PSocketManager sm)
throws IOException {
I2PSocket s = null;
boolean written = false;
try {
synchronized (sm) {
s = sm.connect(dest, new I2PSocketOptions());
}
InputStream in = new BufferedInputStream(s.getInputStream());
OutputStream sout = new BufferedOutputStream(s.getOutputStream());
sout.write(req.toByteArray());
sout.flush();
byte[] buffer = new byte[16384], filtered;
int len;
while ((len = in.read(buffer)) != -1) {
if (len != buffer.length) {
byte[] b2 = new byte[len];
System.arraycopy(buffer, 0, b2, 0, len);
filtered = f.filter(b2);
} else {
filtered = f.filter(buffer);
}
written = true;
out.write(filtered);
}
filtered = f.finish();
written = true;
out.write(filtered);
out.flush();
} catch (IOException ex) {
_log.error("Error while handling eepsite request");
return written;
} catch (I2PException ex) {
_log.error("Error while handling eepsite request");
return written;
} finally {
if (s != null) s.close();
}
return true;
}
}
}

View File

@ -1,4 +1,5 @@
package net.i2p.httptunnel.handler;
import java.io.IOException;
import java.io.OutputStream;
@ -13,10 +14,10 @@ public class ErrorHandler {
private static final Log _log = new Log(ErrorHandler.class);
/* package private */ ErrorHandler() {
/* package private */ErrorHandler() {
}
/**
* @param req the Request
* @param httpl an HTTPListener
@ -24,20 +25,17 @@ public class ErrorHandler {
* @param error the error that happened
* @throws IOException
*/
public void handle(Request req, HTTPListener httpl,
OutputStream out, String error) throws IOException {
// FIXME: Make nicer messages for more likely errors.
out.write(("HTTP/1.1 500 Internal Server Error\r\n"+
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n")
.getBytes("ISO-8859-1"));
out.write(("<html><head><title>"+error+"</title></head><body><h1>"+
error+"</h1>An internal error occurred while "+
"handling a request by HTTPTunnel:<br><b>"+error+
"</b><h2>Complete request:</h2><b>---</b><br><i><pre>\r\n")
.getBytes("ISO-8859-1"));
out.write(req.toByteArray());
out.write(("</pre></i><br><b>---</b></body></html>")
.getBytes("ISO-8859-1"));
out.flush();
public void handle(Request req, HTTPListener httpl, OutputStream out, String error) throws IOException {
// FIXME: Make nicer messages for more likely errors.
out
.write(("HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n\r\n")
.getBytes("ISO-8859-1"));
out
.write(("<html><head><title>" + error + "</title></head><body><h1>" + error
+ "</h1>An internal error occurred while " + "handling a request by HTTPTunnel:<br><b>" + error + "</b><h2>Complete request:</h2><b>---</b><br><i><pre>\r\n")
.getBytes("ISO-8859-1"));
out.write(req.toByteArray());
out.write(("</pre></i><br><b>---</b></body></html>").getBytes("ISO-8859-1"));
out.flush();
}
}
}

View File

@ -1,4 +1,5 @@
package net.i2p.httptunnel.handler;
import java.io.IOException;
import java.io.OutputStream;
@ -14,9 +15,9 @@ public class LocalHandler {
private static final Log _log = new Log(LocalHandler.class);
/* package private */ LocalHandler() {
/* package private */LocalHandler() {
}
/**
* @param req the Request
* @param httpl an HTTPListener
@ -24,16 +25,16 @@ public class LocalHandler {
* @throws IOException
*/
public void handle(Request req, HTTPListener httpl, OutputStream out
/*, boolean fromProxy */) throws IOException {
//FIXME: separate multiple pages, not only a start page
//FIXME: provide some info on this page
out.write(("HTTP/1.1 200 Document following\r\n"+
"Content-Type: text/html; charset=iso-8859-1\r\n\r\n"+
"<html><head><title>Welcome to I2P HTTPTunnel</title>"+
"</head><body><h1>Welcome to I2P HTTPTunnel</h1>You can "+
"browse Eepsites by adding an eepsite name to the request."+
"</body></html>").getBytes("ISO-8859-1"));
out.flush();
/*, boolean fromProxy */) throws IOException {
//FIXME: separate multiple pages, not only a start page
//FIXME: provide some info on this page
out
.write(("HTTP/1.1 200 Document following\r\n" + "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
+ "<html><head><title>Welcome to I2P HTTPTunnel</title>"
+ "</head><body><h1>Welcome to I2P HTTPTunnel</h1>You can "
+ "browse Eepsites by adding an eepsite name to the request." + "</body></html>")
.getBytes("ISO-8859-1"));
out.flush();
}
/**
@ -43,11 +44,10 @@ public class LocalHandler {
* @param out where to write the results
* @throws IOException
*/
public void handleProxyConfWarning(Request req, HTTPListener httpl,
OutputStream out) throws IOException {
//FIXME
public void handleProxyConfWarning(Request req, HTTPListener httpl, OutputStream out) throws IOException {
//FIXME
throw new IOException("jrandom ate the deprecated method. mooo");
//httpl.handleNotImplemented(out);
//httpl.handleNotImplemented(out);
}
@ -58,11 +58,10 @@ public class LocalHandler {
* @param out where to write the results
* @throws IOException
*/
public void handleHTTPWarning(Request req, HTTPListener httpl,
OutputStream out /*, boolean fromProxy */)
throws IOException {
// FIXME
public void handleHTTPWarning(Request req, HTTPListener httpl, OutputStream out /*, boolean fromProxy */)
throws IOException {
// FIXME
throw new IOException("jrandom ate the deprecated method. mooo");
//httpl.handleNotImplemented(out);
//httpl.handleNotImplemented(out);
}
}
}

View File

@ -1,4 +1,5 @@
package net.i2p.httptunnel.handler;
import java.io.IOException;
import java.io.OutputStream;
@ -19,10 +20,10 @@ public class ProxyHandler extends EepHandler {
private static final Log _log = new Log(ErrorHandler.class);
/* package private */ ProxyHandler(ErrorHandler eh) {
super(eh);
/* package private */ProxyHandler(ErrorHandler eh) {
super(eh);
}
/**
* @param req a Request
* @param httpl an HTTPListener
@ -30,24 +31,23 @@ public class ProxyHandler extends EepHandler {
* @throws IOException
*/
public void handle(Request req, HTTPListener httpl, OutputStream out
/*, boolean fromProxy */) throws IOException {
SocketManagerProducer smp = httpl.getSMP();
Destination dest = findProxy();
if (dest == null) {
errorHandler.handle(req, httpl, out,
"Could not find proxy");
return;
}
// one manager for all proxy requests
I2PSocketManager sm = smp.getManager("--proxy--");
Filter f = new NullFilter(); //FIXME: use other filter
if (!handle(req, f, out, dest, sm)) {
errorHandler.handle(req, httpl, out, "Unable to reach peer");
}
/*, boolean fromProxy */) throws IOException {
SocketManagerProducer smp = httpl.getSMP();
Destination dest = findProxy();
if (dest == null) {
errorHandler.handle(req, httpl, out, "Could not find proxy");
return;
}
// one manager for all proxy requests
I2PSocketManager sm = smp.getManager("--proxy--");
Filter f = new NullFilter(); //FIXME: use other filter
if (!handle(req, f, out, dest, sm)) {
errorHandler.handle(req, httpl, out, "Unable to reach peer");
}
}
private Destination findProxy() {
//FIXME!
return NamingService.getInstance().lookup("squid.i2p");
//FIXME!
return NamingService.getInstance().lookup("squid.i2p");
}
}
}

View File

@ -1,4 +1,5 @@
package net.i2p.httptunnel.handler;
import java.io.IOException;
import java.io.OutputStream;
@ -14,17 +15,17 @@ public class RootHandler {
private static final Log _log = new Log(RootHandler.class);
private RootHandler() {
errorHandler=new ErrorHandler();
localHandler=new LocalHandler();
proxyHandler=new ProxyHandler(errorHandler);
eepHandler=new EepHandler(errorHandler);
errorHandler = new ErrorHandler();
localHandler = new LocalHandler();
proxyHandler = new ProxyHandler(errorHandler);
eepHandler = new EepHandler(errorHandler);
}
private ErrorHandler errorHandler;
private ProxyHandler proxyHandler;
private LocalHandler localHandler;
private EepHandler eepHandler;
private static RootHandler instance;
/**
@ -32,12 +33,12 @@ public class RootHandler {
* @return the one and only instance, yay!
*/
public static synchronized RootHandler getInstance() {
if (instance == null) {
instance = new RootHandler();
}
return instance;
if (instance == null) {
instance = new RootHandler();
}
return instance;
}
/**
* The _ROOT_ handler: it passes its workload off to the other handlers.
* @param req a Request
@ -45,76 +46,73 @@ public class RootHandler {
* @param out where to write the results
* @throws IOException
*/
public void handle(Request req, HTTPListener httpl,
OutputStream out) throws IOException {
String url=req.getURL();
System.out.println(url);
/* boolean byProxy = false; */
int pos;
if (url.startsWith("http://")) { // access via proxy
/* byProxy=true; */
if (httpl.firstProxyUse()) {
localHandler.handleProxyConfWarning(req,httpl,out);
return;
}
url = url.substring(7);
pos = url.indexOf("/");
String host;
String rest;
if (pos == -1) {
errorHandler.handle(req, httpl, out, "No host end in URL");
return;
} else {
host = url.substring(0,pos);
url = url.substring(pos);
if ("i2p".equals(host) || "i2p.i2p".equals(host)) {
// normal request; go on below...
} else if (host.endsWith(".i2p")) {
// "old" service request, send a redirect...
out.write(("HTTP/1.1 302 Moved\r\nLocation: "+
"http://i2p.i2p/"+host+url+
"\r\n\r\n").getBytes("ISO-8859-1"));
return;
} else {
// this is for proxying to the real web
proxyHandler.handle(req, httpl, out /*, true */);
return;
}
}
}
if (url.equals("/")) { // main page
url="/_/local/index";
} else if (!url.startsWith("/")) {
errorHandler.handle(req, httpl, out,
"No leading slash in URL: "+url);
return;
}
String dest;
url=url.substring(1);
pos = url.indexOf("/");
if (pos == -1) {
dest=url;
url="/";
} else {
dest = url.substring(0,pos);
url=url.substring(pos);
}
req.setURL(url);
if (dest.equals("_")) { // no eepsite
if (url.startsWith("/local/")) { // local request
req.setURL(url.substring(6));
localHandler.handle(req, httpl, out /*, byProxy */);
} else if (url.startsWith("/http/")) { // http warning
localHandler.handleHTTPWarning(req, httpl, out /*, byProxy */);
} else if (url.startsWith("/proxy/")) { // http proxying
req.setURL("http://"+url.substring(7));
proxyHandler.handle(req, httpl, out /*, byProxy */);
} else {
errorHandler.handle(req, httpl, out,
"No local handler for this URL: "+url);
}
} else {
eepHandler.handle(req, httpl, out, /* byProxy, */ dest);
}
public void handle(Request req, HTTPListener httpl, OutputStream out) throws IOException {
String url = req.getURL();
System.out.println(url);
/* boolean byProxy = false; */
int pos;
if (url.startsWith("http://")) { // access via proxy
/* byProxy=true; */
if (httpl.firstProxyUse()) {
localHandler.handleProxyConfWarning(req, httpl, out);
return;
}
url = url.substring(7);
pos = url.indexOf("/");
String host;
String rest;
if (pos == -1) {
errorHandler.handle(req, httpl, out, "No host end in URL");
return;
} else {
host = url.substring(0, pos);
url = url.substring(pos);
if ("i2p".equals(host) || "i2p.i2p".equals(host)) {
// normal request; go on below...
} else if (host.endsWith(".i2p")) {
// "old" service request, send a redirect...
out
.write(("HTTP/1.1 302 Moved\r\nLocation: " + "http://i2p.i2p/" + host + url + "\r\n\r\n")
.getBytes("ISO-8859-1"));
return;
} else {
// this is for proxying to the real web
proxyHandler.handle(req, httpl, out /*, true */);
return;
}
}
}
if (url.equals("/")) { // main page
url = "/_/local/index";
} else if (!url.startsWith("/")) {
errorHandler.handle(req, httpl, out, "No leading slash in URL: " + url);
return;
}
String dest;
url = url.substring(1);
pos = url.indexOf("/");
if (pos == -1) {
dest = url;
url = "/";
} else {
dest = url.substring(0, pos);
url = url.substring(pos);
}
req.setURL(url);
if (dest.equals("_")) { // no eepsite
if (url.startsWith("/local/")) { // local request
req.setURL(url.substring(6));
localHandler.handle(req, httpl, out /*, byProxy */);
} else if (url.startsWith("/http/")) { // http warning
localHandler.handleHTTPWarning(req, httpl, out /*, byProxy */);
} else if (url.startsWith("/proxy/")) { // http proxying
req.setURL("http://" + url.substring(7));
proxyHandler.handle(req, httpl, out /*, byProxy */);
} else {
errorHandler.handle(req, httpl, out, "No local handler for this URL: " + url);
}
} else {
eepHandler.handle(req, httpl, out, /* byProxy, */dest);
}
}
}
}