1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.coyote;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletConnection;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.MessageBytes;
import org.apache.tomcat.util.http.parser.Host;
import org.apache.tomcat.util.log.UserDataHelper;
import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
import org.apache.tomcat.util.net.DispatchType;
import org.apache.tomcat.util.net.SSLSupport;
import org.apache.tomcat.util.net.SocketEvent;
import org.apache.tomcat.util.net.SocketWrapperBase;
import org.apache.tomcat.util.res.StringManager;
/**
* Provides functionality and attributes common to all supported protocols (currently HTTP and AJP) for processing a
* single request/response.
*/
public abstract class AbstractProcessor extends AbstractProcessorLight implements ActionHook {
private static final StringManager sm = StringManager.getManager(AbstractProcessor.class);
// Used to avoid useless B2C conversion on the host name.
private char[] hostNameC = new char[0];
protected final Adapter adapter;
protected final AsyncStateMachine asyncStateMachine;
private volatile long asyncTimeout = -1;
/*
* Tracks the current async generation when a timeout is dispatched. In the time it takes for a container thread to
* be allocated and the timeout processing to start, it is possible that the application completes this generation
* of async processing and starts a new one. If the timeout is then processed against the new generation, response
* mix-up can occur. This field is used to ensure that any timeout event processed is for the current async
* generation. This prevents the response mix-up.
*/
private volatile long asyncTimeoutGeneration = 0;
protected final Request request;
protected final Response response;
protected volatile SocketWrapperBase<?> socketWrapper = null;
protected volatile SSLSupport sslSupport;
/**
* Error state for the request/response currently being processed.
*/
private ErrorState errorState = ErrorState.NONE;
protected final UserDataHelper userDataHelper;
public AbstractProcessor(Adapter adapter) {
this(adapter, new Request(), new Response());
}
protected AbstractProcessor(Adapter adapter, Request coyoteRequest, Response coyoteResponse) {
this.adapter = adapter;
asyncStateMachine = new AsyncStateMachine(this);
request = coyoteRequest;
response = coyoteResponse;
response.setHook(this);
request.setResponse(response);
request.setHook(this);
userDataHelper = new UserDataHelper(getLog());
}
/**
* Update the current error state to the new error state if the new error state is more severe than the current
* error state.
*
* @param errorState The error status details
* @param t The error which occurred
*/
protected void setErrorState(ErrorState errorState, Throwable t) {
if (getLog().isDebugEnabled()) {
getLog().debug(sm.getString("abstractProcessor.setErrorState", errorState), t);
}
// Use the return value to avoid processing more than one async error
// in a single async cycle.
response.setError();
boolean blockIo = this.errorState.isIoAllowed() && !errorState.isIoAllowed();
this.errorState = this.errorState.getMostSevere(errorState);
// Don't change the status code for IOException since that is almost
// certainly a client disconnect in which case it is preferable to keep
// the original status code http://markmail.org/message/4cxpwmxhtgnrwh7n
if (response.getStatus() < 400 && !(t instanceof IOException)) {
response.setStatus(500);
}
if (t != null) {
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
}
if (blockIo && isAsync()) {
if (asyncStateMachine.asyncError()) {
processSocketEvent(SocketEvent.ERROR, true);
}
}
}
protected ErrorState getErrorState() {
return errorState;
}
@Override
public Request getRequest() {
return request;
}
/**
* Get the associated adapter.
*
* @return the associated adapter
*/
public Adapter getAdapter() {
return adapter;
}
/**
* Set the socket wrapper being used.
*
* @param socketWrapper The socket wrapper
*/
protected void setSocketWrapper(SocketWrapperBase<?> socketWrapper) {
this.socketWrapper = socketWrapper;
}
/**
* @return the socket wrapper being used.
*/
protected final SocketWrapperBase<?> getSocketWrapper() {
return socketWrapper;
}
@Override
public final void setSslSupport(SSLSupport sslSupport) {
this.sslSupport = sslSupport;
}
/**
* Provides a mechanism to trigger processing on a container thread.
*
* @param runnable The task representing the processing that needs to take place on a container thread
*/
protected void execute(Runnable runnable) {
SocketWrapperBase<?> socketWrapper = this.socketWrapper;
if (socketWrapper == null) {
throw new RejectedExecutionException(sm.getString("abstractProcessor.noExecute"));
} else {
socketWrapper.execute(runnable);
}
}
@Override
public boolean isAsync() {
return asyncStateMachine.isAsync();
}
@Override
public SocketState asyncPostProcess() throws IOException {
return asyncStateMachine.asyncPostProcess();
}
@Override
public final SocketState dispatch(SocketEvent status) throws IOException {
if (status == SocketEvent.OPEN_WRITE && response.getWriteListener() != null) {
asyncStateMachine.asyncOperation();
try {
if (flushBufferedWrite()) {
return SocketState.LONG;
}
} catch (IOException ioe) {
if (getLog().isDebugEnabled()) {
getLog().debug(sm.getString("abstractProcessor.asyncFail"), ioe);
}
status = SocketEvent.ERROR;
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, ioe);
}
} else if (status == SocketEvent.OPEN_READ && request.getReadListener() != null) {
dispatchNonBlockingRead();
} else if (status == SocketEvent.ERROR) {
// An I/O error occurred on a non-container thread. This includes:
// - read/write timeouts fired by the Poller in NIO
// - completion handler failures in NIO2
if (request.getAttribute(RequestDispatcher.ERROR_EXCEPTION) == null) {
// Because the error did not occur on a container thread the
// request's error attribute has not been set. If an exception
// is available from the socketWrapper, use it to set the
// request's error attribute here so it is visible to the error
// handling.
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, socketWrapper.getError());
}
if (request.getReadListener() != null || response.getWriteListener() != null) {
// The error occurred during non-blocking I/O. Set the correct
// state else the error handling will trigger an ISE.
asyncStateMachine.asyncOperation();
}
}
RequestInfo rp = request.getRequestProcessor();
try {
rp.setStage(Constants.STAGE_SERVICE);
if (!getAdapter().asyncDispatch(request, response, status)) {
setErrorState(ErrorState.CLOSE_NOW, null);
}
} catch (InterruptedIOException e) {
setErrorState(ErrorState.CLOSE_CONNECTION_NOW, e);
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
setErrorState(ErrorState.CLOSE_NOW, t);
getLog().error(sm.getString("http11processor.request.process"), t);
}
rp.setStage(Constants.STAGE_ENDED);
SocketState state;
if (getErrorState().isError()) {
request.updateCounters();
state = SocketState.CLOSED;
} else if (isAsync()) {
state = SocketState.LONG;
} else {
request.updateCounters();
state = dispatchEndRequest();
}
if (getLog().isTraceEnabled()) {
getLog().trace("Socket: [" + socketWrapper + "], Status in: [" + status + "], State out: [" + state + "]");
}
return state;
}
protected void parseHost(MessageBytes valueMB) {
if (valueMB == null || valueMB.isNull()) {
populateHost();
populatePort();
return;
} else if (valueMB.getLength() == 0) {
// Empty Host header so set sever name to empty string
request.serverName().setString("");
populatePort();
return;
}
ByteChunk valueBC = valueMB.getByteChunk();
byte[] valueB = valueBC.getBytes();
int valueL = valueBC.getLength();
int valueS = valueBC.getStart();
if (hostNameC.length < valueL) {
hostNameC = new char[valueL];
}
try {
// Validates the host name
int colonPos = Host.parse(valueMB);
// Extract the port information first, if any
if (colonPos != -1) {
int port = 0;
for (int i = colonPos + 1; i < valueL; i++) {
char c = (char) valueB[i + valueS];
if (c < '0' || c > '9') {
response.setStatus(400);
setErrorState(ErrorState.CLOSE_CLEAN, null);
return;
}
port = port * 10 + c - '0';
}
request.setServerPort(port);
// Only need to copy the host name up to the :
valueL = colonPos;
}
// Extract the host name
for (int i = 0; i < valueL; i++) {
hostNameC[i] = (char) valueB[i + valueS];
}
request.serverName().setChars(hostNameC, 0, valueL);
} catch (IllegalArgumentException e) {
// IllegalArgumentException indicates that the host name is invalid
UserDataHelper.Mode logMode = userDataHelper.getNextMode();
if (logMode != null) {
String message = sm.getString("abstractProcessor.hostInvalid", valueMB.toString());
switch (logMode) {
case INFO_THEN_DEBUG:
message += sm.getString("abstractProcessor.fallToDebug");
//$FALL-THROUGH$
case INFO:
getLog().info(message, e);
break;
case DEBUG:
getLog().debug(message, e);
}
}
response.setStatus(400);
setErrorState(ErrorState.CLOSE_CLEAN, e);
}
}
/**
* Called when a host header is not present in the request (e.g. HTTP/1.0). It populates the server name with
* appropriate information. The source is expected to vary by protocol.
* <p>
* The default implementation is a NO-OP.
*/
protected void populateHost() {
// NO-OP
}
/**
* Called when a host header is not present or is empty in the request (e.g. HTTP/1.0). It populates the server port
* with appropriate information. The source is expected to vary by protocol.
* <p>
* The default implementation is a NO-OP.
*/
protected void populatePort() {
// NO-OP
}
@Override
public final void action(ActionCode actionCode, Object param) {
switch (actionCode) {
// 'Normal' servlet support
case COMMIT: {
if (!response.isCommitted()) {
try {
// Validate and write response headers
prepareResponse();
} catch (IOException ioe) {
handleIOException(ioe);
}
}
break;
}
case CLOSE: {
action(ActionCode.COMMIT, null);
try {
finishResponse();
} catch (IOException ioe) {
handleIOException(ioe);
}
break;
}
case ACK: {
ack((ContinueResponseTiming) param);
break;
}
case EARLY_HINTS: {
try {
earlyHints();
} catch (IOException ioe) {
handleIOException(ioe);
}
break;
}
case CLIENT_FLUSH: {
action(ActionCode.COMMIT, null);
try {
flush();
} catch (IOException ioe) {
handleIOException(ioe);
response.setErrorException(ioe);
}
break;
}
case AVAILABLE: {
request.setAvailable(available(Boolean.TRUE.equals(param)));
break;
}
case REQ_SET_BODY_REPLAY: {
ByteChunk body = (ByteChunk) param;
setRequestBody(body);
break;
}
// Error handling
case IS_ERROR: {
((AtomicBoolean) param).set(getErrorState().isError());
break;
}
case IS_IO_ALLOWED: {
((AtomicBoolean) param).set(getErrorState().isIoAllowed());
break;
}
case CLOSE_NOW: {
// Prevent further writes to the response
setSwallowResponse();
if (param instanceof Throwable) {
setErrorState(ErrorState.CLOSE_NOW, (Throwable) param);
} else {
setErrorState(ErrorState.CLOSE_NOW, null);
}
break;
}
case DISABLE_SWALLOW_INPUT: {
// Cancelled upload or similar.
// No point reading the remainder of the request.
disableSwallowRequest();
// This is an error state. Make sure it is marked as such.
setErrorState(ErrorState.CLOSE_CLEAN, null);
break;
}
// Request attribute support
case REQ_HOST_ADDR_ATTRIBUTE: {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.remoteAddr().setString(socketWrapper.getRemoteAddr());
}
break;
}
case REQ_PEER_ADDR_ATTRIBUTE: {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.peerAddr().setString(socketWrapper.getRemoteAddr());
}
break;
}
case REQ_HOST_ATTRIBUTE: {
populateRequestAttributeRemoteHost();
break;
}
case REQ_LOCALPORT_ATTRIBUTE: {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.setLocalPort(socketWrapper.getLocalPort());
}
break;
}
case REQ_LOCAL_ADDR_ATTRIBUTE: {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.localAddr().setString(socketWrapper.getLocalAddr());
}
break;
}
case REQ_LOCAL_NAME_ATTRIBUTE: {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.localName().setString(socketWrapper.getLocalName());
}
break;
}
case REQ_REMOTEPORT_ATTRIBUTE: {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.setRemotePort(socketWrapper.getRemotePort());
}
break;
}
// SSL request attribute support
case REQ_SSL_ATTRIBUTE: {
populateSslRequestAttributes();
break;
}
case REQ_SSL_CERTIFICATE: {
try {
sslReHandShake();
} catch (IOException ioe) {
setErrorState(ErrorState.CLOSE_CONNECTION_NOW, ioe);
}
break;
}
// Servlet 3.0 asynchronous support
case ASYNC_START: {
asyncStateMachine.asyncStart((AsyncContextCallback) param);
break;
}
case ASYNC_COMPLETE: {
clearDispatches();
if (asyncStateMachine.asyncComplete()) {
processSocketEvent(SocketEvent.OPEN_READ, true);
}
break;
}
case ASYNC_DISPATCH: {
if (asyncStateMachine.asyncDispatch()) {
processSocketEvent(SocketEvent.OPEN_READ, true);
}
break;
}
case ASYNC_DISPATCHED: {
asyncStateMachine.asyncDispatched();
break;
}
case ASYNC_ERROR: {
asyncStateMachine.asyncError();
break;
}
case ASYNC_IS_ASYNC: {
((AtomicBoolean) param).set(asyncStateMachine.isAsync());
break;
}
case ASYNC_IS_COMPLETING: {
((AtomicBoolean) param).set(asyncStateMachine.isCompleting());
break;
}
case ASYNC_IS_DISPATCHING: {
((AtomicBoolean) param).set(asyncStateMachine.isAsyncDispatching());
break;
}
case ASYNC_IS_ERROR: {
((AtomicBoolean) param).set(asyncStateMachine.isAsyncError());
break;
}
case ASYNC_IS_STARTED: {
((AtomicBoolean) param).set(asyncStateMachine.isAsyncStarted());
break;
}
case ASYNC_IS_TIMINGOUT: {
((AtomicBoolean) param).set(asyncStateMachine.isAsyncTimingOut());
break;
}
case ASYNC_RUN: {
asyncStateMachine.asyncRun((Runnable) param);
break;
}
case ASYNC_SETTIMEOUT: {
if (param == null) {
return;
}
long timeout = ((Long) param).longValue();
setAsyncTimeout(timeout);
break;
}
case ASYNC_TIMEOUT: {
AtomicBoolean result = (AtomicBoolean) param;
result.set(asyncStateMachine.asyncTimeout());
break;
}
case ASYNC_POST_PROCESS: {
try {
asyncStateMachine.asyncPostProcess();
} catch (IOException ioe) {
handleIOException(ioe);
}
break;
}
// Servlet 3.1 non-blocking I/O
case REQUEST_BODY_FULLY_READ: {
AtomicBoolean result = (AtomicBoolean) param;
result.set(isRequestBodyFullyRead());
break;
}
case NB_READ_INTEREST: {
AtomicBoolean isReady = (AtomicBoolean) param;
isReady.set(isReadyForRead());
break;
}
case NB_WRITE_INTEREST: {
AtomicBoolean isReady = (AtomicBoolean) param;
isReady.set(isReadyForWrite());
break;
}
case DISPATCH_READ: {
addDispatch(DispatchType.NON_BLOCKING_READ);
break;
}
case DISPATCH_WRITE: {
addDispatch(DispatchType.NON_BLOCKING_WRITE);
break;
}
case DISPATCH_ERROR: {
addDispatch(DispatchType.NON_BLOCKING_ERROR);
break;
}
case DISPATCH_EXECUTE: {
executeDispatches();
break;
}
// Servlet 3.1 HTTP Upgrade
case UPGRADE: {
doHttpUpgrade((UpgradeToken) param);
break;
}
// Servlet 4.0 Trailers
case IS_TRAILER_FIELDS_READY: {
AtomicBoolean result = (AtomicBoolean) param;
result.set(isTrailerFieldsReady());
break;
}
case IS_TRAILER_FIELDS_SUPPORTED: {
AtomicBoolean result = (AtomicBoolean) param;
result.set(isTrailerFieldsSupported());
break;
}
// Identifiers
case PROTOCOL_REQUEST_ID: {
@SuppressWarnings("unchecked")
AtomicReference<Object> result = (AtomicReference<Object>) param;
result.set(getProtocolRequestId());
break;
}
case SERVLET_CONNECTION: {
@SuppressWarnings("unchecked")
AtomicReference<Object> result = (AtomicReference<Object>) param;
result.set(getServletConnection());
break;
}
}
}
private void handleIOException(IOException ioe) {
if (ioe instanceof CloseNowException) {
// Close the channel but keep the connection open
setErrorState(ErrorState.CLOSE_NOW, ioe);
} else {
// Close the connection and all channels within that connection
setErrorState(ErrorState.CLOSE_CONNECTION_NOW, ioe);
}
}
/**
* Perform any necessary processing for a non-blocking read before dispatching to the adapter.
*/
protected void dispatchNonBlockingRead() {
asyncStateMachine.asyncOperation();
}
/**
* {@inheritDoc}
* <p>
* Subclasses of this base class represent a single request/response pair. The timeout to be processed is,
* therefore, the Servlet asynchronous processing timeout.
*/
@Override
public void timeoutAsync(long now) {
if (now < 0) {
doTimeoutAsync();
} else {
long asyncTimeout = getAsyncTimeout();
if (asyncTimeout > 0) {
long asyncStart = asyncStateMachine.getLastAsyncStart();
if ((now - asyncStart) > asyncTimeout) {
doTimeoutAsync();
}
} else if (!asyncStateMachine.isAvailable()) {
// Timeout the async process if the associated web application
// is no longer running.
doTimeoutAsync();
}
}
}
private void doTimeoutAsync() {
// Avoid multiple timeouts
setAsyncTimeout(-1);
asyncTimeoutGeneration = asyncStateMachine.getCurrentGeneration();
processSocketEvent(SocketEvent.TIMEOUT, true);
}
@Override
public boolean checkAsyncTimeoutGeneration() {
return asyncTimeoutGeneration == asyncStateMachine.getCurrentGeneration();
}
public void setAsyncTimeout(long timeout) {
asyncTimeout = timeout;
}
public long getAsyncTimeout() {
return asyncTimeout;
}
@Override
public void recycle() {
errorState = ErrorState.NONE;
asyncStateMachine.recycle();
}
/**
* When committing the response, we have to validate the set of headers, as well as set up the response filters.
*
* @throws IOException IO exception during commit
*/
protected abstract void prepareResponse() throws IOException;
/**
* Finish the current response.
*
* @throws IOException IO exception during the write
*/
protected abstract void finishResponse() throws IOException;
/**
* Process acknowledgment of the request.
*
* @param continueResponseTiming specifies when an acknowledgment should be sent
*/
protected abstract void ack(ContinueResponseTiming continueResponseTiming);
protected abstract void earlyHints() throws IOException;
/**
* Callback to write data from the buffer.
*
* @throws IOException IO exception during the write
*/
protected abstract void flush() throws IOException;
/**
* Queries if bytes are available in buffers.
*
* @param doRead {@code true} to perform a read when no bytes are availble
*
* @return the amount of bytes that are known to be available
*/
protected abstract int available(boolean doRead);
/**
* Set the specified byte chunk as the request body that will be read. This allows saving and processing requests.
*
* @param body the byte chunk containing all the request bytes
*/
protected abstract void setRequestBody(ByteChunk body);
/**
* The response is finished and no additional bytes need to be sent to the client.
*/
protected abstract void setSwallowResponse();
/**
* Swallowing bytes is required for pipelining requests, so this allows to avoid doing extra operations in case an
* error occurs and the connection is to be closed instead.
*/
protected abstract void disableSwallowRequest();
/**
* Processors that populate request attributes directly (e.g. AJP) should over-ride this method and return
* {@code false}.
*
* @return {@code true} if the SocketWrapper should be used to populate the request attributes, otherwise
* {@code false}.
*/
protected boolean getPopulateRequestAttributesFromSocket() {
return true;
}
/**
* Populate the remote host request attribute. Processors (e.g. AJP) that populate this from an alternative source
* should override this method.
*/
protected void populateRequestAttributeRemoteHost() {
if (getPopulateRequestAttributesFromSocket() && socketWrapper != null) {
request.remoteHost().setString(socketWrapper.getRemoteHost());
}
}
/**
* Populate the TLS related request attributes from the {@link SSLSupport} instance associated with this processor.
* Protocols that populate TLS attributes from a different source (e.g. AJP) should override this method.
*/
@SuppressWarnings("deprecation")
protected void populateSslRequestAttributes() {
try {
if (sslSupport != null) {
Object sslO = sslSupport.getProtocol();
if (sslO != null) {
request.setAttribute(SSLSupport.SECURE_PROTOCOL_KEY, sslO);
request.setAttribute(SSLSupport.PROTOCOL_VERSION_KEY, sslO);
}
sslO = sslSupport.getCipherSuite();
if (sslO != null) {
request.setAttribute(SSLSupport.CIPHER_SUITE_KEY, sslO);
}
sslO = sslSupport.getPeerCertificateChain();
if (sslO != null) {
request.setAttribute(SSLSupport.CERTIFICATE_KEY, sslO);
}
sslO = sslSupport.getKeySize();
if (sslO != null) {
request.setAttribute(SSLSupport.KEY_SIZE_KEY, sslO);
}
sslO = sslSupport.getSessionId();
if (sslO != null) {
request.setAttribute(SSLSupport.SESSION_ID_KEY, sslO);
}
sslO = sslSupport.getRequestedProtocols();
if (sslO != null) {
request.setAttribute(SSLSupport.REQUESTED_PROTOCOL_VERSIONS_KEY, sslO);
}
sslO = sslSupport.getRequestedCiphers();
if (sslO != null) {
request.setAttribute(SSLSupport.REQUESTED_CIPHERS_KEY, sslO);
}
request.setAttribute(SSLSupport.SESSION_MGR, sslSupport);
}
} catch (Exception e) {
getLog().warn(sm.getString("abstractProcessor.socket.ssl"), e);
}
}
/**
* Processors that can perform a TLS re-handshake (e.g. HTTP/1.1) should override this method and implement the
* re-handshake.
*
* @throws IOException If authentication is required then there will be I/O with the client and this exception will
* be thrown if that goes wrong
*/
protected void sslReHandShake() throws IOException {
// NO-OP
}
protected void processSocketEvent(SocketEvent event, boolean dispatch) {
SocketWrapperBase<?> socketWrapper = getSocketWrapper();
if (socketWrapper != null) {
socketWrapper.processSocket(event, dispatch);
}
}
protected boolean isReadyForRead() {
if (available(true) > 0) {
return true;
}
if (!isRequestBodyFullyRead()) {
registerReadInterest();
}
return false;
}
/**
* @return {@code true} if it is known that the request body has been fully read
*/
protected abstract boolean isRequestBodyFullyRead();
/**
* When using non blocking IO, register to get a callback when polling determines that bytes are available for
* reading.
*/
protected abstract void registerReadInterest();
/**
* @return {@code true} if bytes can be written without blocking
*/
protected abstract boolean isReadyForWrite();
protected void executeDispatches() {
SocketWrapperBase<?> socketWrapper = getSocketWrapper();
Iterator<DispatchType> dispatches = getIteratorAndClearDispatches();
if (socketWrapper != null) {
Lock lock = socketWrapper.getLock();
lock.lock();
try {
/*
* This method is called when non-blocking IO is initiated by defining a read and/or write listener in a
* non-container thread. It is called once the non-container thread completes so that the first calls to
* onWritePossible() and/or onDataAvailable() as appropriate are made by the container.
*
* Processing the dispatches requires (TODO confirm applies without APR) that the socket has been added
* to the waitingRequests queue. This may not have occurred by the time that the non-container thread
* completes triggering the call to this method. Therefore, the coded syncs on the SocketWrapper as the
* container thread that initiated this non-container thread holds a lock on the SocketWrapper. The
* container thread will add the socket to the waitingRequests queue before releasing the lock on the
* socketWrapper. Therefore, by obtaining the lock on socketWrapper before processing the dispatches, we
* can be sure that the socket has been added to the waitingRequests queue.
*/
while (dispatches != null && dispatches.hasNext()) {
DispatchType dispatchType = dispatches.next();
socketWrapper.processSocket(dispatchType.getSocketStatus(), false);
}
} finally {
lock.unlock();
}
}
}
/**
* {@inheritDoc} Processors that implement HTTP upgrade must override this method and provide the necessary token.
*/
@Override
public UpgradeToken getUpgradeToken() {
// Should never reach this code but in case we do...
throw new IllegalStateException(sm.getString("abstractProcessor.httpupgrade.notsupported"));
}
/**
* Process an HTTP upgrade. Processors that support HTTP upgrade should override this method and process the
* provided token.
*
* @param upgradeToken Contains all the information necessary for the Processor to process the upgrade
*
* @throws UnsupportedOperationException if the protocol does not support HTTP upgrade
*/
protected void doHttpUpgrade(UpgradeToken upgradeToken) {
// Should never happen
throw new UnsupportedOperationException(sm.getString("abstractProcessor.httpupgrade.notsupported"));
}
/**
* {@inheritDoc} Processors that implement HTTP upgrade must override this method.
*/
@Override
public ByteBuffer getLeftoverInput() {
// Should never reach this code but in case we do...
throw new IllegalStateException(sm.getString("abstractProcessor.httpupgrade.notsupported"));
}
/**
* {@inheritDoc} Processors that implement HTTP upgrade must override this method.
*/
@Override
public boolean isUpgrade() {
return false;
}
protected abstract boolean isTrailerFieldsReady();
/**
* Protocols that support trailer fields should override this method and return {@code true}.
*
* @return {@code true} if trailer fields are supported by this processor, otherwise {@code false}.
*/
protected boolean isTrailerFieldsSupported() {
return false;
}
/**
* Protocols that provide per HTTP request IDs (e.g. Stream ID for HTTP/2) should override this method and return
* the appropriate ID.
*
* @return The ID associated with this request or the empty string if no such ID is defined
*/
protected Object getProtocolRequestId() {
return null;
}
/**
* Protocols must override this method and return an appropriate ServletConnection instance
*
* @return the ServletConnection instance associated with the current request.
*/
protected abstract ServletConnection getServletConnection();
/**
* Flush any pending writes. Used during non-blocking writes to flush any remaining data from a previous incomplete
* write.
*
* @return <code>true</code> if data remains to be flushed at the end of method
*
* @throws IOException If an I/O error occurs while attempting to flush the data
*/
protected abstract boolean flushBufferedWrite() throws IOException;
/**
* Perform any necessary clean-up processing if the dispatch resulted in the completion of processing for the
* current request.
*
* @return The state to return for the socket once the clean-up for the current request has completed
*
* @throws IOException If an I/O error occurs while attempting to end the request
*/
protected abstract SocketState dispatchEndRequest() throws IOException;
@Override
protected final void logAccess(SocketWrapperBase<?> socketWrapper) throws IOException {
// Set the socket wrapper so the access log can read the socket related
// information (e.g. client IP)
setSocketWrapper(socketWrapper);
// Set up the minimal request information
request.setStartTimeNanos(System.nanoTime());
// Set up the minimal response information
response.setStatus(400);
response.setError();
getAdapter().log(request, response, 0);
}
}
|