📄 fetchhttp.html
字号:
<a name="385" href="#385">385</a> <span class="string">"address(es)."</span>, <span class="string">""</span>));<a name="386" href="#386">386</a> e.setExpertSetting(<strong>true</strong>);<a name="387" href="#387">387</a> }<a name="388" href="#388">388</a> <a name="389" href="#389">389</a> <strong>protected</strong> <strong>void</strong> innerProcess(<strong>final</strong> <a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi)<a name="390" href="#390">390</a> throws InterruptedException {<a name="391" href="#391">391</a> <strong>if</strong> (!canFetch(curi)) {<a name="392" href="#392">392</a> <em class="comment">// Cannot fetch this, due to protocol, retries, or other problems</em><a name="393" href="#393">393</a> <strong>return</strong>;<a name="394" href="#394">394</a> }<a name="395" href="#395">395</a> <a name="396" href="#396">396</a> <strong>this</strong>.curisHandled++;<a name="397" href="#397">397</a> <a name="398" href="#398">398</a> <em class="comment">// Note begin time</em><a name="399" href="#399">399</a> curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());<a name="400" href="#400">400</a> <a name="401" href="#401">401</a> <em class="comment">// Get a reference to the HttpRecorder that is set into this ToeThread.</em><a name="402" href="#402">402</a> <a href="../../../../org/archive/util/HttpRecorder.html">HttpRecorder</a> rec = HttpRecorder.getHttpRecorder();<a name="403" href="#403">403</a> <a name="404" href="#404">404</a> <em class="comment">// Shall we get a digest on the content downloaded?</em><a name="405" href="#405">405</a> <strong>boolean</strong> sha1Content = ((Boolean)getUncheckedAttribute(curi,<a name="406" href="#406">406</a> ATTR_SHA1_CONTENT)).booleanValue();<a name="407" href="#407">407</a> <strong>if</strong>(sha1Content) {<a name="408" href="#408">408</a> rec.getRecordedInput().setSha1Digest();<a name="409" href="#409">409</a> } <strong>else</strong> {<a name="410" href="#410">410</a> <em class="comment">// clear</em><a name="411" href="#411">411</a> rec.getRecordedInput().setDigest(<strong>null</strong>);<a name="412" href="#412">412</a> }<a name="413" href="#413">413</a> <a name="414" href="#414">414</a> <em class="comment">// Below we do two inner classes that add check of midfetch</em><a name="415" href="#415">415</a> <em class="comment">// filters just as we're about to receive the response body.</em><a name="416" href="#416">416</a> String curiString = curi.getUURI().toString();<a name="417" href="#417">417</a> <a href="../../../../org/apache/commons/httpclient/HttpMethodBase.html">HttpMethodBase</a> method = <strong>null</strong>;<a name="418" href="#418">418</a> <strong>if</strong> (curi.isPost()) {<a name="419" href="#419">419</a> method = <strong>new</strong> <a href="../../../../org/archive/httpclient/HttpRecorderPostMethod.html">HttpRecorderPostMethod</a>(curiString, rec) {<a name="420" href="#420">420</a> <strong>protected</strong> <strong>void</strong> readResponseBody(<a href="../../../../org/apache/commons/httpclient/HttpState.html">HttpState</a> state,<a name="421" href="#421">421</a> <a href="../../../../org/apache/commons/httpclient/HttpConnection.html">HttpConnection</a> conn)<a name="422" href="#422">422</a> throws IOException, HttpException {<a name="423" href="#423">423</a> addResponseContent(<strong>this</strong>, curi);<a name="424" href="#424">424</a> <strong>if</strong> (checkMidfetchAbort(curi, <strong>this</strong>.httpRecorderMethod, conn)) {<a name="425" href="#425">425</a> doAbort(curi, <strong>this</strong>, MIDFETCH_ABORT_LOG);<a name="426" href="#426">426</a> } <strong>else</strong> {<a name="427" href="#427">427</a> <strong>super</strong>.readResponseBody(state, conn);<a name="428" href="#428">428</a> }<a name="429" href="#429">429</a> }<a name="430" href="#430">430</a> };<a name="431" href="#431">431</a> } <strong>else</strong> {<a name="432" href="#432">432</a> method = <strong>new</strong> <a href="../../../../org/archive/httpclient/HttpRecorderGetMethod.html">HttpRecorderGetMethod</a>(curiString, rec) {<a name="433" href="#433">433</a> <strong>protected</strong> <strong>void</strong> readResponseBody(<a href="../../../../org/apache/commons/httpclient/HttpState.html">HttpState</a> state,<a name="434" href="#434">434</a> <a href="../../../../org/apache/commons/httpclient/HttpConnection.html">HttpConnection</a> conn)<a name="435" href="#435">435</a> throws IOException, HttpException {<a name="436" href="#436">436</a> addResponseContent(<strong>this</strong>, curi);<a name="437" href="#437">437</a> <strong>if</strong> (checkMidfetchAbort(curi, <strong>this</strong>.httpRecorderMethod,<a name="438" href="#438">438</a> conn)) {<a name="439" href="#439">439</a> doAbort(curi, <strong>this</strong>, MIDFETCH_ABORT_LOG);<a name="440" href="#440">440</a> } <strong>else</strong> {<a name="441" href="#441">441</a> <strong>super</strong>.readResponseBody(state, conn);<a name="442" href="#442">442</a> }<a name="443" href="#443">443</a> }<a name="444" href="#444">444</a> };<a name="445" href="#445">445</a> }<a name="446" href="#446">446</a> <a name="447" href="#447">447</a> HostConfiguration customConfigOrNull = configureMethod(curi, method);<a name="448" href="#448">448</a> <a name="449" href="#449">449</a> <em class="comment">// Set httpRecorder into curi. Subsequent code both here and later</em><a name="450" href="#450">450</a> <em class="comment">// in extractors expects to find the HttpRecorder in the CrawlURI.</em><a name="451" href="#451">451</a> curi.setHttpRecorder(rec);<a name="452" href="#452">452</a> <a name="453" href="#453">453</a> <em class="comment">// Populate credentials. Set config so auth. is not automatic.</em><a name="454" href="#454">454</a> <strong>boolean</strong> addedCredentials = populateCredentials(curi, method);<a name="455" href="#455">455</a> method.setDoAuthentication(addedCredentials);<a name="456" href="#456">456</a> <a name="457" href="#457">457</a> <strong>try</strong> {<a name="458" href="#458">458</a> <strong>this</strong>.http.executeMethod(customConfigOrNull, method);<a name="459" href="#459">459</a> } <strong>catch</strong> (RecorderTooMuchHeaderException ex) {<a name="460" href="#460">460</a> <em class="comment">// when too much header material, abort like other truncations</em><a name="461" href="#461">461</a> doAbort(curi, method, HEADER_TRUNC);<a name="462" href="#462">462</a> } <strong>catch</strong> (IOException e) {<a name="463" href="#463">463</a> failedExecuteCleanup(method, curi, e);<a name="464" href="#464">464</a> <strong>return</strong>;<a name="465" href="#465">465</a> } <strong>catch</strong> (ArrayIndexOutOfBoundsException e) {<a name="466" href="#466">466</a> <em class="comment">// For weird windows-only ArrayIndex exceptions in native</em><a name="467" href="#467">467</a> <em class="comment">// code... see</em><a name="468" href="#468">468</a> <em class="comment">// http://forum.java.sun.com/thread.jsp?forum=11&thread=378356</em><a name="469" href="#469">469</a> <em class="comment">// treating as if it were an IOException</em><a name="470" href="#470">470</a> failedExecuteCleanup(method, curi, e);<a name="471" href="#471">471</a> <strong>return</strong>;<a name="472" href="#472">472</a> }<a name="473" href="#473">473</a> <a name="474" href="#474">474</a> <em class="comment">// set softMax on bytes to get (if implied by content-length) </em><a name="475" href="#475">475</a> <strong>long</strong> softMax = method.getResponseContentLength();<a name="476" href="#476">476</a> <a name="477" href="#477">477</a> <em class="comment">// set hardMax on bytes (if set by operator)</em><a name="478" href="#478">478</a> <strong>long</strong> hardMax = getMaxLength(curi);<a name="479" href="#479">479</a> <a name="480" href="#480">480</a> <em class="comment">// Get max fetch rate (bytes/ms). It comes in in KB/sec, which</em><a name="481" href="#481">481</a> <em class="comment">// requires nothing to normalize.</em><a name="482" href="#482">482</a> <strong>int</strong> maxFetchRate = getMaxFetchRate(curi);<a name="483" href="#483">483</a> <a name="484" href="#484">484</a> <strong>try</strong> {<a name="485" href="#485">485</a> <strong>if</strong> (!method.isAborted()) {<a name="486" href="#486">486</a> <em class="comment">// Force read-to-end, so that any socket hangs occur here,</em><a name="487" href="#487">487</a> <em class="comment">// not in later modules.</em><a name="488" href="#488">488</a> rec.getRecordedInput().readFullyOrUntil(softMax,<a name="489" href="#489">489</a> hardMax, 1000 * getTimeout(curi), maxFetchRate);<a name="490" href="#490">490</a> }<a name="491" href="#491">491</a> } <strong>catch</strong> (RecorderTimeoutException ex) {<a name="492" href="#492">492</a> doAbort(curi, method, TIMER_TRUNC);<a name="493" href="#493">493</a> } <strong>catch</strong> (RecorderLengthExceededException ex) {<a name="494" href="#494">494</a> doAbort(curi, method, LENGTH_TRUNC);<a name="495" href="#495">495</a> } <strong>catch</strong> (IOException e) {<a name="496" href="#496">496</a> cleanup(curi, e, <span class="string">"readFully"</span>, S_CONNECT_LOST);<a name="497" href="#497">497</a> <strong>return</strong>;<a name="498" href="#498">498</a> } <strong>catch</strong> (ArrayIndexOutOfBoundsException e) {<a name="499" href="#499">499</a> <em class="comment">// For weird windows-only ArrayIndex exceptions from native code</em><a name="500" href="#500">500</a> <em class="comment">// see http://forum.java.sun.com/thread.jsp?forum=11&thread=378356</em><a name="501" href="#501">501</a> <em class="comment">// treating as if it were an IOException</em><a name="502" href="#502">502</a> cleanup(curi, e, <span class="string">"readFully"</span>, S_CONNECT_LOST);<a name="503" href="#503">503</a> <strong>return</strong>;<a name="504" href="#504">504</a> } <strong>finally</strong> {<a name="505" href="#505">505</a> <em class="comment">// ensure recording has stopped</em><a name="506" href="#506">506</a> rec.closeRecorders();<a name="507" href="#507">507</a> <strong>if</strong> (!method.isAborted()) {<a name="508" href="#508">508</a> method.releaseConnection();<a name="509" href="#509">509</a> }<a name="510" href="#510">510</a> <em class="comment">// Note completion time</em><a name="511" href="#511">511</a> curi.putLong(A_FETCH_COMPLETED_TIME, System.currentTimeMillis());<a name="512" href="#512">512</a> <em class="comment">// Set the response charset into the HttpRecord if available.</em><a name="513" href="#513">513</a> setCharacterEncoding(rec, method);<a name="514" href="#514">514</a> curi.setContentSize(rec.getRecordedInput().getSize());<a name="515" href="#515">515</a> }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -