Request and response moved to http package

This commit is contained in:
Musab Gültekin 2019-06-29 13:36:39 +03:00
parent 59757607eb
commit 1e109c555d
7 changed files with 63 additions and 62 deletions

View File

@ -27,7 +27,7 @@ Simple usage
```go ```go
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://api.ipify.org"}, StartURLs: []string{"http://api.ipify.org"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
}, },
}).Start() }).Start()
@ -44,7 +44,7 @@ func main() {
}).Start() }).Start()
} }
func quotesParse(g *geziyor.Geziyor, r *geziyor.Response) { func quotesParse(g *geziyor.Geziyor, r *http.Response) {
r.HTMLDoc.Find("div.quote").Each(func(i int, s *goquery.Selection) { r.HTMLDoc.Find("div.quote").Each(func(i int, s *goquery.Selection) {
g.Exports <- map[string]interface{}{ g.Exports <- map[string]interface{}{
"text": s.Find("span.text").Text(), "text": s.Find("span.text").Text(),
@ -78,7 +78,7 @@ After reading response, ```ParseFunc func(g *Geziyor, r *Response)``` called.
```go ```go
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://api.ipify.org"}, StartURLs: []string{"http://api.ipify.org"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
}, },
}).Start() }).Start()
@ -95,7 +95,7 @@ geziyor.NewGeziyor(&geziyor.Options{
g.GetRendered("https://httpbin.org/anything", g.Opt.ParseFunc) g.GetRendered("https://httpbin.org/anything", g.Opt.ParseFunc)
g.Head("https://httpbin.org/anything", g.Opt.ParseFunc) g.Head("https://httpbin.org/anything", g.Opt.ParseFunc)
}, },
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
}, },
}).Start() }).Start()
@ -130,7 +130,7 @@ If response isn't HTML, ```response.HTMLDoc``` would be ```nil```.
```go ```go
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://quotes.toscrape.com/"}, StartURLs: []string{"http://quotes.toscrape.com/"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
r.HTMLDoc.Find("div.quote").Each(func(_ int, s *goquery.Selection) { r.HTMLDoc.Find("div.quote").Each(func(_ int, s *goquery.Selection) {
log.Println(s.Find("span.text").Text(), s.Find("small.author").Text()) log.Println(s.Find("span.text").Text(), s.Find("small.author").Text())
}) })
@ -146,7 +146,7 @@ You can export data automatically using exporters. Just send data to ```Geziyor.
```go ```go
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://quotes.toscrape.com/"}, StartURLs: []string{"http://quotes.toscrape.com/"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
r.HTMLDoc.Find("div.quote").Each(func(_ int, s *goquery.Selection) { r.HTMLDoc.Find("div.quote").Each(func(_ int, s *goquery.Selection) {
g.Exports <- map[string]interface{}{ g.Exports <- map[string]interface{}{
"text": s.Find("span.text").Text(), "text": s.Find("span.text").Text(),

View File

@ -149,39 +149,39 @@ func (g *Geziyor) Start() {
} }
// Get issues a GET to the specified URL. // Get issues a GET to the specified URL.
func (g *Geziyor) Get(url string, callback func(g *Geziyor, r *Response)) { func (g *Geziyor) Get(url string, callback func(g *Geziyor, r *http.Response)) {
req, err := stdhttp.NewRequest("GET", url, nil) req, err := stdhttp.NewRequest("GET", url, nil)
if err != nil { if err != nil {
log.Printf("Request creating error %v\n", err) log.Printf("Request creating error %v\n", err)
return return
} }
g.Do(&Request{Request: req}, callback) g.Do(&http.Request{Request: req}, callback)
} }
// GetRendered issues GET request using headless browser // GetRendered issues GET request using headless browser
// Opens up a new Chrome instance, makes request, waits for 1 second to render HTML DOM and closed. // Opens up a new Chrome instance, makes request, waits for 1 second to render HTML DOM and closed.
// Rendered requests only supported for GET requests. // Rendered requests only supported for GET requests.
func (g *Geziyor) GetRendered(url string, callback func(g *Geziyor, r *Response)) { func (g *Geziyor) GetRendered(url string, callback func(g *Geziyor, r *http.Response)) {
req, err := stdhttp.NewRequest("GET", url, nil) req, err := stdhttp.NewRequest("GET", url, nil)
if err != nil { if err != nil {
log.Printf("Request creating error %v\n", err) log.Printf("Request creating error %v\n", err)
return return
} }
g.Do(&Request{Request: req, Rendered: true}, callback) g.Do(&http.Request{Request: req, Rendered: true}, callback)
} }
// Head issues a HEAD to the specified URL // Head issues a HEAD to the specified URL
func (g *Geziyor) Head(url string, callback func(g *Geziyor, r *Response)) { func (g *Geziyor) Head(url string, callback func(g *Geziyor, r *http.Response)) {
req, err := stdhttp.NewRequest("HEAD", url, nil) req, err := stdhttp.NewRequest("HEAD", url, nil)
if err != nil { if err != nil {
log.Printf("Request creating error %v\n", err) log.Printf("Request creating error %v\n", err)
return return
} }
g.Do(&Request{Request: req}, callback) g.Do(&http.Request{Request: req}, callback)
} }
// Do sends an HTTP request // Do sends an HTTP request
func (g *Geziyor) Do(req *Request, callback func(g *Geziyor, r *Response)) { func (g *Geziyor) Do(req *http.Request, callback func(g *Geziyor, r *http.Response)) {
if req.Synchronized { if req.Synchronized {
g.do(req, callback) g.do(req, callback)
} else { } else {
@ -191,7 +191,7 @@ func (g *Geziyor) Do(req *Request, callback func(g *Geziyor, r *Response)) {
} }
// Do sends an HTTP request // Do sends an HTTP request
func (g *Geziyor) do(req *Request, callback func(g *Geziyor, r *Response)) { func (g *Geziyor) do(req *http.Request, callback func(g *Geziyor, r *http.Response)) {
g.acquireSem(req) g.acquireSem(req)
defer g.releaseSem(req) defer g.releaseSem(req)
if !req.Synchronized { if !req.Synchronized {
@ -201,13 +201,13 @@ func (g *Geziyor) do(req *Request, callback func(g *Geziyor, r *Response)) {
for _, middlewareFunc := range g.requestMiddlewares { for _, middlewareFunc := range g.requestMiddlewares {
middlewareFunc(g, req) middlewareFunc(g, req)
if req.cancelled { if req.Cancelled {
return return
} }
} }
// Do request normal or Chrome and read response // Do request normal or Chrome and read response
var response *Response var response *http.Response
var err error var err error
if !req.Rendered { if !req.Rendered {
response, err = g.doRequestClient(req) response, err = g.doRequestClient(req)
@ -233,7 +233,7 @@ func (g *Geziyor) do(req *Request, callback func(g *Geziyor, r *Response)) {
} }
} }
func (g *Geziyor) doRequestClient(req *Request) (*Response, error) { func (g *Geziyor) doRequestClient(req *http.Request) (*http.Response, error) {
// Do request // Do request
resp, err := g.Client.Do(req.Request) resp, err := g.Client.Do(req.Request)
@ -260,7 +260,7 @@ func (g *Geziyor) doRequestClient(req *Request) (*Response, error) {
return nil, errors.Wrap(err, "Reading body error") return nil, errors.Wrap(err, "Reading body error")
} }
response := Response{ response := http.Response{
Response: resp, Response: resp,
Body: body, Body: body,
Meta: req.Meta, Meta: req.Meta,
@ -270,7 +270,7 @@ func (g *Geziyor) doRequestClient(req *Request) (*Response, error) {
return &response, nil return &response, nil
} }
func (g *Geziyor) doRequestChrome(req *Request) (*Response, error) { func (g *Geziyor) doRequestChrome(req *http.Request) (*http.Response, error) {
var body string var body string
var reqID network.RequestID var reqID network.RequestID
var res *network.Response var res *network.Response
@ -317,7 +317,7 @@ func (g *Geziyor) doRequestChrome(req *Request) (*Response, error) {
// Set new URL in case of redirection // Set new URL in case of redirection
req.URL, _ = url.Parse(res.URL) req.URL, _ = url.Parse(res.URL)
response := Response{ response := http.Response{
Response: &stdhttp.Response{ Response: &stdhttp.Response{
Request: req.Request, Request: req.Request,
StatusCode: int(res.Status), StatusCode: int(res.Status),
@ -331,7 +331,7 @@ func (g *Geziyor) doRequestChrome(req *Request) (*Response, error) {
return &response, nil return &response, nil
} }
func (g *Geziyor) acquireSem(req *Request) { func (g *Geziyor) acquireSem(req *http.Request) {
if g.Opt.ConcurrentRequests != 0 { if g.Opt.ConcurrentRequests != 0 {
g.semGlobal <- struct{}{} g.semGlobal <- struct{}{}
} }
@ -349,7 +349,7 @@ func (g *Geziyor) acquireSem(req *Request) {
} }
} }
func (g *Geziyor) releaseSem(req *Request) { func (g *Geziyor) releaseSem(req *http.Request) {
if g.Opt.ConcurrentRequests != 0 { if g.Opt.ConcurrentRequests != 0 {
<-g.semGlobal <-g.semGlobal
} }

View File

@ -8,9 +8,9 @@ import (
"github.com/geziyor/geziyor" "github.com/geziyor/geziyor"
"github.com/geziyor/geziyor/exporter" "github.com/geziyor/geziyor/exporter"
"github.com/geziyor/geziyor/extractor" "github.com/geziyor/geziyor/extractor"
http2 "github.com/geziyor/geziyor/http" "github.com/geziyor/geziyor/http"
"github.com/geziyor/geziyor/metrics" "github.com/geziyor/geziyor/metrics"
"net/http" httpstd "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"unicode/utf8" "unicode/utf8"
@ -20,7 +20,7 @@ func TestSimple(t *testing.T) {
defer leaktest.Check(t)() defer leaktest.Check(t)()
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://api.ipify.org"}, StartURLs: []string{"http://api.ipify.org"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
}, },
}).Start() }).Start()
@ -31,7 +31,7 @@ func TestSimpleCache(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://api.ipify.org"}, StartURLs: []string{"http://api.ipify.org"},
Cache: httpcache.NewMemoryCache(), Cache: httpcache.NewMemoryCache(),
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
g.Exports <- string(r.Body) g.Exports <- string(r.Body)
g.Get("http://api.ipify.org", nil) g.Get("http://api.ipify.org", nil)
@ -48,7 +48,7 @@ func TestQuotes(t *testing.T) {
}).Start() }).Start()
} }
func quotesParse(g *geziyor.Geziyor, r *geziyor.Response) { func quotesParse(g *geziyor.Geziyor, r *http.Response) {
r.HTMLDoc.Find("div.quote").Each(func(i int, s *goquery.Selection) { r.HTMLDoc.Find("div.quote").Each(func(i int, s *goquery.Selection) {
// Export Data // Export Data
g.Exports <- map[string]interface{}{ g.Exports <- map[string]interface{}{
@ -73,7 +73,7 @@ func TestAllLinks(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
AllowedDomains: []string{"books.toscrape.com"}, AllowedDomains: []string{"books.toscrape.com"},
StartURLs: []string{"http://books.toscrape.com/"}, StartURLs: []string{"http://books.toscrape.com/"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
g.Exports <- []string{r.Request.URL.String()} g.Exports <- []string{r.Request.URL.String()}
r.HTMLDoc.Find("a").Each(func(i int, s *goquery.Selection) { r.HTMLDoc.Find("a").Each(func(i int, s *goquery.Selection) {
if href, ok := s.Attr("href"); ok { if href, ok := s.Attr("href"); ok {
@ -91,7 +91,7 @@ func TestStartRequestsFunc(t *testing.T) {
StartRequestsFunc: func(g *geziyor.Geziyor) { StartRequestsFunc: func(g *geziyor.Geziyor) {
g.Get("http://quotes.toscrape.com/", g.Opt.ParseFunc) g.Get("http://quotes.toscrape.com/", g.Opt.ParseFunc)
}, },
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
r.HTMLDoc.Find("a").Each(func(_ int, s *goquery.Selection) { r.HTMLDoc.Find("a").Each(func(_ int, s *goquery.Selection) {
g.Exports <- s.AttrOr("href", "") g.Exports <- s.AttrOr("href", "")
}) })
@ -105,7 +105,7 @@ func TestGetRendered(t *testing.T) {
StartRequestsFunc: func(g *geziyor.Geziyor) { StartRequestsFunc: func(g *geziyor.Geziyor) {
g.GetRendered("https://httpbin.org/anything", g.Opt.ParseFunc) g.GetRendered("https://httpbin.org/anything", g.Opt.ParseFunc)
}, },
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
fmt.Println(r.Header) fmt.Println(r.Header)
}, },
@ -118,7 +118,7 @@ func TestHEADRequest(t *testing.T) {
StartRequestsFunc: func(g *geziyor.Geziyor) { StartRequestsFunc: func(g *geziyor.Geziyor) {
g.Head("https://httpbin.org/anything", g.Opt.ParseFunc) g.Head("https://httpbin.org/anything", g.Opt.ParseFunc)
}, },
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
fmt.Println(string(r.Body)) fmt.Println(string(r.Body))
}, },
}).Start() }).Start()
@ -127,8 +127,8 @@ func TestHEADRequest(t *testing.T) {
func TestCookies(t *testing.T) { func TestCookies(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://quotes.toscrape.com/login"}, StartURLs: []string{"http://quotes.toscrape.com/login"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
if len(g.Client.Cookies("http://quotes.toscrape.com/login")) == 0 { if len(g.Client.Cookies(r.Request.URL.String())) == 0 {
t.Fatal("Cookies is Empty") t.Fatal("Cookies is Empty")
} }
}, },
@ -136,8 +136,8 @@ func TestCookies(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{"http://quotes.toscrape.com/login"}, StartURLs: []string{"http://quotes.toscrape.com/login"},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
if len(g.Client.Cookies("http://quotes.toscrape.com/login")) != 0 { if len(g.Client.Cookies(r.Request.URL.String())) != 0 {
t.Fatal("Cookies exist") t.Fatal("Cookies exist")
} }
}, },
@ -148,7 +148,7 @@ func TestCookies(t *testing.T) {
func TestBasicAuth(t *testing.T) { func TestBasicAuth(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartRequestsFunc: func(g *geziyor.Geziyor) { StartRequestsFunc: func(g *geziyor.Geziyor) {
req, _ := geziyor.NewRequest("GET", "https://httpbin.org/anything", nil) req, _ := http.NewRequest("GET", "https://httpbin.org/anything", nil)
req.SetBasicAuth("username", "password") req.SetBasicAuth("username", "password")
g.Do(req, nil) g.Do(req, nil)
}, },
@ -170,14 +170,14 @@ func TestExtractor(t *testing.T) {
} }
func TestCharsetDetection(t *testing.T) { func TestCharsetDetection(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(httpstd.HandlerFunc(func(w httpstd.ResponseWriter, r *httpstd.Request) {
fmt.Fprint(w, "\xf0ültekin") fmt.Fprint(w, "\xf0ültekin")
})) }))
defer ts.Close() defer ts.Close()
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{ts.URL}, StartURLs: []string{ts.URL},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
if !utf8.Valid(r.Body) { if !utf8.Valid(r.Body) {
t.Fatal() t.Fatal()
} }
@ -187,7 +187,7 @@ func TestCharsetDetection(t *testing.T) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartURLs: []string{ts.URL}, StartURLs: []string{ts.URL},
ParseFunc: func(g *geziyor.Geziyor, r *geziyor.Response) { ParseFunc: func(g *geziyor.Geziyor, r *http.Response) {
if utf8.Valid(r.Body) { if utf8.Valid(r.Body) {
t.Fatal() t.Fatal()
} }
@ -200,10 +200,10 @@ func TestCharsetDetection(t *testing.T) {
func BenchmarkGeziyor_Do(b *testing.B) { func BenchmarkGeziyor_Do(b *testing.B) {
// Create Server // Create Server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(httpstd.HandlerFunc(func(w httpstd.ResponseWriter, r *httpstd.Request) {
fmt.Fprint(w, "Hello, client") fmt.Fprint(w, "Hello, client")
})) }))
ts.Client().Transport = http2.NewClient().Transport ts.Client().Transport = http.NewClient().Transport
defer ts.Close() defer ts.Close()
// As we don't benchmark creating a server, reset timer. // As we don't benchmark creating a server, reset timer.
@ -212,7 +212,7 @@ func BenchmarkGeziyor_Do(b *testing.B) {
geziyor.NewGeziyor(&geziyor.Options{ geziyor.NewGeziyor(&geziyor.Options{
StartRequestsFunc: func(g *geziyor.Geziyor) { StartRequestsFunc: func(g *geziyor.Geziyor) {
// Create Synchronized request to benchmark requests accurately. // Create Synchronized request to benchmark requests accurately.
req, _ := geziyor.NewRequest("GET", ts.URL, nil) req, _ := http.NewRequest("GET", ts.URL, nil)
req.Synchronized = true req.Synchronized = true
// We only bench here ! // We only bench here !

View File

@ -1,4 +1,4 @@
package geziyor package http
import ( import (
"io" "io"
@ -11,13 +11,12 @@ type Request struct {
Meta map[string]interface{} Meta map[string]interface{}
Synchronized bool Synchronized bool
Rendered bool Rendered bool
Cancelled bool
cancelled bool
} }
// Cancel request // Cancel request
func (r *Request) Cancel() { func (r *Request) Cancel() {
r.cancelled = true r.Cancelled = true
} }
// NewRequest returns a new Request given a method, URL, and optional body. // NewRequest returns a new Request given a method, URL, and optional body.

View File

@ -1,4 +1,4 @@
package geziyor package http
import ( import (
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
@ -28,7 +28,8 @@ func (r *Response) JoinURL(relativeURL string) string {
return joinedURL.String() return joinedURL.String()
} }
func (r *Response) isHTML() bool { // IsHTML checks if response content is HTML by looking to content-type header
func (r *Response) IsHTML() bool {
contentType := r.Header.Get("Content-Type") contentType := r.Header.Get("Content-Type")
for _, htmlContentType := range []string{"text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml"} { for _, htmlContentType := range []string{"text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml"} {
if strings.Contains(contentType, htmlContentType) { if strings.Contains(contentType, htmlContentType) {

View File

@ -16,10 +16,10 @@ import (
// RequestMiddleware called before requests made. // RequestMiddleware called before requests made.
// Set request.Cancelled = true to cancel request // Set request.Cancelled = true to cancel request
type RequestMiddleware func(g *Geziyor, r *Request) type RequestMiddleware func(g *Geziyor, r *http.Request)
// ResponseMiddleware called after request response receive // ResponseMiddleware called after request response receive
type ResponseMiddleware func(g *Geziyor, r *Response) type ResponseMiddleware func(g *Geziyor, r *http.Response)
func init() { func init() {
log.SetOutput(os.Stdout) log.SetOutput(os.Stdout)
@ -28,7 +28,7 @@ func init() {
// recoverMiddleware recovers scraping being crashed. // recoverMiddleware recovers scraping being crashed.
// Logs error and stack trace // Logs error and stack trace
func recoverMiddleware(g *Geziyor, r *Request) { func recoverMiddleware(g *Geziyor, r *http.Request) {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Println(r, string(debug.Stack())) log.Println(r, string(debug.Stack()))
g.metrics.PanicCounter.Add(1) g.metrics.PanicCounter.Add(1)
@ -36,7 +36,7 @@ func recoverMiddleware(g *Geziyor, r *Request) {
} }
// allowedDomainsMiddleware checks for request host if it exists in AllowedDomains // allowedDomainsMiddleware checks for request host if it exists in AllowedDomains
func allowedDomainsMiddleware(g *Geziyor, r *Request) { func allowedDomainsMiddleware(g *Geziyor, r *http.Request) {
if len(g.Opt.AllowedDomains) != 0 && !internal.Contains(g.Opt.AllowedDomains, r.Host) { if len(g.Opt.AllowedDomains) != 0 && !internal.Contains(g.Opt.AllowedDomains, r.Host) {
//log.Printf("Domain not allowed: %s\n", req.Host) //log.Printf("Domain not allowed: %s\n", req.Host)
r.Cancel() r.Cancel()
@ -45,7 +45,7 @@ func allowedDomainsMiddleware(g *Geziyor, r *Request) {
} }
// duplicateRequestsMiddleware checks for already visited URLs // duplicateRequestsMiddleware checks for already visited URLs
func duplicateRequestsMiddleware(g *Geziyor, r *Request) { func duplicateRequestsMiddleware(g *Geziyor, r *http.Request) {
if !g.Opt.URLRevisitEnabled { if !g.Opt.URLRevisitEnabled {
key := r.Request.URL.String() + r.Request.Method key := r.Request.URL.String() + r.Request.Method
if _, visited := g.visitedURLs.LoadOrStore(key, struct{}{}); visited { if _, visited := g.visitedURLs.LoadOrStore(key, struct{}{}); visited {
@ -56,7 +56,7 @@ func duplicateRequestsMiddleware(g *Geziyor, r *Request) {
} }
// defaultHeadersMiddleware sets default request headers // defaultHeadersMiddleware sets default request headers
func defaultHeadersMiddleware(g *Geziyor, r *Request) { func defaultHeadersMiddleware(g *Geziyor, r *http.Request) {
r.Header = http.SetDefaultHeader(r.Header, "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") r.Header = http.SetDefaultHeader(r.Header, "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
r.Header = http.SetDefaultHeader(r.Header, "Accept-Charset", "utf-8") r.Header = http.SetDefaultHeader(r.Header, "Accept-Charset", "utf-8")
r.Header = http.SetDefaultHeader(r.Header, "Accept-Language", "en") r.Header = http.SetDefaultHeader(r.Header, "Accept-Language", "en")
@ -64,7 +64,7 @@ func defaultHeadersMiddleware(g *Geziyor, r *Request) {
} }
// delayMiddleware delays requests // delayMiddleware delays requests
func delayMiddleware(g *Geziyor, r *Request) { func delayMiddleware(g *Geziyor, r *http.Request) {
if g.Opt.RequestDelayRandomize { if g.Opt.RequestDelayRandomize {
min := float64(g.Opt.RequestDelay) * 0.5 min := float64(g.Opt.RequestDelay) * 0.5
max := float64(g.Opt.RequestDelay) * 1.5 max := float64(g.Opt.RequestDelay) * 1.5
@ -75,29 +75,29 @@ func delayMiddleware(g *Geziyor, r *Request) {
} }
// logMiddleware logs requests // logMiddleware logs requests
func logMiddleware(g *Geziyor, r *Request) { func logMiddleware(g *Geziyor, r *http.Request) {
log.Println("Fetching: ", r.URL.String()) log.Println("Fetching: ", r.URL.String())
} }
// metricsRequestMiddleware sets stats // metricsRequestMiddleware sets stats
func metricsRequestMiddleware(g *Geziyor, r *Request) { func metricsRequestMiddleware(g *Geziyor, r *http.Request) {
g.metrics.RequestCounter.With("method", r.Method).Add(1) g.metrics.RequestCounter.With("method", r.Method).Add(1)
} }
// parseHTMLMiddleware parses response if response is HTML // parseHTMLMiddleware parses response if response is HTML
func parseHTMLMiddleware(g *Geziyor, r *Response) { func parseHTMLMiddleware(g *Geziyor, r *http.Response) {
if !g.Opt.ParseHTMLDisabled && r.isHTML() { if !g.Opt.ParseHTMLDisabled && r.IsHTML() {
r.HTMLDoc, _ = goquery.NewDocumentFromReader(bytes.NewReader(r.Body)) r.HTMLDoc, _ = goquery.NewDocumentFromReader(bytes.NewReader(r.Body))
} }
} }
// metricsResponseMiddleware sets stats // metricsResponseMiddleware sets stats
func metricsResponseMiddleware(g *Geziyor, r *Response) { func metricsResponseMiddleware(g *Geziyor, r *http.Response) {
g.metrics.ResponseCounter.With("method", r.Request.Method).Add(1) g.metrics.ResponseCounter.With("method", r.Request.Method).Add(1)
} }
// extractorsMiddleware extracts data from loaders conf and exports it to exporters // extractorsMiddleware extracts data from loaders conf and exports it to exporters
func extractorsMiddleware(g *Geziyor, r *Response) { func extractorsMiddleware(g *Geziyor, r *http.Response) {
// Check if we have extractors and exporters // Check if we have extractors and exporters
if len(g.Opt.Extractors) != 0 && len(g.Opt.Exporters) != 0 { if len(g.Opt.Extractors) != 0 && len(g.Opt.Exporters) != 0 {

View File

@ -2,6 +2,7 @@ package geziyor
import ( import (
"github.com/fpfeng/httpcache" "github.com/fpfeng/httpcache"
"github.com/geziyor/geziyor/http"
"github.com/geziyor/geziyor/metrics" "github.com/geziyor/geziyor/metrics"
"time" "time"
) )
@ -19,7 +20,7 @@ type Options struct {
StartRequestsFunc func(g *Geziyor) StartRequestsFunc func(g *Geziyor)
// ParseFunc is callback of StartURLs response. // ParseFunc is callback of StartURLs response.
ParseFunc func(g *Geziyor, r *Response) ParseFunc func(g *Geziyor, r *http.Response)
// Extractors extracts items from pages // Extractors extracts items from pages
Extractors []Extractor Extractors []Extractor