Repository: syncthing/syncthing
File: cmd/stcrashreceiver/stcrashreceiver.go (Go)

Code before:
		defer fd.Close()
		gr, err := gzip.NewReader(fd)
		if err != nil {
			http.Error(w, "Internal server error", http.StatusInternalServerError)
			return
		}
		_, _ = io.Copy(w, gr) // best effort
		return
	}
	if _, err := os.Lstat(fullPath); err == nil {
		http.ServeFile(w, req, fullPath)
		return
	}
	http.Error(w, "Not found", http.StatusNotFound)
}

// serveHead responds to HEAD requests by checking if the named report
// already exists in the system.
func (r *crashReceiver) serveHead(base string, w http.ResponseWriter, _ *http.Request) {
	path := filepath.Join(r.dirFor(base), base)
	fullPath := filepath.Join(r.dir, path)
	if _, err := os.Lstat(fullPath + ".gz"); err == nil {
		// There is a compressed report
		return
	}
	if _, err := os.Lstat(fullPath); err == nil {
		// There is an uncompressed report
		return
	}
	http.Error(w, "Not found", http.StatusNotFound)
}

// servePut accepts and stores the given report.
func (r *crashReceiver) servePut(base string, w http.ResponseWriter, req *http.Request) {
	path := filepath.Join(r.dirFor(base), base)
	fullPath := filepath.Join(r.dir, path)

	// Ensure the destination directory exists
	if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
		log.Printf("Creating directory for report %s: %v", base, err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	// Read at most maxRequestSize of report data.
	log.Println("Receiving report", base)
	lr := io.LimitReader(req.Body, maxRequestSize)
	bs, err := ioutil.ReadAll(lr)
	if err != nil {
		log.Println("Reading report:", err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)

Review comment (suggestion):
It looks like `path` vs `fullpath` is a change in behaviour unrelated to compression. As far as I understand before just `path` was used, but `fullPath` was used to store reports, so it shouldn't have worked/worked just by accident. Now `path` doesn't seem to be used at all except for creating `fullPath`, so could be removed. I feel like I must be missing something here.

Code after:
	if err != nil {
		http.Error(w, "Not found", http.StatusNotFound)
		return
	}

	defer fd.Close()
	gr, err := gzip.NewReader(fd)
	if err != nil {
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}
	_, _ = io.Copy(w, gr) // best effort
}

// serveHead responds to HEAD requests by checking if the named report
// already exists in the system.
func (r *crashReceiver) serveHead(fullPath string, w http.ResponseWriter, _ *http.Request) {
	if _, err := os.Lstat(fullPath); err != nil {
		http.Error(w, "Not found", http.StatusNotFound)
	}
}

// servePut accepts and stores the given report.
func (r *crashReceiver) servePut(reportID, fullPath string, w http.ResponseWriter, req *http.Request) {
	// Ensure the destination directory exists
	if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
		log.Println("Creating directory:", err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	// Read at most maxRequestSize of report data.
	log.Println("Receiving report", reportID)
	lr := io.LimitReader(req.Body, maxRequestSize)
	bs, err := ioutil.ReadAll(lr)
	if err != nil {
		log.Println("Reading report:", err)
		http.Error(w, "Internal server error", http.StatusInternalServerError)
		return
	}

	// Compress the report for storage
	buf := new(bytes.Buffer)
	gw := gzip.NewWriter(buf)
	_, _ = gw.Write(bs) // can't fail
	gw.Close()

	// Create an output file with the compressed report
	err = ioutil.WriteFile(fullPath, buf.Bytes(), 0644)
	if err != nil {
		log.Println("Saving report:", err)