Pass ServeHeaderOptions by value instead of pointer, fine tune httplib tests (#36982)

Pass `ServeHeaderOptions` by value instead of pointer across all call
sites — no nil-check semantics are needed and the struct is small enough
that copying is fine.

## Changes

- **`services/context/base.go`**: `SetServeHeaders` and `ServeContent`
accept `ServeHeaderOptions` (value, not pointer); internal unsafe
pointer cast replaced with a clean type conversion
- **`routers/api/packages/helper/helper.go`**: `ServePackageFile`
variadic changed from `...*context.ServeHeaderOptions` to
`...context.ServeHeaderOptions`; internal variable is now a value type
- **All call sites** (13 files): `&context.ServeHeaderOptions{...}` →
`context.ServeHeaderOptions{...}`

Before/after at the definition level:
```go
// Before
func (b *Base) SetServeHeaders(opt *ServeHeaderOptions) { ... }
func (b *Base) ServeContent(r io.ReadSeeker, opts *ServeHeaderOptions) { ... }
func ServePackageFile(..., forceOpts ...*context.ServeHeaderOptions) { ... }

// After
func (b *Base) SetServeHeaders(opts ServeHeaderOptions) { ... }
func (b *Base) ServeContent(r io.ReadSeeker, opts ServeHeaderOptions) { ... }
func ServePackageFile(..., forceOpts ...context.ServeHeaderOptions) { ... }
```

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Copilot
2026-03-25 16:07:59 -07:00
committed by GitHub
parent bc5c554072
commit a3cc34472b
15 changed files with 77 additions and 97 deletions
+1 -1
View File
@@ -496,7 +496,7 @@ func (ar artifactRoutes) downloadArtifact(ctx *ArtifactContext) {
ctx.Resp.Header().Set("Content-Encoding", "gzip")
}
log.Debug("[artifact] downloadArtifact, name: %s, path: %s, storage: %s, size: %d", artifact.ArtifactName, artifact.ArtifactPath, artifact.StoragePath, artifact.FileSize)
ctx.ServeContent(fd, &context.ServeHeaderOptions{
ctx.ServeContent(fd, context.ServeHeaderOptions{
Filename: artifact.ArtifactName,
LastModified: artifact.CreatedUnix.AsLocalTime(),
})
+1 -1
View File
@@ -54,7 +54,7 @@ func GetRepositoryKey(ctx *context.Context) {
return
}
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
ctx.ServeContent(strings.NewReader(pub), context.ServeHeaderOptions{
ContentType: "application/x-pem-file",
Filename: fmt.Sprintf("%s@%s.rsa.pub", ctx.Package.Owner.LowerName, hex.EncodeToString(fingerprint)),
})
+2 -2
View File
@@ -35,7 +35,7 @@ func GetRepositoryKey(ctx *context.Context) {
return
}
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
ctx.ServeContent(strings.NewReader(pub), context.ServeHeaderOptions{
ContentType: "application/pgp-keys",
})
}
@@ -232,7 +232,7 @@ func GetPackageOrRepositoryFile(ctx *context.Context) {
return
}
ctx.ServeContent(bytes.NewReader(data), &context.ServeHeaderOptions{
ctx.ServeContent(bytes.NewReader(data), context.ServeHeaderOptions{
Filename: filenameOrig,
})
return
+2 -2
View File
@@ -35,7 +35,7 @@ func GetRepositoryKey(ctx *context.Context) {
return
}
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
ctx.ServeContent(strings.NewReader(pub), context.ServeHeaderOptions{
ContentType: "application/pgp-keys",
Filename: "repository.key",
})
@@ -233,7 +233,7 @@ func DownloadPackageFile(ctx *context.Context) {
return
}
helper.ServePackageFile(ctx, s, u, pf, &context.ServeHeaderOptions{
helper.ServePackageFile(ctx, s, u, pf, context.ServeHeaderOptions{
ContentType: "application/vnd.debian.binary-package",
Filename: pf.Name,
LastModified: pf.CreatedUnix.AsLocalTime(),
+3 -3
View File
@@ -39,7 +39,7 @@ func ProcessErrorForUser(ctx *context.Context, status int, errObj any) string {
// ServePackageFile the content of the package file
// If the url is set it will redirect the request, otherwise the content is copied to the response.
func ServePackageFile(ctx *context.Context, s io.ReadSeekCloser, u *url.URL, pf *packages_model.PackageFile, forceOpts ...*context.ServeHeaderOptions) {
func ServePackageFile(ctx *context.Context, s io.ReadSeekCloser, u *url.URL, pf *packages_model.PackageFile, forceOpts ...context.ServeHeaderOptions) {
if u != nil {
ctx.Redirect(u.String())
return
@@ -47,11 +47,11 @@ func ServePackageFile(ctx *context.Context, s io.ReadSeekCloser, u *url.URL, pf
defer s.Close()
var opts *context.ServeHeaderOptions
var opts context.ServeHeaderOptions
if len(forceOpts) > 0 {
opts = forceOpts[0]
} else {
opts = &context.ServeHeaderOptions{
opts = context.ServeHeaderOptions{
Filename: pf.Name,
LastModified: pf.CreatedUnix.AsLocalTime(),
}
+1 -1
View File
@@ -200,7 +200,7 @@ func servePackageFile(ctx *context.Context, params parameters, serveContent bool
return
}
opts := &context.ServeHeaderOptions{
opts := context.ServeHeaderOptions{
ContentLength: &pb.Size,
LastModified: pf.CreatedUnix.AsLocalTime(),
}
+2 -2
View File
@@ -57,7 +57,7 @@ func GetRepositoryKey(ctx *context.Context) {
return
}
ctx.ServeContent(strings.NewReader(pub), &context.ServeHeaderOptions{
ctx.ServeContent(strings.NewReader(pub), context.ServeHeaderOptions{
ContentType: "application/pgp-keys",
Filename: "repository.key",
})
@@ -80,7 +80,7 @@ func CheckRepositoryFileExistence(ctx *context.Context) {
return
}
ctx.SetServeHeaders(&context.ServeHeaderOptions{
ctx.SetServeHeaders(context.ServeHeaderOptions{
Filename: pf.Name,
LastModified: pf.CreatedUnix.AsLocalTime(),
})
+2 -2
View File
@@ -79,7 +79,7 @@ func enumeratePackages(ctx *context.Context, filename string, pvs []*packages_mo
})
}
ctx.SetServeHeaders(&context.ServeHeaderOptions{
ctx.SetServeHeaders(context.ServeHeaderOptions{
Filename: filename + ".gz",
})
@@ -119,7 +119,7 @@ func ServePackageSpecification(ctx *context.Context) {
return
}
ctx.SetServeHeaders(&context.ServeHeaderOptions{
ctx.SetServeHeaders(context.ServeHeaderOptions{
Filename: filename,
})
+2 -2
View File
@@ -281,7 +281,7 @@ func DownloadManifest(ctx *context.Context) {
filename = fmt.Sprintf("Package@swift-%s.swift", swiftVersion)
}
ctx.ServeContent(strings.NewReader(m.Content), &context.ServeHeaderOptions{
ctx.ServeContent(strings.NewReader(m.Content), context.ServeHeaderOptions{
ContentType: "text/x-swift",
Filename: filename,
LastModified: pv.CreatedUnix.AsLocalTime(),
@@ -437,7 +437,7 @@ func DownloadPackageFile(ctx *context.Context) {
Digest: pd.Files[0].Blob.HashSHA256,
})
helper.ServePackageFile(ctx, s, u, pf, &context.ServeHeaderOptions{
helper.ServePackageFile(ctx, s, u, pf, context.ServeHeaderOptions{
Filename: pf.Name,
ContentType: "application/zip",
LastModified: pf.CreatedUnix.AsLocalTime(),