fetchhttp.html

来自「网络爬虫开源代码」· HTML 代码 · 共 643 行 · 第 1/5 页

HTML
643
字号
<a name="508" href="#508">508</a>             <em class="comment">// For weird windows-only ArrayIndex exceptions in native</em><a name="509" href="#509">509</a>             <em class="comment">// code... see</em><a name="510" href="#510">510</a>             <em class="comment">// http://forum.java.sun.com/thread.jsp?forum=11&amp;thread=378356</em><a name="511" href="#511">511</a>             <em class="comment">// treating as if it were an IOException</em><a name="512" href="#512">512</a>             failedExecuteCleanup(method, curi, e);<a name="513" href="#513">513</a>             <strong>return</strong>;<a name="514" href="#514">514</a>         }<a name="515" href="#515">515</a>         <a name="516" href="#516">516</a>         <em class="comment">// set softMax on bytes to get (if implied by content-length) </em><a name="517" href="#517">517</a>         <strong>long</strong> softMax = method.getResponseContentLength();<a name="518" href="#518">518</a> <a name="519" href="#519">519</a>         <strong>try</strong> {<a name="520" href="#520">520</a>             <strong>if</strong> (!method.isAborted()) {<a name="521" href="#521">521</a>                 <em class="comment">// Force read-to-end, so that any socket hangs occur here,</em><a name="522" href="#522">522</a>                 <em class="comment">// not in later modules.</em><a name="523" href="#523">523</a>                 rec.getRecordedInput().readFullyOrUntil(softMax);<a name="524" href="#524">524</a>             }<a name="525" href="#525">525</a>         } <strong>catch</strong> (RecorderTimeoutException ex) {<a name="526" href="#526">526</a>             doAbort(curi, method, TIMER_TRUNC);<a name="527" href="#527">527</a>         } <strong>catch</strong> (RecorderLengthExceededException ex) {<a name="528" href="#528">528</a>             doAbort(curi, method, LENGTH_TRUNC);<a name="529" href="#529">529</a>         } <strong>catch</strong> (IOException e) {<a name="530" href="#530">530</a>             cleanup(curi, e, <span class="string">"readFully"</span>, S_CONNECT_LOST);<a name="531" href="#531">531</a>             <strong>return</strong>;<a name="532" href="#532">532</a>         } <strong>catch</strong> (ArrayIndexOutOfBoundsException e) {<a name="533" href="#533">533</a>             <em class="comment">// For weird windows-only ArrayIndex exceptions from native code</em><a name="534" href="#534">534</a>             <em class="comment">// see http://forum.java.sun.com/thread.jsp?forum=11&amp;thread=378356</em><a name="535" href="#535">535</a>             <em class="comment">// treating as if it were an IOException</em><a name="536" href="#536">536</a>             cleanup(curi, e, <span class="string">"readFully"</span>, S_CONNECT_LOST);<a name="537" href="#537">537</a>             <strong>return</strong>;<a name="538" href="#538">538</a>         } <strong>finally</strong> {<a name="539" href="#539">539</a>             <em class="comment">// ensure recording has stopped</em><a name="540" href="#540">540</a>             rec.closeRecorders();<a name="541" href="#541">541</a>             <strong>if</strong> (!method.isAborted()) {<a name="542" href="#542">542</a>                 method.releaseConnection();<a name="543" href="#543">543</a>             }<a name="544" href="#544">544</a>             <em class="comment">// Note completion time</em><a name="545" href="#545">545</a>             curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());<a name="546" href="#546">546</a>             <em class="comment">// Set the response charset into the HttpRecord if available.</em><a name="547" href="#547">547</a>             setCharacterEncoding(rec, method);<a name="548" href="#548">548</a>             setSizes(curi, rec);<a name="549" href="#549">549</a>         }<a name="550" href="#550">550</a>  <a name="551" href="#551">551</a>         <strong>if</strong> (digestContent) {<a name="552" href="#552">552</a>             curi.setContentDigest(algorithm,<a name="553" href="#553">553</a>                 rec.getRecordedInput().getDigestValue());<a name="554" href="#554">554</a>         }<a name="555" href="#555">555</a>         <strong>if</strong> (logger.isLoggable(Level.INFO)) {<a name="556" href="#556">556</a>             logger.info((curi.isPost()? <span class="string">"POST"</span>: <span class="string">"GET"</span>) + <span class="string">" "</span> +<a name="557" href="#557">557</a>                 curi.getUURI().toString() + <span class="string">" "</span> + method.getStatusCode() +<a name="558" href="#558">558</a>                 <span class="string">" "</span> + rec.getRecordedInput().getSize() + <span class="string">" "</span> +<a name="559" href="#559">559</a>                 curi.getContentType());<a name="560" href="#560">560</a>         }<a name="561" href="#561">561</a> <a name="562" href="#562">562</a>         <strong>if</strong> (curi.isSuccess() &amp;&amp; addedCredentials) {<a name="563" href="#563">563</a>             <em class="comment">// Promote the credentials from the CrawlURI to the CrawlServer</em><a name="564" href="#564">564</a>             <em class="comment">// so they are available for all subsequent CrawlURIs on this</em><a name="565" href="#565">565</a>             <em class="comment">// server.</em><a name="566" href="#566">566</a>             promoteCredentials(curi);<a name="567" href="#567">567</a>             <strong>if</strong> (logger.isLoggable(Level.FINE)) {<a name="568" href="#568">568</a>                 <em class="comment">// Print out the cookie.  Might help with the debugging.</em><a name="569" href="#569">569</a>                 Header setCookie = method.getResponseHeader(<span class="string">"set-cookie"</span>);<a name="570" href="#570">570</a>                 <strong>if</strong> (setCookie != <strong>null</strong>) {<a name="571" href="#571">571</a>                     logger.fine(setCookie.toString().trim());<a name="572" href="#572">572</a>                 }<a name="573" href="#573">573</a>             }<a name="574" href="#574">574</a>         } <strong>else</strong> <strong>if</strong> (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {<a name="575" href="#575">575</a>             <em class="comment">// 401 is not 'success'.</em><a name="576" href="#576">576</a>             handle401(method, curi);<a name="577" href="#577">577</a>         }<a name="578" href="#578">578</a>         <a name="579" href="#579">579</a>         <strong>if</strong> (rec.getRecordedInput().isOpen()) {<a name="580" href="#580">580</a>             logger.severe(curi.toString() + <span class="string">" RIS still open. Should have"</span> +<a name="581" href="#581">581</a>                 <span class="string">" been closed by method release: "</span> +<a name="582" href="#582">582</a>                 Thread.currentThread().getName());<a name="583" href="#583">583</a>             <strong>try</strong> {<a name="584" href="#584">584</a>                 rec.getRecordedInput().close();<a name="585" href="#585">585</a>             } <strong>catch</strong> (IOException e) {<a name="586" href="#586">586</a>                 logger.log(Level.SEVERE,<span class="string">"second-chance RIS close failed"</span>,e);<a name="587" href="#587">587</a>             }<a name="588" href="#588">588</a>         }<a name="589" href="#589">589</a>     }<a name="590" href="#590">590</a> <a name="591" href="#591">591</a>     <em>/**<em>*</em></em><a name="592" href="#592">592</a> <em>     * Update CrawlURI internal sizes based on current transaction (and</em><a name="593" href="#593">593</a> <em>     * in the case of 304s, history) </em><a name="594" href="#594">594</a> <em>     * </em><a name="595" href="#595">595</a> <em>     * @param curi CrawlURI</em><a name="596" href="#596">596</a> <em>     * @param rec HttpRecorder</em><a name="597" href="#597">597</a> <em>     */</em><a name="598" href="#598">598</a>     <strong>protected</strong> <strong>void</strong> setSizes(<strong>final</strong> <a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi, <a href="../../../../org/archive/util/HttpRecorder.html">HttpRecorder</a> rec) {<a name="599" href="#599">599</a>         <em class="comment">// set reporting size</em><a name="600" href="#600">600</a>         curi.setContentSize(rec.getRecordedInput().getSize());<a name="601" href="#601">601</a>         <em class="comment">// special handling for 304-not modified</em><a name="602" href="#602">602</a>         <strong>if</strong> (curi.getFetchStatus() == HttpStatus.SC_NOT_MODIFIED<a name="603" href="#603">603</a>                 &amp;&amp; curi.containsKey(A_FETCH_HISTORY)) {<a name="604" href="#604">604</a>             <a href="../../../../st/ata/util/AList.html">AList</a> history[] = curi.getAList().getAListArray(A_FETCH_HISTORY);<a name="605" href="#605">605</a>             <strong>if</strong> (history[0] != <strong>null</strong><a name="606" href="#606">606</a>                     &amp;&amp; history[0]<a name="607" href="#607">607</a>                             .containsKey(CoreAttributeConstants.A_REFERENCE_LENGTH)) {<a name="608" href="#608">608</a>                 <strong>long</strong> referenceLength = history[0].getLong(A_REFERENCE_LENGTH);<a name="609" href="#609">609</a>                 <em class="comment">// carry-forward previous 'reference-length' for future</em><a name="610" href="#610">610</a>                 curi.putLong(A_REFERENCE_LENGTH, referenceLength);<a name="611" href="#611">611</a>                 <em class="comment">// increase content-size to virtual-size for reporting</em><a name="612" href="#612">612</a>                 curi.setContentSize(rec.getRecordedInput().getSize()<a name="613" href="#613">613</a>                         + referenceLength);<a name="614" href="#614">614</a>             }<a name="615" href="#615">615</a>         }<a name="616" href="#616">616</a>     }<a name="617" href="#617">617</a>     <a name="618" href="#618">618</a>     <strong>protected</strong> <strong>void</strong> doAbort(<a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi, HttpMethod method,<a name="619" href="#619">619</a>             String annotation) {<a name="620" href="#620">620</a>         curi.addAnnotation(annotation);<a name="621" href="#621">621</a>         curi.getHttpRecorder().close();<a name="622" href="#622">622</a>         method.abort();<a name="623" href="#623">623</a>     }<a name="624" href="#624">624</a>     <a name="625" href="#625">625</a>     <strong>protected</strong> <strong>boolean</strong> checkMidfetchAbort(<a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi,<a name="626" href="#626">626</a>             <a href="../../../../org/archive/httpclient/HttpRecorderMethod.html">HttpRecorderMethod</a> method, <a href="../../../../org/apache/commons/httpclient/HttpConnection.html">HttpConnection</a> conn) {<a name="627" href="#627">627</a>         <strong>if</strong> (curi.isPrerequisite() || rulesAccept(getMidfetchRule(curi), curi)) {<a name="628" href="#628">628</a>             <strong>return</strong> false;<a name="629" href="#629">629</a>         }<a name="630" href="#630">630</a>         method.markContentBegin(conn);<a name="631" href="#631">631</a>         <strong>return</strong> <strong>true</strong>;<a name="632" href="#632">632</a>     }<a name="633" href="#633">633</a>     <a name="634" href="#634">634</a>     <strong>protected</strong> <a href="../../../../org/archive/crawler/deciderules/DecideRule.html">DecideRule</a> getMidfetchRul

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?