From 3f812a671443b5e45c7abb7f6e95f5faba3b77aa Mon Sep 17 00:00:00 2001 From: emopers Date: Wed, 25 Nov 2015 18:12:53 -0600 Subject: [PATCH] fixing bug: check that header is non-empty before parsing --- src/com/meterware/httpunit/HttpUnitUtils.java | 5 ++++- test/com/meterware/httpunit/EncodingTest.java | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/com/meterware/httpunit/HttpUnitUtils.java b/src/com/meterware/httpunit/HttpUnitUtils.java index c8a4f05..365875d 100644 --- a/src/com/meterware/httpunit/HttpUnitUtils.java +++ b/src/com/meterware/httpunit/HttpUnitUtils.java @@ -76,6 +76,9 @@ public static boolean isEclipse() { public static String[] parseContentTypeHeader( String header ) { String[] result = new String[] { "text/plain", null }; StringTokenizer st = new StringTokenizer( header, ";=" ); + if (!st.hasMoreTokens()) { + throw new IllegalArgumentException( "Attempting to parse invalid header" ); + } result[0] = st.nextToken(); while (st.hasMoreTokens()) { String parameter = st.nextToken(); @@ -458,4 +461,4 @@ public static boolean setEXCEPTION_DEBUG(boolean exception_debug) { EXCEPTION_DEBUG = exception_debug; return oldExceptionDebug; } -} \ No newline at end of file +} diff --git a/test/com/meterware/httpunit/EncodingTest.java b/test/com/meterware/httpunit/EncodingTest.java index 9d258c6..3a338ed 100644 --- a/test/com/meterware/httpunit/EncodingTest.java +++ b/test/com/meterware/httpunit/EncodingTest.java @@ -81,6 +81,17 @@ public void testParseContentHeader() throws Exception { } + public void testParseEmptyHeader() throws Exception { + String invalidHeader = ""; + try { + String result[]=HttpUnitUtils.parseContentTypeHeader(invalidHeader); + fail(); + } catch (IllegalArgumentException iae) { + assertEquals( "header", "Attempting to parse invalid header" , iae.getMessage() ); + } + } + + public void testSpecifiedEncoding() throws Exception { String hebrewTitle = "\u05d0\u05d1\u05d2\u05d3"; String page = "" + hebrewTitle + "\n" +