1 29 30 package com.caucho.server.http; 31 32 import com.caucho.log.Log; 33 import com.caucho.server.cluster.Server; 34 import com.caucho.server.connection.AbstractHttpRequest; 35 import com.caucho.server.connection.Connection; 36 import com.caucho.server.dispatch.BadRequestException; 37 import com.caucho.server.dispatch.DispatchServer; 38 import com.caucho.server.dispatch.Invocation; 39 import com.caucho.server.dispatch.InvocationDecoder; 40 import com.caucho.server.port.ServerRequest; 41 import com.caucho.server.port.TcpConnection; 42 import com.caucho.server.webapp.ErrorPageManager; 43 import com.caucho.util.CharBuffer; 44 import com.caucho.util.CharSegment; 45 import com.caucho.vfs.ClientDisconnectException; 46 import com.caucho.vfs.QSocket; 47 import com.caucho.vfs.ReadStream; 48 49 import java.io.IOException ; 50 import java.io.InterruptedIOException ; 51 import java.security.cert.X509Certificate ; 52 import java.util.ArrayList ; 53 import java.util.Collections ; 54 import java.util.Enumeration ; 55 import java.util.logging.Level ; 56 import java.util.logging.Logger ; 57 58 61 public class HttpRequest extends AbstractHttpRequest 62 implements ServerRequest { 63 static final Logger log = Log.open(HttpRequest.class); 64 65 static final int HTTP_0_9 = 0x0009; 66 static final int HTTP_1_0 = 0x0100; 67 static final int HTTP_1_1 = 0x0101; 68 69 static final CharBuffer _getCb = new CharBuffer("GET"); 70 static final CharBuffer _headCb = new CharBuffer("HEAD"); 71 static final CharBuffer _postCb = new CharBuffer("POST"); 72 73 static final char []_hostCb = "Host".toCharArray(); 74 static final char []_userAgentCb = "User-Agent".toCharArray(); 75 76 static final CharBuffer _http11Cb = new CharBuffer("HTTP/1.1"); 77 static final CharBuffer _http10Cb = new CharBuffer("HTTP/1.0"); 78 79 private String _scheme; private boolean _isSecure; 81 82 private CharBuffer _method; private String _methodString; 84 85 private CharBuffer _uriHost; private CharSequence _host; 87 private CharBuffer _hostBuffer = new CharBuffer(); 88 89 private final byte []_uri; private int _uriLength; 91 92 private int _urlLengthMax = 8192; 93 94 private CharBuffer _protocol; private int _version; 96 97 private final InvocationKey _invocationKey = new InvocationKey(); 98 99 private final char []_headerBuffer = new char[16 * 1024]; 100 101 private CharSegment []_headerKeys; 102 private CharSegment []_headerValues; 103 private int _headerCapacity = 256; 104 private int _headerSize; 105 106 private ChunkedInputStream _chunkedInputStream = new ChunkedInputStream(); 107 private ContentLengthStream _contentLengthStream = new ContentLengthStream(); 108 109 private ErrorPageManager _errorManager = new ErrorPageManager(); 110 111 private boolean _initAttributes; 112 113 118 HttpRequest(DispatchServer server, Connection conn) 119 { 120 super(server, conn); 121 122 _response = new HttpResponse(this); 123 _response.init(conn.getWriteStream()); 124 125 127 129 _uri = new byte[_urlLengthMax]; 130 131 _method = new CharBuffer(); 132 _uriHost = new CharBuffer(); 133 _protocol = new CharBuffer(); 134 135 _headerCapacity = 256; 136 _headerSize = 0; 137 _headerKeys = new CharSegment[_headerCapacity]; 138 _headerValues = new CharSegment[_headerCapacity]; 139 for (int i = 0; i < _headerCapacity; i++) { 140 _headerKeys[i] = new CharSegment(); 141 _headerValues[i] = new CharSegment(); 142 } 143 } 144 145 148 public final boolean isWaitForRead() 149 { 150 return true; 151 } 152 153 161 public boolean handleRequest() 162 throws IOException 163 { 164 boolean hasRequest = false; 165 try { 166 start(); 167 _response.start(); 168 169 try { 170 try { 171 if (! readRequest(_rawRead)) { 172 if (log.isLoggable(Level.FINE)) 173 log.fine(dbgId() + "read timeout"); 174 175 return false; 176 } 177 178 setStartTime(); 179 180 hasRequest = true; 181 182 _isSecure = _conn.isSecure() || _conn.getLocalPort() == 443; 183 184 if (_protocol.length() == 0) 185 _protocol.append("HTTP/0.9"); 186 187 if (log.isLoggable(Level.FINE)) { 188 log.fine(dbgId() + _method + " " + 189 new String (_uri, 0, _uriLength) + " " + _protocol); 190 log.fine(dbgId() + "Remote-IP: " + _conn.getRemoteHost() + ":" + _conn.getRemotePort()); 191 } 192 193 parseHeaders(_rawRead); 194 195 if (getVersion() >= HTTP_1_1 && isForce10()) { 196 _protocol.clear(); 197 _protocol.append("HTTP/1.0"); 198 _version = HTTP_1_0; 199 } 200 } catch (ClientDisconnectException e) { 201 throw e; 202 } catch (Throwable e) { 203 log.log(Level.FINER, e.toString(), e); 204 205 throw new BadRequestException(String.valueOf(e)); 206 } 207 208 CharSequence host = getHost(); 209 if (host == null && getVersion() >= HTTP_1_1) 210 throw new BadRequestException("HTTP/1.1 requires host"); 211 212 String ipHost = _conn.getVirtualHost(); 213 if (ipHost != null) 214 host = ipHost; 215 216 _invocationKey.init(_isSecure, 217 host, _conn.getLocalPort(), 218 _uri, _uriLength); 219 220 Invocation invocation; 221 222 invocation = _server.getInvocation(_invocationKey); 223 224 if (invocation == null) { 225 invocation = _server.createInvocation(); 226 invocation.setSecure(_isSecure); 227 228 if (host != null) { 229 String hostName = host.toString().toLowerCase(); 230 231 invocation.setHost(hostName); 232 invocation.setPort(_conn.getLocalPort()); 233 234 int p = hostName.indexOf(':'); 237 if (p > 0) 238 invocation.setHostName(hostName.substring(0, p)); 239 else 240 invocation.setHostName(hostName); 241 } 242 243 InvocationDecoder decoder = _server.getInvocationDecoder(); 244 245 decoder.splitQueryAndUnescape(invocation, _uri, _uriLength); 246 247 if (_server.isModified()) { 248 log.info("<server> is modified"); 249 250 _invocation = invocation; 251 if (_server instanceof Server) 252 _invocation.setWebApp(((Server) _server).getErrorWebApp()); 253 254 restartServer(); 255 return false; 256 } 257 258 _server.buildInvocation(_invocationKey.clone(), invocation); 259 } 260 261 setInvocation(invocation); 262 263 invocation.service(this, _response); 264 } finally { 265 finish(); 266 } 267 } catch (ClientDisconnectException e) { 268 _response.killCache(); 269 270 throw e; 271 } catch (Throwable e) { 272 log.log(Level.FINE, e.toString(), e); 273 274 _response.killCache(); 275 killKeepalive(); 276 277 try { 278 _errorManager.sendServletError(e, this, _response); 279 } catch (ClientDisconnectException e1) { 280 throw e1; 281 } catch (Throwable e1) { 282 log.log(Level.FINE, e1.toString(), e1); 283 } 284 285 return false; 286 } finally { 287 if (hasRequest) 288 _response.finish(); 289 else 290 super.finish(); 291 } 292 293 if (log.isLoggable(Level.FINE)) { 294 log.fine(dbgId() + 295 (isKeepalive() ? "keepalive" : "no-keepalive")); 296 } 297 298 return isKeepalive(); 299 } 300 301 305 private boolean isForce10() 306 { 307 return false; 308 } 309 310 314 public boolean isTop() 315 { 316 return true; 317 } 318 319 protected boolean checkLogin() 320 { 321 return true; 322 } 323 324 329 protected void start() 330 throws IOException 331 { 332 super.start(); 333 334 _method.clear(); 335 _methodString = null; 336 _protocol.clear(); 337 _uriLength = 0; 338 _uriHost.clear(); 339 _host = null; 340 341 _headerSize = 0; 342 _initAttributes = false; 343 } 344 345 348 public boolean isSecure() 349 { 350 return _isSecure; 351 } 352 353 360 private boolean readRequest(ReadStream s) 361 throws IOException 362 { 363 int i = 0; 364 365 byte []readBuffer = s.getBuffer(); 366 int readOffset = s.getOffset(); 367 int readLength = s.getLength(); 368 int ch; 369 370 if (readOffset >= readLength) { 371 try { 372 if ((readLength = s.fillBuffer()) < 0) 373 return false; 374 } catch (InterruptedIOException e) { 375 log.fine(dbgId() + "keepalive timeout"); 376 return false; 377 } 378 readOffset = 0; 379 } 380 ch = readBuffer[readOffset++]; 381 382 384 while (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { 386 if (readOffset >= readLength) { 387 if ((readLength = s.fillBuffer()) < 0) 388 return false; 389 390 readOffset = 0; 391 } 392 ch = readBuffer[readOffset++]; 393 } 394 395 char []buffer = _method.getBuffer(); 396 int length = buffer.length; 397 int offset = 0; 398 399 while (true) { 401 if (length <= offset) { 402 } 403 else if (ch >= 'a' && ch <= 'z') 404 buffer[offset++] = ((char) (ch + 'A' - 'a')); 405 else if (ch > ' ') 406 buffer[offset++] = (char) ch; 407 else 408 break; 409 410 if (readLength <= readOffset) { 411 if ((readLength = s.fillBuffer()) < 0) 412 return false; 413 414 readOffset = 0; 415 } 416 ch = readBuffer[readOffset++]; 417 } 418 419 _method.setLength(offset); 420 421 while (ch == ' ' || ch == '\t') { 423 if (readOffset >= readLength) { 424 if ((readLength = s.fillBuffer()) < 0) 425 return false; 426 427 readOffset = 0; 428 } 429 430 ch = readBuffer[readOffset++]; 431 } 432 433 byte []uriBuffer = _uri; 434 int uriLength = 0; 435 436 if (ch != '/') { 438 while (ch > ' ' && ch != '/') { 439 if (readOffset >= readLength) { 440 if ((readLength = s.fillBuffer()) < 0) 441 return false; 442 readOffset = 0; 443 } 444 ch = readBuffer[readOffset++]; 445 } 446 447 if (readOffset >= readLength) { 448 if ((readLength = s.fillBuffer()) < 0) { 449 if (ch == '/') { 450 uriBuffer[uriLength++] = (byte) ch; 451 _uriLength = uriLength; 452 } 453 454 return true; 455 } 456 readOffset = 0; 457 } 458 459 int ch1 = readBuffer[readOffset++]; 460 461 if (ch1 != '/') { 462 uriBuffer[uriLength++] = (byte) ch; 463 ch = ch1; 464 } 465 else { 466 host: 468 while (true) { 469 if (readOffset >= readLength) { 470 if ((readLength = s.fillBuffer()) < 0) { 471 return true; 472 } 473 readOffset = 0; 474 } 475 ch = readBuffer[readOffset++]; 476 477 switch (ch) { 478 case ' ': case '\t': case '\n': case '\r': 479 break host; 480 481 case '?': 482 break host; 483 484 case '/': 485 break host; 486 487 default: 488 _uriHost.append((char) ch); 489 break; 490 } 491 } 492 } 493 } 494 495 uri: 497 while (true) { 498 switch (ch) { 499 case ' ': case '\t': case '\n': case '\r': 500 break uri; 501 502 default: 503 uriBuffer[uriLength++] = (byte) ch; 507 break; 508 } 509 510 if (readOffset >= readLength) { 511 readOffset = 0; 512 if ((readLength = s.fillBuffer()) < 0) { 513 _uriLength = uriLength; 514 return true; 515 } 516 } 517 ch = readBuffer[readOffset++]; 518 } 519 520 _uriLength = uriLength; 521 522 while (ch == ' ' || ch == '\t') { 524 if (readOffset >= readLength) { 525 readOffset = 0; 526 if ((readLength = s.fillBuffer()) < 0) 527 return true; 528 } 529 ch = readBuffer[readOffset++]; 530 } 531 532 buffer = _protocol.getBuffer(); 533 length = buffer.length; 534 offset = 0; 535 while (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n') { 537 if (offset >= length) { 538 } 539 else if (ch >= 'a' && ch <= 'z') 540 buffer[offset++] = ((char) (ch + 'A' - 'a')); 541 else 542 buffer[offset++] = (char) ch; 543 544 if (readOffset >= readLength) { 545 readOffset = 0; 546 if ((readLength = s.fillBuffer()) < 0) { 547 _protocol.setLength(offset); 548 return true; 549 } 550 } 551 ch = readBuffer[readOffset++]; 552 } 553 _protocol.setLength(offset); 554 555 if (offset != 8) { 556 _protocol.append("HTTP/0.9"); 557 _version = HTTP_0_9; 558 } 559 else if (buffer[7] == '1') _version = HTTP_1_1; 561 else if (buffer[7] == '0') _version = HTTP_1_0; 563 else 564 _version = HTTP_0_9; 565 566 while (ch != '\n') { 568 if (readOffset >= readLength) { 569 if ((readLength = s.fillBuffer()) < 0) 570 return true; 571 readOffset = 0; 572 } 573 ch = readBuffer[readOffset++]; 574 } 575 576 s.setOffset(readOffset); 577 578 return true; 579 } 580 581 586 private void parseHeaders(ReadStream s) throws IOException 587 { 588 int version = getVersion(); 591 592 if (version < HTTP_1_0) { 593 return; 594 } 595 596 if (version < HTTP_1_1) 597 killKeepalive(); 598 599 byte []readBuffer = s.getBuffer(); 600 int readOffset = s.getOffset(); 601 int readLength = s.getLength(); 602 603 char []headerBuffer = _headerBuffer; 604 int headerOffset = 1; 605 int headerBufferSize = headerBuffer.length; 606 headerBuffer[0] = 'z'; 607 int headerSize = 0; 608 _headerSize = 0; 609 610 CharSegment []headerKeys = _headerKeys; 611 CharSegment []headerValues = _headerValues; 612 613 boolean debug = log.isLoggable(Level.FINE); 614 615 while (true) { 616 int ch; 617 618 int keyOffset = headerOffset; 619 620 while (true) { 622 if (readLength <= readOffset) { 623 readOffset = 0; 624 if ((readLength = s.fillBuffer()) <= 0) 625 return; 626 } 627 ch = readBuffer[readOffset++]; 628 629 if (ch == '\n') { 630 s.setOffset(readOffset); 631 return; 632 } 633 else if (ch == ':') 634 break; 635 636 headerBuffer[headerOffset++] = (char) ch; 637 } 638 639 while (headerBuffer[headerOffset - 1] == ' ') 640 headerOffset--; 641 642 int keyLength = headerOffset - keyOffset; 643 headerKeys[headerSize].init(headerBuffer, keyOffset, keyLength); 644 645 do { 646 if (readLength <= readOffset) { 647 readOffset = 0; 648 if ((readLength = s.fillBuffer()) <= 0) 649 return; 650 } 651 ch = readBuffer[readOffset++]; 652 } while (ch == ' ' || ch == '\t'); 653 654 int valueOffset = headerOffset; 655 656 while (true) { 658 if (readLength <= readOffset) { 659 readOffset = 0; 660 if ((readLength = s.fillBuffer()) <= 0) 661 break; 662 } 663 664 if (ch == '\n') { 665 int ch1 = readBuffer[readOffset]; 666 667 if (ch1 == ' ' || ch1 == '\t') { 668 ch = ' '; 669 readOffset++; 670 671 if (headerBuffer[headerOffset - 1] == '\r') 672 headerOffset--; 673 } 674 else 675 break; 676 } 677 678 headerBuffer[headerOffset++] = (char) ch; 679 680 ch = readBuffer[readOffset++]; 681 } 682 683 while (headerBuffer[headerOffset - 1] <= ' ') 684 headerOffset--; 685 686 int valueLength = headerOffset - valueOffset; 687 headerValues[headerSize].init(headerBuffer, valueOffset, valueLength); 688 689 addHeaderInt(headerBuffer, keyOffset, keyLength, 690 headerValues[headerSize]); 691 692 if (debug) { 693 log.fine(dbgId() + 694 headerKeys[headerSize] + ": " + headerValues[headerSize]); 695 } 696 697 headerSize++; 698 _headerSize = headerSize; 699 } 700 } 701 702 705 int getVersion() 706 { 707 if (_version > 0) 708 return _version; 709 710 CharSegment protocol = getProtocolBuffer(); 711 if (protocol.length() < 8) { 712 _version = HTTP_0_9; 713 return _version; 714 } 715 716 if (protocol.equals("HTTP/1.0")) { 717 _version = HTTP_1_0; 718 return _version; 719 } 720 else if (protocol.equals("HTTP/1.1")) { 721 _version = HTTP_1_1; 722 return HTTP_1_1; 723 } 724 else if (protocol.equals("HTTP/0.9")) { 725 _version = HTTP_0_9; 726 return HTTP_0_9; 727 } 728 729 int i = protocol.indexOf('/'); 730 int len = protocol.length(); 731 int major = 0; 732 for (i++; i < len; i++) { 733 char ch = protocol.charAt(i); 734 735 if (ch >= '0' && ch <= '9') 736 major = 10 * major + ch - '0'; 737 else if (ch == '.') 738 break; 739 else { 740 _version = HTTP_1_0; 741 return _version; 742 } 743 } 744 745 int minor = 0; 746 for (i++; i < len; i++) { 747 char ch = protocol.charAt(i); 748 749 if (ch >= '0' && ch <= '9') 750 minor = 10 * minor + ch - '0'; 751 else 752 break; 753 } 754 755 _version = 256 * major + minor; 756 757 return _version; 758 } 759 760 763 public String getMethod() 764 { 765 if (_methodString == null) { 766 CharSegment cb = getMethodBuffer(); 767 if (cb.length() == 0) { 768 _methodString = "GET"; 769 return _methodString; 770 } 771 772 switch (cb.charAt(0)) { 773 case 'G': 774 _methodString = cb.equals(_getCb) ? "GET" : cb.toString(); 775 break; 776 777 case 'H': 778 _methodString = cb.equals(_headCb) ? "HEAD" : cb.toString(); 779 break; 780 781 case 'P': 782 _methodString = cb.equals(_postCb) ? "POST" : cb.toString(); 783 break; 784 785 default: 786 _methodString = cb.toString(); 787 } 788 } 789 790 return _methodString; 791 792 } 793 794 797 public CharSegment getMethodBuffer() 798 { 799 return _method; 800 } 801 802 805 protected CharSequence getHost() 806 { 807 if (_host != null) 808 return _host; 809 810 String virtualHost = _conn.getVirtualHost(); 811 if (virtualHost != null) 812 _host = virtualHost; 813 else if (_uriHost.length() > 0) 814 _host = _uriHost; 815 else 816 _host = _hostHeader; 817 818 return _host; 819 } 820 821 824 public byte []getUriBuffer() 825 { 826 return _uri; 827 } 828 829 832 public int getUriLength() 833 { 834 return _uriLength; 835 } 836 837 840 public String getProtocol() 841 { 842 switch (_version) { 843 case HTTP_1_1: 844 return "HTTP/1.1"; 845 case HTTP_1_0: 846 return "HTTP/1.0"; 847 case HTTP_0_9: 848 default: 849 return "HTTP/0.9"; 850 } 851 } 852 853 856 public CharSegment getProtocolBuffer() 857 { 858 return _protocol; 859 } 860 861 868 public void setHeader(String key, String value) 869 { 870 int tail; 871 872 if (_headerSize > 0) { 873 tail = (_headerValues[_headerSize - 1].getOffset() + 874 _headerValues[_headerSize - 1].getLength()); 875 } 876 else 877 tail = 0; 878 879 char []headerBuffer = _headerBuffer; 880 for (int i = key.length() - 1; i >= 0; i--) 881 headerBuffer[tail + i] = key.charAt(i); 882 883 _headerKeys[_headerSize].init(headerBuffer, tail, key.length()); 884 885 tail += key.length(); 886 887 for (int i = value.length() - 1; i >= 0; i--) 888 headerBuffer[tail + i] = value.charAt(i); 889 890 _headerValues[_headerSize].init(headerBuffer, tail, value.length()); 891 _headerSize++; 892 } 893 894 897 @Override 898 public int getHeaderSize() 899 { 900 return _headerSize; 901 } 902 903 906 @Override 907 public CharSegment getHeaderKey(int index) 908 { 909 return _headerKeys[index]; 910 } 911 912 915 @Override 916 public CharSegment getHeaderValue(int index) 917 { 918 return _headerValues[index]; 919 } 920 921 924 public String getHeader(String key) 925 { 926 CharSegment buf = getHeaderBuffer(key); 927 if (buf != null) 928 return buf.toString(); 929 else 930 return null; 931 } 932 933 939 public CharSegment getHeaderBuffer(char []testBuf, int length) 940 { 941 char []keyBuf = _headerBuffer; 942 CharSegment []headerKeys = _headerKeys; 943 944 for (int i = _headerSize - 1; i >= 0; i--) { 945 CharSegment key = headerKeys[i]; 946 947 if (key.length() != length) 948 continue; 949 950 int offset = key.getOffset(); 951 int j; 952 for (j = length - 1; j >= 0; j--) { 953 char a = testBuf[j]; 954 char b = keyBuf[offset + j]; 955 if (a == b) 956 continue; 957 958 if (a >= 'A' && a <= 'Z') 959 a += 'a' - 'A'; 960 if (b >= 'A' && b <= 'Z') 961 b += 'a' - 'A'; 962 if (a != b) 963 break; 964 } 965 966 if (j < 0) 967 return _headerValues[i]; 968 } 969 970 return null; 971 } 972 973 976 public CharSegment getHeaderBuffer(String key) 977 { 978 int i = matchNextHeader(0, key); 979 980 if (i >= 0) 981 return _headerValues[i]; 982 else 983 return null; 984 } 985 986 992 public void getHeaderBuffers(String key, ArrayList <CharSegment> values) 993 { 994 int i = -1; 995 while ((i = matchNextHeader(i + 1, key)) >= 0) 996 values.add(_headerValues[i]); 997 } 998 999 1005 public Enumeration getHeaders(String key) 1006 { 1007 ArrayList <String > values = new ArrayList <String >(); 1008 int i = -1; 1009 while ((i = matchNextHeader(i + 1, key)) >= 0) 1010 values.add(_headerValues[i].toString()); 1011 1012 return Collections.enumeration(values); 1013 } 1014 1015 1023 private int matchNextHeader(int i, String key) 1024 { 1025 int size = _headerSize; 1026 int length = key.length(); 1027 1028 char []keyBuf = _headerBuffer; 1029 1030 for (; i < size; i++) { 1031 CharSegment header = _headerKeys[i]; 1032 1033 if (header.length() != length) 1034 continue; 1035 1036 int offset = header.getOffset(); 1037 1038 int j; 1039 for (j = 0; j < length; j++) { 1040 char a = key.charAt(j); 1041 char b = keyBuf[offset + j]; 1042 if (a == b) 1043 continue; 1044 1045 if (a >= 'A' && a <= 'Z') 1046 a += 'a' - 'A'; 1047 if (b >= 'A' && b <= 'Z') 1048 b += 'a' - 'A'; 1049 if (a != b) 1050 break; 1051 } 1052 1053 if (j == length) 1054 return i; 1055 } 1056 1057 return -1; 1058 } 1059 1060 1063 public Enumeration getHeaderNames() 1064 { 1065 ArrayList <String > names = new ArrayList <String >(); 1066 1067 for (int i = 0; i < _headerSize; i++) { 1068 CharSegment name = _headerKeys[i]; 1069 1070 int j; 1071 for (j = 0; j < names.size(); j++) { 1072 String oldName = names.get(j); 1073 if (name.matches(oldName)) 1074 break; 1075 } 1076 if (j == names.size()) 1077 names.add(j, name.toString()); 1078 } 1079 1080 return Collections.enumeration(names); 1081 } 1082 1083 1086 public boolean initStream(ReadStream readStream, ReadStream rawRead) 1087 throws IOException 1088 { 1089 int contentLength = getContentLength(); 1090 1091 rawRead.setSibling(null); 1094 1095 String te; 1096 if (contentLength < 0 && HTTP_1_1 <= getVersion() && 1097 (te = getHeader("Transfer-Encoding")) != null) { 1098 _chunkedInputStream.init(rawRead); 1099 readStream.init(_chunkedInputStream, null); 1100 return true; 1101 } 1102 else if (contentLength >= 0) { 1104 _contentLengthStream.init(rawRead, contentLength); 1105 _readStream.init(_contentLengthStream, null); 1106 1107 return true; 1108 } 1109 else if (getMethod().equals("POST")) { 1110 _contentLengthStream.init(rawRead, 0); 1111 _readStream.init(_contentLengthStream, null); 1112 1113 throw new com.caucho.server.dispatch.BadRequestException("POST requires content-length"); 1114 } 1115 1116 else { 1117 _contentLengthStream.init(rawRead, 0); 1118 _readStream.init(_contentLengthStream, null); 1119 1120 return false; 1121 } 1122 } 1123 1124 protected void skip() 1125 throws IOException 1126 { 1127 if (getMethod() == "GET") 1128 return; 1129 1130 super.skip(); 1131 } 1132 1133 1141 1214 1215 1218 1283 1284 1287 public Object getAttribute(String name) 1288 { 1289 if (! _initAttributes) 1290 initAttributes(); 1291 1292 return super.getAttribute(name); 1293 } 1294 1295 1298 public Enumeration <String > getAttributeNames() 1299 { 1300 if (! _initAttributes) 1301 initAttributes(); 1302 1303 return super.getAttributeNames(); 1304 } 1305 1306 1309 public String findSessionIdFromConnection() 1310 { 1311 TcpConnection tcpConn = _tcpConn; 1312 1313 if (! _isSecure || tcpConn == null) 1314 return null; 1315 1316 QSocket socket = tcpConn.getSocket(); 1339 return null; 1340 } 1341 1342 1345 public ReadStream getRawInput() 1346 { 1347 return _rawRead; 1348 } 1349 1350 1353 private void initAttributes() 1354 { 1355 _initAttributes = true; 1356 1357 TcpConnection tcpConn = _tcpConn; 1358 1359 if (! _isSecure || tcpConn == null) 1360 return; 1361 1362 QSocket socket = tcpConn.getSocket(); 1363 1364 String cipherSuite = socket.getCipherSuite(); 1365 super.setAttribute("javax.servlet.request.cipher_suite", cipherSuite); 1366 1367 int keySize = socket.getCipherBits(); 1368 if (keySize != 0) 1369 super.setAttribute("javax.servlet.request.key_size", 1370 new Integer (keySize)); 1371 1372 try { 1373 X509Certificate []certs = socket.getClientCertificates(); 1374 if (certs != null && certs.length > 0) { 1375 super.setAttribute("javax.servlet.request.X509Certificate", certs[0]); 1376 super.setAttribute(com.caucho.server.security.AbstractAuthenticator.LOGIN_NAME, 1377 certs[0].getSubjectDN()); 1378 } 1379 } catch (Exception e) { 1380 log.log(Level.FINER, e.toString(), e); 1381 } 1382 } 1383 1384 public void protocolCloseEvent() 1385 { 1386 } 1387 1388 1391 public void finish() 1392 throws IOException 1393 { 1394 super.finish(); 1395 1396 skip(); 1397 } 1398 1399 String dbgId() 1400 { 1401 if ("".equals(_server.getServerId())) 1402 return "[" + _conn.getId() + "] "; 1403 else 1404 return "[" + _server.getServerId() + ", " + _conn.getId() + "] "; 1405 } 1406 1407 public String toString() 1408 { 1409 return "HttpRequest" + dbgId(); 1410 } 1411} 1412 | Popular Tags |