github.com/bosssauce/ponzu@v0.11.1-0.20200102001432-9bc41b703131/system/api/push.go (about) 1 package api 2 3 import ( 4 "log" 5 "net/http" 6 "strings" 7 8 "github.com/ponzu-cms/ponzu/system/item" 9 10 "github.com/tidwall/gjson" 11 ) 12 13 const errRecursivePush = "recursive push not allowed" 14 15 func push(res http.ResponseWriter, req *http.Request, pt interface{}, data []byte) { 16 // Push(target string, opts *PushOptions) error 17 if pusher, ok := res.(http.Pusher); ok { 18 if p, ok := pt.(item.Pushable); ok { 19 // get fields to pull values from data 20 fields, err := p.Push(res, req) 21 if err != nil { 22 log.Println("[Pushable] error:", err) 23 return 24 } 25 26 // parse values from data to push 27 values := gjson.GetManyBytes(data, fields...) 28 29 // push all values from Pushable items' fields 30 for i := range values { 31 val := values[i] 32 val.ForEach(func(k, v gjson.Result) bool { 33 if v.String() == "null" { 34 return true 35 } 36 37 // check that the push is not to its parent URL 38 if v.String() == (req.URL.Path + "?" + req.URL.RawQuery) { 39 return true 40 } 41 42 err := pusher.Push(v.String(), nil) 43 // check for error, "http2: recursive push not allowed" 44 // and return, suppressing a log message 45 // XXX: errRecursivePush has been co-located to this 46 // package instead of importing golang.org/x/net/http2 47 // to get the error itself. 48 if err != nil && strings.Contains(err.Error(), errRecursivePush) { 49 return true 50 } 51 if err != nil { 52 log.Println("Error during Push of value:", v.String(), err) 53 } 54 55 return true 56 }) 57 } 58 } 59 } 60 61 }