?? fetchhttp.html
字號:
<a name="379" href="#379">379</a> <strong>new</strong> Boolean(<strong>true</strong>)));<a name="380" href="#380">380</a> e.setOverrideable(<strong>true</strong>);<a name="381" href="#381">381</a> e.setExpertSetting(<strong>true</strong>);<a name="382" href="#382">382</a> e = addElementToDefinition(<strong>new</strong> <a href="../../../../org/archive/crawler/settings/SimpleType.html">SimpleType</a>(ATTR_SEND_CONNECTION_CLOSE,<a name="383" href="#383">383</a> <span class="string">"Send 'Connection: close' header with every request."</span>,<a name="384" href="#384">384</a> <strong>new</strong> Boolean(<strong>true</strong>)));<a name="385" href="#385">385</a> e.setOverrideable(<strong>true</strong>);<a name="386" href="#386">386</a> e.setExpertSetting(<strong>true</strong>);<a name="387" href="#387">387</a> e = addElementToDefinition(<strong>new</strong> <a href="../../../../org/archive/crawler/settings/SimpleType.html">SimpleType</a>(ATTR_SEND_REFERER,<a name="388" href="#388">388</a> <span class="string">"Send 'Referer' header with every request.\n"</span> +<a name="389" href="#389">389</a> <span class="string">"The 'Referer' header contans the location the crawler came "</span> +<a name="390" href="#390">390</a> <span class="string">" from, "</span> +<a name="391" href="#391">391</a> <span class="string">"the page the current URI was discovered in. The 'Referer' "</span> +<a name="392" href="#392">392</a> <span class="string">"usually is "</span> +<a name="393" href="#393">393</a> <span class="string">"logged on the remote server and can be of assistance to "</span> +<a name="394" href="#394">394</a> <span class="string">"webmasters trying to figure how a crawler got to a "</span> +<a name="395" href="#395">395</a> <span class="string">"particular area on a site."</span>,<a name="396" href="#396">396</a> <strong>new</strong> Boolean(<strong>true</strong>)));<a name="397" href="#397">397</a> e.setOverrideable(<strong>true</strong>);<a name="398" href="#398">398</a> e.setExpertSetting(<strong>true</strong>);<a name="399" href="#399">399</a> e = addElementToDefinition(<strong>new</strong> <a href="../../../../org/archive/crawler/settings/SimpleType.html">SimpleType</a>(ATTR_SEND_RANGE,<a name="400" href="#400">400</a> <span class="string">"Send 'Range' header when a limit ("</span> + ATTR_MAX_LENGTH_BYTES +<a name="401" href="#401">401</a> <span class="string">") on document size.\n"</span> +<a name="402" href="#402">402</a> <span class="string">"Be polite to the HTTP servers and send the 'Range' header,"</span> +<a name="403" href="#403">403</a> <span class="string">"stating that you are only interested in the first n bytes. "</span> +<a name="404" href="#404">404</a> <span class="string">"Only pertinent if "</span> + ATTR_MAX_LENGTH_BYTES + <span class="string">" > 0. "</span> +<a name="405" href="#405">405</a> <span class="string">"Sending the 'Range' header results in a "</span> +<a name="406" href="#406">406</a> <span class="string">"'206 Partial Content' status response, which is better than "</span> +<a name="407" href="#407">407</a> <span class="string">"just cutting the response mid-download. On rare occasion, "</span> +<a name="408" href="#408">408</a> <span class="string">" sending 'Range' will "</span> +<a name="409" href="#409">409</a> <span class="string">"generate '416 Request Range Not Satisfiable' response."</span>,<a name="410" href="#410">410</a> <strong>new</strong> Boolean(false)));<a name="411" href="#411">411</a> e.setOverrideable(<strong>true</strong>);<a name="412" href="#412">412</a> e.setExpertSetting(<strong>true</strong>);<a name="413" href="#413">413</a> e = addElementToDefinition(<strong>new</strong> <a href="../../../../org/archive/crawler/settings/SimpleType.html">SimpleType</a>(ATTR_LOCAL_ADDRESS,<a name="414" href="#414">414</a> <span class="string">"Local IP address or hostname to use when making connections "</span> +<a name="415" href="#415">415</a> <span class="string">"(binding sockets). When not specified, uses default local"</span> +<a name="416" href="#416">416</a> <span class="string">"address(es)."</span>, <span class="string">""</span>));<a name="417" href="#417">417</a> e.setExpertSetting(<strong>true</strong>);<a name="418" href="#418">418</a> }<a name="419" href="#419">419</a> <a name="420" href="#420">420</a> <strong>protected</strong> <strong>void</strong> innerProcess(<strong>final</strong> <a href="../../../../org/archive/crawler/datamodel/CrawlURI.html">CrawlURI</a> curi)<a name="421" href="#421">421</a> throws InterruptedException {<a name="422" href="#422">422</a> <strong>if</strong> (!canFetch(curi)) {<a name="423" href="#423">423</a> <em class="comment">// Cannot fetch this, due to protocol, retries, or other problems</em><a name="424" href="#424">424</a> <strong>return</strong>;<a name="425" href="#425">425</a> }<a name="426" href="#426">426</a> <a name="427" href="#427">427</a> <strong>this</strong>.curisHandled++;<a name="428" href="#428">428</a> <a name="429" href="#429">429</a> <em class="comment">// Note begin time</em><a name="430" href="#430">430</a> curi.putLong(A_FETCH_BEGAN_TIME, System.currentTimeMillis());<a name="431" href="#431">431</a> <a name="432" href="#432">432</a> <em class="comment">// Get a reference to the HttpRecorder that is set into this ToeThread.</em><a name="433" href="#433">433</a> <a href="../../../../org/archive/util/HttpRecorder.html">HttpRecorder</a> rec = HttpRecorder.getHttpRecorder();<a name="434" href="#434">434</a> <a name="435" href="#435">435</a> <em class="comment">// Shall we get a digest on the content downloaded?</em><a name="436" href="#436">436</a> <strong>boolean</strong> digestContent = ((Boolean)getUncheckedAttribute(curi,<a name="437" href="#437">437</a> ATTR_DIGEST_CONTENT)).booleanValue();<a name="438" href="#438">438</a> String algorithm = <strong>null</strong>;<a name="439" href="#439">439</a> <strong>if</strong> (digestContent) {<a name="440" href="#440">440</a> algorithm = ((String)getUncheckedAttribute(curi,<a name="441" href="#441">441</a> ATTR_DIGEST_ALGORITHM));<a name="442" href="#442">442</a> rec.getRecordedInput().setDigest(algorithm);<a name="443" href="#443">443</a> } <strong>else</strong> {<a name="444" href="#444">444</a> <em class="comment">// clear</em><a name="445" href="#445">445</a> rec.getRecordedInput().setDigest((MessageDigest)<strong>null</strong>);<a name="446" href="#446">446</a> } <a name="447" href="#447">447</a> <a name="448" href="#448">448</a> <em class="comment">// Below we do two inner classes that add check of midfetch</em><a name="449" href="#449">449</a> <em class="comment">// filters just as we're about to receive the response body.</em><a name="450" href="#450">450</a> String curiString = curi.getUURI().toString();<a name="451" href="#451">451</a> <a href="../../../../org/apache/commons/httpclient/HttpMethodBase.html">HttpMethodBase</a> method = <strong>null</strong>;<a name="452" href="#452">452</a> <strong>if</strong> (curi.isPost()) {<a name="453" href="#453">453</a> method = <strong>new</strong> <a href="../../../../org/archive/httpclient/HttpRecorderPostMethod.html">HttpRecorderPostMethod</a>(curiString, rec) {<a name="454" href="#454">454</a> <strong>protected</strong> <strong>void</strong> readResponseBody(<a href="../../../../org/apache/commons/httpclient/HttpState.html">HttpState</a> state,<a name="455" href="#455">455</a> <a href="../../../../org/apache/commons/httpclient/HttpConnection.html">HttpConnection</a> conn)<a name="456" href="#456">456</a> throws IOException, HttpException {<a name="457" href="#457">457</a> addResponseContent(<strong>this</strong>, curi);<a name="458" href="#458">458</a> <strong>if</strong> (checkMidfetchAbort(curi, <strong>this</strong>.httpRecorderMethod, conn)) {<a name="459" href="#459">459</a> doAbort(curi, <strong>this</strong>, MIDFETCH_ABORT_LOG);<a name="460" href="#460">460</a> } <strong>else</strong> {<a name="461" href="#461">461</a> <strong>super</strong>.readResponseBody(state, conn);<a name="462" href="#462">462</a> }<a name="463" href="#463">463</a> }<a name="464" href="#464">464</a> };<a name="465" href="#465">465</a> } <strong>else</strong> {<a name="466" href="#466">466</a> method = <strong>new</strong> <a href="../../../../org/archive/httpclient/HttpRecorderGetMethod.html">HttpRecorderGetMethod</a>(curiString, rec) {<a name="467" href="#467">467</a> <strong>protected</strong> <strong>void</strong> readResponseBody(<a href="../../../../org/apache/commons/httpclient/HttpState.html">HttpState</a> state,<a name="468" href="#468">468</a> <a href="../../../../org/apache/commons/httpclient/HttpConnection.html">HttpConnection</a> conn)<a name="469" href="#469">469</a> throws IOException, HttpException {<a name="470" href="#470">470</a> addResponseContent(<strong>this</strong>, curi);<a name="471" href="#471">471</a> <strong>if</strong> (checkMidfetchAbort(curi, <strong>this</strong>.httpRecorderMethod,<a name="472" href="#472">472</a> conn)) {<a name="473" href="#473">473</a> doAbort(curi, <strong>this</strong>, MIDFETCH_ABORT_LOG);<a name="474" href="#474">474</a> } <strong>else</strong> {<a name="475" href="#475">475</a> <strong>super</strong>.readResponseBody(state, conn);<a name="476" href="#476">476</a> }<a name="477" href="#477">477</a> }<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> HostConfiguration customConfigOrNull = configureMethod(curi, method);<a name="482" href="#482">482</a> <a name="483" href="#483">483</a> <em class="comment">// Set httpRecorder into curi. Subsequent code both here and later</em><a name="484" href="#484">484</a> <em class="comment">// in extractors expects to find the HttpRecorder in the CrawlURI.</em><a name="485" href="#485">485</a> curi.setHttpRecorder(rec);<a name="486" href="#486">486</a> <a name="487" href="#487">487</a> <em class="comment">// Populate credentials. Set config so auth. is not automatic.</em><a name="488" href="#488">488</a> <strong>boolean</strong> addedCredentials = populateCredentials(curi, method);<a name="489" href="#489">489</a> method.setDoAuthentication(addedCredentials);<a name="490" href="#490">490</a> <a name="491" href="#491">491</a> <em class="comment">// set hardMax on bytes (if set by operator)</em><a name="492" href="#492">492</a> <strong>long</strong> hardMax = getMaxLength(curi);<a name="493" href="#493">493</a> <em class="comment">// set overall timeout (if set by operator)</em><a name="494" href="#494">494</a> <strong>long</strong> timeoutMs = 1000 * getTimeout(curi);<a name="495" href="#495">495</a> <em class="comment">// Get max fetch rate (bytes/ms). It comes in in KB/sec</em><a name="496" href="#496">496</a> <strong>long</strong> maxRateKBps = getMaxFetchRate(curi);<a name="497" href="#497">497</a> rec.getRecordedInput().setLimits(hardMax, timeoutMs, maxRateKBps);<a name="498" href="#498">498</a> <a name="499" href="#499">499</a> <strong>try</strong> {<a name="500" href="#500">500</a> <strong>this</strong>.http.executeMethod(customConfigOrNull, method);<a name="501" href="#501">501</a> } <strong>catch</strong> (RecorderTooMuchHeaderException ex) {<a name="502" href="#502">502</a> <em class="comment">// when too much header material, abort like other truncations</em><a name="503" href="#503">503</a> doAbort(curi, method, HEADER_TRUNC);<a name="504" href="#504">504</a> } <strong>catch</strong> (IOException e) {<a name="505" href="#505">505</a> failedExecuteCleanup(method, curi, e);<a name="506" href="#506">506</a> <strong>return</strong>;<a name="507" href="#507">507</a> } <strong>catch</strong> (ArrayIndexOutOfBoundsException e) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -