You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generic.go 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package httplistener
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/irccloud/irccat/dispatcher"
  6. "github.com/spf13/viper"
  7. "net/http"
  8. )
  9. // Examples of using curl to post to /send.
  10. //
  11. // echo "Hello, world" | curl -d @- http://irccat.example.com/send
  12. // echo "#test,@alice Hello, world" | curl -d @- http://irccat.example.com/send
  13. //
  14. func (hl *HTTPListener) genericHandler(w http.ResponseWriter, request *http.Request) {
  15. if request.Method != "POST" {
  16. http.NotFound(w, request)
  17. return
  18. }
  19. // Optional simple auth via token
  20. secret := viper.GetString("http.listeners.generic.secret")
  21. if secret != "" {
  22. auth := request.Header.Get("Authorization")
  23. expecting := fmt.Sprintf("Bearer %s", secret)
  24. if auth != expecting {
  25. http.Error(w, "Invalid Authorization", http.StatusUnauthorized)
  26. log.Warningf("%s - Invalid Authorization!", request.RemoteAddr)
  27. return
  28. }
  29. }
  30. body := new(bytes.Buffer)
  31. body.ReadFrom(request.Body)
  32. message := body.String()
  33. if message == "" {
  34. log.Warningf("%s - No message body in POST request", request.RemoteAddr)
  35. return
  36. }
  37. dispatcher.Send(hl.irc, message, log, request.RemoteAddr)
  38. }