This commit is contained in:
parent
fd4ddaf6d6
commit
59668090df
|
@ -13,11 +13,12 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"p83.nl/go/websub-hub/cmd/hubserver/storage"
|
"p83.nl/go/websub-hub/cmd/hubserver/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type subscriptionHandler struct {
|
type subscriptionHandler struct {
|
||||||
store storage.Service
|
store storage.Service
|
||||||
baseURL string
|
baseURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,51 +29,54 @@ func (handler *subscriptionHandler) handlePublish(w http.ResponseWriter, r *http
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
req, err := http.NewRequest("GET", topic, nil)
|
req, err := http.NewRequest("GET", topic, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "could not create request")
|
||||||
}
|
}
|
||||||
req.Header.Add("Accept", "*/*")
|
req.Header.Add("Accept", "*/*")
|
||||||
res, err := client.Do(req)
|
res, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrapf(err, "could not request topic %s", topic)
|
||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
feedContentType := res.Header.Get("Content-Type")
|
feedContentType := res.Header.Get("Content-Type")
|
||||||
feedContent, err := ioutil.ReadAll(res.Body)
|
feedContent, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "could not read body")
|
||||||
}
|
}
|
||||||
|
|
||||||
if subs, err := handler.store.Subscribers(topic); err != nil {
|
subs, err := handler.store.Subscribers(topic)
|
||||||
for _, sub := range subs {
|
if err != nil {
|
||||||
log.Printf("publish: creating post to %s\n", sub.Callback)
|
return errors.Wrap(err, "could not get subscribers")
|
||||||
postReq, err := http.NewRequest("POST", sub.Callback, strings.NewReader(string(feedContent)))
|
}
|
||||||
if err != nil {
|
|
||||||
log.Printf("While creating request to %s: %s", sub.Callback, err)
|
for _, sub := range subs {
|
||||||
continue
|
log.Printf("publish: creating post to %s\n", sub.Callback)
|
||||||
}
|
postReq, err := http.NewRequest("POST", sub.Callback, strings.NewReader(string(feedContent)))
|
||||||
postReq.Header.Add("Content-Type", feedContentType)
|
if err != nil {
|
||||||
postReq.Header.Add("Link",
|
log.Printf("could not creating request to %s: %s", sub.Callback, err)
|
||||||
fmt.Sprintf(
|
continue
|
||||||
"<%s>; rel=hub, <%s>; rel=self",
|
|
||||||
handler.baseURL,
|
|
||||||
topic,
|
|
||||||
))
|
|
||||||
if sub.Secret != "" {
|
|
||||||
mac := hmac.New(sha1.New, []byte(sub.Secret))
|
|
||||||
mac.Write(feedContent)
|
|
||||||
signature := mac.Sum(nil)
|
|
||||||
postReq.Header.Add("X-Hub-Signature", fmt.Sprintf("sha1=%x", signature))
|
|
||||||
}
|
|
||||||
postRes, err := client.Do(postReq)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("While POSTing to %s: %s", sub.Callback, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Printf("publish: post send to %s\n", sub.Callback)
|
|
||||||
log.Println("Response:")
|
|
||||||
_ = postRes.Write(os.Stdout)
|
|
||||||
}
|
}
|
||||||
|
postReq.Header.Add("Content-Type", feedContentType)
|
||||||
|
postReq.Header.Add("Link",
|
||||||
|
fmt.Sprintf(
|
||||||
|
"<%s>; rel=hub, <%s>; rel=self",
|
||||||
|
handler.baseURL,
|
||||||
|
topic,
|
||||||
|
))
|
||||||
|
if sub.Secret != "" {
|
||||||
|
mac := hmac.New(sha1.New, []byte(sub.Secret))
|
||||||
|
mac.Write(feedContent)
|
||||||
|
signature := mac.Sum(nil)
|
||||||
|
postReq.Header.Add("X-Hub-Signature", fmt.Sprintf("sha1=%x", signature))
|
||||||
|
}
|
||||||
|
postRes, err := client.Do(postReq)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("could not publish to %s: %s", sub.Callback, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Printf("publish: post send to %s\n", sub.Callback)
|
||||||
|
log.Println("Response:")
|
||||||
|
_ = postRes.Write(os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue
Block a user