all repos

mugit @ 56a787b

馃惍 git server that your cow will love
1 files changed, 22 insertions(+), 48 deletions(-)
http: dont gzip upload-pack; add helper for smart http
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-02-13 01:58:03 +0200
Authored at: 2026-02-13 01:47:34 +0200
Change ID: rrrsmvzqxkuuvvzqummotuzlokszpynk
Parent: f39f7dd
M internal/handlers/git.go
路路路
        1
        1
         package handlers

      
        2
        2
         

      
        3
        3
         import (

      
        4
        
        -	"compress/gzip"

      
        5
        4
         	"fmt"

      
        6
        5
         	"io"

      
        7
        6
         	"log/slog"

      路路路
        31
        30
         }

      
        32
        31
         

      
        33
        32
         func (h *handlers) infoRefs(w http.ResponseWriter, r *http.Request) {

      
        34
        
        -	name := getNormalizedName(r.PathValue("name"))

      
        35
        
        -	_, err := h.openPublicRepo(name, "")

      
        
        33
        +	path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"))

      
        36
        34
         	if err != nil {

      
        37
        35
         		h.write404(w, err)

      
        38
        36
         		return

      
        39
        37
         	}

      
        40
        38
         

      
        41
        
        -	repoPath := repoNameToPath(name)

      
        42
        
        -	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)

      
        43
        
        -	if err != nil {

      
        44
        
        -		w.WriteHeader(http.StatusBadRequest)

      
        45
        
        -		slog.Error("git: info/refs", "err", err)

      
        46
        
        -		return

      
        47
        
        -	}

      
        48
        
        -

      
        49
        39
         	w.Header().Set("content-type", "application/x-git-upload-pack-advertisement")

      
        50
        40
         	w.WriteHeader(http.StatusOK)

      
        51
        41
         	if err := gitx.InfoRefs(r.Context(), path, w); err != nil {

      路路路
        56
        46
         }

      
        57
        47
         

      
        58
        48
         func (h *handlers) uploadPack(w http.ResponseWriter, r *http.Request) {

      
        59
        
        -	name := getNormalizedName(r.PathValue("name"))

      
        60
        
        -	_, err := h.openPublicRepo(name, "")

      
        
        49
        +	path, err := h.checkRepoPublicityAndGetPath(r.PathValue("name"))

      
        61
        50
         	if err != nil {

      
        62
        51
         		h.write404(w, err)

      
        63
        52
         		return

      
        64
        53
         	}

      
        65
        54
         

      
        66
        
        -	reader := io.Reader(r.Body)

      
        67
        
        -	if r.Header.Get("Content-Encoding") == "gzip" {

      
        68
        
        -		gr, gerr := gzip.NewReader(r.Body)

      
        69
        
        -		if gerr != nil {

      
        70
        
        -			http.Error(w, "invalid gzip encoding", http.StatusBadRequest)

      
        71
        
        -			slog.Error("git: gzip reader", "err", gerr)

      
        72
        
        -			return

      
        73
        
        -		}

      
        74
        
        -		defer gr.Close()

      
        75
        
        -		reader = gr

      
        76
        
        -	}

      
        77
        
        -

      
        78
        
        -	repoPath := repoNameToPath(name)

      
        79
        
        -	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)

      
        80
        
        -	if err != nil {

      
        81
        
        -		http.Error(w, "invalid path", http.StatusBadRequest)

      
        82
        
        -		slog.Error("git: upload-pack path", "err", err)

      
        83
        
        -		return

      
        84
        
        -	}

      
        85
        
        -

      
        86
        
        -	w.Header().Set("content-type", "application/x-git-upload-pack-result")

      
        87
        
        -	w.Header().Set("Connection", "Keep-Alive")

      
        88
        
        -	w.Header().Set("Transfer-Encoding", "chunked")

      
        
        55
        +	w.Header().Set("Content-Type", "application/x-git-upload-pack-result")

      
        
        56
        +	w.Header().Set("Cache-Control", "no-cache")

      
        89
        57
         	w.WriteHeader(http.StatusOK)

      
        90
        58
         

      
        91
        
        -	if err := gitx.UploadPack(r.Context(), path, true, reader, newFlushWriter(w)); err != nil {

      
        92
        
        -		// Don't call w.WriteHeader here - connection already started!

      
        
        59
        +	if err := gitx.UploadPack(r.Context(), path, true, r.Body, newFlushWriter(w)); err != nil {

      
        93
        60
         		slog.Error("git: upload-pack", "err", err)

      
        94
        61
         		return

      
        95
        62
         	}

      
        96
        63
         }

      
        97
        64
         

      
        98
        65
         func (h *handlers) archiveHandler(w http.ResponseWriter, r *http.Request) {

      
        99
        
        -	name := getNormalizedName(r.PathValue("name"))

      
        100
        66
         	ref := r.PathValue("ref")

      
        101
        
        -

      
        102
        
        -	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoNameToPath(name))

      
        103
        
        -	if err != nil {

      
        104
        
        -		http.Error(w, "invalid path", http.StatusBadRequest)

      
        105
        
        -		slog.Error("git: upload-pack path", "err", err)

      
        106
        
        -		return

      
        107
        
        -	}

      
        108
        
        -

      
        109
        
        -	_, err = h.openPublicRepo(name, ref)

      
        
        67
        +	name := r.PathValue("name")

      
        
        68
        +	path, err := h.checkRepoPublicityAndGetPath(name)

      
        110
        69
         	if err != nil {

      
        111
        70
         		h.write404(w, err)

      
        112
        71
         		return

      路路路
        121
        80
         		slog.Error("git: archive", "ref", ref, "err", err)

      
        122
        81
         		return

      
        123
        82
         	}

      
        
        83
        +}

      
        
        84
        +

      
        
        85
        +func (h *handlers) checkRepoPublicityAndGetPath(name string) (string, error) {

      
        
        86
        +	repoPath := repoNameToPath(name)

      
        
        87
        +	_, err := h.openPublicRepo(name, "")

      
        
        88
        +	if err != nil {

      
        
        89
        +		return "", err

      
        
        90
        +	}

      
        
        91
        +

      
        
        92
        +	path, err := securejoin.SecureJoin(h.c.Repo.Dir, repoPath)

      
        
        93
        +	if err != nil {

      
        
        94
        +		return "", err

      
        
        95
        +	}

      
        
        96
        +

      
        
        97
        +	return path, nil

      
        124
        98
         }

      
        125
        99
         

      
        126
        100
         type flushWriter struct {