crawlscope 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/README.md +7 -0
- data/UPGRADE.md +84 -0
- data/docs/architecture.md +73 -0
- data/docs/link-map.md +203 -0
- data/docs/rails.md +9 -0
- data/docs/rules.md +49 -0
- data/lib/crawlscope/cli.rb +86 -5
- data/lib/crawlscope/configuration.rb +2 -1
- data/lib/crawlscope/crawl.rb +11 -79
- data/lib/crawlscope/crawling/fetcher_factory.rb +31 -0
- data/lib/crawlscope/crawling/target.rb +45 -0
- data/lib/crawlscope/crawling/target_resolver.rb +75 -0
- data/lib/crawlscope/documents/canonical.rb +22 -0
- data/lib/crawlscope/documents/robots.rb +57 -0
- data/lib/crawlscope/http.rb +37 -13
- data/lib/crawlscope/link_map/audit.rb +240 -0
- data/lib/crawlscope/link_map/extractor.rb +59 -0
- data/lib/crawlscope/link_map/graph.rb +165 -0
- data/lib/crawlscope/link_map/manifest.rb +68 -0
- data/lib/crawlscope/link_map/report.rb +217 -0
- data/lib/crawlscope/link_map/robots_policy.rb +72 -0
- data/lib/crawlscope/link_map/scope.rb +82 -0
- data/lib/crawlscope/link_map/sitemap_urls.rb +103 -0
- data/lib/crawlscope/page.rb +4 -0
- data/lib/crawlscope/rake_tasks.rb +11 -1
- data/lib/crawlscope/reporter.rb +19 -162
- data/lib/crawlscope/reporting/issue_presenter.rb +162 -0
- data/lib/crawlscope/reporting/url_formatter.rb +29 -0
- data/lib/crawlscope/rule_registry.rb +2 -2
- data/lib/crawlscope/rules/content_quality.rb +18 -0
- data/lib/crawlscope/rules/indexability.rb +9 -14
- data/lib/crawlscope/rules/links/canonical_checks.rb +102 -0
- data/lib/crawlscope/rules/links/extractor.rb +100 -0
- data/lib/crawlscope/rules/links/graph.rb +39 -0
- data/lib/crawlscope/rules/links/inbound_checks.rb +124 -0
- data/lib/crawlscope/rules/links/outgoing_checks.rb +104 -0
- data/lib/crawlscope/rules/links/resolution_check.rb +88 -0
- data/lib/crawlscope/rules/links/scope.rb +23 -0
- data/lib/crawlscope/rules/links/target_lookup.rb +38 -0
- data/lib/crawlscope/rules/links/url_checks.rb +54 -0
- data/lib/crawlscope/rules/links.rb +18 -600
- data/lib/crawlscope/rules/metadata.rb +2 -2
- data/lib/crawlscope/rules/structured_data.rb +26 -77
- data/lib/crawlscope/rules/uniqueness.rb +1 -1
- data/lib/crawlscope/run.rb +16 -0
- data/lib/crawlscope/schemas.rb +10 -2
- data/lib/crawlscope/server_timing/reporter.rb +2 -13
- data/lib/crawlscope/sitemap.rb +16 -5
- data/lib/crawlscope/structured_data/audit.rb +17 -76
- data/lib/crawlscope/structured_data/check.rb +2 -0
- data/lib/crawlscope/structured_data/document.rb +14 -1
- data/lib/crawlscope/structured_data/entry.rb +31 -0
- data/lib/crawlscope/structured_data/outcome.rb +11 -0
- data/lib/crawlscope/structured_data/report.rb +6 -14
- data/lib/crawlscope/structured_data/reporter.rb +6 -11
- data/lib/crawlscope/structured_data/validator.rb +64 -0
- data/lib/crawlscope/version.rb +1 -1
- data/lib/tasks/crawlscope_tasks.rake +6 -1
- data/test/crawlscope/cli_test.rb +21 -0
- data/test/crawlscope/content_quality_rule_test.rb +51 -0
- data/test/crawlscope/link_map_test.rb +337 -0
- data/test/crawlscope/links_rule_test.rb +16 -0
- data/test/crawlscope/rake_tasks_test.rb +10 -0
- data/test/crawlscope/reporter_test.rb +12 -0
- data/test/crawlscope/schema_registry_test.rb +22 -0
- data/test/crawlscope/structured_data_audit_test.rb +60 -0
- data/test/crawlscope/structured_data_report_test.rb +32 -0
- data/test/crawlscope/structured_data_rule_test.rb +11 -1
- data/test/crawlscope/structured_data_validator_test.rb +24 -0
- metadata +46 -1
|
@@ -12,6 +12,7 @@ module Crawlscope
|
|
|
12
12
|
DEFAULT_TIMEOUT_SECONDS = 20
|
|
13
13
|
|
|
14
14
|
attr_writer :allowed_statuses, :base_url, :browser_factory, :concurrency, :fetch_executor, :network_idle_timeout_seconds, :output, :profile_token, :renderer, :rule_registry, :schema_registry, :scroll_page, :site_name, :sitemap_path, :timeout_seconds
|
|
15
|
+
attr_accessor :required_structured_data_types
|
|
15
16
|
|
|
16
17
|
def allowed_statuses
|
|
17
18
|
value = resolve(@allowed_statuses)
|
|
@@ -78,7 +79,7 @@ module Crawlscope
|
|
|
78
79
|
value = resolve(@rule_registry)
|
|
79
80
|
return value unless value.nil?
|
|
80
81
|
|
|
81
|
-
RuleRegistry.default(site_name: site_name)
|
|
82
|
+
RuleRegistry.default(site_name: site_name, required_types: required_structured_data_types)
|
|
82
83
|
end
|
|
83
84
|
|
|
84
85
|
def audit(base_url: self.base_url, sitemap_path: self.sitemap_path, rule_names: nil)
|
data/lib/crawlscope/crawl.rb
CHANGED
|
@@ -28,7 +28,10 @@ module Crawlscope
|
|
|
28
28
|
issues = IssueCollection.new
|
|
29
29
|
|
|
30
30
|
collect(pages, issues)
|
|
31
|
-
|
|
31
|
+
@target_resolver = Crawling::TargetResolver.new(
|
|
32
|
+
base_url: @base_url, pages: pages,
|
|
33
|
+
crawler: Crawler.new(page_fetcher: @page_fetcher, concurrency: @concurrency, fetch_executor: @fetch_executor)
|
|
34
|
+
)
|
|
32
35
|
scan(urls, pages, issues)
|
|
33
36
|
|
|
34
37
|
Result.new(
|
|
@@ -58,24 +61,13 @@ module Crawlscope
|
|
|
58
61
|
urls
|
|
59
62
|
end
|
|
60
63
|
|
|
61
|
-
def browser
|
|
62
|
-
Browser.new(
|
|
63
|
-
base_url: @base_url,
|
|
64
|
-
timeout_seconds: @timeout_seconds,
|
|
65
|
-
network_idle_timeout_seconds: @network_idle_timeout_seconds,
|
|
66
|
-
profile_token: @profile_token,
|
|
67
|
-
scroll_page: @scroll_page
|
|
68
|
-
)
|
|
69
|
-
rescue LoadError => error
|
|
70
|
-
raise ConfigurationError, "Browser rendering requires the ferrum gem (#{error.message})"
|
|
71
|
-
end
|
|
72
|
-
|
|
73
64
|
def page
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
65
|
+
Crawling::FetcherFactory.new(
|
|
66
|
+
base_url: @base_url, renderer: @renderer, timeout_seconds: @timeout_seconds,
|
|
67
|
+
browser_factory: @browser_factory, profile_token: @profile_token,
|
|
68
|
+
network_idle_timeout_seconds: @network_idle_timeout_seconds,
|
|
69
|
+
scroll_page: @scroll_page, adapter: http_adapter
|
|
70
|
+
).call
|
|
79
71
|
end
|
|
80
72
|
|
|
81
73
|
def http_adapter
|
|
@@ -119,20 +111,6 @@ module Crawlscope
|
|
|
119
111
|
end
|
|
120
112
|
end
|
|
121
113
|
|
|
122
|
-
def cache(pages)
|
|
123
|
-
@pages = {}
|
|
124
|
-
@targets = {}
|
|
125
|
-
|
|
126
|
-
pages.each do |page|
|
|
127
|
-
cache_page(page)
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def cache_page(page)
|
|
132
|
-
@pages[page.normalized_url] = page unless page.normalized_url.to_s.empty?
|
|
133
|
-
@pages[page.normalized_final_url] = page unless page.normalized_final_url.to_s.empty?
|
|
134
|
-
end
|
|
135
|
-
|
|
136
114
|
def scan(urls, pages, issues)
|
|
137
115
|
@rules.each do |rule|
|
|
138
116
|
rule.call(urls: urls, pages: pages, issues: issues, context: context)
|
|
@@ -144,53 +122,7 @@ module Crawlscope
|
|
|
144
122
|
end
|
|
145
123
|
|
|
146
124
|
def resolve_all(target_urls)
|
|
147
|
-
|
|
148
|
-
[target_url, Url.normalize(target_url, base_url: @base_url)]
|
|
149
|
-
end
|
|
150
|
-
normalized_urls = normalized_by_url.values.compact.uniq
|
|
151
|
-
missing_urls = []
|
|
152
|
-
|
|
153
|
-
normalized_urls.each do |normalized_url|
|
|
154
|
-
next if @targets.key?(normalized_url)
|
|
155
|
-
|
|
156
|
-
resolved = resolved_page(normalized_url)
|
|
157
|
-
if resolved
|
|
158
|
-
@targets[normalized_url] = resolved
|
|
159
|
-
else
|
|
160
|
-
missing_urls << normalized_url
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
fetched_pages(missing_urls).each do |page|
|
|
165
|
-
normalized_url = Url.normalize(page.url, base_url: @base_url)
|
|
166
|
-
cache_page(page)
|
|
167
|
-
@targets[normalized_url] = resolution(page, normalized_url, crawled: false)
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
normalized_by_url.to_h { |target_url, normalized_url| [target_url, @targets[normalized_url]] }
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
def fetched_pages(normalized_urls)
|
|
174
|
-
return [] if normalized_urls.empty?
|
|
175
|
-
|
|
176
|
-
Crawler.new(page_fetcher: @page_fetcher, concurrency: @concurrency, fetch_executor: @fetch_executor).call(normalized_urls)
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
def resolved_page(normalized_url)
|
|
180
|
-
page = @pages[normalized_url]
|
|
181
|
-
resolution(page, normalized_url, crawled: true) if page
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
def resolution(page, normalized_url, crawled:)
|
|
185
|
-
{
|
|
186
|
-
crawled: crawled,
|
|
187
|
-
doc: page.doc,
|
|
188
|
-
error: page.error,
|
|
189
|
-
final_url: page.normalized_final_url || normalized_url,
|
|
190
|
-
headers: page.headers,
|
|
191
|
-
html: page.html?,
|
|
192
|
-
status: page.status
|
|
193
|
-
}
|
|
125
|
+
@target_resolver.call(target_urls)
|
|
194
126
|
end
|
|
195
127
|
|
|
196
128
|
def redirected?(page)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module Crawling
|
|
5
|
+
class FetcherFactory
|
|
6
|
+
def initialize(base_url:, renderer:, timeout_seconds:, browser_factory: nil,
|
|
7
|
+
network_idle_timeout_seconds: Configuration::DEFAULT_BROWSER_NETWORK_IDLE_TIMEOUT_SECONDS,
|
|
8
|
+
scroll_page: Configuration::DEFAULT_BROWSER_SCROLL_PAGE, profile_token: nil, adapter: nil)
|
|
9
|
+
@base_url = base_url
|
|
10
|
+
@renderer = renderer
|
|
11
|
+
@timeout_seconds = timeout_seconds
|
|
12
|
+
@browser_factory = browser_factory
|
|
13
|
+
@network_idle_timeout_seconds = network_idle_timeout_seconds
|
|
14
|
+
@scroll_page = scroll_page
|
|
15
|
+
@profile_token = profile_token
|
|
16
|
+
@adapter = adapter
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call
|
|
20
|
+
return Http.new(base_url: @base_url, timeout_seconds: @timeout_seconds, adapter: @adapter, profile_token: @profile_token) unless @renderer == :browser
|
|
21
|
+
return @browser_factory.call if @browser_factory
|
|
22
|
+
|
|
23
|
+
Browser.new(base_url: @base_url, timeout_seconds: @timeout_seconds,
|
|
24
|
+
network_idle_timeout_seconds: @network_idle_timeout_seconds,
|
|
25
|
+
scroll_page: @scroll_page, profile_token: @profile_token)
|
|
26
|
+
rescue LoadError => error
|
|
27
|
+
raise ConfigurationError, "Browser rendering requires the ferrum gem (#{error.message})"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module Crawling
|
|
5
|
+
Target = Data.define(:target_url, :resolution) do
|
|
6
|
+
def allowed?(statuses)
|
|
7
|
+
statuses.include?(status)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def final_path
|
|
11
|
+
Url.path(final_url)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def final_url
|
|
15
|
+
value = resolution[:final_url].to_s
|
|
16
|
+
value.empty? ? target_url : value
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def ignored_error?
|
|
20
|
+
resolution && status.nil? && resolution[:crawled] && resolution[:error]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def html?
|
|
24
|
+
resolution && resolution[:html]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def noindex?
|
|
28
|
+
Documents::Robots.noindex_header?(resolution[:headers] || {}) ||
|
|
29
|
+
Documents::Robots.noindex_meta?(resolution[:doc])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def status
|
|
33
|
+
resolution && resolution[:status]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def redirect?
|
|
37
|
+
(status && (300..399).cover?(status.to_i)) || final_url != target_url
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def unresolved?
|
|
41
|
+
resolution.nil? || (status.nil? && !ignored_error?)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module Crawling
|
|
5
|
+
class TargetResolver
|
|
6
|
+
def initialize(base_url:, crawler:, pages:)
|
|
7
|
+
@base_url = base_url
|
|
8
|
+
@crawler = crawler
|
|
9
|
+
@pages = {}
|
|
10
|
+
@targets = {}
|
|
11
|
+
|
|
12
|
+
pages.each do |page|
|
|
13
|
+
cache_page(page)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call(target_urls)
|
|
18
|
+
normalized_by_url = Array(target_urls).to_h do |target_url|
|
|
19
|
+
[target_url, Url.normalize(target_url, base_url: @base_url)]
|
|
20
|
+
end
|
|
21
|
+
normalized_urls = normalized_by_url.values.compact.uniq
|
|
22
|
+
missing_urls = []
|
|
23
|
+
|
|
24
|
+
normalized_urls.each do |normalized_url|
|
|
25
|
+
next if @targets.key?(normalized_url)
|
|
26
|
+
|
|
27
|
+
resolved = resolved_page(normalized_url)
|
|
28
|
+
if resolved
|
|
29
|
+
@targets[normalized_url] = resolved
|
|
30
|
+
else
|
|
31
|
+
missing_urls << normalized_url
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
fetched_pages(missing_urls).each do |page|
|
|
36
|
+
normalized_url = Url.normalize(page.url, base_url: @base_url)
|
|
37
|
+
cache_page(page)
|
|
38
|
+
@targets[normalized_url] = resolution(page, normalized_url, crawled: false)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
normalized_by_url.to_h { |target_url, normalized_url| [target_url, @targets[normalized_url]] }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def cache_page(page)
|
|
47
|
+
@pages[page.normalized_url] = page unless page.normalized_url.to_s.empty?
|
|
48
|
+
@pages[page.normalized_final_url] = page unless page.normalized_final_url.to_s.empty?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def fetched_pages(normalized_urls)
|
|
52
|
+
return [] if normalized_urls.empty?
|
|
53
|
+
|
|
54
|
+
@crawler.call(normalized_urls)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def resolved_page(normalized_url)
|
|
58
|
+
page = @pages[normalized_url]
|
|
59
|
+
resolution(page, normalized_url, crawled: true) if page
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def resolution(page, normalized_url, crawled:)
|
|
63
|
+
{
|
|
64
|
+
crawled: crawled,
|
|
65
|
+
doc: page.doc,
|
|
66
|
+
error: page.error,
|
|
67
|
+
final_url: page.normalized_final_url || normalized_url,
|
|
68
|
+
headers: page.headers,
|
|
69
|
+
html: page.html?,
|
|
70
|
+
status: page.status
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module Documents
|
|
5
|
+
class Canonical
|
|
6
|
+
attr_reader :href
|
|
7
|
+
|
|
8
|
+
def initialize(doc:, base_url:)
|
|
9
|
+
@href = doc&.at_css('link[rel="canonical"]')&.[]("href").to_s.strip
|
|
10
|
+
@base_url = base_url
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def present?
|
|
14
|
+
!href.empty?
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def url
|
|
18
|
+
Url.normalize(href, base_url: @base_url) if present?
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Crawlscope
|
|
4
|
+
module Documents
|
|
5
|
+
class Robots
|
|
6
|
+
ROBOTS_META_SELECTOR = 'meta[name="robots"], meta[name="googlebot"]'
|
|
7
|
+
X_ROBOTS_TAG_HEADER = "x-robots-tag"
|
|
8
|
+
|
|
9
|
+
def self.noindex_header?(headers)
|
|
10
|
+
noindex?(header_value(headers, X_ROBOTS_TAG_HEADER))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.noindex_meta?(doc)
|
|
14
|
+
return false unless doc
|
|
15
|
+
|
|
16
|
+
doc.css(ROBOTS_META_SELECTOR).any? { |tag| noindex?(tag["content"].to_s) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.header_value(headers, name)
|
|
20
|
+
headers.find { |key, _value| key.to_s.casecmp?(name) }&.last.to_s
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.directives(value)
|
|
24
|
+
value
|
|
25
|
+
.split(",")
|
|
26
|
+
.map { |directive| directive.split(":", 2).last.to_s.strip }
|
|
27
|
+
.reject(&:empty?)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.noindex?(value)
|
|
31
|
+
new(value).noindex?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def initialize(value)
|
|
35
|
+
@directives = self.class.directives(value)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def noindex?
|
|
39
|
+
include?("noindex") || include?("none")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def nofollow?
|
|
43
|
+
include?("nofollow") || include?("none")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def follow?
|
|
47
|
+
include?("follow")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def include?(value)
|
|
53
|
+
@directives.any? { |directive| directive.casecmp?(value) }
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
data/lib/crawlscope/http.rb
CHANGED
|
@@ -9,12 +9,14 @@ module Crawlscope
|
|
|
9
9
|
class Http
|
|
10
10
|
MAX_REDIRECTS = 5
|
|
11
11
|
USER_AGENT = "Mozilla/5.0 (compatible; Crawlscope/1.0)"
|
|
12
|
+
BodyLimitExceeded = Class.new(Error)
|
|
12
13
|
|
|
13
|
-
def initialize(base_url:, timeout_seconds:, adapter: nil, profile_token: nil)
|
|
14
|
+
def initialize(base_url:, timeout_seconds:, adapter: nil, profile_token: nil, follow_redirects: true)
|
|
14
15
|
@base_url = base_url
|
|
15
16
|
@timeout_seconds = timeout_seconds
|
|
16
17
|
@adapter = adapter
|
|
17
18
|
@profile_token = profile_token
|
|
19
|
+
@follow_redirects = follow_redirects
|
|
18
20
|
@connections_by_thread = Concurrent::Map.new
|
|
19
21
|
end
|
|
20
22
|
|
|
@@ -26,9 +28,18 @@ module Crawlscope
|
|
|
26
28
|
@connections_by_thread.clear
|
|
27
29
|
end
|
|
28
30
|
|
|
29
|
-
def fetch(url)
|
|
31
|
+
def fetch(url, max_body_bytes: nil, parse_html: true)
|
|
32
|
+
streamed_body = +"".b if max_body_bytes
|
|
30
33
|
response = connection.get(url) do |request|
|
|
31
34
|
request.headers["User-Agent"] = USER_AGENT
|
|
35
|
+
if max_body_bytes
|
|
36
|
+
request.options.on_data = lambda do |chunk, size, _env|
|
|
37
|
+
raise BodyLimitExceeded, "Response exceeded its byte limit" if size > max_body_bytes
|
|
38
|
+
|
|
39
|
+
streamed_body.clear if size == chunk.bytesize
|
|
40
|
+
streamed_body << chunk
|
|
41
|
+
end
|
|
42
|
+
end
|
|
32
43
|
RequestHeaders.add_profile_token(
|
|
33
44
|
request.headers,
|
|
34
45
|
url: url,
|
|
@@ -40,9 +51,9 @@ module Crawlscope
|
|
|
40
51
|
final_url = response.env.url.to_s
|
|
41
52
|
final_url = url if final_url.empty?
|
|
42
53
|
headers = response.headers.to_h
|
|
43
|
-
body = response.body.to_s
|
|
44
|
-
doc = if response.status == 200 && html_response?(headers)
|
|
45
|
-
Nokogiri::HTML(body)
|
|
54
|
+
body = streamed_body || response.body.to_s
|
|
55
|
+
doc = if parse_html && response.status == 200 && html_response?(headers)
|
|
56
|
+
Nokogiri::HTML(body, nil, streamed_body ? charset(headers) : nil)
|
|
46
57
|
end
|
|
47
58
|
|
|
48
59
|
Page.new(
|
|
@@ -55,7 +66,7 @@ module Crawlscope
|
|
|
55
66
|
body: body,
|
|
56
67
|
doc: doc
|
|
57
68
|
)
|
|
58
|
-
rescue Faraday::Error, SocketError, SystemCallError, Timeout::Error => error
|
|
69
|
+
rescue Faraday::Error, SocketError, SystemCallError, Timeout::Error, BodyLimitExceeded => error
|
|
59
70
|
Page.new(
|
|
60
71
|
url: url,
|
|
61
72
|
normalized_url: Url.normalize(url, base_url: @base_url),
|
|
@@ -74,11 +85,13 @@ module Crawlscope
|
|
|
74
85
|
def connection
|
|
75
86
|
@connections_by_thread.compute_if_absent(Thread.current.object_id) do
|
|
76
87
|
Faraday.new do |faraday|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
if @follow_redirects
|
|
89
|
+
faraday.response(
|
|
90
|
+
:follow_redirects,
|
|
91
|
+
limit: MAX_REDIRECTS,
|
|
92
|
+
callback: RequestHeaders.method(:strip_profile_token_on_cross_origin_redirect)
|
|
93
|
+
)
|
|
94
|
+
end
|
|
82
95
|
faraday.options.timeout = @timeout_seconds
|
|
83
96
|
faraday.options.open_timeout = @timeout_seconds
|
|
84
97
|
faraday.adapter @adapter if @adapter
|
|
@@ -87,8 +100,19 @@ module Crawlscope
|
|
|
87
100
|
end
|
|
88
101
|
|
|
89
102
|
def html_response?(headers)
|
|
90
|
-
|
|
91
|
-
|
|
103
|
+
type = content_type(headers)
|
|
104
|
+
type.empty? || type.include?("text/html")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def charset(headers)
|
|
108
|
+
label = content_type(headers)[/;\s*charset\s*=\s*["']?([\w.-]+)/i, 1]
|
|
109
|
+
Encoding.find(label).name if label
|
|
110
|
+
rescue ArgumentError
|
|
111
|
+
nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def content_type(headers)
|
|
115
|
+
headers["content-type"] || headers.find { |key, _value| key.to_s.casecmp("content-type").zero? }&.last.to_s
|
|
92
116
|
end
|
|
93
117
|
end
|
|
94
118
|
end
|