2019-06-11 06:19:11 +00:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type crashReceiver struct {
|
2023-01-23 07:38:55 +00:00
|
|
|
store *diskStore
|
|
|
|
sentry *sentryService
|
2019-06-11 06:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *crashReceiver) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
// The final path component should be a SHA256 hash in hex, so 64 hex
|
|
|
|
// characters. We don't care about case on the request but use lower
|
|
|
|
// case internally.
|
2019-07-28 09:13:04 +00:00
|
|
|
reportID := strings.ToLower(path.Base(req.URL.Path))
|
|
|
|
if len(reportID) != 64 {
|
2019-06-11 06:19:11 +00:00
|
|
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-07-28 09:13:04 +00:00
|
|
|
for _, c := range reportID {
|
2019-06-11 06:19:11 +00:00
|
|
|
if c >= 'a' && c <= 'f' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c >= '0' && c <= '9' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
http.Error(w, "Bad request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Method {
|
2019-07-28 09:13:04 +00:00
|
|
|
case http.MethodGet:
|
2023-01-23 07:38:55 +00:00
|
|
|
r.serveGet(reportID, w, req)
|
2019-06-11 06:19:11 +00:00
|
|
|
case http.MethodHead:
|
2023-01-23 07:38:55 +00:00
|
|
|
r.serveHead(reportID, w, req)
|
2019-06-11 06:19:11 +00:00
|
|
|
case http.MethodPut:
|
2023-01-23 07:38:55 +00:00
|
|
|
r.servePut(reportID, w, req)
|
2019-06-11 06:19:11 +00:00
|
|
|
default:
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-28 09:13:04 +00:00
|
|
|
// serveGet responds to GET requests by serving the uncompressed report.
|
2023-01-23 07:38:55 +00:00
|
|
|
func (r *crashReceiver) serveGet(reportID string, w http.ResponseWriter, _ *http.Request) {
|
|
|
|
bs, err := r.store.Get(reportID)
|
2019-07-28 09:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2023-01-23 07:38:55 +00:00
|
|
|
w.Write(bs)
|
2019-07-28 09:13:04 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 06:19:11 +00:00
|
|
|
// serveHead responds to HEAD requests by checking if the named report
|
|
|
|
// already exists in the system.
|
2023-01-23 07:38:55 +00:00
|
|
|
func (r *crashReceiver) serveHead(reportID string, w http.ResponseWriter, _ *http.Request) {
|
|
|
|
if !r.store.Exists(reportID) {
|
2019-06-11 06:19:11 +00:00
|
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// servePut accepts and stores the given report.
|
2023-01-23 07:38:55 +00:00
|
|
|
func (r *crashReceiver) servePut(reportID string, w http.ResponseWriter, req *http.Request) {
|
2023-11-24 07:38:34 +00:00
|
|
|
result := "receive_failure"
|
|
|
|
defer func() {
|
|
|
|
metricCrashReportsTotal.WithLabelValues(result).Inc()
|
|
|
|
}()
|
|
|
|
|
2019-06-11 06:19:11 +00:00
|
|
|
// Read at most maxRequestSize of report data.
|
2019-07-28 09:13:04 +00:00
|
|
|
log.Println("Receiving report", reportID)
|
2019-06-11 06:19:11 +00:00
|
|
|
lr := io.LimitReader(req.Body, maxRequestSize)
|
2021-11-22 07:59:47 +00:00
|
|
|
bs, err := io.ReadAll(lr)
|
2019-06-11 06:19:11 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Reading report:", err)
|
|
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-24 07:38:34 +00:00
|
|
|
result = "success"
|
|
|
|
|
2023-01-23 07:38:55 +00:00
|
|
|
// Store the report
|
|
|
|
if !r.store.Put(reportID, bs) {
|
|
|
|
log.Println("Failed to store report (queue full):", reportID)
|
2023-11-24 07:38:34 +00:00
|
|
|
result = "queue_failure"
|
2019-06-11 06:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send the report to Sentry
|
2023-09-20 06:39:01 +00:00
|
|
|
if !r.sentry.Send(reportID, userIDFor(req), bs) {
|
2023-01-23 07:38:55 +00:00
|
|
|
log.Println("Failed to send report to sentry (queue full):", reportID)
|
2023-11-24 07:38:34 +00:00
|
|
|
result = "sentry_failure"
|
2019-06-11 06:19:11 +00:00
|
|
|
}
|
|
|
|
}
|