Fix DefaultFileNameMap behavior for URL cases involving a fragment.

DefaultFileNameMap searched for occurrences of each of '/', '.', '#'
in the full string given rather than only the portions remaining
after cutting off ignored parts (e.g., '/' or '.' that occurred
as part of the fragment). The logic did check for a '.' that occurred
before the last '/' but it didn't check for a '.' that occurred after
'#'.

Further, the logic checked for the last occurrence of '#'. It appears
that RFC 3986 only allows a single '#' in a URI and java.net.URL does
indeed throw URISyntaxException when more than one is present; however,
android.net.Uri accepts multiple '#' and considers the fragment to
start after the first one; therefore, this CL also changes the logic
to cut off the fragment part at the first, rather than last, occurrence
of '#'.

Further, the logic recognized directories (ending in '/'), but only
for URLs that did not include a fragment (it would have returned
null rather than "text/html" for such URLs).

This CL fixes all of the above by splitting off parts of the URL
in priority order, using substring(). This might have a very minor
performance hit because character data might be copied multiple
times, but is easier to follow.

This CL also expands test coverage to cover some cases that would
previously have failed.

Fixes: 37035456
Test: atest CtsLibcoreTestCases:libcore.java.net.URLConnectionTest
Change-Id: I26fbecc4e1da0cf1065ea8a4fa43920035d97a1c
2 files changed