View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.rewrite.handler;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.eclipse.jetty.http.HttpHeader;
27  import org.eclipse.jetty.http.HttpHeaderValue;
28  import org.eclipse.jetty.util.ArrayTernaryTrie;
29  import org.eclipse.jetty.util.Trie;
30  
31  /**
32   * MSIE (Microsoft Internet Explorer) SSL Rule.
33   * Disable keep alive for SSL from IE5 or IE6 on Windows 2000.
34   *  
35   * 
36   *
37   */
38  public class MsieSslRule extends Rule
39  {
40      private static final int IEv5 = '5';
41      private static final int IEv6 = '6';
42      private static Trie<Boolean> __IE6_BadOS = new ArrayTernaryTrie<>();
43      {
44          __IE6_BadOS.put("NT 5.01", Boolean.TRUE);
45          __IE6_BadOS.put("NT 5.0",Boolean.TRUE);
46          __IE6_BadOS.put("NT 4.0",Boolean.TRUE);
47          __IE6_BadOS.put("98",Boolean.TRUE);
48          __IE6_BadOS.put("98; Win 9x 4.90",Boolean.TRUE);
49          __IE6_BadOS.put("95",Boolean.TRUE);
50          __IE6_BadOS.put("CE",Boolean.TRUE);
51      }
52      
53      public MsieSslRule()
54      {
55          _handling = false;
56          _terminating = false;
57      }
58      
59      @Override
60      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
61      {
62          if (request.isSecure())
63          {
64              String user_agent = request.getHeader(HttpHeader.USER_AGENT.asString());
65              
66              if (user_agent!=null)
67              {
68                  int msie=user_agent.indexOf("MSIE");
69                  if (msie>0 && user_agent.length()-msie>5)
70                  {
71                      // Get Internet Explorer Version
72                      int ieVersion = user_agent.charAt(msie+5);
73                      
74                      if ( ieVersion<=IEv5)
75                      {
76                          response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
77                          return target;
78                      }
79  
80                      if (ieVersion==IEv6)
81                      {
82                          int windows = user_agent.indexOf("Windows",msie+5);
83                          if (windows>0)
84                          {
85                              int end=user_agent.indexOf(')',windows+8);
86                              if(end<0 || __IE6_BadOS.get(user_agent,windows+8,end-windows-8)!=null)
87                              {
88                                  response.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString());
89                                  return target;
90                              }
91                          }
92                      }
93                  }
94              }
95          }
96          return null;
97      }
98  }