github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/document_exists.go (about) 1 package html 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/values" 9 "github.com/MontFerret/ferret/pkg/runtime/values/types" 10 ) 11 12 // DOCUMENT_EXISTS returns a boolean value indicating whether a web page exists by a given url. 13 // @param {String} url - Target url. 14 // @param {Object} [options] - Request options. 15 // @param {Object} [options.headers] - Request headers. 16 // @return {Boolean} - A boolean value indicating whether a web page exists by a given url. 17 func DocumentExists(ctx context.Context, args ...core.Value) (core.Value, error) { 18 if err := core.ValidateArgs(args, 1, 2); err != nil { 19 return nil, err 20 } 21 22 if err := core.ValidateType(args[0], types.String); err != nil { 23 return nil, err 24 } 25 26 url := args[0].String() 27 28 client := http.Client{} 29 req, err := http.NewRequest("GET", url, nil) 30 31 if err != nil { 32 return values.None, err 33 } 34 35 if len(args) > 1 { 36 if err := core.ValidateType(args[1], types.Object); err != nil { 37 return nil, err 38 } 39 40 options := args[1].(*values.Object) 41 42 if options.Has("headers") { 43 headersOpt := options.MustGet("headers") 44 45 if err := core.ValidateType(headersOpt, types.Object); err != nil { 46 return nil, err 47 } 48 49 headers := headersOpt.(*values.Object) 50 51 req.Header = http.Header{} 52 53 headers.ForEach(func(value core.Value, key string) bool { 54 req.Header.Set(key, value.String()) 55 56 return true 57 }) 58 } 59 } 60 61 resp, err := client.Do(req.WithContext(ctx)) 62 63 if err != nil { 64 return values.False, nil 65 } 66 67 var exists values.Boolean 68 69 if resp.StatusCode >= 200 && resp.StatusCode <= 299 { 70 exists = values.True 71 } 72 73 return exists, nil 74 }