MCPcopy Index your code
hub / github.com/git-bug/git-bug / ServeHTTP

Method ServeHTTP

api/http/git_file_handler.go:27–61  ·  view source on GitHub ↗
(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

25}
26
27func (gfh *gitFileHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
28 var repo *cache.RepoCache
29 var err error
30
31 repoVar := mux.Vars(r)["repo"]
32 switch repoVar {
33 case "":
34 repo, err = gfh.mrc.DefaultRepo()
35 default:
36 repo, err = gfh.mrc.ResolveRepo(repoVar)
37 }
38
39 if err != nil {
40 http.Error(rw, "invalid repo reference", http.StatusBadRequest)
41 return
42 }
43
44 hash := repository.Hash(mux.Vars(r)["hash"])
45 if !hash.IsValid() {
46 http.Error(rw, "invalid git hash", http.StatusBadRequest)
47 return
48 }
49
50 // TODO: this mean that the whole file will be buffered in memory
51 // This can be a problem for big files. There might be a way around
52 // that by implementing a io.ReadSeeker that would read and discard
53 // data when a seek is called.
54 data, err := repo.ReadData(hash)
55 if err != nil {
56 http.Error(rw, err.Error(), http.StatusInternalServerError)
57 return
58 }
59
60 http.ServeContent(rw, r, "", time.Now(), bytes.NewReader(data))
61}

Callers 2

TestGitFileHandlersFunction · 0.45
MiddlewareFunction · 0.45

Calls 6

ReadDataMethod · 0.95
HashTypeAlias · 0.92
DefaultRepoMethod · 0.80
ResolveRepoMethod · 0.80
IsValidMethod · 0.80
ErrorMethod · 0.45

Tested by 1

TestGitFileHandlersFunction · 0.36