Skip to content

Commit

Permalink
fixes to the recent issues with the scrape function
Browse files Browse the repository at this point in the history
  • Loading branch information
caffix committed Jun 20, 2023
1 parent 499265b commit 111af71
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions datasrcs/scripting/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ func (s *Script) request(L *lua.LState) int {
Password: pass,
})

if err != nil {
if err != nil || resp == nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
estr := "no HTTP response"
if err != nil {
estr = err.Error()
}
L.Push(lua.LString(estr))
} else {
L.Push(responseToTable(L, resp))
L.Push(lua.LNil)
Expand Down Expand Up @@ -170,9 +174,11 @@ func (s *Script) scrape(L *lua.LState) int {
if resp, err := s.req(ctx, url, body, hdr, &http.BasicAuth{
Username: id,
Password: pass,
}); err == nil && resp.StatusCode >= 200 && resp.StatusCode < 400 {
if num := s.internalSendNames(ctx, resp.Body); num > 0 {
sucess = lua.LTrue
}); err == nil {
if resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 400 {
if num := s.internalSendNames(ctx, resp.Body); num > 0 {
sucess = lua.LTrue
}
}
} else {
s.sys.Config().Log.Print(s.String() + ": scrape: " + err.Error())
Expand Down Expand Up @@ -212,7 +218,7 @@ func (s *Script) req(ctx context.Context, url, data string, hdr http.Header, aut
if cfg.Verbose {
cfg.Log.Printf("%s: %s: %v", s.String(), url, err)
}
} else if dsc != nil && dsc.TTL > 0 && resp.StatusCode >= 200 && resp.StatusCode < 400 {
} else if dsc != nil && dsc.TTL > 0 && resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 400 {
_ = s.setCachedResponse(ctx, url+data, resp)
}
return resp, err
Expand Down

0 comments on commit 111af71

Please sign in to comment.