use of org.apache.http.impl.DefaultBHttpServerConnection in project oap by oaplatform.
the class Server method accepted.
@SneakyThrows
public void accepted(final Socket socket) {
try {
final DefaultBHttpServerConnection connection = connectionFactory.createConnection(socket);
final String connectionId = connection.toString();
executor.submit(() -> {
try {
connections.put(connectionId, connection);
log.debug("connection accepted: {}", connection);
final HttpContext httpContext = createHttpContext(socket);
Thread.currentThread().setName(connection.toString());
log.debug("start handling {}", connection);
while (!Thread.interrupted() && connection.isOpen()) httpService.handleRequest(connection, httpContext);
} catch (SocketException e) {
if (socketClosed(e))
log.debug("Socket closed: {}", connection);
else if (connectionReset(e))
log.warn("Connection reset: {}", connection);
else
log.error(e.getMessage(), e);
} catch (ConnectionClosedException e) {
log.debug("connection closed: {}", connection);
} catch (Throwable e) {
log.error(e.getMessage(), e);
} finally {
connections.remove(connectionId);
Closeables.close(connection);
}
});
} catch (final IOException e) {
log.warn(e.getMessage());
connections.values().forEach(Closeables::close);
connections.clear();
throw e;
}
}
use of org.apache.http.impl.DefaultBHttpServerConnection in project JSCover by tntim96.
the class PersistentStaticHttpServer method run.
public void run() {
DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024);
try {
conn.bind(socket);
try {
boolean keepAlive = true;
while (keepAlive && !socket.isClosed()) {
// fully read the request, whatever it is
HttpRequest request = conn.receiveRequestHeader();
logger.log(FINE, "Received request: {0}", request);
keepAlive = isKeepAlive(request);
if (request instanceof HttpEntityEnclosingRequest) {
conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
// consume all content to allow reuse
EntityUtils.consume(entity);
}
}
// send static content or reject the method
String method = request.getRequestLine().getMethod();
if (method.matches("(?i)get|post|put"))
sendOkContent(conn);
else
rejectMethod(conn);
}
} finally {
ioUtils.closeQuietly(conn);
ioUtils.closeQuietly(socket);
}
} catch (HttpException | IOException e) {
e.printStackTrace();
ioUtils.closeQuietly(socket);
}
}
Aggregations