Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *   http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    * 
   17    */
   18   
   19   package org.apache.jmeter.protocol.http.util;
   20   
   21   import java.net.MalformedURLException;
   22   import java.net.URL;
   23   import java.nio.charset.Charset;
   24   import java.util.regex.Matcher;
   25   import java.util.regex.Pattern;
   26   
   27   import org.apache.jorphan.util.JOrphanUtils;
   28   
   29   // @see TestHTTPUtils for unit tests
   30   
   31   /**
   32    * General purpose conversion utilities related to HTTP/HTML
   33    */
   34   public class ConversionUtils {
   35   
   36       private static final String CHARSET_EQ = "charset="; // $NON-NLS-1$
   37       private static final int CHARSET_EQ_LEN = CHARSET_EQ.length();
   38   
   39       /**
   40        * Extract the encoding (charset) from the Content-Type,
   41        * e.g. "text/html; charset=utf-8".
   42        * 
   43        * @param contentType
   44        * @return the charset encoding - or null, if none was found or the charset is not supported
   45        */
   46       public static String getEncodingFromContentType(String contentType){
   47           String charSet = null;
   48           if (contentType != null) {
   49               int charSetStartPos = contentType.toLowerCase(java.util.Locale.ENGLISH).indexOf(CHARSET_EQ);
   50               if (charSetStartPos >= 0) {
   51                   charSet = contentType.substring(charSetStartPos + CHARSET_EQ_LEN);
   52                   if (charSet != null) {
   53                       // Remove quotes from charset name
   54                       charSet = JOrphanUtils.replaceAllChars(charSet, '"', "");
   55                       charSet = charSet.trim();
   56                       if (charSet.length() > 0) {
   57                           // See Bug 44784
   58                           int semi = charSet.indexOf(";");
   59                           if (semi == 0){
   60                               return null;
   61                           }
   62                           if (semi != -1) {
   63                               charSet = charSet.substring(0,semi);
   64                           }
   65                           if (!Charset.isSupported(charSet)){
   66                               return null;
   67                           }
   68                           return charSet;
   69                       }
   70                       return null;
   71                   }
   72               }
   73           }
   74           return charSet;
   75       }
   76       
   77       /**
   78        * Generate a relative URL, allowing for extraneous leading "../" segments.
   79        * The Java {@link URL#URL(URL, String)} constructor does not remove these.
   80        * 
   81        * @param baseURL
   82        * @param location relative location, possibly with extraneous leading "../"
   83        * @return URL with extraneous ../ removed
   84        * @throws MalformedURLException
   85        */
   86       public static URL makeRelativeURL(URL baseURL, String location) throws MalformedURLException{
   87           URL initial = new URL(baseURL,location);
   88           // skip expensive processing if it cannot apply
   89           if (!location.startsWith("../")){// $NON-NLS-1$
   90               return initial;
   91           }
   92           String path = initial.getPath();
   93           // Match /../[../] etc.
   94           Pattern p = Pattern.compile("^/((?:\\.\\./)+)"); // $NON-NLS-1$
   95           Matcher m = p.matcher(path);
   96           if (m.lookingAt()){
   97               String prefix = m.group(1); // get ../ or ../../ etc.
   98               if (location.startsWith(prefix)){
   99                   return new URL(baseURL, location.substring(prefix.length()));
  100               }
  101           }
  102           return initial;
  103       }
  104   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.http.util » [javadoc | source]