Skip to content

Commit

Permalink
gh-30 update & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow0 committed Apr 16, 2024
1 parent 29b5594 commit 99f1564
Show file tree
Hide file tree
Showing 24 changed files with 246 additions and 123 deletions.
2 changes: 1 addition & 1 deletion bench/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ echo "Warming up"
$JMETER -n -t bench/test.jmx -Jhost=127.0.0.1 -Jt=20 -Jport=8080 -Jout=/dev/null
sleep 10
echo "Testing.."
$JMETER -n -t bench/test.jmx -Jhost=127.0.0.1 -Jt=60 -Jport=8080 -Jout=out/$1.csv
$JMETER -n -t bench/test.jmx -Jhost=127.0.0.1 -Jt=60 -Jc=10 -Jport=8080 -Jout=out/$1.csv
${1}_stop
sleep 10
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void add(BuilderContext ctx) {
ctx.self().addMethod("createContext", Modifier.Keyword.PROTECTED, Modifier.Keyword.FINAL).setType(t.getClass(ServletContextImpl.class))
.addParameter(String.class, "vhost").addMarkerAnnotation(Override.class).createBody()
.addStatement(new ReturnStmt(new ObjectCreationExpr(null, t.getClass(ServletContextImpl.class),
Utils.list(Utils.text(ctx.descriptor().name), new NameExpr("vhost"), Utils.mapString(ctx.descriptor().param, t), Names.SERVLETS, Names.EVENTS,
Utils.list(Utils.text(ctx.descriptor().name), new NameExpr("vhost"), Utils.mapString(ctx.descriptor().param, t), Names.EVENTS,
new ObjectCreationExpr(null, t.getClass(ctx.sessionFactory()), Utils.list()), Utils.mapString(ctx.descriptor().localeMapping, t),
Utils.mapString(ctx.descriptor().mimeTypes, t)))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class AbstractHttpServer extends NIOServerBuilder {
/** the servlet context */
protected ServletContextImpl ctx;
/** the servlet manager */
protected ServletManager servlets;
protected ServletManager manager;
/** the events */
protected EventManager events;

Expand Down Expand Up @@ -78,12 +78,12 @@ protected void process(NIOServer server, CommandLine cli) throws Exception {
if (value == null)
value = address.getHostString();

servlets = createServletManager();
manager = createServletManager();
events = createEventManager();
ctx = createContext(value);

loadInitializer();
servlets.initialize(ctx, createServlets(), createFilters());
manager.initialize(ctx, createServlets(), createFilters());
events.fireContextInitialized(ctx);

AtomicInteger i = new AtomicInteger();
Expand All @@ -94,7 +94,7 @@ protected void process(NIOServer server, CommandLine cli) throws Exception {
return t;
});
int keepAliveIdle = parseInt(cli, keepAlive, -1);
server.bind(address, () -> new HttpConnection(executor, ctx, keepAliveIdle));
server.bind(address, () -> new HttpConnection(executor, ctx, manager, events, keepAliveIdle));
}

/**
Expand All @@ -120,7 +120,7 @@ public void process(String[] arg) throws Exception {
} finally {
nioServer.stop();
nioServer.await();
events.fireContextDestroyed(ctx);
events.fireContextDestroyed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@

import unknow.server.util.io.Buffers.Walker;

/**
* decode utf8
*/
public final class Decode implements Walker {
private final CharsetDecoder d = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
private final char[] tmp = new char[2048];
private final CharBuffer cbuf = CharBuffer.wrap(tmp);
private final ByteBuffer bbuf = ByteBuffer.allocate(4096);
private final StringBuilder sb;

/**
* @param sb where to read data
*/
public Decode(StringBuilder sb) {
this.sb = sb;
}
Expand Down Expand Up @@ -58,6 +64,10 @@ private void decode() {
bbuf.compact();
}

/**
* finish the decoding
* @return true if all data was decoded
*/
public boolean done() {
try {
if (m != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import jakarta.servlet.ServletInputStream;
import unknow.server.servlet.impl.ServletContextImpl;
import unknow.server.servlet.impl.out.AbstractServletOutput;
import unknow.server.servlet.utils.EventManager;

public interface HttpAdapter {
ServletContextImpl ctx();

EventManager events();

ServletInputStream createInput();

InetSocketAddress getRemote();
Expand All @@ -19,4 +22,6 @@ public interface HttpAdapter {
AbstractServletOutput createOutput();

void commit() throws IOException;

void sendError(int sc, Throwable t, String msg) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.ServletContext;
import unknow.server.nio.NIOConnection;
import unknow.server.servlet.HttpProcessor.HttpProcessorFactory;
import unknow.server.servlet.http11.Http11Processor;
import unknow.server.servlet.impl.ServletContextImpl;
import unknow.server.servlet.utils.EventManager;
import unknow.server.servlet.utils.ServletManager;

public class HttpConnection extends NIOConnection {
private static final Logger logger = LoggerFactory.getLogger(HttpConnection.class);

private static final HttpProcessorFactory[] VERSIONS = new HttpProcessorFactory[] { /*Http2Processor.Factory, */Http11Processor.Factory };

private final ExecutorService executor;
private final ServletContextImpl ctx;
protected final ServletContextImpl ctx;
protected final ServletManager manager;
protected final EventManager events;
private final int keepAliveIdle;

private HttpProcessor p;
Expand All @@ -29,11 +34,15 @@ public class HttpConnection extends NIOConnection {
* create new RequestBuilder
* @param executor the executor
* @param ctx the servlet context
* @param events
* @param manager
*/
protected HttpConnection(ExecutorService executor, ServletContextImpl ctx, int keepAliveIdle) {
protected HttpConnection(ExecutorService executor, ServletContextImpl ctx, ServletManager manager, EventManager events, int keepAliveIdle) {
this.executor = executor;
this.keepAliveIdle = keepAliveIdle;
this.ctx = ctx;
this.manager = manager;
this.events = events;
this.keepAliveIdle = keepAliveIdle;
}

@Override
Expand Down Expand Up @@ -85,7 +94,7 @@ public Future<?> submit(Runnable r) {
return executor.submit(r);
}

public ServletContextImpl getCtx() {
public ServletContext getCtx() {
return ctx;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

public interface HttpProcessor {

/** called when we have more data */
/**
* called when we have more data
* @throws InterruptedException on interrupt
*/
void process() throws InterruptedException;

/** @return true if the connection is closed */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@
import unknow.server.servlet.impl.ServletRequestImpl;
import unknow.server.servlet.impl.ServletResponseImpl;
import unknow.server.servlet.utils.EventManager;
import unknow.server.servlet.utils.ServletManager;

public abstract class HttpWorker implements Runnable, HttpAdapter {
private static final Logger logger = LoggerFactory.getLogger(HttpWorker.class);

protected final HttpConnection co;
protected final EventManager events;
protected final ServletManager manager;
protected ServletRequestImpl req;
protected ServletResponseImpl res;

public HttpWorker(HttpConnection co) {
this.co = co;
this.events = co.getCtx().getEvents();
this.manager = co.manager;
this.req = new ServletRequestImpl(this, DispatcherType.REQUEST);
this.res = new ServletResponseImpl(this);
}

@Override
public ServletContextImpl ctx() {
return co.getCtx();
public final ServletContextImpl ctx() {
return co.ctx;
}

@Override
public final EventManager events() {
return co.events;
}

protected abstract boolean doStart() throws IOException, InterruptedException;
Expand All @@ -49,20 +55,20 @@ protected final void doRun() {
co.getOut().close();
return;
}
events.fireRequestInitialized(req);
FilterChain s = co.getCtx().getServletManager().find(req);
co.events.fireRequestInitialized(req);
FilterChain s = manager.find(req);
try {
s.doFilter(req, res);
co.pendingRead.clear();
} catch (UnavailableException e) {
// TODO add page with retry-after
res.sendError(503, e, null);
sendError(503, e, null);
} catch (Exception e) {
logger.error("failed to service '{}'", s, e);
if (!res.isCommitted())
res.sendError(500);
}
events.fireRequestDestroyed(req);
co.events.fireRequestDestroyed(req);
req.clearInput();
res.close();
} catch (Exception e) {
logger.error("processor error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import unknow.server.servlet.HttpProcessor;
import unknow.server.util.io.BuffersUtils;

/**
* http/1.1 implementation
*/
public class Http11Processor implements HttpProcessor {
private static final Logger logger = LoggerFactory.getLogger(Http11Processor.class);

Expand All @@ -21,6 +24,10 @@ public class Http11Processor implements HttpProcessor {

private volatile Future<?> exec = CompletableFuture.completedFuture(null);

/**
* new http11 processor
* @param co the connection
*/
public Http11Processor(HttpConnection co) {
this.co = co;
}
Expand All @@ -41,6 +48,7 @@ public final void close() {
exec.cancel(true);
}

/** the processor factory */
public static final HttpProcessorFactory Factory = co -> {
if (BuffersUtils.indexOf(co.pendingRead, END, 0, MAX_START_SIZE) > 0)
return new Http11Processor(co);
Expand Down
Loading

0 comments on commit 99f1564

Please sign in to comment.