📄 cookiespecbase.html
字号:
<a name="430" href="#430">430</a> <a name="431" href="#431">431</a> <em class="comment">// another security check... we musn't allow the server to give us a</em><a name="432" href="#432">432</a> <em class="comment">// cookie that doesn't match this path</em><a name="433" href="#433">433</a> <a name="434" href="#434">434</a> <strong>if</strong> (!path.startsWith(cookie.getPath())) {<a name="435" href="#435">435</a> <strong>throw</strong> <strong>new</strong> MalformedCookieException(<a name="436" href="#436">436</a> <span class="string">"Illegal path attribute \""</span> + cookie.getPath() <a name="437" href="#437">437</a> + <span class="string">"\". Path of origin: \""</span> + path + <span class="string">"\""</span>);<a name="438" href="#438">438</a> }<a name="439" href="#439">439</a> }<a name="440" href="#440">440</a> <a name="441" href="#441">441</a> <a name="442" href="#442">442</a> <em>/**<em>*</em></em><a name="443" href="#443">443</a> <em> * Return <tt>true</tt> if the cookie should be submitted with a request</em><a name="444" href="#444">444</a> <em> * with given attributes, <tt>false</tt> otherwise.</em><a name="445" href="#445">445</a> <em> * @param host the host to which the request is being submitted</em><a name="446" href="#446">446</a> <em> * @param port the port to which the request is being submitted (ignored)</em><a name="447" href="#447">447</a> <em> * @param path the path to which the request is being submitted</em><a name="448" href="#448">448</a> <em> * @param secure <tt>true</tt> if the request is using a secure connection</em><a name="449" href="#449">449</a> <em> * @param cookie {@link Cookie} to be matched</em><a name="450" href="#450">450</a> <em> * @return true if the cookie matches the criterium</em><a name="451" href="#451">451</a> <em> */</em><a name="452" href="#452">452</a> <a name="453" href="#453">453</a> <strong>public</strong> <strong>boolean</strong> match(String host, <strong>int</strong> port, String path, <a name="454" href="#454">454</a> <strong>boolean</strong> secure, <strong>final</strong> <a href="../../../../../org/apache/commons/httpclient/Cookie.html">Cookie</a> cookie) {<a name="455" href="#455">455</a> <a name="456" href="#456">456</a> LOG.trace(<span class="string">"enter CookieSpecBase.match("</span><a name="457" href="#457">457</a> + <span class="string">"String, int, String, boolean, Cookie"</span>);<a name="458" href="#458">458</a> <a name="459" href="#459">459</a> <strong>if</strong> (host == <strong>null</strong>) {<a name="460" href="#460">460</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<a name="461" href="#461">461</a> <span class="string">"Host of origin may not be null"</span>);<a name="462" href="#462">462</a> }<a name="463" href="#463">463</a> <strong>if</strong> (host.trim().equals(<span class="string">""</span>)) {<a name="464" href="#464">464</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<a name="465" href="#465">465</a> <span class="string">"Host of origin may not be blank"</span>);<a name="466" href="#466">466</a> }<a name="467" href="#467">467</a> <strong>if</strong> (port < 0) {<a name="468" href="#468">468</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"Invalid port: "</span> + port);<a name="469" href="#469">469</a> }<a name="470" href="#470">470</a> <strong>if</strong> (path == <strong>null</strong>) {<a name="471" href="#471">471</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<a name="472" href="#472">472</a> <span class="string">"Path of origin may not be null."</span>);<a name="473" href="#473">473</a> }<a name="474" href="#474">474</a> <strong>if</strong> (cookie == <strong>null</strong>) {<a name="475" href="#475">475</a> <strong>throw</strong> <strong>new</strong> IllegalArgumentException(<span class="string">"Cookie may not be null"</span>);<a name="476" href="#476">476</a> }<a name="477" href="#477">477</a> <strong>if</strong> (path.trim().equals(<span class="string">""</span>)) {<a name="478" href="#478">478</a> path = PATH_DELIM;<a name="479" href="#479">479</a> }<a name="480" href="#480">480</a> host = host.toLowerCase();<a name="481" href="#481">481</a> <strong>if</strong> (cookie.getDomain() == <strong>null</strong>) {<a name="482" href="#482">482</a> LOG.warn(<span class="string">"Invalid cookie state: domain not specified"</span>);<a name="483" href="#483">483</a> <strong>return</strong> false;<a name="484" href="#484">484</a> }<a name="485" href="#485">485</a> <strong>if</strong> (cookie.getPath() == <strong>null</strong>) {<a name="486" href="#486">486</a> LOG.warn(<span class="string">"Invalid cookie state: path not specified"</span>);<a name="487" href="#487">487</a> <strong>return</strong> false;<a name="488" href="#488">488</a> }<a name="489" href="#489">489</a> <a name="490" href="#490">490</a> <strong>return</strong><a name="491" href="#491">491</a> <em class="comment">// only add the cookie if it hasn't yet expired </em><a name="492" href="#492">492</a> (cookie.getExpiryDate() == <strong>null</strong> <a name="493" href="#493">493</a> || cookie.getExpiryDate().after(<strong>new</strong> Date()))<a name="494" href="#494">494</a> <em class="comment">// and the domain pattern matches </em><a name="495" href="#495">495</a> && (domainMatch(host, cookie.getDomain()))<a name="496" href="#496">496</a> <em class="comment">// and the path is null or matching</em><a name="497" href="#497">497</a> && (pathMatch(path, cookie.getPath()))<a name="498" href="#498">498</a> <em class="comment">// and if the secure flag is set, only if the request is </em><a name="499" href="#499">499</a> <em class="comment">// actually secure </em><a name="500" href="#500">500</a> && (cookie.getSecure() ? secure : <strong>true</strong>); <a name="501" href="#501">501</a> }<a name="502" href="#502">502</a> <a name="503" href="#503">503</a> <em>/**<em>*</em></em><a name="504" href="#504">504</a> <em> * Performs domain-match as implemented in common browsers.</em><a name="505" href="#505">505</a> <em> * @param host The target host.</em><a name="506" href="#506">506</a> <em> * @param domain The cookie domain attribute.</em><a name="507" href="#507">507</a> <em> * @return true if the specified host matches the given domain.</em><a name="508" href="#508">508</a> <em> */</em><a name="509" href="#509">509</a> <strong>public</strong> <strong>boolean</strong> domainMatch(<strong>final</strong> String host, String domain) {<a name="510" href="#510">510</a> <strong>if</strong> (host.equals(domain)) {<a name="511" href="#511">511</a> <strong>return</strong> <strong>true</strong>;<a name="512" href="#512">512</a> }<a name="513" href="#513">513</a> <strong>if</strong> (!domain.startsWith(<span class="string">"."</span>)) {<a name="514" href="#514">514</a> domain = <span class="string">"."</span> + domain;<a name="515" href="#515">515</a> }<a name="516" href="#516">516</a> <strong>return</strong> host.endsWith(domain) || host.equals(domain.substring(1));<a name="517" href="#517">517</a> }<a name="518" href="#518">518</a> <a name="519" href="#519">519</a> <em>/**<em>*</em></em><a name="520" href="#520">520</a> <em> * Performs path-match as implemented in common browsers.</em><a name="521" href="#521">521</a> <em> * @param path The target path.</em><a name="522" href="#522">522</a> <em> * @param topmostPath The cookie path attribute.</em><a name="523" href="#523">523</a> <em> * @return true if the paths match</em><a name="524" href="#524">524</a> <em> */</em><a name="525" href="#525">525</a> <strong>public</strong> <strong>boolean</strong> pathMatch(<strong>final</strong> String path, <strong>final</strong> String topmostPath) {<a name="526" href="#526">526</a> <strong>boolean</strong> match = path.startsWith (topmostPath);<a name="527" href="#527">527</a> <em class="comment">// if there is a match and these values are not exactly the same we have</em><a name="528" href="#528">528</a> <em class="comment">// to make sure we're not matcing "/foobar" and "/foo"</em><a name="529" href="#529">529</a> <strong>if</strong> (match && path.length() != topmostPath.length()) {<a name="530" href="#530">530</a> <strong>if</strong> (!topmostPath.endsWith(PATH_DELIM)) {<a name="531" href="#531">531</a> match = (path.charAt(topmostPath.length()) == PATH_DELIM_CHAR);<a name="532" href="#532">532</a> }<a name="533" href="#533">533</a> }<a name="534" href="#534">534</a> <strong>return</strong> match;<a name="535" href="#535">535</a> }<a name="536" href="#536">536</a> <a name="537" href="#537">537</a> <em>/**<em>*</em></em><a name="538" href="#538">538</a> <em> * Return an array of {@link Cookie}s that should be submitted with a</em><a name="539" href="#539">539</a> <em> * request with given attributes, <tt>false</tt> otherwise.</em><a name="540" href="#540">540</a> <em> * @param host the host to which the request is being submitted</em><a name="541" href="#541">541</a> <em> * @param port the port to which the request is being submitted (currently</em><a name="542" href="#542">542</a> <em> * ignored)</em><a name="543" href="#543">543</a> <em> * @param path the path to which the request is being submitted</em><a name="544" href="#544">544</a> <em> * @param secure <tt>true</tt> if the request is using a secure protocol</em><a name="545" href="#545">545</a> <em> * @param cookies an array of <tt>Cookie</tt>s to be matched</em><a name="546" href="#546">546</a> <em> * @return an array of <tt>Cookie</tt>s matching the criterium</em><a name="547" href="#547">547</a> <em> * </em><a name="548" href="#548">548</a> <em>// BEGIN IA CHANGES</em><a name="549" href="#549">549</a> <em> * @deprecated use match(String, int, String, boolean, SortedMap)</em><a name="550" href="#550">550</a> <em>// END IA CHANGES</em><a name="551" href="#551">551</a> <em> */</em><a name="552" href="#552">552</a> <a name="553" href="#553">553</a> <strong>public</strong> <a href="../../../../../org/apache/commons/httpclient/Cookie.html">Cookie</a>[] match(String host, <strong>int</strong> port, String path, <a name="554" href="#554">554</a> <strong>boolean</strong> secure, <strong>final</strong> <a href="../../../../../org/apache/commons/httpclient/Cookie.html">Cookie</a> cookies[]) {<a name="555" href="#555">555</a> <a name="556" href="#556">556</a> LOG.trace(<span class="string">"enter CookieSpecBase.match("</span><a name="557" href="#557">557</a> + <span class="string">"String, int, String, boolean, Cookie[])"</span>);<a name="558" href="#558">558</a> <a name="559" href="#559">559</a> <strong>if</strong> (cookies == <strong>null</strong>) {<a name="560" href="#560">560</a> <strong>return</strong> <strong>null</strong>;<a name="561" href="#561">561</a> }<a name="562" href="#562">562</a> List matching = <strong>new</strong> LinkedList();<a name="563" href="#563">563</a> <strong>for</strong> (<strong>int</strong> i = 0; i < cookies.length; i++) {<a name="564" href="#564">564</a> <strong>if</strong> (match(host, port, path, secure, cookies[i])) {<a name="565" href="#565">565</a> addInPathOrder(matching, cookies[i]);<a name="566" href="#566">566</a> }<a name="567" href="#567">567</a> }<a name="568" href="#568">568</a> <strong>return</strong> (Cookie[]) matching.toArray(<strong>new</strong> Cookie[matching.size()]);<a name="569" href="#569">569</a> }<a name="570" href="#570">570</a> <a name="571" href="#571">571</a> <em class="comment">// BEGIN IA CHANGES</em><a name="572" href="#572">572</a> <em>/**<em>*</em></em><a name="573" href="#573">573</a> <em> * Return an array of {@link Cookie}s that should be submitted with a</em><a name="574" href="#574">574</a> <em> * request with given attributes, <tt>false</tt> otherwise. </em><a name="575" href="#575">575</a> <em> * </em>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -