1 2 3 27 28 29 package org.apache.coyote.tomcat5; 30 31 32 import java.io.InputStream ; 33 import java.io.IOException ; 34 import java.io.BufferedReader ; 35 import java.io.UnsupportedEncodingException ; 36 import java.net.InetAddress ; 37 import java.net.Socket ; 38 import java.net.UnknownHostException ; 40 import java.security.Principal ; 42 import java.text.SimpleDateFormat ; 43 import java.util.ArrayList ; 44 import java.util.Enumeration ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.Locale ; 48 import java.util.Map ; 49 import java.util.TimeZone ; 50 import java.util.TreeMap ; 51 import java.security.AccessController ; 52 import java.security.PrivilegedExceptionAction ; 53 import java.security.PrivilegedActionException ; 54 55 import javax.security.auth.Subject ; 56 import javax.servlet.FilterChain ; 57 import javax.servlet.RequestDispatcher ; 58 import javax.servlet.ServletContext ; 59 import javax.servlet.ServletInputStream ; 60 import javax.servlet.ServletRequestAttributeEvent ; 61 import javax.servlet.ServletRequestAttributeListener ; 62 import javax.servlet.http.Cookie ; 63 import javax.servlet.http.HttpServletRequest ; 64 import javax.servlet.http.HttpServletResponse ; 65 import javax.servlet.http.HttpSession ; 66 67 import org.apache.tomcat.util.buf.B2CConverter; 68 import org.apache.tomcat.util.buf.ByteChunk; 70 import org.apache.tomcat.util.buf.CharChunk; 71 import org.apache.tomcat.util.buf.MessageBytes; 73 import org.apache.tomcat.util.http.Cookies; 74 import org.apache.tomcat.util.http.FastHttpDateFormat; 75 import org.apache.tomcat.util.http.Parameters; 76 import org.apache.tomcat.util.http.ServerCookie; 77 import org.apache.tomcat.util.http.mapper.MappingData; 78 79 import org.apache.coyote.ActionCode; 80 import org.apache.coyote.Request; 81 82 import org.apache.catalina.Connector; 83 import org.apache.catalina.Context; 84 import org.apache.catalina.Globals; 85 import org.apache.catalina.Host; 86 import org.apache.catalina.HttpRequest; 87 import org.apache.catalina.HttpResponse; 88 import org.apache.catalina.Logger; 89 import org.apache.catalina.Manager; 90 import org.apache.catalina.Realm; 91 import org.apache.catalina.Session; 92 import org.apache.catalina.ValveContext; 93 import org.apache.catalina.Wrapper; 94 95 import org.apache.catalina.core.ApplicationFilterFactory; 96 import org.apache.catalina.util.Enumerator; 97 import org.apache.catalina.util.ParameterMap; 98 import org.apache.catalina.util.RequestUtil; 99 import org.apache.catalina.util.StringManager; 100 import org.apache.catalina.util.StringParser; 101 import org.apache.catalina.security.SecurityUtil; 102 import com.sun.org.apache.commons.logging.Log; 103 import com.sun.org.apache.commons.logging.LogFactory; 105 107 import com.sun.appserv.ProxyHandler; 109 111 118 119 public class CoyoteRequest 120 implements HttpRequest, HttpServletRequest { 121 122 123 125 126 public CoyoteRequest() { 127 formats = (SimpleDateFormat []) staticDateFormats.get(); 129 formats[0].setTimeZone(TimeZone.getTimeZone("GMT")); 130 formats[1].setTimeZone(TimeZone.getTimeZone("GMT")); 131 formats[2].setTimeZone(TimeZone.getTimeZone("GMT")); 132 } 134 135 136 138 139 142 protected Request coyoteRequest; 143 144 149 public void setCoyoteRequest(Request coyoteRequest) { 150 this.coyoteRequest = coyoteRequest; 151 inputBuffer.setRequest(coyoteRequest); 152 153 populateSSLAttributes(); 155 } 157 158 161 public Request getCoyoteRequest() { 162 return (this.coyoteRequest); 163 } 164 165 166 168 169 172 protected static StringManager sm = 173 StringManager.getManager(Constants.Package); 174 175 private static final Log log = LogFactory.getLog(CoyoteRequest.class); 177 179 182 protected Cookie [] cookies = null; 183 184 191 192 195 private static ThreadLocal staticDateFormats = new ThreadLocal () { 196 protected Object initialValue() { 197 SimpleDateFormat [] f = new SimpleDateFormat [3]; 198 f[0] = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", 199 Locale.US); 200 f[1] = new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", 201 Locale.US); 202 f[2] = new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US); 203 return f; 204 } 205 }; 206 protected SimpleDateFormat formats[]; 207 209 212 protected static Locale defaultLocale = Locale.getDefault(); 213 214 215 218 protected HashMap attributes = new HashMap (); 219 220 221 224 private HashMap readOnlyAttributes = new HashMap (); 225 226 227 230 protected ArrayList locales = new ArrayList (); 231 232 233 237 private transient HashMap notes = new HashMap (); 238 239 240 243 protected String authType = null; 244 245 246 249 protected Object dispatcherType = null; 250 251 252 255 protected InputBuffer inputBuffer = new InputBuffer(); 256 257 258 261 protected CoyoteInputStream inputStream = 262 new CoyoteInputStream(inputBuffer); 263 264 265 268 protected CoyoteReader reader = new CoyoteReader(inputBuffer); 269 270 271 274 protected boolean usingInputStream = false; 275 276 277 280 protected boolean usingReader = false; 281 282 283 286 protected Principal userPrincipal = null; 287 288 289 292 protected boolean sessionParsed = false; 293 294 295 298 protected boolean requestParametersParsed = false; 299 300 301 304 protected boolean cookiesParsed = false; 305 306 307 310 protected boolean secure = false; 311 312 313 316 protected Subject subject = null; 317 318 319 322 protected static int CACHED_POST_LEN = 8192; 323 protected byte[] postData = null; 324 325 326 329 protected ParameterMap parameterMap = new ParameterMap(); 330 331 332 335 protected Session session = null; 336 337 338 341 protected Object requestDispatcherPath = null; 342 343 344 347 protected boolean requestedSessionCookie = false; 348 349 350 353 protected String requestedSessionId = null; 354 355 356 359 protected boolean requestedSessionURL = false; 360 361 362 365 protected Socket socket = null; 366 367 368 371 protected boolean localesParsed = false; 372 373 374 377 private StringParser parser = new StringParser(); 378 379 382 protected int localPort = -1; 383 384 387 protected String remoteAddr = null; 388 389 390 393 protected String remoteHost = null; 394 395 396 399 protected int remotePort = -1; 400 401 404 protected String localName = null; 405 406 407 410 protected String localAddr = null; 411 412 415 418 419 423 private int dispatchDepth = 0; 424 425 428 private static int maxDispatchDepth = Constants.DEFAULT_MAX_DISPATCH_DEPTH; 429 431 432 436 private static final String match = 437 ";" + Globals.SESSION_PARAMETER_NAME + "="; 438 439 442 private static final char[] SESSION_ID = match.toCharArray(); 443 445 446 private String jrouteId; 448 450 451 453 457 public void recycle() { 458 459 context = null; 460 wrapper = null; 461 462 dispatcherType = null; 463 requestDispatcherPath = null; 464 465 authType = null; 466 inputBuffer.recycle(); 467 usingInputStream = false; 468 usingReader = false; 469 userPrincipal = null; 470 subject = null; 471 sessionParsed = false; 472 requestParametersParsed = false; 473 cookiesParsed = false; 474 locales.clear(); 475 localesParsed = false; 476 secure = false; 477 remoteAddr = null; 478 remoteHost = null; 479 remotePort = -1; 480 localPort = -1; 481 localAddr = null; 482 localName = null; 483 484 attributes.clear(); 485 notes.clear(); 486 cookies = null; 487 488 if (session != null) { 489 session.endAccess(); 490 } 491 session = null; 492 requestedSessionCookie = false; 493 requestedSessionId = null; 494 requestedSessionURL = false; 495 498 dispatchDepth = 0; 500 parameterMap.setLocked(false); 501 parameterMap.clear(); 502 503 mappingData.recycle(); 504 505 if (Constants.SECURITY) { 506 if (facade != null) { 507 facade.clear(); 508 facade = null; 509 } 510 if (inputStream != null) { 511 inputStream.clear(); 512 inputStream = null; 513 } 514 if (reader != null) { 515 reader.clear(); 516 reader = null; 517 } 518 } 519 520 } 521 522 523 525 526 529 public String getAuthorization() { 530 return (coyoteRequest.getHeader(Constants.AUTHORIZATION_HEADER)); 531 } 532 533 538 public void setAuthorization(String authorization) { 539 } 541 542 543 546 protected Connector connector; 547 548 551 public Connector getConnector() { 552 return (this.connector); 553 } 554 555 560 public void setConnector(Connector connector) { 561 this.connector = connector; 562 } 563 564 565 568 protected Context context = null; 569 570 573 public Context getContext() { 574 return (this.context); 575 } 576 577 578 586 public void setContext(Context context) { 587 this.context = context; 588 } 589 590 591 594 protected FilterChain filterChain = null; 595 596 599 public FilterChain getFilterChain() { 600 return (this.filterChain); 601 } 602 603 608 public void setFilterChain(FilterChain filterChain) { 609 this.filterChain = filterChain; 610 } 611 612 613 616 public Host getHost() { 617 if (getContext() == null) 618 return null; 619 return (Host) getContext().getParent(); 620 } 622 623 624 631 public void setHost(Host host) { 632 mappingData.host = host; 633 } 634 635 636 639 protected static final String info = 640 "org.apache.coyote.catalina.CoyoteRequest/1.0"; 641 642 647 public String getInfo() { 648 return (info); 649 } 650 651 652 655 protected MappingData mappingData = new MappingData(); 656 657 660 public MappingData getMappingData() { 661 return (mappingData); 662 } 663 664 665 668 protected CoyoteRequestFacade facade = null; 669 670 674 public HttpServletRequest getRequest() { 675 if (facade == null) { 676 facade = new CoyoteRequestFacade(this); 677 } 678 return (facade); 679 } 680 681 682 685 protected org.apache.catalina.Response response = null; 686 687 690 public org.apache.catalina.Response getResponse() { 691 return (this.response); 692 } 693 694 699 public void setResponse(org.apache.catalina.Response response) { 700 this.response = response; 701 } 702 703 704 710 public Socket getSocket() { 711 return (socket); 712 } 713 714 719 public void setSocket(Socket socket) { 720 this.socket = socket; 721 remoteHost = null; 722 remoteAddr = null; 723 remotePort = -1; 724 localPort = -1; 725 localAddr = null; 726 localName = null; 727 } 728 729 730 733 public InputStream getStream() { 734 if (inputStream == null) { 735 inputStream = new CoyoteInputStream(inputBuffer); 736 } 737 return inputStream; 738 } 739 740 745 public void setStream(InputStream stream) { 746 } 748 749 750 753 protected B2CConverter URIConverter = null; 754 755 758 protected B2CConverter getURIConverter() { 759 return URIConverter; 760 } 761 762 767 protected void setURIConverter(B2CConverter URIConverter) { 768 this.URIConverter = URIConverter; 769 } 770 771 772 775 protected ValveContext valveContext = null; 776 777 780 public ValveContext getValveContext() { 781 return (this.valveContext); 782 } 783 784 789 public void setValveContext(ValveContext valveContext) { 790 this.valveContext = valveContext; 791 } 792 793 794 797 protected Wrapper wrapper = null; 798 799 802 public Wrapper getWrapper() { 803 return (this.wrapper); 804 } 805 806 807 813 public void setWrapper(Wrapper wrapper) { 814 this.wrapper = wrapper; 815 } 816 817 818 820 821 827 public ServletInputStream createInputStream() 828 throws IOException { 829 if (inputStream == null) { 830 inputStream = new CoyoteInputStream(inputBuffer); 831 } 832 return inputStream; 833 } 834 835 836 842 public void finishRequest() throws IOException { 843 } 845 846 847 853 public Object getNote(String name) { 854 return (notes.get(name)); 855 } 856 857 858 862 public Iterator getNoteNames() { 863 return (notes.keySet().iterator()); 864 } 865 866 867 873 public void removeNote(String name) { 874 notes.remove(name); 875 } 876 877 878 885 public void setNote(String name, Object value) { 886 notes.put(name, value); 887 } 888 889 890 895 public void setContentLength(int length) { 896 } 898 899 900 907 public void setContentType(String type) { 908 } 910 911 912 917 public void setProtocol(String protocol) { 918 } 920 921 922 927 public void setRemoteAddr(String remoteAddr) { 928 } 930 931 932 938 public void setRemoteHost(String remoteHost) { 939 } 941 942 943 949 public void setScheme(String scheme) { 950 } 952 953 954 960 public void setSecure(boolean secure) { 961 this.secure = secure; 962 } 963 964 965 970 public void setServerName(String name) { 971 coyoteRequest.serverName().setString(name); 972 } 973 974 975 980 public void setServerPort(int port) { 981 coyoteRequest.setServerPort(port); 982 } 983 984 985 987 988 994 public Object getAttribute(String name) { 995 996 if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) { 997 return (dispatcherType == null) 998 ? ApplicationFilterFactory.REQUEST_INTEGER 999 : dispatcherType; 1000 } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) { 1001 return (requestDispatcherPath == null) 1002 ? getRequestPathMB().toString() 1003 : requestDispatcherPath.toString(); 1004 } 1005 1006 Object attr=attributes.get(name); 1007 1008 if(attr!=null) 1009 return(attr); 1010 1011 attr = coyoteRequest.getAttribute(name); 1012 if(attr != null) 1013 return attr; 1014 if(Constants.SSL_CERTIFICATE_ATTR.equals(name)) { 1016 coyoteRequest.action(ActionCode.ACTION_REQ_SSL_CERTIFICATE, null); 1017 attr = getAttribute(Globals.CERTIFICATES_ATTR); 1018 if(attr != null) 1019 attributes.put(name, attr); 1020 } else if( isSSLAttribute(name) ) { 1021 1037 populateSSLAttributes(); 1039 attr = attributes.get(name); 1041 } 1042 return attr; 1043 } 1044 1045 1046 1049 static boolean isSSLAttribute(String name) { 1050 return Globals.CERTIFICATES_ATTR.equals(name) || 1051 Globals.CIPHER_SUITE_ATTR.equals(name) || 1052 Globals.KEY_SIZE_ATTR.equals(name); 1053 } 1054 1055 1059 public Enumeration getAttributeNames() { 1060 return new Enumerator(attributes.keySet(), true); 1061 } 1062 1063 1064 1067 public String getCharacterEncoding() { 1068 return (coyoteRequest.getCharacterEncoding()); 1069 } 1070 1071 1072 1075 public int getContentLength() { 1076 return (coyoteRequest.getContentLength()); 1077 } 1078 1079 1080 1083 public String getContentType() { 1084 return (coyoteRequest.getContentType()); 1085 } 1086 1087 1088 1097 public ServletInputStream getInputStream() throws IOException { 1098 1099 if (usingReader) 1100 throw new IllegalStateException 1101 (sm.getString("coyoteRequest.getInputStream.ise")); 1102 1103 usingInputStream = true; 1104 if (inputStream == null) { 1105 inputStream = new CoyoteInputStream(inputBuffer); 1106 } 1107 return inputStream; 1108 1109 } 1110 1111 1112 1118 public Locale getLocale() { 1119 1120 if (!localesParsed) 1121 parseLocales(); 1122 1123 if (locales.size() > 0) { 1124 return ((Locale ) locales.get(0)); 1125 } else { 1126 return (defaultLocale); 1127 } 1128 1129 } 1130 1131 1132 1138 public Enumeration getLocales() { 1139 1140 if (!localesParsed) 1141 parseLocales(); 1142 1143 if (locales.size() > 0) 1144 return (new Enumerator(locales)); 1145 ArrayList results = new ArrayList (); 1146 results.add(defaultLocale); 1147 return (new Enumerator(results)); 1148 1149 } 1150 1151 1152 1159 public String getParameter(String name) { 1160 1161 if (!requestParametersParsed) 1162 parseRequestParameters(); 1163 1164 return coyoteRequest.getParameters().getParameter(name); 1165 1166 } 1167 1168 1169 1170 1179 public Map getParameterMap() { 1180 1181 if (parameterMap.isLocked()) 1182 return parameterMap; 1183 1184 Enumeration e = getParameterNames(); 1185 while (e.hasMoreElements()) { 1186 String name = e.nextElement().toString(); 1187 String [] values = getParameterValues(name); 1188 parameterMap.put(name, values); 1189 } 1190 1191 parameterMap.setLocked(true); 1192 1193 return parameterMap; 1194 1195 } 1196 1197 1198 1201 public Enumeration getParameterNames() { 1202 1203 if (!requestParametersParsed) 1204 parseRequestParameters(); 1205 1206 return coyoteRequest.getParameters().getParameterNames(); 1207 1208 } 1209 1210 1211 1217 public String [] getParameterValues(String name) { 1218 1219 if (!requestParametersParsed) 1220 parseRequestParameters(); 1221 1222 return coyoteRequest.getParameters().getParameterValues(name); 1223 1224 } 1225 1226 1227 1230 public String getProtocol() { 1231 return coyoteRequest.protocol().toString(); 1232 } 1233 1234 1235 1244 public BufferedReader getReader() throws IOException { 1245 1246 if (usingInputStream) 1247 throw new IllegalStateException 1248 (sm.getString("coyoteRequest.getReader.ise")); 1249 1250 usingReader = true; 1251 inputBuffer.checkConverter(); 1252 if (reader == null) { 1253 reader = new CoyoteReader(inputBuffer); 1254 } 1255 return reader; 1256 1257 } 1258 1259 1260 1268 public String getRealPath(String path) { 1269 1270 if (context == null) 1271 return (null); 1272 ServletContext servletContext = context.getServletContext(); 1273 if (servletContext == null) 1274 return (null); 1275 else { 1276 try { 1277 return (servletContext.getRealPath(path)); 1278 } catch (IllegalArgumentException e) { 1279 return (null); 1280 } 1281 } 1282 1283 } 1284 1285 1286 1289 public String getRemoteAddr() { 1290 if (remoteAddr == null) { 1291 1292 if (connector.getAuthPassthroughEnabled() 1294 && connector.getProxyHandler() != null) { 1295 remoteAddr = connector.getProxyHandler().getRemoteAddress( 1296 getRequest()); 1297 if (remoteAddr == null) { 1298 log.warn(sm.getString( 1299 "coyoteRequest.nullRemoteAddressFromProxy")); 1300 } 1301 return remoteAddr; 1302 } 1303 1305 if (socket != null) { 1306 InetAddress inet = socket.getInetAddress(); 1307 remoteAddr = inet.getHostAddress(); 1308 } else { 1309 coyoteRequest.action 1310 (ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE, coyoteRequest); 1311 remoteAddr = coyoteRequest.remoteAddr().toString(); 1312 } 1313 } 1314 return remoteAddr; 1315 } 1316 1317 1318 1321 public String getRemoteHost() { 1322 if (remoteHost == null) { 1323 if (!connector.getEnableLookups()) { 1324 remoteHost = getRemoteAddr(); 1325 } else if (connector.getAuthPassthroughEnabled() 1327 && connector.getProxyHandler() != null) { 1328 String addr = 1329 connector.getProxyHandler().getRemoteAddress(getRequest()); 1330 if (addr != null) { 1331 try { 1332 remoteHost = InetAddress.getByName(addr).getHostName(); 1333 } catch (UnknownHostException e) { 1334 log.warn(sm.getString( 1335 "coyoteRequest.unknownHost", 1336 addr), 1337 e); 1338 } 1339 } else { 1340 log.warn(sm.getString( 1341 "coyoteRequest.nullRemoteAddressFromProxy")); 1342 } 1343 } else if (socket != null) { 1345 InetAddress inet = socket.getInetAddress(); 1346 remoteHost = inet.getHostName(); 1347 } else { 1348 coyoteRequest.action 1349 (ActionCode.ACTION_REQ_HOST_ATTRIBUTE, coyoteRequest); 1350 remoteHost = coyoteRequest.remoteHost().toString(); 1351 } 1352 } 1353 return remoteHost; 1354 } 1355 1356 1360 public int getRemotePort(){ 1361 if (remotePort == -1) { 1362 if (socket != null) { 1363 remotePort = socket.getPort(); 1364 } else { 1365 coyoteRequest.action 1366 (ActionCode.ACTION_REQ_REMOTEPORT_ATTRIBUTE, coyoteRequest); 1367 remotePort = coyoteRequest.getRemotePort(); 1368 } 1369 } 1370 return remotePort; 1371 } 1372 1373 1377 public String getLocalName(){ 1378 if (localName == null) { 1379 if (socket != null) { 1380 InetAddress inet = socket.getLocalAddress(); 1381 localName = inet.getHostName(); 1382 } else { 1383 coyoteRequest.action 1384 (ActionCode.ACTION_REQ_LOCAL_NAME_ATTRIBUTE, coyoteRequest); 1385 localName = coyoteRequest.localName().toString(); 1386 } 1387 } 1388 return localName; 1389 } 1390 1391 1395 public String getLocalAddr(){ 1396 if (localAddr == null) { 1397 if (socket != null) { 1398 InetAddress inet = socket.getLocalAddress(); 1399 localAddr = inet.getHostAddress(); 1400 } else { 1401 coyoteRequest.action 1402 (ActionCode.ACTION_REQ_LOCAL_ADDR_ATTRIBUTE, coyoteRequest); 1403 localAddr = coyoteRequest.localAddr().toString(); 1404 } 1405 } 1406 return localAddr; 1407 } 1408 1409 1410 1414 public int getLocalPort(){ 1415 if (localPort == -1){ 1416 if (socket != null) { 1417 localPort = socket.getLocalPort(); 1418 } else { 1419 coyoteRequest.action 1420 (ActionCode.ACTION_REQ_LOCALPORT_ATTRIBUTE, coyoteRequest); 1421 localPort = coyoteRequest.getLocalPort(); 1422 } 1423 } 1424 return localPort; 1425 } 1426 1427 1433 public RequestDispatcher getRequestDispatcher(String path) { 1434 1435 if (context == null) 1436 return (null); 1437 1438 if (path == null) 1440 return (null); 1441 else if (path.startsWith("/")) 1442 return (context.getServletContext().getRequestDispatcher(path)); 1443 1444 String servletPath = (String ) getAttribute(Globals.INCLUDE_SERVLET_PATH_ATTR); 1446 if (servletPath == null) 1447 servletPath = getServletPath(); 1448 1449 String pathInfo = getPathInfo(); 1451 String requestPath = null; 1452 1453 if (pathInfo == null) { 1454 requestPath = servletPath; 1455 } else { 1456 requestPath = servletPath + pathInfo; 1457 } 1458 1459 int pos = requestPath.lastIndexOf('/'); 1460 String relative = null; 1461 if (pos >= 0) { 1462 relative = RequestUtil.normalize 1463 (requestPath.substring(0, pos + 1) + path); 1464 } else { 1465 relative = RequestUtil.normalize(requestPath + path); 1466 } 1467 1468 return (context.getServletContext().getRequestDispatcher(relative)); 1469 1470 } 1471 1472 1473 1476 public String getScheme() { 1477 if (getConnector() != null 1479 && getConnector().getAuthPassthroughEnabled()) { 1480 ProxyHandler proxyHandler = getConnector().getProxyHandler(); 1481 if (proxyHandler != null 1482 && proxyHandler.getSSLKeysize(getRequest()) > 0) { 1483 return "https"; 1484 } 1485 } 1486 1488 return (coyoteRequest.scheme().toString()); 1489 } 1490 1491 1492 1495 public String getServerName() { 1496 return (coyoteRequest.serverName().toString()); 1497 } 1498 1499 1500 1503 public int getServerPort() { 1504 return (coyoteRequest.getServerPort()); 1505 } 1506 1507 1508 1511 public boolean isSecure() { 1512 return (secure); 1513 } 1514 1515 1516 1521 public void removeAttribute(String name) { 1522 Object value = null; 1523 boolean found = false; 1524 1525 if (readOnlyAttributes.containsKey(name)) { 1529 return; 1530 } 1531 found = attributes.containsKey(name); 1532 if (found) { 1533 value = attributes.get(name); 1534 attributes.remove(name); 1535 } else { 1536 return; 1537 } 1538 1539 Object listeners[] = context.getApplicationEventListeners(); 1541 if ((listeners == null) || (listeners.length == 0)) 1542 return; 1543 ServletRequestAttributeEvent event = 1544 new ServletRequestAttributeEvent (context.getServletContext(), 1545 getRequest(), name, value); 1546 for (int i = 0; i < listeners.length; i++) { 1547 if (!(listeners[i] instanceof ServletRequestAttributeListener )) 1548 continue; 1549 ServletRequestAttributeListener listener = 1550 (ServletRequestAttributeListener ) listeners[i]; 1551 try { 1552 listener.attributeRemoved(event); 1553 } catch (Throwable t) { 1554 log(sm.getString("coyoteRequest.attributeEvent"), t); 1555 attributes.put( Globals.EXCEPTION_ATTR, t ); 1557 } 1558 } 1559 } 1560 1561 1562 1568 public void setAttribute(String name, Object value) { 1569 1570 if (name == null) 1572 throw new IllegalArgumentException 1573 (sm.getString("coyoteRequest.setAttribute.namenull")); 1574 1575 if (value == null) { 1577 removeAttribute(name); 1578 return; 1579 } 1580 1581 if (name.equals(Globals.DISPATCHER_TYPE_ATTR)) { 1582 dispatcherType = value; 1583 return; 1584 } else if (name.equals(Globals.DISPATCHER_REQUEST_PATH_ATTR)) { 1585 requestDispatcherPath = value; 1586 return; 1587 } 1588 1589 Object oldValue = null; 1590 boolean replaced = false; 1591 1592 if (readOnlyAttributes.containsKey(name)) { 1596 return; 1597 } 1598 1599 oldValue = attributes.put(name, value); 1600 if (oldValue != null) { 1601 replaced = true; 1602 } 1603 1604 if (name.startsWith("grizzly.")) { 1607 coyoteRequest.setAttribute(name, value); 1608 } 1609 1611 Object listeners[] = context.getApplicationEventListeners(); 1613 if ((listeners == null) || (listeners.length == 0)) 1614 return; 1615 ServletRequestAttributeEvent event = null; 1616 if (replaced) 1617 event = 1618 new ServletRequestAttributeEvent (context.getServletContext(), 1619 getRequest(), name, oldValue); 1620 else 1621 event = 1622 new ServletRequestAttributeEvent (context.getServletContext(), 1623 getRequest(), name, value); 1624 1625 for (int i = 0; i < listeners.length; i++) { 1626 if (!(listeners[i] instanceof ServletRequestAttributeListener )) 1627 continue; 1628 ServletRequestAttributeListener listener = 1629 (ServletRequestAttributeListener ) listeners[i]; 1630 try { 1631 if (replaced) { 1632 listener.attributeReplaced(event); 1633 } else { 1634 listener.attributeAdded(event); 1635 } 1636 } catch (Throwable t) { 1637 log(sm.getString("coyoteRequest.attributeEvent"), t); 1638 attributes.put( Globals.EXCEPTION_ATTR, t ); 1640 } 1641 } 1642 } 1643 1644 1645 1662 public void setCharacterEncoding(String enc) 1663 throws UnsupportedEncodingException { 1664 1665 if (requestParametersParsed || usingReader) { 1667 log.warn(sm.getString("coyoteRequest.setCharacterEncoding.ise", 1668 enc)); 1669 return; 1670 } 1671 1673 byte buffer[] = new byte[1]; 1675 buffer[0] = (byte) 'a'; 1676 1677 1682 final byte[] finalBuffer = buffer; 1685 final String finalEnc = enc; 1686 String dummy = null; 1687 if (System.getSecurityManager() != null) { 1688 try { 1689 dummy = (String ) AccessController.doPrivileged( 1690 new PrivilegedExceptionAction () { 1691 public Object run() throws UnsupportedEncodingException { 1692 return new String (finalBuffer, finalEnc); 1693 } 1694 }); 1695 } catch (PrivilegedActionException pae) { 1696 throw (UnsupportedEncodingException ) pae.getCause(); 1697 } 1698 } else { 1699 dummy = new String (buffer, enc); 1700 } 1701 1703 coyoteRequest.setCharacterEncoding(enc); 1705 1706 } 1707 1708 1709 1713 public static void setMaxDispatchDepth(int depth) { 1714 maxDispatchDepth = depth; 1715 } 1716 1717 1718 public static int getMaxDispatchDepth(){ 1719 return maxDispatchDepth; 1720 } 1721 1722 1725 public int incrementDispatchDepth() { 1726 return ++dispatchDepth; 1727 } 1728 1729 1730 1733 public int decrementDispatchDepth() { 1734 return --dispatchDepth; 1735 } 1736 1737 1738 1741 public boolean isMaxDispatchDepthReached() { 1742 return dispatchDepth > maxDispatchDepth; 1743 } 1744 1746 1747 1749 1750 1755 public void addCookie(Cookie cookie) { 1756 1757 if (!cookiesParsed) 1759 parseCookies(); 1760 1761 int size = 0; 1762 if (cookies != null) { 1763 size = cookies.length; 1764 } 1765 1766 Cookie [] newCookies = new Cookie [size + 1]; 1767 for (int i = 0; i < size; i++) { 1768 newCookies[i] = cookies[i]; 1769 } 1770 newCookies[size] = cookie; 1771 1772 cookies = newCookies; 1773 1774 } 1775 1776 1777 1783 public void addHeader(String name, String value) { 1784 } 1786 1787 1788 1794 public void addLocale(Locale locale) { 1795 locales.add(locale); 1796 } 1797 1798 1799 1807 public void addParameter(String name, String values[]) { 1808 coyoteRequest.getParameters().addParameterValues(name, values); 1809 } 1810 1811 1812 1815 public void clearCookies() { 1816 cookiesParsed = true; 1817 cookies = null; 1818 } 1819 1820 1821 1824 public void clearHeaders() { 1825 } 1827 1828 1829 1832 public void clearLocales() { 1833 locales.clear(); 1834 } 1835 1836 1837 1840 public void clearParameters() { 1841 } 1843 1844 1845 1852 public void setAuthType(String type) { 1853 this.authType = type; 1854 } 1855 1856 1857 1864 public void setContextPath(String path) { 1865 1866 if (path == null) { 1867 mappingData.contextPath.setString(""); 1868 } else { 1869 mappingData.contextPath.setString(path); 1870 } 1871 1872 } 1873 1874 1875 1880 public void setMethod(String method) { 1881 } 1883 1884 1885 1891 public void setQueryString(String query) { 1892 } 1894 1895 1896 1903 public void setPathInfo(String path) { 1904 mappingData.pathInfo.setString(path); 1905 } 1906 1907 1908 1915 public void setRequestedSessionCookie(boolean flag) { 1916 1917 this.requestedSessionCookie = flag; 1918 1919 } 1920 1921 1922 1928 public void setRequestedSessionId(String id) { 1929 1930 this.requestedSessionId = id; 1931 1932 } 1933 1934 1935 1942 public void setRequestedSessionURL(boolean flag) { 1943 1944 this.requestedSessionURL = flag; 1945 1946 } 1947 1948 1949 1955 public void setRequestURI(String uri) { 1956 } 1958 1959 1960 1965 public void setDecodedRequestURI(String uri) { 1966 } 1968 1969 1970 1975 public String getDecodedRequestURI() { 1976 return (coyoteRequest.decodedURI().toString()); 1977 } 1978 1979 1980 1985 public MessageBytes getDecodedRequestURIMB() { 1986 return (coyoteRequest.decodedURI()); 1987 } 1988 1989 1990 1997 public void setServletPath(String path) { 1998 if (path != null) 1999 mappingData.wrapperPath.setString(path); 2000 } 2001 2002 2003 2010 public void setUserPrincipal(Principal principal) { 2011 2012 if (SecurityUtil.isPackageProtectionEnabled()){ 2013 HttpSession session = getSession(false); 2014 if ( (subject != null) && 2015 (!subject.getPrincipals().contains(principal)) ){ 2016 subject.getPrincipals().add(principal); 2017 } else if (session != null && 2018 session.getAttribute(Globals.SUBJECT_ATTR) == null) { 2019 subject = new Subject (); 2020 subject.getPrincipals().add(principal); 2021 } 2022 if (session != null){ 2023 session.setAttribute(Globals.SUBJECT_ATTR, subject); 2024 } 2025 } 2026 2027 this.userPrincipal = principal; 2028 } 2029 2030 2031 2033 2034 2037 public String getAuthType() { 2038 return (authType); 2039 } 2040 2041 2042 2046 public String getContextPath() { 2047 return (mappingData.contextPath.toString()); 2048 } 2049 2050 2051 2056 public MessageBytes getContextPathMB() { 2057 return (mappingData.contextPath); 2058 } 2059 2060 2061 2064 public Cookie [] getCookies() { 2065 2066 if (!cookiesParsed) 2067 parseCookies(); 2068 2069 return cookies; 2070 2071 } 2072 2073 2074 2077 public void setCookies(Cookie [] cookies) { 2078 2079 this.cookies = cookies; 2080 2081 } 2082 2083 2084 2093 public long getDateHeader(String name) { 2094 2095 String value = getHeader(name); 2096 if (value == null) 2097 return (-1L); 2098 2099 long result = FastHttpDateFormat.parseDate(value, formats); 2101 if (result != (-1L)) { 2102 return result; 2103 } 2104 throw new IllegalArgumentException (value); 2105 2106 } 2107 2108 2109 2115 public String getHeader(String name) { 2116 return coyoteRequest.getHeader(name); 2117 } 2118 2119 2120 2126 public Enumeration getHeaders(String name) { 2127 return coyoteRequest.getMimeHeaders().values(name); 2128 } 2129 2130 2131 2134 public Enumeration getHeaderNames() { 2135 return coyoteRequest.getMimeHeaders().names(); 2136 } 2137 2138 2139 2148 public int getIntHeader(String name) { 2149 2150 String value = getHeader(name); 2151 if (value == null) { 2152 return (-1); 2153 } else { 2154 return (Integer.parseInt(value)); 2155 } 2156 2157 } 2158 2159 2160 2163 public String getMethod() { 2164 return coyoteRequest.method().toString(); 2165 } 2166 2167 2168 2171 public String getPathInfo() { 2172 return (mappingData.pathInfo.toString()); 2173 } 2174 2175 2176 2181 public MessageBytes getPathInfoMB() { 2182 return (mappingData.pathInfo); 2183 } 2184 2185 2186 2190 public String getPathTranslated() { 2191 2192 if (context == null) 2193 return (null); 2194 2195 if (getPathInfo() == null) { 2196 return (null); 2197 } else { 2198 return (context.getServletContext().getRealPath(getPathInfo())); 2199 } 2200 2201 } 2202 2203 2204 2207 public String getQueryString() { 2208 String queryString = coyoteRequest.queryString().toString(); 2209 2210 if (queryString == null || queryString.equals("")) { 2211 return (null); 2212 } else { 2213 return queryString; 2214 } 2215 } 2216 2217 2218 2222 public String getRemoteUser() { 2223 2224 if (userPrincipal != null) { 2225 return (userPrincipal.getName()); 2226 } else { 2227 return (null); 2228 } 2229 2230 } 2231 2232 2233 2238 public MessageBytes getRequestPathMB() { 2239 return (mappingData.requestPath); 2240 } 2241 2242 2243 2246 public String getRequestedSessionId() { 2247 return (requestedSessionId); 2248 } 2249 2250 2251 2254 public String getRequestURI() { 2255 return coyoteRequest.requestURI().toString(); 2256 } 2257 2258 2259 2275 public StringBuffer getRequestURL() { 2276 2277 StringBuffer url = new StringBuffer (); 2278 String scheme = getScheme(); 2279 int port = getServerPort(); 2280 if (port < 0) 2281 port = 80; 2283 url.append(scheme); 2284 url.append("://"); 2285 url.append(getServerName()); 2286 if ((scheme.equals("http") && (port != 80)) 2287 || (scheme.equals("https") && (port != 443))) { 2288 url.append(':'); 2289 url.append(port); 2290 } 2291 url.append(getRequestURI()); 2292 2293 return (url); 2294 2295 } 2296 2297 2298 2302 public String getServletPath() { 2303 return (mappingData.wrapperPath.toString()); 2304 } 2305 2306 2307 2312 public MessageBytes getServletPathMB() { 2313 return (mappingData.wrapperPath); 2314 } 2315 2316 2317 2321 public HttpSession getSession() { 2322 Session session = doGetSession(true); 2323 if (session != null) { 2324 return session.getSession(); 2325 } else { 2326 return null; 2327 } 2328 } 2329 2330 2331 2337 public HttpSession getSession(boolean create) { 2338 Session session = doGetSession(create); 2339 if (session != null) { 2340 return session.getSession(); 2341 } else { 2342 return null; 2343 } 2344 } 2345 2346 2351 public void setSession(Session newSess) { 2352 session = newSess; 2353 } 2354 2355 2356 2360 public boolean isRequestedSessionIdFromCookie() { 2361 2362 if (requestedSessionId != null) 2363 return (requestedSessionCookie); 2364 else 2365 return (false); 2366 2367 } 2368 2369 2370 2374 public boolean isRequestedSessionIdFromURL() { 2375 2376 if (requestedSessionId != null) 2377 return (requestedSessionURL); 2378 else 2379 return (false); 2380 2381 } 2382 2383 2384 2391 public boolean isRequestedSessionIdFromUrl() { 2392 return (isRequestedSessionIdFromURL()); 2393 } 2394 2395 2396 2400 public boolean isRequestedSessionIdValid() { 2401 2402 if (requestedSessionId == null) 2403 return (false); 2404 if (context == null) 2405 return (false); 2406 Manager manager = context.getManager(); 2407 if (manager == null) 2408 return (false); 2409 Session session = null; 2410 try { 2411 session = manager.findSession(requestedSessionId); 2412 } catch (IOException e) { 2413 session = null; 2414 } 2415 if ((session != null) && session.isValid()) 2416 return (true); 2417 else 2418 return (false); 2419 2420 } 2421 2422 2423 2429 public boolean isUserInRole(String role) { 2430 2431 2436 Principal userPrincipal = this.getUserPrincipal(); 2437 2439 if (userPrincipal == null) 2441 return (false); 2442 2443 if (context == null) 2445 return (false); 2446 Realm realm = context.getRealm(); 2447 if (realm == null) 2448 return (false); 2449 2450 if (wrapper != null) { 2452 String realRole = wrapper.findSecurityReference(role); 2453 2454 if ((realRole != null) && 2456 realm.hasRole(this, (HttpResponse) response, 2458 userPrincipal, realRole)) 2459 return (true); 2460 } 2461 2462 2464 return (realm.hasRole(this, (HttpResponse) response, 2466 userPrincipal, role)); 2467 } 2469 2470 2471 2474 public Principal getUserPrincipal() { 2475 return (userPrincipal); 2476 } 2477 2478 2479 2483 public Session getSessionInternal() { 2484 return doGetSession(true); 2485 } 2486 2487 2488 2494 public Session getSessionInternal(boolean create) { 2495 return doGetSession(create); 2496 } 2497 2498 2499 2501 2502 protected Session doGetSession(boolean create) { 2503 2504 if (context == null) 2506 return (null); 2507 2508 if ((session != null) && !session.isValid()) 2510 session = null; 2511 if (session != null) 2512 return (session); 2513 2514 Manager manager = null; 2516 if (context != null) 2517 manager = context.getManager(); 2518 if (manager == null) 2519 return (null); if (requestedSessionId != null) { 2521 try { 2522 session = manager.findSession(requestedSessionId); 2523 } catch (IOException e) { 2524 session = null; 2525 } 2526 if ((session != null) && !session.isValid()) 2527 session = null; 2528 if (session != null) { 2529 session.access(); 2530 return (session); 2531 } 2532 } 2533 2534 if (!create) 2536 return (null); 2537 if ((context != null) && (response != null) && 2538 context.getCookies() && 2539 response.getResponse().isCommitted()) { 2540 throw new IllegalStateException 2541 (sm.getString("coyoteRequest.sessionCreateCommitted")); 2542 } 2543 2544 if (requestedSessionId != null && context.getReuseSessionID()) { 2546 session = manager.createSession(requestedSessionId); 2547 } else { 2548 session = manager.createSession(); 2550 } 2552 2554 if ((session != null) && (getContext() != null) 2556 && getContext().getCookies()) { 2557 Cookie cookie = new Cookie (Globals.SESSION_COOKIE_NAME, 2558 session.getIdInternal()); 2559 configureSessionCookie(cookie); 2560 ((HttpServletResponse ) response).addCookie(cookie); 2561 } 2562 2563 if (session != null) { 2564 session.access(); 2565 return (session); 2566 } else { 2567 return (null); 2568 } 2569 2570 } 2571 2572 2577 protected void configureSessionCookie(Cookie cookie) { 2578 cookie.setMaxAge(-1); 2579 String contextPath = null; 2580 if (getContext() != null) { 2581 contextPath = getContext().getPath(); 2584 } 2586 if ((contextPath != null) && (contextPath.length() > 0)) { 2587 cookie.setPath(contextPath); 2588 } else { 2589 cookie.setPath("/"); 2590 } 2591 if (isSecure()) { 2592 cookie.setSecure(true); 2593 } 2594 } 2595 2596 2597 2600 protected void parseCookies() { 2601 2602 cookiesParsed = true; 2603 2604 Cookies serverCookies = coyoteRequest.getCookies(); 2605 int count = serverCookies.getCookieCount(); 2606 if (count <= 0) 2607 return; 2608 2609 cookies = new Cookie [count]; 2610 2611 int idx=0; 2612 for (int i = 0; i < count; i++) { 2613 ServerCookie scookie = serverCookies.getCookie(i); 2614 try { 2615 Cookie cookie = new Cookie (scookie.getName().toString(), 2616 scookie.getValue().toString()); 2617 cookie.setPath(scookie.getPath().toString()); 2618 cookie.setVersion(scookie.getVersion()); 2619 String domain = scookie.getDomain().toString(); 2620 if (domain != null) { 2621 cookie.setDomain(scookie.getDomain().toString()); 2622 } 2623 cookies[idx++] = cookie; 2624 } catch(IllegalArgumentException e) { 2625 ; } 2627 } 2628 if( idx < count ) { 2629 Cookie [] ncookies = new Cookie [idx]; 2630 System.arraycopy(cookies, 0, ncookies, 0, idx); 2631 cookies = ncookies; 2632 } 2633 2634 } 2635 2636 2637 2640 protected void parseRequestParameters() { 2641 2642 2645 2646 Parameters parameters = coyoteRequest.getParameters(); 2647 2648 String enc = getCharacterEncoding(); 2651 requestParametersParsed = true; 2658 if (enc != null) { 2660 parameters.setEncoding(enc); 2661 parameters.setQueryStringEncoding(enc); 2662 } else { 2663 parameters.setEncoding 2664 (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); 2665 parameters.setQueryStringEncoding 2666 (org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); 2667 } 2668 2669 parameters.handleQueryParameters(); 2670 2671 if (usingInputStream || usingReader) 2672 return; 2673 2674 if (!getMethod().equalsIgnoreCase("POST")) 2675 return; 2676 2677 String contentType = getContentType(); 2678 if (contentType == null) 2679 contentType = ""; 2680 int semicolon = contentType.indexOf(';'); 2681 if (semicolon >= 0) { 2682 contentType = contentType.substring(0, semicolon).trim(); 2683 } else { 2684 contentType = contentType.trim(); 2685 } 2686 if (!("application/x-www-form-urlencoded".equals(contentType))) 2687 return; 2688 2689 int len = getContentLength(); 2690 2691 if (len > 0) { 2692 int maxPostSize = ((CoyoteConnector) connector).getMaxPostSize(); 2693 if ((maxPostSize > 0) && (len > maxPostSize)) { 2694 log(sm.getString("coyoteRequest.postTooLarge")); 2695 throw new IllegalStateException ("Post too large"); 2696 } 2697 try { 2698 2712 byte[] formData = getPostBody(); 2714 if (formData != null) { 2715 parameters.processParameters(formData, 0, len); 2716 } 2717 } catch (Throwable t) { 2719 ; } 2721 } 2722 2723 } 2724 2725 2726 2732 protected byte[] getPostBody() throws IOException { 2733 2734 int len = getContentLength(); 2735 byte[] formData = null; 2736 2737 if (len < CACHED_POST_LEN) { 2738 if (postData == null) 2739 postData = new byte[CACHED_POST_LEN]; 2740 formData = postData; 2741 } else { 2742 formData = new byte[len]; 2743 } 2744 int actualLen = readPostBody(formData, len); 2745 if (actualLen == len) { 2746 return formData; 2747 } 2748 2749 return null; 2750 } 2751 2753 2754 2757 protected int readPostBody(byte body[], int len) 2758 throws IOException { 2759 2760 int offset = 0; 2761 do { 2762 int inputLen = getStream().read(body, offset, len - offset); 2763 if (inputLen <= 0) { 2764 return offset; 2765 } 2766 offset += inputLen; 2767 } while ((len - offset) > 0); 2768 return len; 2769 2770 } 2771 2772 2773 2776 protected void parseLocales() { 2777 2778 localesParsed = true; 2779 2780 Enumeration values = getHeaders("accept-language"); 2781 2782 while (values.hasMoreElements()) { 2783 String value = values.nextElement().toString(); 2784 parseLocalesHeader(value); 2785 } 2786 2787 } 2788 2789 2790 2793 protected void parseLocalesHeader(String value) { 2794 2795 TreeMap locales = new TreeMap (); 2800 2801 int white = value.indexOf(' '); 2803 if (white < 0) 2804 white = value.indexOf('\t'); 2805 if (white >= 0) { 2806 StringBuffer sb = new StringBuffer (); 2807 int len = value.length(); 2808 for (int i = 0; i < len; i++) { 2809 char ch = value.charAt(i); 2810 if ((ch != ' ') && (ch != '\t')) 2811 sb.append(ch); 2812 } 2813 value = sb.toString(); 2814 } 2815 2816 parser.setString(value); int length = parser.getLength(); 2819 while (true) { 2820 2821 int start = parser.getIndex(); 2823 if (start >= length) 2824 break; 2825 int end = parser.findChar(','); 2826 String entry = parser.extract(start, end).trim(); 2827 parser.advance(); 2829 double quality = 1.0; 2831 int semi = entry.indexOf(";q="); 2832 if (semi >= 0) { 2833 try { 2834 quality = Double.parseDouble(entry.substring(semi + 3)); 2835 } catch (NumberFormatException e) { 2836 quality = 0.0; 2837 } 2838 entry = entry.substring(0, semi); 2839 } 2840 2841 if (quality < 0.00005) 2843 continue; if ("*".equals(entry)) 2845 continue; 2847 String language = null; 2849 String country = null; 2850 String variant = null; 2851 int dash = entry.indexOf('-'); 2852 if (dash < 0) { 2853 language = entry; 2854 country = ""; 2855 variant = ""; 2856 } else { 2857 language = entry.substring(0, dash); 2858 country = entry.substring(dash + 1); 2859 int vDash = country.indexOf('-'); 2860 if (vDash > 0) { 2861 String cTemp = country.substring(0, vDash); 2862 variant = country.substring(vDash + 1); 2863 country = cTemp; 2864 } else { 2865 variant = ""; 2866 } 2867 } 2868 2869 Locale locale = new Locale (language, country, variant); 2871 Double key = new Double (-quality); ArrayList values = (ArrayList ) locales.get(key); 2873 if (values == null) { 2874 values = new ArrayList (); 2875 locales.put(key, values); 2876 } 2877 values.add(locale); 2878 2879 } 2880 2881 Iterator keys = locales.keySet().iterator(); 2884 while (keys.hasNext()) { 2885 Double key = (Double ) keys.next(); 2886 ArrayList list = (ArrayList ) locales.get(key); 2887 Iterator values = list.iterator(); 2888 while (values.hasNext()) { 2889 Locale locale = (Locale ) values.next(); 2890 addLocale(locale); 2891 } 2892 } 2893 2894 } 2895 2896 2897 2901 protected void parseSessionId() { 2902 2903 CharChunk uriCC = coyoteRequest.decodedURI().getCharChunk(); 2904 int semicolon = uriCC.indexOf(match, 0, match.length(), 0); 2905 2906 if (semicolon > 0) { 2907 2908 int start = uriCC.getStart(); 2910 int end = uriCC.getEnd(); 2911 2912 int sessionIdStart = start + semicolon + match.length(); 2913 int semicolon2 = uriCC.indexOf(';', sessionIdStart); 2914 2925 String sessionId = null; 2927 if (semicolon2 >= 0) { 2928 sessionId = new String (uriCC.getBuffer(), sessionIdStart, 2929 semicolon2 - semicolon - match.length()); 2930 } else { 2931 sessionId = new String (uriCC.getBuffer(), sessionIdStart, 2932 end - sessionIdStart); 2933 } 2934 int jrouteIndex = sessionId.lastIndexOf(':'); 2935 if (jrouteIndex > 0) { 2936 setRequestedSessionId(sessionId.substring(0, jrouteIndex)); 2937 if (jrouteIndex < (sessionId.length()-1)) { 2938 setJrouteId(sessionId.substring(jrouteIndex+1)); 2939 } 2940 } else { 2941 setRequestedSessionId(sessionId); 2942 } 2943 2945 setRequestedSessionURL(true); 2946 2947 ByteChunk uriBC = coyoteRequest.requestURI().getByteChunk(); 2949 start = uriBC.getStart(); 2950 end = uriBC.getEnd(); 2951 semicolon = uriBC.indexOf(match, 0, match.length(), 0); 2952 2953 if (semicolon > 0) { 2954 sessionIdStart = start + semicolon; 2955 semicolon2 = uriCC.indexOf 2956 (';', start + semicolon + match.length()); 2957 uriBC.setEnd(start + semicolon); 2958 byte[] buf = uriBC.getBuffer(); 2959 if (semicolon2 >= 0) { 2960 for (int i = 0; i < end - start - semicolon2; i++) { 2961 buf[start + semicolon + i] 2962 = buf[start + i + semicolon2]; 2963 } 2964 uriBC.setBytes(buf, start, semicolon 2965 + (end - start - semicolon2)); 2966 } 2967 } 2968 2969 } else { 2970 setRequestedSessionId(null); 2971 setRequestedSessionURL(false); 2972 } 2973 2974 } 2975 2977 2978 2982 void parseJrouteCookie() { 2983 2984 Cookies serverCookies = coyoteRequest.getCookies(); 2985 int count = serverCookies.getCookieCount(); 2986 if (count <= 0) { 2987 return; 2988 } 2989 2990 for (int i=0; i<count; i++) { 2991 ServerCookie scookie = serverCookies.getCookie(i); 2992 if (scookie.getName().equals(Constants.JROUTE_COOKIE)) { 2993 setJrouteId(scookie.getValue().toString()); 2994 break; 2995 } 2996 } 2997 } 2998 2999 3004 void setJrouteId(String jrouteId) { 3005 this.jrouteId = jrouteId; 3006 } 3007 3008 3016 public String getJrouteId() { 3017 return jrouteId; 3018 } 3019 3021 3022 3026 protected void parseSessionCookiesId() { 3027 3028 Cookies serverCookies = coyoteRequest.getCookies(); 3030 int count = serverCookies.getCookieCount(); 3031 if (count <= 0) 3032 return; 3033 3034 for (int i = 0; i < count; i++) { 3035 ServerCookie scookie = serverCookies.getCookie(i); 3036 if (scookie.getName().equals(Globals.SESSION_COOKIE_NAME)) { 3037 if (!isRequestedSessionIdFromCookie()) { 3039 B2CConverter.convertASCII(scookie.getValue()); 3041 setRequestedSessionId 3042 (scookie.getValue().toString()); 3043 setRequestedSessionCookie(true); 3044 setRequestedSessionURL(false); 3045 if (log.isDebugEnabled()) 3046 log.debug(" Requested cookie session id is " + 3047 ((HttpServletRequest ) getRequest()) 3048 .getRequestedSessionId()); 3049 } else { 3050 if (!isRequestedSessionIdValid()) { 3051 B2CConverter.convertASCII(scookie.getValue()); 3053 setRequestedSessionId 3054 (scookie.getValue().toString()); 3055 } 3056 } 3057 } 3058 } 3059 3060 } 3061 3063 3067 protected void convertURI(MessageBytes uri) 3068 throws Exception { 3069 3070 ByteChunk bc = uri.getByteChunk(); 3071 CharChunk cc = uri.getCharChunk(); 3072 cc.allocate(bc.getLength(), -1); 3073 3074 String enc = connector.getURIEncoding(); 3075 if (enc != null) { 3076 B2CConverter conv = getURIConverter(); 3077 try { 3078 if (conv == null) { 3079 conv = new B2CConverter(enc); 3080 setURIConverter(conv); 3081 } else { 3082 conv.recycle(); 3083 } 3084 } catch (IOException e) { 3085 log.error("Invalid URI encoding; using HTTP default"); 3087 connector.setURIEncoding(null); 3088 } 3089 if (conv != null) { 3090 try { 3091 conv.convert(bc, cc); 3092 uri.setChars(cc.getBuffer(), cc.getStart(), 3093 cc.getLength()); 3094 return; 3095 } catch (IOException e) { 3096 log.error("Invalid URI character encoding; trying ascii"); 3097 cc.recycle(); 3098 } 3099 } 3100 } 3101 3102 byte[] bbuf = bc.getBuffer(); 3104 char[] cbuf = cc.getBuffer(); 3105 int start = bc.getStart(); 3106 for (int i = 0; i < bc.getLength(); i++) { 3107 cbuf[i] = (char) (bbuf[i + start] & 0xff); 3108 } 3109 uri.setChars(cbuf, 0, bc.getLength()); 3110 3111 } 3112 3114 3115 3120 private void log(String message) { 3121 3122 Logger logger = connector.getContainer().getLogger(); 3123 String localName = "CoyoteRequest"; 3124 if (logger != null) 3125 logger.log(localName + " " + message); 3126 else 3127 System.out.println(localName + " " + message); 3128 3129 } 3130 3131 3132 3138 private void log(String message, Throwable throwable) { 3139 3140 Logger logger = connector.getContainer().getLogger(); 3141 String localName = "CoyoteRequest"; 3142 if (logger != null) 3143 logger.log(localName + " " + message, throwable); 3144 else { 3145 System.out.println(localName + " " + message); 3146 throwable.printStackTrace(System.out); 3147 } 3148 3149 } 3150 3151 3152 private void populateSSLAttributes() { 3154 coyoteRequest.action(ActionCode.ACTION_REQ_SSL_ATTRIBUTE, 3155 coyoteRequest); 3156 Object attr = coyoteRequest.getAttribute(Globals.CERTIFICATES_ATTR); 3157 if( attr != null) { 3158 attributes.put(Globals.CERTIFICATES_ATTR, attr); 3159 } 3160 attr = coyoteRequest.getAttribute(Globals.CIPHER_SUITE_ATTR); 3161 if(attr != null) { 3162 attributes.put(Globals.CIPHER_SUITE_ATTR, attr); 3163 } 3164 attr = coyoteRequest.getAttribute(Globals.KEY_SIZE_ATTR); 3165 if(attr != null) { 3166 attributes.put(Globals.KEY_SIZE_ATTR, attr); 3167 } 3168 } 3169 } 3171 | Popular Tags |