You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by tu...@discoverfinancial.com on 2001/06/07 23:31:05 UTC

Data problems in URL arguments

Hi,
If the URL Config arguments (POST or GET parameters) contain characters
that are not accepted by xml (such as <, >, &, quote and apostrophe), then
saving/ reading these arguments leads to exceptions.

The following changes were required (code is included ahead) to take care
of this situation.

1) A class XMLDataConverter was created that converted all strings to a
format acceptable by xml. Similar classes can be found in xerces and other
xml parsers/engines but I wasn't sure if we should create dependencies on
these.
2) ArgumentHandler.writeArgument is modified appropriately such that the
above class is invoked when the arguments are being saved.
3) ArgumentHandler.argument(String) is modified to manage the handling of
xml tags. Please refer to the comments in the code that try to explain why
changes were needed to this method.

thanks
Tushar
=============================================================
New file: org.apache.jmeter.util.XMLDataHandler

package org.apache.jmeter.util;

/**
 * Converts string data to xml by removing all non-xml characters and
replacing them with
 * appropriate XML types.
 * @author: Tushar Bhatia
 */
public class XMLDataConverter {
     private static String TOKENS = "&'\"<>\n\r\t\f\b\\";

     /** Converts a string to the XML format. Removes quote, apostrophe,
less than, greater than and
          ampersand characters and substitutes with the appropriate xml
replacements.
     */
     public static String convertToXML(String input){
          if(input == null)
               return null;
          String retVal = "";
          StringBuffer buffer = new StringBuffer(input);
          int length = buffer.length();
          for(int i = 0; i < length; i++){
               char ch = buffer.charAt(i);
               int chInt = (int)ch;
               if(Character.isLetterOrDigit(ch) || ch=='\n' || ch=='\r' ||
ch=='\t' || ch==' ')
                    continue;
               int chType = Character.getType(ch);
               if(chType == Character.CONTROL || chType ==
Character.UNASSIGNED)
                    buffer.setCharAt(i,  ' ');
          }
          input = buffer.toString();
          java.util.StringTokenizer tokenizer = new
java.util.StringTokenizer(input, TOKENS, true);
          while(tokenizer.hasMoreTokens()) {
               String nextToken = tokenizer.nextToken();
               length = nextToken.length();
               if(length > 1)
                    retVal += nextToken;
               else if(length == 1){ // could be a delimiter
                    char ch = nextToken.charAt(0);
                    switch(ch) {
                         case '&':
                              retVal += "&amp;";

                              break;
                         case '\'':
                              retVal += "&apos;";
                              break;
                         case '"':
                              retVal += "&quot;";
                              break;
                         case '<':
                              retVal += "&lt;";
                              break;
                         case '>':
                              retVal += "&gt;";
                              break;
                         case '\n':
                         case '\r':
                         case '\t':
                         case '\f':
                         case '\b':
                         case '\\':
                              retVal += "&#" + (int)ch + ";";
                              break;
                         default:
                              retVal += nextToken;
                    }
               }
          }
          return retVal;
     }
}

=================================================
changes to ArgumentHandler.java


Index: ./src/org/apache/jmeter/save/handlers/ArgumentsHandler.java
===================================================================
RCS file:
/home/cvspublic/jakarta-jmeter/src/org/apache/jmeter/save/handlers/ArgumentsHandler.java,v
retrieving revision 1.4
diff -r1.4 ArgumentsHandler.java
77a78,83
>
>    /** set to true each time a new Argument starts.
>         @see argument(java.lang.String) for an explanation of this
variable.
>    */
>    private boolean newArgumentStarted = false;
>
102c108,111
<              out.write(arg.getValue().toString());

---
>              String value = arg.getValue().toString();
>              if(value != null)
>                   value =
org.apache.jmeter.util.XMLDataConverter.convertToXML(value.toString());
>              out.write((value != null)?value:"");

123a133,134
>         currentArgument = currentArgument.trim();
>         newArgumentStarted = true;
125c136,143
<

---
>
>    /** This method is invoked each time there are arguments (not
attributes) for

>         any xml tag. eg. <xmlTag attribute="value">Argument Data</xmlTag>
contains one attribute
>         and the argument 'Argument Data'.
>         This works ok as long as 'Argument Data' contains no special xml
characters such as < or > etc.
>         If it does and the argument was '5&lt;6' (i.e 5 is less than 6),
the method would be invoked
>         3 times with data being (5, <, 6). Changes in this method take
care of this situation.
>    */
132,133c150,168
<         args.addArgument(currentArgument,data);

<         currentArgument = null;

---
>         if(newArgumentStarted == false) {
>              // we already have visited this function for the
sameCurrentArgument
>              // this may be the 2nd (3rd,..) call to this method for the
same currentArgument.
>              // We have to get the data already stored against the
>              // currentArgument and append the new data to it
>              String oldData = (String)args.getValueAt(args.getRowCount()
- 1, 1);
>              args.setValueAt(oldData + data, args.getRowCount() - 1, 1);
>         }
>         else
>              args.addArgument(currentArgument,data);
>         // because if there are multiple parts to the CDATA in one
argument eg. &lt;abcd&gt;
>         // there would be three calls to this method.
>         if(newArgumentStarted == true) {
>              currentArgument = null; // we will not need it again
>              newArgumentStarted = false;    // this is no longer new.
>         }
>
>         //args.addArgument(currentArgument,data);

>         //currentArgument = null;




---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org