This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The problem was that java.net.URI and java.net.URL could not parse url strings that were based on backslash escape rather than %xx escape. The General Rule is this: 1. any url given to e.g. HTTPSession or HTTPMethod or HTTPFactory is assumed to be the url that is wanted on the server side. 2. #1 means that, again as a general rule, urlstrings given to those classes should not be %xx escaped unless that is what you want on the server side. 3. The key to understand is that a. those classes will internally %xx encode the url string they are given even if the url string already is %xx encoded. b. on the server side, the %xx encoding will be performed on the incoming url string. This means that the %xx encoded string you originally sent is the one given to the servlet. Bottom line: do not do %xx encoding yourself unless your server side code is expected to see a %xx encoded url. Anyway, the fix is to create a procedure -- HTTPUtil.parseToURI() -- that can properly create a java.net.URI object from a backslash escaped url string. Any time you need to create a URI object, you should do so using that procedure.
- Loading branch information
1 parent
e97632c
commit 2d2b597
Showing
10 changed files
with
299 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
209 changes: 209 additions & 0 deletions
209
cdm-test/src/test/java/ucar/nc2/util/net/TestURIParse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/* | ||
* Copyright (c) 1998 - 2012. University Corporation for Atmospheric Research/Unidata | ||
* Portions of this software were developed by the Unidata Program at the | ||
* University Corporation for Atmospheric Research. | ||
* | ||
* Access and use of this software shall impose the following obligations | ||
* and understandings on the user. The user is granted the right, without | ||
* any fee or cost, to use, copy, modify, alter, enhance and distribute | ||
* this software, and any derivative works thereof, and its supporting | ||
* documentation for any purpose whatsoever, provided that this entire | ||
* notice appears in all copies of the software, derivative works and | ||
* supporting documentation. Further, UCAR requests that the user credit | ||
* UCAR/Unidata in any publications that result from the use of this | ||
* software or in any product that includes this software. The names UCAR | ||
* and/or Unidata, however, may not be used in any advertising or publicity | ||
* to endorse or promote any products or commercial entity unless specific | ||
* written permission is obtained from UCAR/Unidata. The user also | ||
* understands that UCAR/Unidata is not obligated to provide the user with | ||
* any support, consulting, training or assistance of any kind with regard | ||
* to the use, operation and performance of this software nor to provide | ||
* the user with any updates, revisions, new versions or "bug fixes." | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL, | ||
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING | ||
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | ||
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | ||
* WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package ucar.nc2.util.net; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import ucar.httpservices.HTTPFactory; | ||
import ucar.httpservices.HTTPMethod; | ||
import ucar.httpservices.HTTPUtil; | ||
import ucar.nc2.util.UnitTestCommon; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
/** | ||
* Test HTTPUtil.parseToURI on a variety of input cases. | ||
*/ | ||
|
||
public class TestURIParse extends UnitTestCommon | ||
{ | ||
static public boolean DEBUG = false; | ||
static public boolean DOCARON = false; | ||
|
||
static final String CARON = "http://localhost:8081/thredds/cdmremote/scanCdmUnitTests/formats/hdf5/grid_1_3d_xyz_aug.h5?req=data&var=HDFEOS_INFORMATION/StructMetadata\\.0"; | ||
|
||
static final String[] filetests = {}; | ||
|
||
static final String[] httptests = { | ||
"http://ucar.edu:8081/dts/test\\/fake\\.01", | ||
CARON, | ||
}; | ||
|
||
////////////////////////////////////////////////// | ||
|
||
// Define the test sets | ||
|
||
int passcount = 0; | ||
int xfailcount = 0; | ||
int failcount = 0; | ||
boolean verbose = true; | ||
boolean pass = false; | ||
|
||
String datadir = null; | ||
String threddsroot = null; | ||
|
||
public TestURIParse() | ||
{ | ||
setTitle("HTTPUtil.parseToURI tests"); | ||
} | ||
|
||
@Test | ||
public void | ||
testParse() throws Exception | ||
{ | ||
pass = true; | ||
for(int i = 0; i < httptests.length; i++) { | ||
boolean passthis = true; | ||
URI uri = null; | ||
try { | ||
uri = HTTPUtil.parseToURI(httptests[i]); | ||
} catch (URISyntaxException use) { | ||
System.err.println("Parse error: " + use.getMessage()); | ||
if(DEBUG) use.printStackTrace(System.err); | ||
uri = null; | ||
passthis = false; | ||
} | ||
String raw = dumpraw(uri); | ||
if(DEBUG) System.err.printf("raw= |%s|%n", raw); | ||
System.err.printf("Test A: " | ||
+ "input :: actual%n" | ||
+ "\t |%s|%n" | ||
+ "\t:: |%s|%n", | ||
httptests[i],dump(uri)); | ||
if(!httptests[i].equals(dump(uri))) { | ||
passthis = false; | ||
} | ||
// Second test is for idempotence of %xx form. | ||
try { | ||
uri = HTTPUtil.parseToURI(raw); | ||
} catch (URISyntaxException use) { | ||
System.err.println("Parse error: " + use.getMessage()); | ||
if(DEBUG) use.printStackTrace(System.err); | ||
uri = null; | ||
passthis = false; | ||
} | ||
System.err.printf("Test B: " | ||
+ "input :: actual%n" | ||
+ "\t |%s|%n" | ||
+ "\t:: |%s|%n", | ||
raw,dumpraw(uri)); | ||
if(!raw.equals(dumpraw(uri))) { | ||
passthis = false; | ||
} | ||
System.err.println(passthis ? "Pass" : "Fail"); | ||
if(!passthis) pass = false; | ||
} | ||
Assert.assertTrue("TestMisc.testURX", pass); | ||
} | ||
|
||
// Temporary to test Caron's case specifically | ||
@Test | ||
public void | ||
testCaron() | ||
{ | ||
if(!DOCARON) return; | ||
try { | ||
try (HTTPMethod m = HTTPFactory.Get(CARON)) { | ||
int code = m.execute(); | ||
Assert.assertTrue("Unexpected return code: " + code, code == 200); | ||
} | ||
} catch (Exception use) { | ||
use.printStackTrace(); | ||
Assert.assertTrue("URISyntaxException", false); | ||
} | ||
} | ||
|
||
|
||
static protected boolean | ||
uriCompare(URI uri1, URI uri2) | ||
{ | ||
boolean ok = true; | ||
ok = ok && uriPartCompare(uri1.getScheme(), uri2.getScheme()); | ||
ok = ok && uriPartCompare(uri1.getHost(), uri2.getHost()); | ||
ok = ok && (uri1.getPort() == uri2.getPort()); | ||
ok = ok && uriPartCompare(uri1.getPath(), uri2.getPath()); | ||
ok = ok && uriPartCompare(uri1.getQuery(), uri2.getQuery()); | ||
ok = ok && uriPartCompare(uri1.getFragment(), uri2.getFragment()); | ||
return ok; | ||
} | ||
|
||
static protected boolean | ||
uriCompareRaw(URI uri1, URI uri2) | ||
{ | ||
boolean ok = true; | ||
ok = ok && uriPartCompare(uri1.getScheme(), uri2.getScheme()); | ||
ok = ok && uriPartCompare(uri1.getHost(), uri2.getHost()); | ||
ok = ok && (uri1.getPort() == uri2.getPort()); | ||
ok = ok && uriPartCompare(uri1.getRawPath(), uri2.getRawPath()); | ||
ok = ok && uriPartCompare(uri1.getRawQuery(), uri2.getRawQuery()); | ||
ok = ok && uriPartCompare(uri1.getRawFragment(), uri2.getRawFragment()); | ||
return ok; | ||
} | ||
|
||
static protected boolean | ||
uriPartCompare(String s1, String s2) | ||
{ | ||
if(s1 == s2) return true; | ||
if(s1 == null || s2 == null) return false; | ||
return (s1.equals(s2)); | ||
} | ||
|
||
static protected String | ||
dump(URI uri) | ||
{ | ||
StringBuilder buf = new StringBuilder(); | ||
buf.append(uri.getScheme()).append("://"); | ||
buf.append(uri.getHost()); | ||
if(uri.getPort() >= 0) buf.append(':').append(uri.getPort()); | ||
if(uri.getPath() != null) buf.append(uri.getPath()); | ||
if(uri.getQuery() != null) buf.append('?').append(uri.getQuery()); | ||
if(uri.getFragment() != null) buf.append('#').append(uri.getFragment()); | ||
return buf.toString(); | ||
} | ||
|
||
static protected String | ||
dumpraw(URI uri) | ||
{ | ||
StringBuilder buf = new StringBuilder(); | ||
buf.append(uri.getScheme()).append("://"); | ||
buf.append(uri.getHost()); | ||
if(uri.getPort() >= 0) buf.append(':').append(uri.getPort()); | ||
if(uri.getRawPath() != null) buf.append(uri.getRawPath()); | ||
if(uri.getRawQuery() != null) buf.append('?').append(uri.getRawQuery()); | ||
if(uri.getRawFragment() != null) buf.append('#').append(uri.getRawFragment()); | ||
return buf.toString(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.