Skip to content

Commit

Permalink
gh-30 update processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow0 committed Feb 8, 2024
1 parent 308d634 commit d21c854
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package unknow.server.http;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
Expand All @@ -14,6 +16,7 @@
import jakarta.servlet.DispatcherType;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import unknow.server.http.HttpProcessor.HttpProcessorFactory;
import unknow.server.http.servlet.ServletContextImpl;
import unknow.server.http.servlet.ServletRequestImpl;
import unknow.server.http.servlet.ServletResponseImpl;
Expand All @@ -23,6 +26,8 @@
public class HttpConnection extends NIOConnection {
private static final Logger logger = LoggerFactory.getLogger(HttpConnection.class);

private static final List<HttpProcessorFactory> VERSIONS = Arrays.asList(HttpProcessor11.Factory);

private final ExecutorService executor;
private final ServletContextImpl ctx;
private final int keepAliveIdle;
Expand All @@ -45,16 +50,17 @@ protected HttpConnection(ExecutorService executor, ServletContextImpl ctx, int k
this.req = new ServletRequestImpl(this, DispatcherType.REQUEST);
}

@Override
protected void onInit() {
p = new HttpProcessor11(getCtx(), keepAliveIdle);
}

@Override
public final void onRead() throws InterruptedException {
if (!exec.isDone())
return;
if (!p.init(this))

for (HttpProcessorFactory f : VERSIONS) {
p = f.create(this);
if (p != null)
break;
}
if (p == null)
return;
exec = executor.submit(p);
}
Expand All @@ -65,8 +71,7 @@ public final void onWrite() { // OK

private void cleanup() {
exec.cancel(true);
p.close();
p = new HttpProcessor11(getCtx(), keepAliveIdle);
p = null;
pendingRead.clear();
}

Expand Down Expand Up @@ -127,4 +132,7 @@ public ServletContextImpl getCtx() {
return ctx;
}

public int getkeepAlive() {
return keepAliveIdle;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package unknow.server.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -13,40 +11,31 @@
import unknow.server.http.utils.EventManager;
import unknow.server.http.utils.ServletManager;
import unknow.server.nio.NIOConnection.Out;
import unknow.server.util.io.Buffers;

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

protected HttpConnection co;
protected final HttpConnection co;
protected final ServletContextImpl ctx;
protected final ServletManager servlets;
protected final EventManager events;
protected final int keepAliveIdle;

protected HttpProcessor(ServletContextImpl ctx, int keepAliveIdle) {
this.ctx = ctx;
protected HttpProcessor(HttpConnection co) {
this.co = co;
this.ctx = co.getCtx();
this.servlets = ctx.getServletManager();
this.events = ctx.getEvents();
this.keepAliveIdle = keepAliveIdle;
this.keepAliveIdle = co.getkeepAlive();
}

protected abstract boolean canProcess(HttpConnection co) throws InterruptedException;

protected abstract boolean fillRequest(ServletRequestImpl req) throws InterruptedException, IOException;

protected abstract void doRun(ServletRequestImpl req, ServletResponseImpl res) throws IOException;

public final boolean init(HttpConnection co) throws InterruptedException {
if (!canProcess(co))
return false;
this.co = co;
return true;
}

@SuppressWarnings("resource")
@Override
public void run() {
public final void run() {
boolean close = false;
ServletRequestImpl req = co.req;
ServletResponseImpl res = co.res;
Expand Down Expand Up @@ -80,33 +69,20 @@ public void run() {
} catch (@SuppressWarnings("unused") IOException e1) { //ok
}
} finally {
co = null;
if (close)
out.close();
else
out.flush();
}
}

public final void close() {
co = null;
}

protected Buffers readBuffer() {
if (co == null)
throw new ProcessDoneException();
return co.pendingRead;
}

public OutputStream getOut() {
if (co == null)
throw new ProcessDoneException();
return co.getOut();
}

public InputStream getIn() {
if (co == null)
throw new ProcessDoneException();
return co.getIn();
public static interface HttpProcessorFactory {
/**
* create a processor if it can process it
* @param co the connection
* @return the processor or null
* @throws InterruptedException on interrupt
*/
HttpProcessor create(HttpConnection co) throws InterruptedException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

import jakarta.servlet.FilterChain;
import jakarta.servlet.UnavailableException;
import unknow.server.http.servlet.ServletContextImpl;
import unknow.server.http.servlet.ServletRequestImpl;
import unknow.server.http.servlet.ServletResponseImpl;
import unknow.server.util.io.Buffers;
import unknow.server.util.io.BuffersUtils;
import unknow.server.util.io.BuffersUtils.IndexOfBloc;

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

private static final byte[] END = new byte[] { '\r', '\n', '\r', '\n' };

private static final byte[] CRLF = { '\r', '\n' };
private static final byte[] PARAM_SEP = { '&', '=' };
private static final byte[] SPACE_SLASH = { ' ', '/' };
Expand All @@ -39,29 +39,19 @@ public class HttpProcessor11 extends HttpProcessor {

private static final int MAX_START_SIZE = 8192;

private final IndexOfBloc end;

private final StringBuilder sb;
private final Decode decode;

public HttpProcessor11(ServletContextImpl ctx, int keepAliveIdle) {
super(ctx, keepAliveIdle);
public HttpProcessor11(HttpConnection co) {
super(co);

end = new IndexOfBloc(new byte[] { '\r', '\n', '\r', '\n' });
sb = new StringBuilder();
decode = new Decode(sb);
}

@Override
protected boolean canProcess(HttpConnection co) throws InterruptedException {
end.reset();
co.pendingRead.walk(end, 0, MAX_START_SIZE);
return end.index() > 0;
}

@Override
protected boolean fillRequest(ServletRequestImpl req) throws InterruptedException, IOException {
Buffers b = readBuffer();
Buffers b = co.pendingRead;
int i = BuffersUtils.indexOf(b, SPACE_SLASH, 0, MAX_METHOD_SIZE);
if (i < 0) {
co.sendError(HttpError.BAD_REQUEST.code, null, null);
Expand Down Expand Up @@ -196,4 +186,9 @@ protected final void doRun(ServletRequestImpl req, ServletResponseImpl res) thro
}
}

public static final HttpProcessorFactory Factory = co -> {
if (BuffersUtils.indexOf(co.pendingRead, END, 0, MAX_START_SIZE) > 0)
return new HttpProcessor11(co);
return null;
};
}

0 comments on commit d21c854

Please sign in to comment.