2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.ClosedChannelException;
25 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.atomic.AtomicBoolean;
27 import java.util.concurrent.atomic.AtomicInteger;
29 import javax.servlet.DispatcherType;
30 import javax.servlet.RequestDispatcher;
31 import javax.servlet.http.HttpServletRequest;
33 import org.eclipse.jetty.http.HttpField;
34 import org.eclipse.jetty.http.HttpFields;
35 import org.eclipse.jetty.http.HttpGenerator;
36 import org.eclipse.jetty.http.HttpGenerator.ResponseInfo;
37 import org.eclipse.jetty.http.HttpHeader;
38 import org.eclipse.jetty.http.HttpHeaderValue;
39 import org.eclipse.jetty.http.HttpMethod;
40 import org.eclipse.jetty.http.HttpParser;
41 import org.eclipse.jetty.http.HttpStatus;
42 import org.eclipse.jetty.http.HttpURI;
43 import org.eclipse.jetty.http.HttpVersion;
44 import org.eclipse.jetty.http.MimeTypes;
45 import org.eclipse.jetty.io.ByteBufferPool;
46 import org.eclipse.jetty.io.ChannelEndPoint;
47 import org.eclipse.jetty.io.EndPoint;
48 import org.eclipse.jetty.io.EofException;
49 import org.eclipse.jetty.server.HttpChannelState.Action;
50 import org.eclipse.jetty.server.handler.ContextHandler;
51 import org.eclipse.jetty.server.handler.ErrorHandler;
52 import org.eclipse.jetty.util.Callback;
53 import org.eclipse.jetty.util.SharedBlockingCallback.Blocker;
54 import org.eclipse.jetty.util.URIUtil;
55 import org.eclipse.jetty.util.log.Log;
56 import org.eclipse.jetty.util.log.Logger;
57 import org.eclipse.jetty.util.thread.Scheduler;
60 /* ------------------------------------------------------------ */
62 * Represents a single endpoint for HTTP semantic processing.
63 * The HttpChannel is both a HttpParser.RequestHandler, where it passively receives events from
64 * an incoming HTTP request, and a Runnable, where it actively takes control of the request/response
65 * life cycle and calls the application (perhaps suspending and resuming with multiple calls to run).
66 * The HttpChannel signals the switch from passive mode to active mode by returning true to one of the
67 * HttpParser.RequestHandler callbacks. The completion of the active phase is signalled by a call to
68 * HttpTransport.completed().
71 public class HttpChannel<T> implements HttpParser.RequestHandler<T>, Runnable, HttpParser.ProxyHandler
73 private static final Logger LOG = Log.getLogger(HttpChannel.class);
74 private static final ThreadLocal<HttpChannel<?>> __currentChannel = new ThreadLocal<>();
76 /* ------------------------------------------------------------ */
77 /** Get the current channel that this thread is dispatched to.
78 * @see Request#getAttribute(String) for a more general way to access the HttpChannel
79 * @return the current HttpChannel or null
81 public static HttpChannel<?> getCurrentHttpChannel()
83 return __currentChannel.get();
86 protected static HttpChannel<?> setCurrentHttpChannel(HttpChannel<?> channel)
88 HttpChannel<?> last=__currentChannel.get();
90 __currentChannel.remove();
92 __currentChannel.set(channel);
96 private final AtomicBoolean _committed = new AtomicBoolean();
97 private final AtomicInteger _requests = new AtomicInteger();
98 private final Connector _connector;
99 private final HttpConfiguration _configuration;
100 private final EndPoint _endPoint;
101 private final HttpTransport _transport;
102 private final HttpURI _uri;
103 private final HttpChannelState _state;
104 private final Request _request;
105 private final Response _response;
106 private HttpVersion _version = HttpVersion.HTTP_1_1;
107 private boolean _expect = false;
108 private boolean _expect100Continue = false;
109 private boolean _expect102Processing = false;
111 public HttpChannel(Connector connector, HttpConfiguration configuration, EndPoint endPoint, HttpTransport transport, HttpInput<T> input)
113 _connector = connector;
114 _configuration = configuration;
115 _endPoint = endPoint;
116 _transport = transport;
118 _uri = new HttpURI(URIUtil.__CHARSET);
119 _state = new HttpChannelState(this);
121 _request = new Request(this, input);
122 _response = new Response(this, new HttpOutput(this));
125 public HttpChannelState getState()
130 public HttpVersion getHttpVersion()
135 * @return the number of requests handled by this connection
137 public int getRequests()
139 return _requests.get();
142 public Connector getConnector()
147 public HttpTransport getHttpTransport()
152 public ByteBufferPool getByteBufferPool()
154 return _connector.getByteBufferPool();
157 public HttpConfiguration getHttpConfiguration()
159 return _configuration;
162 public Server getServer()
164 return _connector.getServer();
167 public Request getRequest()
172 public Response getResponse()
177 public EndPoint getEndPoint()
182 public InetSocketAddress getLocalAddress()
184 return _endPoint.getLocalAddress();
187 public InetSocketAddress getRemoteAddress()
189 return _endPoint.getRemoteAddress();
193 public int getHeaderCacheSize()
195 return _configuration.getHeaderCacheSize();
199 * If the associated response has the Expect header set to 100 Continue,
200 * then accessing the input stream indicates that the handler/servlet
201 * is ready for the request body and thus a 100 Continue response is sent.
203 * @throws IOException if the InputStream cannot be created
205 public void continue100(int available) throws IOException
207 // If the client is expecting 100 CONTINUE, then send it now.
208 // TODO: consider using an AtomicBoolean ?
209 if (isExpecting100Continue())
211 _expect100Continue = false;
213 // is content missing?
216 if (_response.isCommitted())
217 throw new IOException("Committed before 100 Continues");
219 // TODO: break this dependency with HttpGenerator
220 boolean committed = sendResponse(HttpGenerator.CONTINUE_100_INFO, null, false);
222 throw new IOException("Concurrent commit while trying to send 100-Continue");
229 _committed.set(false);
231 _expect100Continue = false;
232 _expect102Processing = false;
244 /* ------------------------------------------------------------ */
246 * @return True if the channel is ready to continue handling (ie it is not suspended)
248 public boolean handle()
250 LOG.debug("{} handle enter", this);
252 final HttpChannel<?>last = setCurrentHttpChannel(this);
254 String threadName = null;
255 if (LOG.isDebugEnabled())
257 threadName = Thread.currentThread().getName();
258 Thread.currentThread().setName(threadName + " - " + _uri);
261 HttpChannelState.Action action = _state.handling();
264 // Loop here to handle async request redispatches.
265 // The loop is controlled by the call to async.unhandle in the
266 // finally block below. Unhandle will return false only if an async dispatch has
267 // already happened when unhandle is called.
268 loop: while (action.ordinal()<HttpChannelState.Action.WAIT.ordinal() && getServer().isRunning())
273 LOG.debug("{} action {}",this,action);
277 case REQUEST_DISPATCH:
278 _request.setHandled(false);
279 _response.getHttpOutput().reopen();
280 _request.setDispatcherType(DispatcherType.REQUEST);
282 for (HttpConfiguration.Customizer customizer : _configuration.getCustomizers())
283 customizer.customize(getConnector(),_configuration,_request);
284 getServer().handle(this);
288 _request.setHandled(false);
289 _response.getHttpOutput().reopen();
290 _request.setDispatcherType(DispatcherType.ASYNC);
291 getServer().handleAsync(this);
295 _request.setHandled(false);
296 _response.getHttpOutput().reopen();
297 _request.setDispatcherType(DispatcherType.ERROR);
299 Throwable ex=_state.getAsyncContextEvent().getThrowable();
300 String reason="Async Timeout";
303 reason="Async Exception";
304 _request.setAttribute(RequestDispatcher.ERROR_EXCEPTION,ex);
306 _request.setAttribute(RequestDispatcher.ERROR_STATUS_CODE,new Integer(500));
307 _request.setAttribute(RequestDispatcher.ERROR_MESSAGE,reason);
308 _request.setAttribute(RequestDispatcher.ERROR_REQUEST_URI,_request.getRequestURI());
310 _response.setStatusWithReason(500,reason);
313 ErrorHandler eh = ErrorHandler.getErrorHandler(getServer(),_state.getContextHandler());
314 if (eh instanceof ErrorHandler.ErrorPageMapper)
316 String error_page=((ErrorHandler.ErrorPageMapper)eh).getErrorPage((HttpServletRequest)_state.getAsyncContextEvent().getSuppliedRequest());
317 if (error_page!=null)
318 _state.getAsyncContextEvent().setDispatchPath(error_page);
321 getServer().handleAsync(this);
326 ContextHandler handler=_state.getContextHandler();
328 handler.handle(_request.getHttpInput());
330 _request.getHttpInput().run();
336 ContextHandler handler=_state.getContextHandler();
339 handler.handle(_response.getHttpOutput());
341 _response.getHttpOutput().run();
352 if ("ContinuationThrowable".equals(e.getClass().getSimpleName()))
363 if (e instanceof EofException)
366 LOG.warn(String.valueOf(_uri), e);
368 _request.setHandled(true);
373 if (error && _state.isAsyncStarted())
374 _state.errorComplete();
375 action = _state.unhandle();
382 setCurrentHttpChannel(last);
383 if (threadName != null && LOG.isDebugEnabled())
384 Thread.currentThread().setName(threadName);
387 if (action==Action.COMPLETE)
393 if (!_response.isCommitted() && !_request.isHandled())
394 _response.sendError(404);
396 // Complete generating the response
397 _response.closeOutput();
399 catch(EofException|ClosedChannelException e)
405 LOG.warn("complete failed",e);
409 _request.setHandled(true);
410 _transport.completed();
414 LOG.debug("{} handle exit, result {}", this, action);
416 return action!=Action.WAIT;
420 * <p>Sends an error 500, performing a special logic to detect whether the request is suspended,
421 * to avoid concurrent writes from the application.</p>
422 * <p>It may happen that the application suspends, and then throws an exception, while an application
423 * spawned thread writes the response content; in such case, we attempt to commit the error directly
424 * bypassing the {@link ErrorHandler} mechanisms and the response OutputStream.</p>
426 * @param x the Throwable that caused the problem
428 protected void handleException(Throwable x)
432 _request.setAttribute(RequestDispatcher.ERROR_EXCEPTION,x);
433 _request.setAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE,x.getClass());
434 if (_state.isSuspended())
436 HttpFields fields = new HttpFields();
437 fields.add(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE);
438 ResponseInfo info = new ResponseInfo(_request.getHttpVersion(), fields, 0, HttpStatus.INTERNAL_SERVER_ERROR_500, null, _request.isHead());
439 boolean committed = sendResponse(info, null, true);
441 LOG.warn("Could not send response error 500: "+x);
442 _request.getAsyncContext().complete();
444 else if (isCommitted())
447 if (!(x instanceof EofException))
448 LOG.warn("Could not send response error 500: "+x);
452 _response.setHeader(HttpHeader.CONNECTION.asString(),HttpHeaderValue.CLOSE.asString());
453 _response.sendError(500, x.getMessage());
456 catch (IOException e)
458 // We tried our best, just log
459 LOG.debug("Could not commit response error 500", e);
463 public boolean isExpecting100Continue()
465 return _expect100Continue;
468 public boolean isExpecting102Processing()
470 return _expect102Processing;
474 public String toString()
476 return String.format("%s@%x{r=%s,a=%s,uri=%s}",
477 getClass().getSimpleName(),
481 _state.getState()==HttpChannelState.State.IDLE?"-":_request.getRequestURI()
486 public void proxied(String protocol, String sAddr, String dAddr, int sPort, int dPort)
488 _request.setAttribute("PROXY", protocol);
489 _request.setServerName(sAddr);
490 _request.setServerPort(dPort);
491 _request.setRemoteAddr(InetSocketAddress.createUnresolved(sAddr,sPort));
495 public boolean startRequest(HttpMethod httpMethod, String method, ByteBuffer uri, HttpVersion version)
498 _expect100Continue = false;
499 _expect102Processing = false;
501 _request.setTimeStamp(System.currentTimeMillis());
502 _request.setMethod(httpMethod, method);
504 if (httpMethod == HttpMethod.CONNECT)
505 _uri.parseConnect(uri.array(),uri.arrayOffset()+uri.position(),uri.remaining());
507 _uri.parse(uri.array(),uri.arrayOffset()+uri.position(),uri.remaining());
508 _request.setUri(_uri);
513 path = _uri.getDecodedPath();
517 LOG.warn("Failed UTF-8 decode for request path, trying ISO-8859-1");
519 path = _uri.getDecodedPath(StandardCharsets.ISO_8859_1);
522 String info = URIUtil.canonicalPath(path);
526 if( path==null && _uri.getScheme()!=null &&_uri.getHost()!=null)
529 _request.setRequestURI("");
533 badMessage(400,null);
537 _request.setPathInfo(info);
538 _version = version == null ? HttpVersion.HTTP_0_9 : version;
539 _request.setHttpVersion(_version);
545 public boolean parsedHeader(HttpField field)
547 HttpHeader header=field.getHeader();
548 String value=field.getValue();
556 if (_version.getVersion()>=HttpVersion.HTTP_1_1.getVersion())
558 HttpHeaderValue expect = HttpHeaderValue.CACHE.get(value);
559 switch (expect == null ? HttpHeaderValue.UNKNOWN : expect)
562 _expect100Continue = true;
566 _expect102Processing = true;
570 String[] values = value.split(",");
571 for (int i = 0; values != null && i < values.length; i++)
573 expect = HttpHeaderValue.CACHE.get(values[i].trim());
581 _expect100Continue = true;
584 _expect102Processing = true;
596 MimeTypes.Type mime = MimeTypes.CACHE.get(value);
597 String charset = (mime == null || mime.getCharset() == null) ? MimeTypes.getCharsetFromContentType(value) : mime.getCharset().toString();
599 _request.setCharacterEncodingUnchecked(charset);
605 if (field.getName()!=null)
606 _request.getHttpFields().add(field);
611 public boolean parsedHostHeader(String host, int port)
613 if (_uri.getHost()==null)
615 _request.setServerName(host);
616 _request.setServerPort(port);
622 public boolean headerComplete()
624 _requests.incrementAndGet();
631 if (_configuration.getSendDateHeader())
632 _response.getHttpFields().put(_connector.getServer().getDateField());
636 if (_configuration.getSendDateHeader())
637 _response.getHttpFields().put(_connector.getServer().getDateField());
641 badMessage(HttpStatus.EXPECTATION_FAILED_417,null);
648 throw new IllegalStateException();
655 public boolean content(T item)
657 if (LOG.isDebugEnabled())
658 LOG.debug("{} content {}", this, item);
659 @SuppressWarnings("unchecked")
660 HttpInput<T> input = (HttpInput<T>)_request.getHttpInput();
667 public boolean messageComplete()
669 LOG.debug("{} messageComplete", this);
670 _request.getHttpInput().messageComplete();
675 public void earlyEOF()
677 _request.getHttpInput().earlyEOF();
681 public void badMessage(int status, String reason)
683 if (status < 400 || status > 599)
684 status = HttpStatus.BAD_REQUEST_400;
688 if (_state.handling()==Action.REQUEST_DISPATCH)
690 ByteBuffer content=null;
691 HttpFields fields=new HttpFields();
693 ErrorHandler handler=getServer().getBean(ErrorHandler.class);
695 content=handler.badMessageError(status,reason,fields);
697 sendResponse(new ResponseInfo(HttpVersion.HTTP_1_1,fields,0,status,reason,false),content ,true);
700 catch (IOException e)
706 if (_state.unhandle()==Action.COMPLETE)
709 throw new IllegalStateException();
713 protected boolean sendResponse(ResponseInfo info, ByteBuffer content, boolean complete, final Callback callback)
715 // TODO check that complete only set true once by changing _committed to AtomicRef<Enum>
716 boolean committing = _committed.compareAndSet(false, true);
719 // We need an info to commit
721 info = _response.newResponseInfo();
723 // wrap callback to process 100 or 500 responses
724 final int status=info.getStatus();
725 final Callback committed = (status<200&&status>=100)?new Commit100Callback(callback):new CommitCallback(callback);
728 _transport.send(info, content, complete, committed);
732 // This is a normal write
733 _transport.send(content, complete, callback);
737 callback.failed(new IllegalStateException("committed"));
742 protected boolean sendResponse(ResponseInfo info, ByteBuffer content, boolean complete) throws IOException
744 try(Blocker blocker = _response.getHttpOutput().acquireWriteBlockingCallback())
746 boolean committing = sendResponse(info,content,complete,blocker);
752 public boolean isCommitted()
754 return _committed.get();
758 * <p>Non-Blocking write, committing the response if needed.</p>
760 * @param content the content buffer to write
761 * @param complete whether the content is complete for the response
762 * @param callback Callback when complete or failed
764 protected void write(ByteBuffer content, boolean complete, Callback callback)
766 sendResponse(null,content,complete,callback);
769 protected void execute(Runnable task)
771 _connector.getExecutor().execute(task);
774 public Scheduler getScheduler()
776 return _connector.getScheduler();
779 /* ------------------------------------------------------------ */
781 * @return true if the HttpChannel can efficiently use direct buffer (typically this means it is not over SSL or a multiplexed protocol)
783 public boolean useDirectBuffers()
785 return getEndPoint() instanceof ChannelEndPoint;
789 * If a write or similar to this channel fails this method should be called. The standard implementation
790 * of {@link #failed()} is a noop. But the different implementations of HttpChannel might want to take actions.
796 private class CommitCallback implements Callback
798 private final Callback _callback;
800 private CommitCallback(Callback callback)
802 _callback = callback;
806 public void succeeded()
808 _callback.succeeded();
812 public void failed(final Throwable x)
814 if (x instanceof EofException || x instanceof ClosedChannelException)
818 _response.getHttpOutput().closed();
822 LOG.warn("Commit failed",x);
823 _transport.send(HttpGenerator.RESPONSE_500_INFO,null,true,new Callback()
826 public void succeeded()
829 _response.getHttpOutput().closed();
833 public void failed(Throwable th)
837 _response.getHttpOutput().closed();
844 private class Commit100Callback extends CommitCallback
846 private Commit100Callback(Callback callback)
852 public void succeeded()
854 _committed.set(false);