Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add NewDocumentWithTimeout #240

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package goquery

import (
"errors"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
"io"
"net"
"net/http"
"net/url"

"github.com/andybalholm/cascadia"

"golang.org/x/net/html"
"time"
)

// Document represents an HTML document to be manipulated. Unlike jQuery, which
Expand Down Expand Up @@ -40,6 +40,34 @@ func NewDocument(url string) (*Document, error) {
return NewDocumentFromResponse(res)
}

func NewDocumentWithTimeout(url string, timeout time.Duration) (*Document, error) {
// Load the URL
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: timeout,
}).Dial,
TLSHandshakeTimeout: 60 * time.Second,
}
client := &http.Client{
Timeout: timeout,
Transport: netTransport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
req, e := http.NewRequest(http.MethodGet, url, nil)
if e != nil {
return nil, e
}

res, e := client.Do(req)
//res, e := http.Get(url)
if e != nil {
return nil, e
}
return NewDocumentFromResponse(res)
}

// NewDocumentFromReader returns a Document from a generic reader.
// It returns an error as second value if the reader's data cannot be parsed
// as html. It does *not* check if the reader is also an io.Closer, so the
Expand Down