Skip to content

Bundle list routing + other bugfixes #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions cmd/git-bundle-web-server/bundle-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"github.com/github/git-bundle-server/internal/bundles"
"github.com/github/git-bundle-server/internal/common"
"github.com/github/git-bundle-server/internal/core"
"github.com/github/git-bundle-server/internal/log"
Expand Down Expand Up @@ -70,7 +73,7 @@ func (b *bundleWebServer) serve(w http.ResponseWriter, r *http.Request) {
defer exitRegion()

path := r.URL.Path
owner, repo, file, err := b.parseRoute(ctx, path)
owner, repo, filename, err := b.parseRoute(ctx, path)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Printf("Failed to parse route: %s\n", err)
Expand All @@ -97,20 +100,35 @@ func (b *bundleWebServer) serve(w http.ResponseWriter, r *http.Request) {
return
}

if file == "" {
file = "bundle-list"
var fileToServe string
if filename == "" {
if path[len(path)-1] == '/' {
// Trailing slash, so the bundle URIs should be relative to the
// request's URL as if it were a directory
fileToServe = filepath.Join(repository.WebDir, bundles.BundleListFilename)
} else {
// No trailing slash, so the bundle URIs should be relative to the
// request's URL as if it were a file
fileToServe = filepath.Join(repository.WebDir, bundles.RepoBundleListFilename)
}
} else if filename == bundles.BundleListFilename || filename == bundles.RepoBundleListFilename {
// If the request identifies a non-bundle "reserved" file, return 404
w.WriteHeader(http.StatusNotFound)
fmt.Printf("Failed to open file\n")
return
} else {
fileToServe = filepath.Join(repository.WebDir, filename)
}

fileToServe := repository.WebDir + "/" + file
data, err := os.ReadFile(fileToServe)
file, err := os.OpenFile(fileToServe, os.O_RDONLY, 0)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Printf("Failed to read file\n")
fmt.Printf("Failed to open file\n")
return
}

fmt.Printf("Successfully serving content for %s/%s\n", route, file)
w.Write(data)
fmt.Printf("Successfully serving content for %s/%s\n", route, filename)
http.ServeContent(w, r, filename, time.UnixMicro(0), file)
}

func (b *bundleWebServer) StartServerAsync(ctx context.Context) {
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/container-helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func BuildGitBundleServerContainer(logger log.TraceLogger) *DependencyContainer
registerDependency(container, func(ctx context.Context) bundles.BundleProvider {
return bundles.NewBundleProvider(
logger,
GetDependency[common.FileSystem](ctx, container),
GetDependency[git.GitHelper](ctx, container),
)
})
Expand Down
Loading