📄 uurifactory.html
字号:
<a name="412" href="#412">412</a> <a name="413" href="#413">413</a> <em class="comment">// Kill newlines etc</em><a name="414" href="#414">414</a> uri = TextUtils.replaceAll(NEWLINE, uri, EMPTY_STRING);<a name="415" href="#415">415</a> <a name="416" href="#416">416</a> <em class="comment">// Test for the case of more than two slashes after the http(s) scheme.</em><a name="417" href="#417">417</a> <em class="comment">// Replace with two slashes as mozilla does if found.</em><a name="418" href="#418">418</a> <em class="comment">// See [ 788219 ] URI Syntax Errors stop page parsing.</em><a name="419" href="#419">419</a> Matcher matcher = HTTP_SCHEME_SLASHES.matcher(uri);<a name="420" href="#420">420</a> <strong>if</strong> (matcher.matches()) {<a name="421" href="#421">421</a> uri = matcher.group(1) + matcher.group(2);<a name="422" href="#422">422</a> }<a name="423" href="#423">423</a> <a name="424" href="#424">424</a> <em class="comment">// now, minimally escape any whitespace</em><a name="425" href="#425">425</a> uri = escapeWhitespace(uri);<a name="426" href="#426">426</a> <a name="427" href="#427">427</a> <em class="comment">// For further processing, get uri elements. See the RFC2396REGEX</em><a name="428" href="#428">428</a> <em class="comment">// comment above for explaination of group indices used in the below.</em><a name="429" href="#429">429</a> matcher = RFC2396REGEX.matcher(uri);<a name="430" href="#430">430</a> <strong>if</strong> (!matcher.matches()) {<a name="431" href="#431">431</a> <strong>throw</strong> <strong>new</strong> URIException(<span class="string">"Failed parse of "</span> + uri);<a name="432" href="#432">432</a> }<a name="433" href="#433">433</a> String uriScheme = checkUriElementAndLowerCase(matcher.group(2));<a name="434" href="#434">434</a> String uriSchemeSpecificPart = checkUriElement(matcher.group(3));<a name="435" href="#435">435</a> String uriAuthority = checkUriElement(matcher.group(5));<a name="436" href="#436">436</a> String uriPath = checkUriElement(matcher.group(6));<a name="437" href="#437">437</a> String uriQuery = checkUriElement(matcher.group(8));<a name="438" href="#438">438</a> <em class="comment">// UNUSED String uriFragment = checkUriElement(matcher.group(10));</em><a name="439" href="#439">439</a> <a name="440" href="#440">440</a> <em class="comment">// If a scheme, is it a supported scheme?</em><a name="441" href="#441">441</a> <strong>if</strong> (uriScheme != <strong>null</strong> && uriScheme.length() > 0 &&<a name="442" href="#442">442</a> <strong>this</strong>.schemes != <strong>null</strong>) {<a name="443" href="#443">443</a> <strong>if</strong> (!(Arrays.binarySearch(schemes,uriScheme)>=0)) {<a name="444" href="#444">444</a> <em class="comment">// unsupported; see if silently ignored</em><a name="445" href="#445">445</a> <strong>if</strong>((Arrays.binarySearch(ignoredSchemes,uriScheme)>=0)) {<a name="446" href="#446">446</a> <strong>throw</strong> <strong>new</strong> URIException(<a name="447" href="#447">447</a> IGNORED_SCHEME, <span class="string">"Ignored scheme: "</span> + uriScheme);<a name="448" href="#448">448</a> } <strong>else</strong> {<a name="449" href="#449">449</a> <strong>throw</strong> <strong>new</strong> URIException(<span class="string">"Unsupported scheme: "</span> + uriScheme);<a name="450" href="#450">450</a> }<a name="451" href="#451">451</a> }<a name="452" href="#452">452</a> }<a name="453" href="#453">453</a> <a name="454" href="#454">454</a> <em class="comment">// Test if relative URI. If so, need a base to resolve against.</em><a name="455" href="#455">455</a> <strong>if</strong> (uriScheme == <strong>null</strong> || uriScheme.length() <= 0) {<a name="456" href="#456">456</a> <strong>if</strong> (base == <strong>null</strong>) {<a name="457" href="#457">457</a> <strong>throw</strong> <strong>new</strong> URIException(<span class="string">"Relative URI but no base: "</span> + uri);<a name="458" href="#458">458</a> }<a name="459" href="#459">459</a> }<a name="460" href="#460">460</a> <a name="461" href="#461">461</a> <em class="comment">// fixup authority portion: lowercase/IDN-punycode any domain; </em><a name="462" href="#462">462</a> <em class="comment">// remove stray trailing spaces</em><a name="463" href="#463">463</a> uriAuthority = fixupAuthority(uriAuthority);<a name="464" href="#464">464</a> <a name="465" href="#465">465</a> <em class="comment">// Do some checks if absolute path.</em><a name="466" href="#466">466</a> <strong>if</strong> (uriSchemeSpec<strong>if</strong>icPart != <strong>null</strong> &&<a name="467" href="#467">467</a> uriSchemeSpecificPart.startsWith(SLASH)) {<a name="468" href="#468">468</a> <strong>if</strong> (uriPath != <strong>null</strong>) {<a name="469" href="#469">469</a> <em class="comment">// Eliminate '..' if its first thing in the path. IE does this.</em><a name="470" href="#470">470</a> uriPath = TextUtils.replaceFirst(SLASHDOTDOTSLASH, uriPath,<a name="471" href="#471">471</a> SLASH);<a name="472" href="#472">472</a> }<a name="473" href="#473">473</a> <em class="comment">// Ensure root URLs end with '/': browsers always send "/"</em><a name="474" href="#474">474</a> <em class="comment">// on the request-line, so we should consider "http://host"</em><a name="475" href="#475">475</a> <em class="comment">// to be "http://host/".</em><a name="476" href="#476">476</a> <strong>if</strong> (uriPath == <strong>null</strong> || EMPTY_STRING.equals(uriPath)) {<a name="477" href="#477">477</a> uriPath = SLASH;<a name="478" href="#478">478</a> }<a name="479" href="#479">479</a> }<a name="480" href="#480">480</a> <a name="481" href="#481">481</a> <strong>if</strong> (uriAuthority != <strong>null</strong>) {<a name="482" href="#482">482</a> <strong>if</strong> (uriScheme != <strong>null</strong> && uriScheme.length() > 0 &&<a name="483" href="#483">483</a> uriScheme.equals(HTTP)) {<a name="484" href="#484">484</a> uriAuthority = checkPort(uriAuthority);<a name="485" href="#485">485</a> uriAuthority = stripTail(uriAuthority, HTTP_PORT);<a name="486" href="#486">486</a> } <strong>else</strong> <strong>if</strong> (uriScheme != <strong>null</strong> && uriScheme.length() > 0 &&<a name="487" href="#487">487</a> uriScheme.equals(HTTPS)) {<a name="488" href="#488">488</a> uriAuthority = checkPort(uriAuthority);<a name="489" href="#489">489</a> uriAuthority = stripTail(uriAuthority, HTTPS_PORT);<a name="490" href="#490">490</a> }<a name="491" href="#491">491</a> <em class="comment">// Strip any prefix dot or tail dots from the authority.</em><a name="492" href="#492">492</a> uriAuthority = stripTail(uriAuthority, DOT);<a name="493" href="#493">493</a> uriAuthority = stripPrefix(uriAuthority, DOT);<a name="494" href="#494">494</a> } <strong>else</strong> {<a name="495" href="#495">495</a> <em class="comment">// no authority; may be relative. consider stripping scheme</em><a name="496" href="#496">496</a> <em class="comment">// to work-around org.apache.commons.httpclient.URI bug</em><a name="497" href="#497">497</a> <em class="comment">// ( http://issues.apache.org/jira/browse/HTTPCLIENT-587 )</em><a name="498" href="#498">498</a> <strong>if</strong> (uriScheme != <strong>null</strong> && base != <strong>null</strong><a name="499" href="#499">499</a> && uriScheme.equals(base.getScheme())) {<a name="500" href="#500">500</a> <em class="comment">// uriScheme redundant and will only confound httpclient.URI</em><a name="501" href="#501">501</a> uriScheme = <strong>null</strong>; <a name="502" href="#502">502</a> }<a name="503" href="#503">503</a> }<a name="504" href="#504">504</a> <a name="505" href="#505">505</a> <em class="comment">// Ensure minimal escaping. Use of 'lax' URI and URLCodec </em><a name="506" href="#506">506</a> <em class="comment">// means minimal escaping isn't necessarily complete/consistent.</em><a name="507" href="#507">507</a> <em class="comment">// There is a chance such lax encoding will throw exceptions</em><a name="508" href="#508">508</a> <em class="comment">// later at inconvenient times. </em><a name="509" href="#509">509</a> <em class="comment">//</em><a name="510" href="#510">510</a> <em class="comment">// One reason for these bad escapings -- though not the only --</em><a name="511" href="#511">511</a> <em class="comment">// is that the page is using an encoding other than the ASCII or the</em><a name="512" href="#512">512</a> <em class="comment">// UTF-8 that is our default URI encoding. In this case the parent</em><a name="513" href="#513">513</a> <em class="comment">// class is burping on the passed URL encoding. If the page encoding</em><a name="514" href="#514">514</a> <em class="comment">// was passed into this factory, the encoding seems to be parsed</em><a name="515" href="#515">515</a> <em class="comment">// correctly (See the testEscapedEncoding unit test).</em><a name="516" href="#516">516</a> <em class="comment">//</em><a name="517" href="#517">517</a> <em class="comment">// This fixup may cause us to miss content. There is the charset case</em><a name="518" href="#518">518</a> <em class="comment">// noted above. TODO: Look out for cases where we fail other than for</em><a name="519" href="#519">519</a> <em class="comment">// the above given reason which will be fixed when we address</em><a name="520" href="#520">520</a> <em class="comment">// '[ 913687 ] Make extractors interrogate for charset'.</em><a name="521" href="#521">521</a> <a name="522" href="#522">522</a> uriPath = ensureMinimalEscaping(uriPath, charset);<a name="523" href="#523">523</a> uriQuery = ensureMinimalEscaping(uriQuery, charset,<a name="524" href="#524">524</a> LaxURLCodec.QUERY_SAFE);<a name="525" href="#525">525</a> <a name="526" href="#526">526</a> <em class="comment">// Preallocate. The '1's and '2's in below are space for ':',</em><a name="527" href="#527">527</a> <em class="comment">// '//', etc. URI characters.</em><a name="528" href="#528">528</a> MutableString s = <strong>new</strong> MutableString(<a name="529" href="#529">529</a> ((uriScheme != <strong>null</strong>)? uriScheme.length(): 0)<a name="530" href="#530">530</a> + 1 <em class="comment">// ';' </em><a name="531" href="#531">531</a> + ((uriAuthority != <strong>null</strong>)? uriAuthority.length(): 0)<a name="532" href="#532">532</a> + 2 <em class="comment">// '//'</em><a name="533" href="#533">533</a> + ((uriPath != <strong>null</strong>)? uriPath.length(): 0)<a name="534" href="#534">534</a> + 1 <em class="comment">// '?'</em><a name="535" href="#535">535</a> + ((uriQuery != <strong>null</strong>)? uriQuery.length(): 0));<a name="536" href="#536">536</a> appendNonNull(s, uriScheme, <span class="string">":"</span>, <strong>true</strong>);<a name="537" href="#537">537</a> appendNonNull(s, uriAuthority, <span class="string">"//"</span>, false);<a name="538" href="#538">538</a> appendNonNull(s, uriPath, <span class="string">""</span>, false);<a name="539" href="#539">539</a> appendNonNull(s, uriQuery, <span class="string">"?"</span>, false);<a name="540" href="#540">540</a> <strong>return</strong> s.toString();<a name="541" href="#541">541</a> }<a name="542" href="#542">542</a> <a name="543" href="#543">543</a> <em>/**<em>*</em></em><a name="544" href="#544">544</a> <em> * Fixup 'authority' portion of URI, by removing any stray </em><a name="545" href="#545">545</a> <em> * encoded spaces, lowercasing any domain names, and applying</em><a name="546" href="#546">546</a> <em> * IDN-punycoding to Unicode domains. </em><a name="547" href="#547">547</a> <em> * </em><a name="548" href="#548">548</a> <em> * @param uriAuthority the authority string to fix</em><a name="549" href="#549">549</a> <em> * @return fixed version</em><a name="550" href="#550">550</a> <em> * @throws URIException</em><a name="551" href="#551">551</a> <em> */</em>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -