Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
crawl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
crawl
Commits
6f5bef5f
Commit
6f5bef5f
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Use a global http.Client with sane settings
parent
979f2e8d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
client.go
+31
-0
31 additions, 0 deletions
client.go
cmd/crawl/crawl.go
+16
-8
16 additions, 8 deletions
cmd/crawl/crawl.go
with
47 additions
and
8 deletions
client.go
0 → 100644
+
31
−
0
View file @
6f5bef5f
package
crawl
import
(
"crypto/tls"
"net/http"
"net/http/cookiejar"
"time"
)
var
defaultClientTimeout
=
60
*
time
.
Second
var
DefaultClient
*
http
.
Client
// DefaultClient returns a http.Client suitable for crawling: does not
// follow redirects, accepts invalid TLS certificates, sets a
// reasonable timeout for requests.
func
init
()
{
jar
,
_
:=
cookiejar
.
New
(
nil
)
DefaultClient
=
&
http
.
Client
{
Timeout
:
defaultClientTimeout
,
Transport
:
&
http
.
Transport
{
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
true
,
},
},
CheckRedirect
:
func
(
req
*
http
.
Request
,
via
[]
*
http
.
Request
)
error
{
return
http
.
ErrUseLastResponse
},
Jar
:
jar
,
}
}
This diff is collapsed.
Click to expand it.
cmd/crawl/crawl.go
+
16
−
8
View file @
6f5bef5f
...
...
@@ -11,6 +11,7 @@ import (
"log"
"net/http"
"os"
"runtime/pprof"
"strconv"
"strings"
"sync"
...
...
@@ -30,6 +31,8 @@ var (
validSchemes
=
flag
.
String
(
"schemes"
,
"http,https"
,
"comma-separated list of allowed protocols"
)
alwaysIncludeRelated
=
flag
.
Bool
(
"include-related"
,
false
,
"always include related resources (css, images, etc)"
)
outputFile
=
flag
.
String
(
"output"
,
"crawl.warc.gz"
,
"output WARC file"
)
cpuprofile
=
flag
.
String
(
"cpuprofile"
,
""
,
"create cpu profile"
)
)
func
extractLinks
(
c
*
crawl
.
Crawler
,
u
string
,
depth
int
,
resp
*
http
.
Response
,
err
error
)
error
{
...
...
@@ -147,14 +150,10 @@ func (c *crawlStats) Dump() {
fmt
.
Fprintf
(
os
.
Stderr
,
"stats: downloaded %d bytes (%.4g KB/s), status: %v
\n
"
,
c
.
bytes
,
rate
,
c
.
states
)
}
var
(
stats
*
crawlStats
client
*
http
.
Client
)
var
stats
*
crawlStats
func
fetch
(
urlstr
string
)
(
*
http
.
Response
,
error
)
{
resp
,
err
:=
client
.
Get
(
urlstr
)
resp
,
err
:=
c
rawl
.
DefaultC
lient
.
Get
(
urlstr
)
if
err
==
nil
{
stats
.
Update
(
resp
)
}
...
...
@@ -162,8 +161,6 @@ func fetch(urlstr string) (*http.Response, error) {
}
func
init
()
{
client
=
&
http
.
Client
{}
stats
=
&
crawlStats
{
states
:
make
(
map
[
int
]
int
),
start
:
time
.
Now
(),
...
...
@@ -191,6 +188,17 @@ func (b *byteCounter) Read(buf []byte) (int, error) {
func
main
()
{
flag
.
Parse
()
if
*
cpuprofile
!=
""
{
f
,
err
:=
os
.
Create
(
*
cpuprofile
)
if
err
!=
nil
{
log
.
Fatal
(
"could not create CPU profile: "
,
err
)
}
if
err
:=
pprof
.
StartCPUProfile
(
f
);
err
!=
nil
{
log
.
Fatal
(
"could not start CPU profile: "
,
err
)
}
defer
pprof
.
StopCPUProfile
()
}
outf
,
err
:=
os
.
Create
(
*
outputFile
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment