Created
February 2, 2014 18:56
-
-
Save jnorthrup/8773030 to your computer and use it in GitHub Desktop.
Scoria rxf-server Proxy initilaizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cydesign.strombolian.server; | |
import cydesign.config.Ddl; | |
import one.xio.AsioVisitor; | |
import one.xio.HttpHeaders; | |
import one.xio.HttpMethod; | |
import rxf.server.BlobAntiPatternObject; | |
import rxf.server.CouchNamespace; | |
import rxf.server.PreRead; | |
import rxf.server.Rfc822HeaderState; | |
import rxf.server.daemon.ProxyDaemon; | |
import rxf.server.web.inf.ProtocolMethodDispatch; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
import java.util.regex.Pattern; | |
import static java.nio.channels.SelectionKey.*; | |
import static one.xio.HttpMethod.UTF8; | |
import static one.xio.HttpMethod.enqueue; | |
public class Scoria { | |
static {//boilerplate | |
} | |
public static void main(String[] args) throws InterruptedException { | |
System.setProperty("rxf.server.proxy.port", "5984"); | |
System.setProperty("rxf.server.realtime.unit", TimeUnit.MINUTES.name()); | |
BlobAntiPatternObject.getEXECUTOR_SERVICE().submit(new Runnable() { | |
public void run() { | |
try { | |
LinkedHashMap<Pattern, Class<? extends AsioVisitor.Impl>> putmap = new LinkedHashMap<>(), headMap = new LinkedHashMap<>(), delMap = new LinkedHashMap<>(); | |
CouchNamespace.NAMESPACE.put(HttpMethod.PUT, putmap); | |
CouchNamespace.NAMESPACE.put(HttpMethod.HEAD, headMap); | |
CouchNamespace.NAMESPACE.put(HttpMethod.DELETE, delMap); | |
ProtocolMethodDispatch.GETmap.clear(); | |
ProtocolMethodDispatch.POSTmap.clear(); | |
for (Ddl ddl : Ddl.values()) { | |
Pattern pattern = Pattern.compile("^/" + ddl.name() + "\\W?.*"); | |
ProtocolMethodDispatch.GETmap.put(pattern, MyProxy.class); | |
ProtocolMethodDispatch.POSTmap.put(pattern, MyProxy.class); | |
putmap.put(pattern, MyProxy.class); | |
headMap.put(pattern, MyProxy.class); | |
delMap.put(pattern, MyProxy.class); | |
} | |
InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); | |
ServerSocketChannel channel = ServerSocketChannel.open().bind(new InetSocketAddress(loopbackAddress, 8888)); | |
channel.configureBlocking(false); | |
HttpMethod.enqueue(channel, SelectionKey.OP_ACCEPT); | |
ProtocolMethodDispatch protocoldecoder = new ProtocolMethodDispatch(); | |
HttpMethod.init(protocoldecoder); | |
} catch (Exception ignored) { | |
} | |
} | |
}); | |
Scoria scoria = new Scoria(); | |
synchronized (scoria) { | |
scoria.wait(); | |
} | |
} | |
public static class MyProxy extends AsioVisitor.Impl implements PreRead { | |
@Override | |
public void onRead(final SelectionKey outerKey) throws Exception { | |
final SocketChannel outterChannel = (SocketChannel) outerKey.channel(); | |
Object[] attachment = (Object[]) outerKey.attachment(); | |
Rfc822HeaderState.HttpRequest req = ((Rfc822HeaderState) (attachment[1])).$req(); | |
ByteBuffer cursor = ((ByteBuffer) (attachment[2])); | |
ByteBuffer headersBuf = req.headerBuf().duplicate(); | |
System.err.println("" + UTF8.decode(req.headerBuf().duplicate())); | |
String s = BlobAntiPatternObject.getCOUCHADDR().toString(); | |
String prefix = s.split("/", 2)[1]; | |
req.headerString(HttpHeaders.Host, prefix); | |
InetSocketAddress address = | |
(InetSocketAddress) outterChannel.socket().getRemoteSocketAddress(); | |
//grab a frame of int offsets | |
Map<String, int[]> headers = HttpHeaders.getHeaders(headersBuf.duplicate()); | |
String decode = UTF8.decode(headersBuf.duplicate()).toString(); | |
int[] hosts = headers.get("Host"); | |
Class<String> clazz = String.class; | |
String as = req.as(clazz); | |
ByteBuffer slice2 = | |
UTF8.encode("Host: " + prefix + "\r\nX-Origin-Host: " + address.toString().split("/", 2)[1] | |
+ "\r\n" ); | |
cursor.flip().position(headersBuf.limit()); | |
final ByteBuffer inwardBuffer = | |
ByteBuffer.allocateDirect(8 << 10).put( | |
(ByteBuffer) cursor.clear().limit(1 + hosts[0] - ProxyDaemon.HOSTPREFIXLEN)).put( | |
(ByteBuffer) cursor.limit(headersBuf.limit() - 2).position(hosts[1])).put(slice2).put((byte) ('\r'&0xff)).put((byte) ('\n'&0xff)) | |
.put(cursor); | |
cursor = null; | |
System.err.println("" + UTF8.decode((ByteBuffer) inwardBuffer.duplicate().flip())); | |
InetSocketAddress remote; | |
remote = BlobAntiPatternObject.getCOUCHADDR(); | |
final SocketChannel innerChannel = | |
(SocketChannel) SocketChannel.open().configureBlocking(false); | |
innerChannel.connect(remote); | |
enqueue(innerChannel, OP_CONNECT,new Impl() { | |
@Override | |
public void onConnect(SelectionKey key) throws Exception { | |
if (innerChannel.finishConnect()) | |
ProxyDaemon.pipe(key, outerKey, inwardBuffer, (ByteBuffer) ByteBuffer.allocateDirect(8 << 10) | |
.clear()); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment