1 17 18 19 package org.apache.catalina.connector; 20 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.BufferedReader ; 25 import java.io.UnsupportedEncodingException ; 26 import java.security.Principal ; 27 import java.text.SimpleDateFormat ; 28 import java.util.ArrayList ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.TimeZone ; 35 import java.util.TreeMap ; 36 37 import javax.security.auth.Subject ; 38 import javax.servlet.FilterChain ; 39 import javax.servlet.RequestDispatcher ; 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletInputStream ; 42 import javax.servlet.ServletRequestAttributeEvent ; 43 import javax.servlet.ServletRequestAttributeListener ; 44 import javax.servlet.http.Cookie ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpSession ; 47 48 import org.apache.tomcat.util.buf.B2CConverter; 49 import org.apache.tomcat.util.buf.MessageBytes; 50 import org.apache.tomcat.util.buf.StringCache; 51 import org.apache.tomcat.util.http.Cookies; 52 import org.apache.tomcat.util.http.FastHttpDateFormat; 53 import org.apache.tomcat.util.http.Parameters; 54 import org.apache.tomcat.util.http.ServerCookie; 55 import org.apache.tomcat.util.http.mapper.MappingData; 56 57 import org.apache.coyote.ActionCode; 58 59 import org.apache.catalina.Context; 60 import org.apache.catalina.Globals; 61 import org.apache.catalina.Host; 62 import org.apache.catalina.Manager; 63 import org.apache.catalina.Realm; 64 import org.apache.catalina.Session; 65 import org.apache.catalina.Wrapper; 66 import org.apache.catalina.core.ApplicationFilterFactory; 67 import org.apache.catalina.realm.GenericPrincipal; 68 import org.apache.catalina.util.Enumerator; 69 import org.apache.catalina.util.ParameterMap; 70 import org.apache.catalina.util.RequestUtil; 71 import org.apache.catalina.util.StringManager; 72 import org.apache.catalina.util.StringParser; 73 74 75 82 83 public class Request 84 implements HttpServletRequest { 85 86 87 89 90 static { 91 new StringCache.ByteEntry(); 93 new StringCache.CharEntry(); 94 } 95 96 public Request() { 97 98 formats[0].setTimeZone(GMT_ZONE); 99 formats[1].setTimeZone(GMT_ZONE); 100 formats[2].setTimeZone(GMT_ZONE); 101 102 } 103 104 105 107 108 111 protected org.apache.coyote.Request coyoteRequest; 112 113 118 public void setCoyoteRequest(org.apache.coyote.Request coyoteRequest) { 119 this.coyoteRequest = coyoteRequest; 120 inputBuffer.setRequest(coyoteRequest); 121 } 122 123 126 public org.apache.coyote.Request getCoyoteRequest() { 127 return (this.coyoteRequest); 128 } 129 130 131 133 134 protected static final TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT"); 135 136 137 140 protected static StringManager sm = 141 StringManager.getManager(Constants.Package); 142 143 144 147 protected Cookie [] cookies = null; 148 149 150 156 protected SimpleDateFormat formats[] = { 157 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 158 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 159 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US) 160 }; 161 162 163 166 protected static Locale defaultLocale = Locale.getDefault(); 167 168 169 172 protected HashMap attributes = new HashMap (); 173 174 175 178 private HashMap readOnlyAttributes = new HashMap (); 179 180 181 184 protected ArrayList locales = new ArrayList (); 185 186 187 191 private transient HashMap notes = new HashMap (); 192 193 194 197 protected String authType = null; 198 199 200 203 protected CometEventImpl event = null; 204 205 206 209 protected boolean comet = false; 210 211 212 215 protected Object dispatcherType = null; 216 217 218 221 protected InputBuffer inputBuffer = new InputBuffer(); 222 223 224 227 protected CoyoteInputStream inputStream = 228 new CoyoteInputStream(inputBuffer); 229 230 231 234 protected CoyoteReader reader = new CoyoteReader(inputBuffer); 235 236 237 240 protected boolean usingInputStream = false; 241 242 243 246 protected boolean usingReader = false; 247 248 249 252 protected Principal userPrincipal = null; 253 254 255 258 protected boolean sessionParsed = false; 259 260 261 264 protected boolean parametersParsed = false; 265 266 267 270 protected boolean cookiesParsed = false; 271 272 273 276 protected boolean secure = false; 277 278 279 282 protected transient Subject subject = null; 283 284 285 288 protected static int CACHED_POST_LEN = 8192; 289 protected byte[] postData = null; 290 291 292 295 protected ParameterMap parameterMap = new ParameterMap(); 296 297 298 301 protected Session session = null; 302 303 304 307 protected Object requestDispatcherPath = null; 308 309 310 313 protected boolean requestedSessionCookie = false; 314 315 316 319 protected String requestedSessionId = null; 320 321 322 325 protected boolean requestedSessionURL = false; 326 327 328 331 protected boolean localesParsed = false; 332 333 334 337 private StringParser parser = new StringParser(); 338 339 340 343 protected int localPort = -1; 344 345 348 protected String remoteAddr = null; 349 350 351 354 protected String remoteHost = null; 355 356 357 360 protected int remotePort = -1; 361 362 365 protected String localAddr = null; 366 367 368 371 protected String localName = null; 372 373 374 376 377 381 public void recycle() { 382 383 context = null; 384 wrapper = null; 385 386 dispatcherType = null; 387 requestDispatcherPath = null; 388 389 comet = false; 390 if (event != null) { 391 event.clear(); 392 event = null; 393 } 394 395 authType = null; 396 inputBuffer.recycle(); 397 usingInputStream = false; 398 usingReader = false; 399 userPrincipal = null; 400 subject = null; 401 sessionParsed = false; 402 parametersParsed = false; 403 cookiesParsed = false; 404 locales.clear(); 405 localesParsed = false; 406 secure = false; 407 remoteAddr = null; 408 remoteHost = null; 409 remotePort = -1; 410 localPort = -1; 411 localAddr = null; 412 localName = null; 413 414 attributes.clear(); 415 notes.clear(); 416 cookies = null; 417 418 if (session != null) { 419 session.endAccess(); 420 } 421 session = null; 422 requestedSessionCookie = false; 423 requestedSessionId = null; 424 requestedSessionURL = false; 425 426 if (Constants.SECURITY || Connector.RECYCLE_FACADES) { 427 parameterMap = new ParameterMap(); 428 } else { 429 parameterMap.setLocked(false); 430 parameterMap.clear(); 431 } 432 433 mappingData.recycle(); 434 435 if (Constants.SECURITY || Connector.RECYCLE_FACADES) { 436 if (facade != null) { 437 facade.clear(); 438 facade = null; 439 } 440 if (inputStream != null) { 441 inputStream.clear(); 442 inputStream = null; 443 } 444 if (reader != null) { 445 reader.clear(); 446 reader = null; 447 } 448 } 449 450 } 451 452 453 456 public void clearEncoders() { 457 inputBuffer.clearEncoders(); 458 } 459 460 461 463 464 467 protected Connector connector; 468 469 472 public Connector getConnector() { 473 return (this.connector); 474 } 475 476 481 public void setConnector(Connector connector) { 482 this.connector = connector; 483 } 484 485 486 489 protected Context context = null; 490 491 494 public Context getContext() { 495 return (this.context); 496 } 497 498 499 507 public void setContext(Context context) { 508 this.context = context; 509 } 510 511 512 515 protected FilterChain filterChain = null; 516 517 520 public FilterChain getFilterChain() { 521 return (this.filterChain); 522 } 523 524 529 public void setFilterChain(FilterChain filterChain) { 530 this.filterChain = filterChain; 531 } 532 533 534 537 public Host getHost() { 538 if (getContext() == null) 539 return null; 540 return (Host) getContext().getParent(); 541 } 543 544 545 552 public void setHost(Host host) { 553 mappingData.host = host; 554 } 555 556 557 560 protected static final String info = 561 "org.apache.coyote.catalina.CoyoteRequest/1.0"; 562 563 568 public String getInfo() { 569 return (info); 570 } 571 572 573 576 protected MappingData mappingData = new MappingData(); 577 578 581 public MappingData getMappingData() { 582 return (mappingData); 583 } 584 585 586 589 protected RequestFacade facade = null; 590 591 595 public HttpServletRequest getRequest() { 596 if (facade == null) { 597 facade = new RequestFacade(this); 598 } 599 return (facade); 600 } 601 602 603 606 protected org.apache.catalina.connector.Response response = null; 607 608 611 public org.apache.catalina.connector.Response getResponse() { 612 return (this.response); 613 } 614 615 620 public void setResponse(org.apache.catalina.connector.Response response) { 621 this.response = response; 622 } 623 624 627 public InputStream getStream() { 628 if (inputStream == null) { 629 inputStream = new CoyoteInputStream(inputBuffer); 630 } 631 return inputStream; 632 } 633 634 639 public void setStream(InputStream stream) { 640 } 642 643 644 647 protected B2CConverter URIConverter = null; 648 649 652 protected B2CConverter getURIConverter() { 653 return URIConverter; 654 } 655 656 661 protected void setURIConverter(B2CConverter URIConverter) { 662 this.URIConverter = URIConverter; 663 } 664 665 666 669 protected Wrapper wrapper = null; 670 671 674 public Wrapper getWrapper() { 675 return (this.wrapper); 676 } 677 678 679 685 public void setWrapper(Wrapper wrapper) { 686 this.wrapper = wrapper; 687 } 688 689 690 692 693 699 public ServletInputStream createInputStream() 700 throws IOException { 701 if (inputStream == null) { 702 inputStream = new CoyoteInputStream(inputBuffer); 703 } 704 return inputStream; 705 } 706 707 708 714 public void finishRequest() throws IOException { 715 } 717 718 719 725 public Object getNote(String name) { 726 return (notes.get(name)); 727 } 728 729 730 734 public Iterator getNoteNames() { 735 return (notes.keySet().iterator()); 736 } 737 738 739 745 public void removeNote(String name) { 746 notes.remove(name); 747 } 748 749 750 757 public void setNote(String name, Object value) { 758 notes.put(name, value); 759 } 760 761 762 767 public void setContentLength(int length) { 768 } 770 771 772 779 public void setContentType(String type) { 780 } 782 783 784 789 public void setProtocol(String protocol) { 790 } 792 793 794 799 public void setRemoteAddr(String remoteAddr) { 800 } 802 803 804 810 public void setRemoteHost(String remoteHost) { 811 } 813 814 815 821 public void setScheme(String scheme) { 822 } 824 825 826 832 public void setSecure(boolean secure) { 833 this.secure = secure; 834 } 835 836 837 842 public void setServerName(String name) { 843 coyoteRequest.serverName().setString(name); 844 } 845 846 847 852 public void setServerPort(int port) { 853 coyoteRequest.setServerPort(port); 854 } 855 856 857 859 860 866 public Object getAttribute(String name) { 867 868 if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) { 869 return (dispatcherType == null) 870 ? ApplicationFilterFactory.REQUEST_INTEGER 871 : dispatcherType; 872 } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) { 873 return (requestDispatcherPath == null) 874 ? getRequestPathMB().toString() 875 : requestDispatcherPath.toString(); 876 } 877 878 Object attr=attributes.get(name); 879 880 if(attr!=null) 881 return(attr); 882 883 attr = coyoteRequest.getAttribute(name); 884 if(attr != null) 885 return attr; 886 if( isSSLAttribute(name) ) { 887 coyoteRequest.action(ActionCode.ACTION_REQ_SSL_ATTRIBUTE, 888 coyoteRequest); 889 attr = coyoteRequest.getAttribute(Globals.CERTIFICATES_ATTR); 890 if( attr != null) { 891 attributes.put(Globals.CERTIFICATES_ATTR, attr); 892 } 893 attr = coyoteRequest.getAttribute(Globals.CIPHER_SUITE_ATTR); 894 if(attr != null) { 895 attributes.put(Globals.CIPHER_SUITE_ATTR, attr); 896 } 897 attr = coyoteRequest.getAttribute(Globals.KEY_SIZE_ATTR); 898 if(attr != null) { 899 attributes.put(Globals.KEY_SIZE_ATTR, attr); 900 } 901 attr = coyoteRequest.getAttribute(Globals.SSL_SESSION_ID_ATTR); 902 if(attr != null) { 903 attributes.put(Globals.SSL_SESSION_ID_ATTR, attr); 904 } 905 attr = attributes.get(name); 906 } 907 return attr; 908 } 909 910 911 914 static boolean isSSLAttribute(String name) { 915 return Globals.CERTIFICATES_ATTR.equals(name) || 916 Globals.CIPHER_SUITE_ATTR.equals(name) || 917 Globals.KEY_SIZE_ATTR.equals(name) || 918 Globals.SSL_SESSION_ID_ATTR.equals(name); 919 } 920 921 925 public Enumeration getAttributeNames() { 926 if (isSecure()) { 927 getAttribute(Globals.CERTIFICATES_ATTR); 928 } 929 return new Enumerator(attributes.keySet(), true); 930 } 931 932 933 936 public String getCharacterEncoding() { 937 return (coyoteRequest.getCharacterEncoding()); 938 } 939 940 941 944 public int getContentLength() { 945 return (coyoteRequest.getContentLength()); 946 } 947 948 949 952 public String getContentType() { 953 return (coyoteRequest.getContentType()); 954 } 955 956 957 966 public ServletInputStream getInputStream() throws IOException { 967 968 if (usingReader) 969 throw new IllegalStateException 970 (sm.getString("coyoteRequest.getInputStream.ise")); 971 972 usingInputStream = true; 973 if (inputStream == null) { 974 inputStream = new CoyoteInputStream(inputBuffer); 975 } 976 return inputStream; 977 978 } 979 980 981 987 public Locale getLocale() { 988 989 if (!localesParsed) 990 parseLocales(); 991 992 if (locales.size() > 0) { 993 return ((Locale ) locales.get(0)); 994 } else { 995 return (defaultLocale); 996 } 997 998 } 999 1000 1001 1007 public Enumeration getLocales() { 1008 1009 if (!localesParsed) 1010 parseLocales(); 1011 1012 if (locales.size() > 0) 1013 return (new Enumerator(locales)); 1014 ArrayList results = new ArrayList (); 1015 results.add(defaultLocale); 1016 return (new Enumerator(results)); 1017 1018 } 1019 1020 1021 1028 public String getParameter(String name) { 1029 1030 if (!parametersParsed) 1031 parseParameters(); 1032 1033 return coyoteRequest.getParameters().getParameter(name); 1034 1035 } 1036 1037 1038 1039 1048 public Map getParameterMap() { 1049 1050 if (parameterMap.isLocked()) 1051 return parameterMap; 1052 1053 Enumeration enumeration = getParameterNames(); 1054 while (enumeration.hasMoreElements()) { 1055 String name = enumeration.nextElement().toString(); 1056 String [] values = getParameterValues(name); 1057 parameterMap.put(name, values); 1058 } 1059 1060 parameterMap.setLocked(true); 1061 1062 return parameterMap; 1063 1064 } 1065 1066 1067 1070 public Enumeration getParameterNames() { 1071 1072 if (!parametersParsed) 1073 parseParameters(); 1074 1075 return coyoteRequest.getParameters().getParameterNames(); 1076 1077 } 1078 1079 1080 1086 public String [] getParameterValues(String name) { 1087 1088 if (!parametersParsed) 1089 parseParameters(); 1090 1091 return coyoteRequest.getParameters().getParameterValues(name); 1092 1093 } 1094 1095 1096 1099 public String getProtocol() { 1100 return coyoteRequest.protocol().toString(); 1101 } 1102 1103 1104 1113 public BufferedReader getReader() throws IOException { 1114 1115 if (usingInputStream) 1116 throw new IllegalStateException 1117 (sm.getString("coyoteRequest.getReader.ise")); 1118 1119 usingReader = true; 1120 inputBuffer.checkConverter(); 1121 if (reader == null) { 1122 reader = new CoyoteReader(inputBuffer); 1123 } 1124 return reader; 1125 1126 } 1127 1128 1129 1137 public String getRealPath(String path) { 1138 1139 if (context == null) 1140 return (null); 1141 ServletContext servletContext = context.getServletContext(); 1142 if (servletContext == null) 1143 return (null); 1144 else { 1145 try { 1146 return (servletContext.getRealPath(path)); 1147 } catch (IllegalArgumentException e) { 1148 return (null); 1149 } 1150 } 1151 1152 } 1153 1154 1155 1158 public String getRemoteAddr() { 1159 if (remoteAddr == null) { 1160 coyoteRequest.action 1161 (ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE, coyoteRequest); 1162 remoteAddr = coyoteRequest.remoteAddr().toString(); 1163 } 1164 return remoteAddr; 1165 } 1166 1167 1168 1171 public String getRemoteHost() { 1172 if (remoteHost == null) { 1173 if (!connector.getEnableLookups()) { 1174 remoteHost = getRemoteAddr(); 1175 } else { 1176 coyoteRequest.action 1177 (ActionCode.ACTION_REQ_HOST_ATTRIBUTE, coyoteRequest); 1178 remoteHost = coyoteRequest.remoteHost().toString(); 1179 } 1180 } 1181 return remoteHost; 1182 } 1183 1184 1188 public int getRemotePort(){ 1189 if (remotePort == -1) { 1190 coyoteRequest.action 1191 (ActionCode.ACTION_REQ_REMOTEPORT_ATTRIBUTE, coyoteRequest); 1192 remotePort = coyoteRequest.getRemotePort(); 1193 } 1194 return remotePort; 1195 } 1196 1197 1201 public String getLocalName(){ 1202 if (localName == null) { 1203 coyoteRequest.action 1204 (ActionCode.ACTION_REQ_LOCAL_NAME_ATTRIBUTE, coyoteRequest); 1205 localName = coyoteRequest.localName().toString(); 1206 } 1207 return localName; 1208 } 1209 1210 1214 public String getLocalAddr(){ 1215 if (localAddr == null) { 1216 coyoteRequest.action 1217 (ActionCode.ACTION_REQ_LOCAL_ADDR_ATTRIBUTE, coyoteRequest); 1218 localAddr = coyoteRequest.localAddr().toString(); 1219 } 1220 return localAddr; 1221 } 1222 1223 1224 1228 public int getLocalPort(){ 1229 if (localPort == -1){ 1230 coyoteRequest.action 1231 (ActionCode.ACTION_REQ_LOCALPORT_ATTRIBUTE, coyoteRequest); 1232 localPort = coyoteRequest.getLocalPort(); 1233 } 1234 return localPort; 1235 } 1236 1237 1243 public RequestDispatcher getRequestDispatcher(String path) { 1244 1245 if (context == null) 1246 return (null); 1247 1248 if (path == null) 1250 return (null); 1251 else if (path.startsWith("/")) 1252 return (context.getServletContext().getRequestDispatcher(path)); 1253 1254 String servletPath = (String ) getAttribute(Globals.INCLUDE_SERVLET_PATH_ATTR); 1256 if (servletPath == null) 1257 servletPath = getServletPath(); 1258 1259 String pathInfo = getPathInfo(); 1261 String requestPath = null; 1262 1263 if (pathInfo == null) { 1264 requestPath = servletPath; 1265 } else { 1266 requestPath = servletPath + pathInfo; 1267 } 1268 1269 int pos = requestPath.lastIndexOf('/'); 1270 String relative = null; 1271 if (pos >= 0) { 1272 relative = RequestUtil.normalize 1273 (requestPath.substring(0, pos + 1) + path); 1274 } else { 1275 relative = RequestUtil.normalize(requestPath + path); 1276 } 1277 1278 return (context.getServletContext().getRequestDispatcher(relative)); 1279 1280 } 1281 1282 1283 1286 public String getScheme() { 1287 return (coyoteRequest.scheme().toString()); 1288 } 1289 1290 1291 1294 public String getServerName() { 1295 return (coyoteRequest.serverName().toString()); 1296 } 1297 1298 1299 1302 public int getServerPort() { 1303 return (coyoteRequest.getServerPort()); 1304 } 1305 1306 1307 1310 public boolean isSecure() { 1311 return (secure); 1312 } 1313 1314 1315 1320 public void removeAttribute(String name) { 1321 Object value = null; 1322 boolean found = false; 1323 1324 if (readOnlyAttributes.containsKey(name)) { 1328 return; 1329 } 1330 1331 if (name.startsWith("org.apache.tomcat.")) { 1333 coyoteRequest.getAttributes().remove(name); 1334 } 1335 1336 found = attributes.containsKey(name); 1337 if (found) { 1338 value = attributes.get(name); 1339 attributes.remove(name); 1340 } else { 1341 return; 1342 } 1343 1344 Object listeners[] = context.getApplicationEventListeners(); 1346 if ((listeners == null) || (listeners.length == 0)) 1347 return; 1348 ServletRequestAttributeEvent event = 1349 new ServletRequestAttributeEvent (context.getServletContext(), 1350 getRequest(), name, value); 1351 for (int i = 0; i < listeners.length; i++) { 1352 if (!(listeners[i] instanceof ServletRequestAttributeListener )) 1353 continue; 1354 ServletRequestAttributeListener listener = 1355 (ServletRequestAttributeListener ) listeners[i]; 1356 try { 1357 listener.attributeRemoved(event); 1358 } catch (Throwable t) { 1359 context.getLogger().error(sm.getString("coyoteRequest.attributeEvent"), t); 1360 attributes.put( Globals.EXCEPTION_ATTR, t ); 1362 } 1363 } 1364 } 1365 1366 1367 1373 public void setAttribute(String name, Object value) { 1374 1375 if (name == null) 1377 throw new IllegalArgumentException 1378 (sm.getString("coyoteRequest.setAttribute.namenull")); 1379 1380 if (value == null) { 1382 removeAttribute(name); 1383 return; 1384 } 1385 1386 if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) { 1387 dispatcherType = value; 1388 return; 1389 } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) { 1390 requestDispatcherPath = value; 1391 return; 1392 } 1393 1394 Object oldValue = null; 1395 boolean replaced = false; 1396 1397 if (readOnlyAttributes.containsKey(name)) { 1401 return; 1402 } 1403 1404 oldValue = attributes.put(name, value); 1405 if (oldValue != null) { 1406 replaced = true; 1407 } 1408 1409 if (name.startsWith("org.apache.tomcat.")) { 1411 coyoteRequest.setAttribute(name, value); 1412 } 1413 1414 Object listeners[] = context.getApplicationEventListeners(); 1416 if ((listeners == null) || (listeners.length == 0)) 1417 return; 1418 ServletRequestAttributeEvent event = null; 1419 if (replaced) 1420 event = 1421 new ServletRequestAttributeEvent (context.getServletContext(), 1422 getRequest(), name, oldValue); 1423 else 1424 event = 1425 new ServletRequestAttributeEvent (context.getServletContext(), 1426 getRequest(), name, value); 1427 1428 for (int i = 0; i < listeners.length; i++) { 1429 if (!(listeners[i] instanceof ServletRequestAttributeListener )) 1430 continue; 1431 ServletRequestAttributeListener listener = 1432 (ServletRequestAttributeListener ) listeners[i]; 1433 try { 1434 if (replaced) { 1435 listener.attributeReplaced(event); 1436 } else { 1437 listener.attributeAdded(event); 1438 } 1439 } catch (Throwable t) { 1440 context.getLogger().error(sm.getString("coyoteRequest.attributeEvent"), t); 1441 attributes.put( Globals.EXCEPTION_ATTR, t ); 1443 } 1444 } 1445 } 1446 1447 1448 1460 public void setCharacterEncoding(String enc) 1461 throws UnsupportedEncodingException { 1462 1463 if (usingReader) 1464 return; 1465 1466 byte buffer[] = new byte[1]; 1468 buffer[0] = (byte) 'a'; 1469 String dummy = new String (buffer, enc); 1470 1471 coyoteRequest.setCharacterEncoding(enc); 1473 1474 } 1475 1476 1477 1479 1480 1485 public void addCookie(Cookie cookie) { 1486 1487 if (!cookiesParsed) 1488 parseCookies(); 1489 1490 int size = 0; 1491 if (cookies != null) { 1492 size = cookies.length; 1493 } 1494 1495 Cookie [] newCookies = new Cookie [size + 1]; 1496 for (int i = 0; i < size; i++) { 1497 newCookies[i] = cookies[i]; 1498 } 1499 newCookies[size] = cookie; 1500 1501 cookies = newCookies; 1502 1503 } 1504 1505 1506 1512 public void addHeader(String name, String value) { 1513 } 1515 1516 1517 1523 public void addLocale(Locale locale) { 1524 locales.add(locale); 1525 } 1526 1527 1528 1536 public void addParameter(String name, String values[]) { 1537 coyoteRequest.getParameters().addParameterValues(name, values); 1538 } 1539 1540 1541 1544 public void clearCookies() { 1545 cookiesParsed = true; 1546 cookies = null; 1547 } 1548 1549 1550 1553 public void clearHeaders() { 1554 } 1556 1557 1558 1561 public void clearLocales() { 1562 locales.clear(); 1563 } 1564 1565 1566 1569 public void clearParameters() { 1570 } 1572 1573 1574 1581 public void setAuthType(String type) { 1582 this.authType = type; 1583 } 1584 1585 1586 1593 public void setContextPath(String path) { 1594 1595 if (path == null) { 1596 mappingData.contextPath.setString(""); 1597 } else { 1598 mappingData.contextPath.setString(path); 1599 } 1600 1601 } 1602 1603 1604 1609 public void setMethod(String method) { 1610 } 1612 1613 1614 1620 public void setQueryString(String query) { 1621 } 1623 1624 1625 1632 public void setPathInfo(String path) { 1633 mappingData.pathInfo.setString(path); 1634 } 1635 1636 1637 1644 public void setRequestedSessionCookie(boolean flag) { 1645 1646 this.requestedSessionCookie = flag; 1647 1648 } 1649 1650 1651 1657 public void setRequestedSessionId(String id) { 1658 1659 this.requestedSessionId = id; 1660 1661 } 1662 1663 1664 1671 public void setRequestedSessionURL(boolean flag) { 1672 1673 this.requestedSessionURL = flag; 1674 1675 } 1676 1677 1678 1684 public void setRequestURI(String uri) { 1685 } 1687 1688 1689 1694 public void setDecodedRequestURI(String uri) { 1695 } 1697 1698 1699 1704 public String getDecodedRequestURI() { 1705 return (coyoteRequest.decodedURI().toString()); 1706 } 1707 1708 1709 1714 public MessageBytes getDecodedRequestURIMB() { 1715 return (coyoteRequest.decodedURI()); 1716 } 1717 1718 1719 1726 public void setServletPath(String path) { 1727 if (path != null) 1728 mappingData.wrapperPath.setString(path); 1729 } 1730 1731 1732 1739 public void setUserPrincipal(Principal principal) { 1740 1741 if (System.getSecurityManager() != null){ 1742 HttpSession session = getSession(false); 1743 if ( (subject != null) && 1744 (!subject.getPrincipals().contains(principal)) ){ 1745 subject.getPrincipals().add(principal); 1746 } else if (session != null && 1747 session.getAttribute(Globals.SUBJECT_ATTR) == null) { 1748 subject = new Subject (); 1749 subject.getPrincipals().add(principal); 1750 } 1751 if (session != null){ 1752 session.setAttribute(Globals.SUBJECT_ATTR, subject); 1753 } 1754 } 1755 1756 this.userPrincipal = principal; 1757 } 1758 1759 1760 1762 1763 1766 public String getAuthType() { 1767 return (authType); 1768 } 1769 1770 1771 1775 public String getContextPath() { 1776 return (mappingData.contextPath.toString()); 1777 } 1778 1779 1780 1785 public MessageBytes getContextPathMB() { 1786 return (mappingData.contextPath); 1787 } 1788 1789 1790 1793 public Cookie [] getCookies() { 1794 1795 if (!cookiesParsed) 1796 parseCookies(); 1797 1798 return cookies; 1799 1800 } 1801 1802 1803 1806 public void setCookies(Cookie [] cookies) { 1807 1808 this.cookies = cookies; 1809 1810 } 1811 1812 1813 1822 public long getDateHeader(String name) { 1823 1824 String value = getHeader(name); 1825 if (value == null) 1826 return (-1L); 1827 1828 long result = FastHttpDateFormat.parseDate(value, formats); 1830 if (result != (-1L)) { 1831 return result; 1832 } 1833 throw new IllegalArgumentException (value); 1834 1835 } 1836 1837 1838 1844 public String getHeader(String name) { 1845 return coyoteRequest.getHeader(name); 1846 } 1847 1848 1849 1855 public Enumeration getHeaders(String name) { 1856 return coyoteRequest.getMimeHeaders().values(name); 1857 } 1858 1859 1860 1863 public Enumeration getHeaderNames() { 1864 return coyoteRequest.getMimeHeaders().names(); 1865 } 1866 1867 1868 1877 public int getIntHeader(String name) { 1878 1879 String value = getHeader(name); 1880 if (value == null) { 1881 return (-1); 1882 } else { 1883 return (Integer.parseInt(value)); 1884 } 1885 1886 } 1887 1888 1889 1892 public String getMethod() { 1893 return coyoteRequest.method().toString(); 1894 } 1895 1896 1897 1900 public String getPathInfo() { 1901 return (mappingData.pathInfo.toString()); 1902 } 1903 1904 1905 1910 public MessageBytes getPathInfoMB() { 1911 return (mappingData.pathInfo); 1912 } 1913 1914 1915 1919 public String getPathTranslated() { 1920 1921 if (context == null) 1922 return (null); 1923 1924 if (getPathInfo() == null) { 1925 return (null); 1926 } else { 1927 return (context.getServletContext().getRealPath(getPathInfo())); 1928 } 1929 1930 } 1931 1932 1933 1936 public String getQueryString() { 1937 String queryString = coyoteRequest.queryString().toString(); 1938 if (queryString == null || queryString.equals("")) { 1939 return (null); 1940 } else { 1941 return queryString; 1942 } 1943 } 1944 1945 1946 1950 public String getRemoteUser() { 1951 1952 if (userPrincipal != null) { 1953 return (userPrincipal.getName()); 1954 } else { 1955 return (null); 1956 } 1957 1958 } 1959 1960 1961 1966 public MessageBytes getRequestPathMB() { 1967 return (mappingData.requestPath); 1968 } 1969 1970 1971 1974 public String getRequestedSessionId() { 1975 return (requestedSessionId); 1976 } 1977 1978 1979 1982 public String getRequestURI() { 1983 return coyoteRequest.requestURI().toString(); 1984 } 1985 1986 1987 2003 public StringBuffer getRequestURL() { 2004 2005 StringBuffer url = new StringBuffer (); 2006 String scheme = getScheme(); 2007 int port = getServerPort(); 2008 if (port < 0) 2009 port = 80; 2011 url.append(scheme); 2012 url.append("://"); 2013 url.append(getServerName()); 2014 if ((scheme.equals("http") && (port != 80)) 2015 || (scheme.equals("https") && (port != 443))) { 2016 url.append(':'); 2017 url.append(port); 2018 } 2019 url.append(getRequestURI()); 2020 2021 return (url); 2022 2023 } 2024 2025 2026 2030 public String getServletPath() { 2031 return (mappingData.wrapperPath.toString()); 2032 } 2033 2034 2035 2040 public MessageBytes getServletPathMB() { 2041 return (mappingData.wrapperPath); 2042 } 2043 2044 2045 2049 public HttpSession getSession() { 2050 Session session = doGetSession(true); 2051 if (session != null) { 2052 return session.getSession(); 2053 } else { 2054 return null; 2055 } 2056 } 2057 2058 2059 2065 public HttpSession getSession(boolean create) { 2066 Session session = doGetSession(create); 2067 if (session != null) { 2068 return session.getSession(); 2069 } else { 2070 return null; 2071 } 2072 } 2073 2074 2075 2079 public boolean isRequestedSessionIdFromCookie() { 2080 2081 if (requestedSessionId != null) 2082 return (requestedSessionCookie); 2083 else 2084 return (false); 2085 2086 } 2087 2088 2089 2093 public boolean isRequestedSessionIdFromURL() { 2094 2095 if (requestedSessionId != null) 2096 return (requestedSessionURL); 2097 else 2098 return (false); 2099 2100 } 2101 2102 2103 2110 public boolean isRequestedSessionIdFromUrl() { 2111 return (isRequestedSessionIdFromURL()); 2112 } 2113 2114 2115 2119 public boolean isRequestedSessionIdValid() { 2120 2121 if (requestedSessionId == null) 2122 return (false); 2123 if (context == null) 2124 return (false); 2125 Manager manager = context.getManager(); 2126 if (manager == null) 2127 return (false); 2128 Session session = null; 2129 try { 2130 session = manager.findSession(requestedSessionId); 2131 } catch (IOException e) { 2132 session = null; 2133 } 2134 if ((session != null) && session.isValid()) 2135 return (true); 2136 else 2137 return (false); 2138 2139 } 2140 2141 2142 2148 public boolean isUserInRole(String role) { 2149 2150 if (userPrincipal == null) 2152 return (false); 2153 2154 if (context == null) 2156 return (false); 2157 Realm realm = context.getRealm(); 2158 if (realm == null) 2159 return (false); 2160 2161 if (wrapper != null) { 2163 String realRole = wrapper.findSecurityReference(role); 2164 if ((realRole != null) && 2165 realm.hasRole(userPrincipal, realRole)) 2166 return (true); 2167 } 2168 2169 return (realm.hasRole(userPrincipal, role)); 2171 2172 } 2173 2174 2175 2178 public Principal getPrincipal() { 2179 return (userPrincipal); 2180 } 2181 2182 2183 2186 public Principal getUserPrincipal() { 2187 if (userPrincipal instanceof GenericPrincipal) { 2188 return ((GenericPrincipal) userPrincipal).getUserPrincipal(); 2189 } else { 2190 return (userPrincipal); 2191 } 2192 } 2193 2194 2195 2199 public Session getSessionInternal() { 2200 return doGetSession(true); 2201 } 2202 2203 2204 2210 public Session getSessionInternal(boolean create) { 2211 return doGetSession(create); 2212 } 2213 2214 2215 2219 public CometEventImpl getEvent() { 2220 if (event == null) { 2221 event = new CometEventImpl(this, response); 2222 } 2223 return event; 2224 } 2225 2226 2227 2230 public boolean isComet() { 2231 return comet; 2232 } 2233 2234 2235 2238 public void setComet(boolean comet) { 2239 this.comet = comet; 2240 } 2241 2242 2243 2245 2246 protected Session doGetSession(boolean create) { 2247 2248 if (context == null) 2250 return (null); 2251 2252 if ((session != null) && !session.isValid()) 2254 session = null; 2255 if (session != null) 2256 return (session); 2257 2258 Manager manager = null; 2260 if (context != null) 2261 manager = context.getManager(); 2262 if (manager == null) 2263 return (null); if (requestedSessionId != null) { 2265 try { 2266 session = manager.findSession(requestedSessionId); 2267 } catch (IOException e) { 2268 session = null; 2269 } 2270 if ((session != null) && !session.isValid()) 2271 session = null; 2272 if (session != null) { 2273 session.access(); 2274 return (session); 2275 } 2276 } 2277 2278 if (!create) 2280 return (null); 2281 if ((context != null) && (response != null) && 2282 context.getCookies() && 2283 response.getResponse().isCommitted()) { 2284 throw new IllegalStateException 2285 (sm.getString("coyoteRequest.sessionCreateCommitted")); 2286 } 2287 2288 if (connector.getEmptySessionPath() 2292 && isRequestedSessionIdFromCookie()) { 2293 session = manager.createSession(getRequestedSessionId()); 2294 } else { 2295 session = manager.createSession(null); 2296 } 2297 2298 if ((session != null) && (getContext() != null) 2300 && getContext().getCookies()) { 2301 Cookie cookie = new Cookie (Globals.SESSION_COOKIE_NAME, 2302 session.getIdInternal()); 2303 configureSessionCookie(cookie); 2304 response.addCookieInternal(cookie); 2305 } 2306 2307 if (session != null) { 2308 session.access(); 2309 return (session); 2310 } else { 2311 return (null); 2312 } 2313 2314 } 2315 2316 2321 protected void configureSessionCookie(Cookie cookie) { 2322 cookie.setMaxAge(-1); 2323 String contextPath = null; 2324 if (!connector.getEmptySessionPath() && (getContext() != null)) { 2325 contextPath = getContext().getEncodedPath(); 2326 } 2327 if ((contextPath != null) && (contextPath.length() > 0)) { 2328 cookie.setPath(contextPath); 2329 } else { 2330 cookie.setPath("/"); 2331 } 2332 if (isSecure()) { 2333 cookie.setSecure(true); 2334 } 2335 } 2336 2337 2340 protected void parseCookies() { 2341 2342 cookiesParsed = true; 2343 2344 Cookies serverCookies = coyoteRequest.getCookies(); 2345 int count = serverCookies.getCookieCount(); 2346 if (count <= 0) 2347 return; 2348 2349 cookies = new Cookie [count]; 2350 2351 int idx=0; 2352 for (int i = 0; i < count; i++) { 2353 ServerCookie scookie = serverCookies.getCookie(i); 2354 try { 2355 Cookie cookie = new Cookie (scookie.getName().toString(), 2356 scookie.getValue().toString()); 2357 cookie.setPath(scookie.getPath().toString()); 2358 cookie.setVersion(scookie.getVersion()); 2359 String domain = scookie.getDomain().toString(); 2360 if (domain != null) { 2361 cookie.setDomain(scookie.getDomain().toString()); 2362 } 2363 cookies[idx++] = cookie; 2364 } catch(IllegalArgumentException e) { 2365 } 2367 } 2368 if( idx < count ) { 2369 Cookie [] ncookies = new Cookie [idx]; 2370 System.arraycopy(cookies, 0, ncookies, 0, idx); 2371 cookies = ncookies; 2372 } 2373 2374 } 2375 2376 2379 protected void parseParameters() { 2380 2381 parametersParsed = true; 2382 2383 Parameters parameters = coyoteRequest.getParameters(); 2384 2385 String enc = getCharacterEncoding(); 2388 2389 boolean useBodyEncodingForURI = connector.getUseBodyEncodingForURI(); 2390 if (enc != null) { 2391 parameters.setEncoding(enc); 2392 if (useBodyEncodingForURI) { 2393 parameters.setQueryStringEncoding(enc); 2394 } 2395 } else { 2396 parameters.setEncoding 2397 (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); 2398 if (useBodyEncodingForURI) { 2399 parameters.setQueryStringEncoding 2400 (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); 2401 } 2402 } 2403 2404 parameters.handleQueryParameters(); 2405 2406 if (usingInputStream || usingReader) 2407 return; 2408 2409 if (!getMethod().equalsIgnoreCase("POST")) 2410 return; 2411 2412 String contentType = getContentType(); 2413 if (contentType == null) 2414 contentType = ""; 2415 int semicolon = contentType.indexOf(';'); 2416 if (semicolon >= 0) { 2417 contentType = contentType.substring(0, semicolon).trim(); 2418 } else { 2419 contentType = contentType.trim(); 2420 } 2421 if (!("application/x-www-form-urlencoded".equals(contentType))) 2422 return; 2423 2424 int len = getContentLength(); 2425 2426 if (len > 0) { 2427 int maxPostSize = connector.getMaxPostSize(); 2428 if ((maxPostSize > 0) && (len > maxPostSize)) { 2429 context.getLogger().info 2430 (sm.getString("coyoteRequest.postTooLarge")); 2431 throw new IllegalStateException ("Post too large"); 2432 } 2433 try { 2434 byte[] formData = null; 2435 if (len < CACHED_POST_LEN) { 2436 if (postData == null) 2437 postData = new byte[CACHED_POST_LEN]; 2438 formData = postData; 2439 } else { 2440 formData = new byte[len]; 2441 } 2442 int actualLen = readPostBody(formData, len); 2443 if (actualLen == len) { 2444 parameters.processParameters(formData, 0, len); 2445 } 2446 } catch (Throwable t) { 2447 ; } 2449 } 2450 2451 } 2452 2453 2454 2457 protected int readPostBody(byte body[], int len) 2458 throws IOException { 2459 2460 int offset = 0; 2461 do { 2462 int inputLen = getStream().read(body, offset, len - offset); 2463 if (inputLen <= 0) { 2464 return offset; 2465 } 2466 offset += inputLen; 2467 } while ((len - offset) > 0); 2468 return len; 2469 2470 } 2471 2472 2473 2476 protected void parseLocales() { 2477 2478 localesParsed = true; 2479 2480 Enumeration values = getHeaders("accept-language"); 2481 2482 while (values.hasMoreElements()) { 2483 String value = values.nextElement().toString(); 2484 parseLocalesHeader(value); 2485 } 2486 2487 } 2488 2489 2490 2493 protected void parseLocalesHeader(String value) { 2494 2495 TreeMap locales = new TreeMap (); 2500 2501 int white = value.indexOf(' '); 2503 if (white < 0) 2504 white = value.indexOf('\t'); 2505 if (white >= 0) { 2506 StringBuffer sb = new StringBuffer (); 2507 int len = value.length(); 2508 for (int i = 0; i < len; i++) { 2509 char ch = value.charAt(i); 2510 if ((ch != ' ') && (ch != '\t')) 2511 sb.append(ch); 2512 } 2513 value = sb.toString(); 2514 } 2515 2516 parser.setString(value); int length = parser.getLength(); 2519 while (true) { 2520 2521 int start = parser.getIndex(); 2523 if (start >= length) 2524 break; 2525 int end = parser.findChar(','); 2526 String entry = parser.extract(start, end).trim(); 2527 parser.advance(); 2529 double quality = 1.0; 2531 int semi = entry.indexOf(";q="); 2532 if (semi >= 0) { 2533 try { 2534 quality = Double.parseDouble(entry.substring(semi + 3)); 2535 } catch (NumberFormatException e) { 2536 quality = 0.0; 2537 } 2538 entry = entry.substring(0, semi); 2539 } 2540 2541 if (quality < 0.00005) 2543 continue; if ("*".equals(entry)) 2545 continue; 2547 String language = null; 2549 String country = null; 2550 String variant = null; 2551 int dash = entry.indexOf('-'); 2552 if (dash < 0) { 2553 language = entry; 2554 country = ""; 2555 variant = ""; 2556 } else { 2557 language = entry.substring(0, dash); 2558 country = entry.substring(dash + 1); 2559 int vDash = country.indexOf('-'); 2560 if (vDash > 0) { 2561 String cTemp = country.substring(0, vDash); 2562 variant = country.substring(vDash + 1); 2563 country = cTemp; 2564 } else { 2565 variant = ""; 2566 } 2567 } 2568 2569 Locale locale = new Locale (language, country, variant); 2571 Double key = new Double (-quality); ArrayList values = (ArrayList ) locales.get(key); 2573 if (values == null) { 2574 values = new ArrayList (); 2575 locales.put(key, values); 2576 } 2577 values.add(locale); 2578 2579 } 2580 2581 Iterator keys = locales.keySet().iterator(); 2584 while (keys.hasNext()) { 2585 Double key = (Double ) keys.next(); 2586 ArrayList list = (ArrayList ) locales.get(key); 2587 Iterator values = list.iterator(); 2588 while (values.hasNext()) { 2589 Locale locale = (Locale ) values.next(); 2590 addLocale(locale); 2591 } 2592 } 2593 2594 } 2595 2596} 2597 | Popular Tags |