| 88 | } |
| 89 | |
| 90 | func (h *HTTPHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) { |
| 91 | if !PathMatches(h.Config.Path, request.URL.Path) { |
| 92 | response.WriteHeader(http.StatusNotFound) |
| 93 | |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | if request.Method != http.MethodPost { |
| 98 | response.Header().Set("Allow", http.MethodPost) |
| 99 | response.WriteHeader(http.StatusMethodNotAllowed) |
| 100 | |
| 101 | return |
| 102 | } |
| 103 | |
| 104 | if h.Authorize != nil && !h.Authorize(request) { |
| 105 | response.WriteHeader(http.StatusUnauthorized) |
| 106 | |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | envelope, err := ParsePush(request, h.Config.BodyLimit) |
| 111 | if err != nil { |
| 112 | h.warnf("watch: invalid push payload: %v", err) |
| 113 | response.WriteHeader(http.StatusBadRequest) |
| 114 | |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | payload, err := DecodePushPayload(envelope) |
| 119 | if err != nil { |
| 120 | h.warnf("watch: invalid push data: %v", err) |
| 121 | response.WriteHeader(http.StatusBadRequest) |
| 122 | |
| 123 | return |
| 124 | } |
| 125 | |
| 126 | if payload.EmailAddress != "" && !strings.EqualFold(payload.EmailAddress, h.Config.Account) { |
| 127 | h.warnf("watch: ignoring push for %s", payload.EmailAddress) |
| 128 | response.WriteHeader(http.StatusAccepted) |
| 129 | |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | if h.Process == nil { |
| 134 | h.warnf("watch: handle push failed: missing processor") |
| 135 | response.WriteHeader(http.StatusInternalServerError) |
| 136 | |
| 137 | return |
| 138 | } |
| 139 | |
| 140 | processed, err := h.Process(request.Context(), Notification{ |
| 141 | HistoryID: payload.HistoryID, |
| 142 | MessageID: payload.MessageID, |
| 143 | }) |
| 144 | if err != nil { |
| 145 | if errors.Is(err, ErrNoNewMessages) { |
| 146 | response.WriteHeader(http.StatusAccepted) |
| 147 | |