This commit is contained in:
Greg Wilkins 2017-05-11 12:33:37 +02:00
parent 2679715a30
commit 861432a334
5 changed files with 170 additions and 21 deletions

View File

@ -32,7 +32,7 @@
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.15</version>
<version>0.18</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>

View File

@ -19,11 +19,15 @@
package org.eclipse.jetty.unixsocket;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import org.eclipse.jetty.toolchain.test.IO;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
@ -32,25 +36,51 @@ public class UnixSocketClient
public static void main(String[] args) throws Exception
{
java.io.File path = new java.io.File("/tmp/jetty.sock");
String data = "GET / HTTP/1.1\r\nHost: unixsock\r\n\r\n";
UnixSocketAddress address = new UnixSocketAddress(path);
UnixSocketChannel channel = UnixSocketChannel.open(address);
System.out.println("connected to " + channel.getRemoteSocketAddress());
java.io.File content = new java.io.File("/tmp/data.txt");
PrintWriter w = new PrintWriter(Channels.newOutputStream(channel));
InputStreamReader r = new InputStreamReader(Channels.newInputStream(channel));
String method = "GET";
int content_length = 0;
String body = null;
if (content.exists())
{
method = "POST";
body = IO.readToString(content);
content_length = body.length();
}
String data = method+" / HTTP/1.1\r\n"
+ "Host: unixsock\r\n"
+ "Content-Length: "+content_length+"\r\n"
+ "Connection: close\r\n"
+ "\r\n";
if (body!=null)
data += body;
while (true)
{
UnixSocketAddress address = new UnixSocketAddress(path);
UnixSocketChannel channel = UnixSocketChannel.open(address);
System.out.println("connected to " + channel.getRemoteSocketAddress());
PrintWriter w = new PrintWriter(new OutputStreamWriter(Channels.newOutputStream(channel),StandardCharsets.ISO_8859_1));
InputStreamReader r = new InputStreamReader(Channels.newInputStream(channel));
w.print(data);
w.flush();
CharBuffer result = CharBuffer.allocate(4096);
r.read(result);
result.flip();
System.out.println("read from server: " + result.toString());
Thread.sleep(1000);
String total="";
int l = 0;
while (l>=0)
{
if (l>0)
{
result.flip();
total += result.toString();
}
result.clear();
l = r.read(result);
}
System.out.println("read from server: " + total);
}
}
}

View File

@ -0,0 +1,90 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.unixsocket;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.ProxyConnectionFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
public class UnixSocketProxyServer
{
public static void main (String... args) throws Exception
{
Server server = new Server();
HttpConnectionFactory http = new HttpConnectionFactory();
ProxyConnectionFactory proxy = new ProxyConnectionFactory(http.getProtocol());
UnixSocketConnector connector = new UnixSocketConnector(server,proxy,http);
server.addConnector(connector);
Path socket = Paths.get(connector.getUnixSocket());
if (Files.exists(socket))
Files.delete(socket);
server.setHandler(new AbstractHandler.ErrorDispatchHandler()
{
@Override
protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
int l = 0;
if (request.getContentLength()!=0)
{
InputStream in = request.getInputStream();
byte[] buffer = new byte[4096];
int r = 0;
while (r>=0)
{
l += r;
r = in.read(buffer);
}
}
baseRequest.setHandled(true);
response.setStatus(200);
response.getWriter().write("Hello World "+new Date() + "\r\n");
response.getWriter().write("remote="+request.getRemoteAddr()+":"+request.getRemotePort()+"\r\n");
response.getWriter().write("local ="+request.getLocalAddr()+":"+request.getLocalPort()+"\r\n");
response.getWriter().write("read ="+l+"\r\n");
}
});
server.start();
while (true)
{
Thread.sleep(5000);
connector.dumpStdErr();
}
// server.join();
}
}

View File

@ -19,6 +19,11 @@
package org.eclipse.jetty.unixsocket;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -37,26 +42,50 @@ public class UnixSocketServer
Server server = new Server();
HttpConnectionFactory http = new HttpConnectionFactory();
ProxyConnectionFactory proxy = new ProxyConnectionFactory(http.getProtocol());
UnixSocketConnector connector = new UnixSocketConnector(server,proxy,http);
UnixSocketConnector connector = new UnixSocketConnector(server,http);
server.addConnector(connector);
Path socket = Paths.get(connector.getUnixSocket());
if (Files.exists(socket))
Files.delete(socket);
server.setHandler(new AbstractHandler.ErrorDispatchHandler()
{
@Override
protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
int l = 0;
if (request.getContentLength()!=0)
{
InputStream in = request.getInputStream();
byte[] buffer = new byte[4096];
int r = 0;
while (r>=0)
{
l += r;
r = in.read(buffer);
}
}
baseRequest.setHandled(true);
response.setStatus(200);
response.getWriter().write("Hello World\r\n");
response.getWriter().write("Hello World "+new Date() + "\r\n");
response.getWriter().write("remote="+request.getRemoteAddr()+":"+request.getRemotePort()+"\r\n");
response.getWriter().write("local ="+request.getLocalAddr()+":"+request.getLocalPort()+"\r\n");
response.getWriter().write("read ="+l+"\r\n");
}
});
server.start();
server.join();
while (true)
{
Thread.sleep(5000);
System.err.println();
System.err.println("==============================");
connector.dumpStdErr();
}
// server.join();
}
}

View File

@ -2,6 +2,6 @@ org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
#org.eclipse.jetty.LEVEL=DEBUG
#org.eclipse.jetty.client.LEVEL=DEBUG
#org.eclipse.jetty.proxy.LEVEL=DEBUG
org.eclipse.jetty.unixsocket.LEVEL=DEBUG
org.eclipse.jetty.io.LEVEL=DEBUG
org.eclipse.jetty.server.ProxyConnectionFactory.LEVEL=DEBUG
#org.eclipse.jetty.unixsocket.LEVEL=DEBUG
#org.eclipse.jetty.io.LEVEL=DEBUG
#org.eclipse.jetty.server.ProxyConnectionFactory.LEVEL=DEBUG