mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 17:07:54 +00:00
handle serve raw case
This commit is contained in:
parent
8dac935cd8
commit
40478215d0
4 changed files with 56 additions and 43 deletions
|
@ -5,8 +5,10 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -41,6 +43,9 @@ type Client struct {
|
||||||
|
|
||||||
followSymlinks bool
|
followSymlinks bool
|
||||||
supportLFS bool
|
supportLFS bool
|
||||||
|
|
||||||
|
forbiddenMimeTypes map[string]bool
|
||||||
|
defaultMimeType string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, followSymlinks, supportLFS bool) (*Client, error) {
|
func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, followSymlinks, supportLFS bool) (*Client, error) {
|
||||||
|
@ -52,12 +57,29 @@ func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, follo
|
||||||
|
|
||||||
stdClient := http.Client{Timeout: 10 * time.Second}
|
stdClient := http.Client{Timeout: 10 * time.Second}
|
||||||
|
|
||||||
|
// TODO: pass down
|
||||||
|
var (
|
||||||
|
forbiddenMimeTypes map[string]bool
|
||||||
|
defaultMimeType string
|
||||||
|
)
|
||||||
|
|
||||||
|
if forbiddenMimeTypes == nil {
|
||||||
|
forbiddenMimeTypes = make(map[string]bool)
|
||||||
|
}
|
||||||
|
if defaultMimeType == "" {
|
||||||
|
defaultMimeType = "application/octet-stream"
|
||||||
|
}
|
||||||
|
|
||||||
sdk, err := gitea.NewClient(giteaRoot, gitea.SetHTTPClient(&stdClient), gitea.SetToken(giteaAPIToken))
|
sdk, err := gitea.NewClient(giteaRoot, gitea.SetHTTPClient(&stdClient), gitea.SetToken(giteaAPIToken))
|
||||||
return &Client{
|
return &Client{
|
||||||
sdkClient: sdk,
|
sdkClient: sdk,
|
||||||
responseCache: respCache,
|
responseCache: respCache,
|
||||||
|
|
||||||
followSymlinks: followSymlinks,
|
followSymlinks: followSymlinks,
|
||||||
supportLFS: supportLFS,
|
supportLFS: supportLFS,
|
||||||
|
|
||||||
|
forbiddenMimeTypes: forbiddenMimeTypes,
|
||||||
|
defaultMimeType: defaultMimeType,
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +149,10 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
|
|
||||||
// now we are sure it's content
|
// now we are sure it's content
|
||||||
{
|
{
|
||||||
|
// Set the MIME type
|
||||||
|
mimeType := client.getMimeTypeByExtension(resource)
|
||||||
|
resp.Response.Header.Set(contentTypeHeader, mimeType)
|
||||||
|
|
||||||
contentLeng, err2 := strconv.ParseInt(resp.Header.Get(contentLengthHeader), 20, 64)
|
contentLeng, err2 := strconv.ParseInt(resp.Header.Get(contentLengthHeader), 20, 64)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Error().Err(err2).Msg("could not parse content length")
|
log.Error().Err(err2).Msg("could not parse content length")
|
||||||
|
@ -140,7 +166,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
|
|
||||||
// TODO: at the sime time !!!
|
// TODO: at the sime time !!!
|
||||||
/*
|
/*
|
||||||
we need a "go"
|
we create a new type that implement an writer witch write to cache based on key etc ...
|
||||||
// TODO: cache is half-empty if request is cancelled - does the ctx.Err() below do the trick?
|
// TODO: cache is half-empty if request is cancelled - does the ctx.Err() below do the trick?
|
||||||
// err = res.BodyWriteTo(io.MultiWriter(ctx.Response().BodyWriter(), &cacheBodyWriter))
|
// err = res.BodyWriteTo(io.MultiWriter(ctx.Response().BodyWriter(), &cacheBodyWriter))
|
||||||
*/
|
*/
|
||||||
|
@ -149,7 +175,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
if err := client.responseCache.Set(cacheKey, FileResponse{
|
if err := client.responseCache.Set(cacheKey, FileResponse{
|
||||||
Exists: true,
|
Exists: true,
|
||||||
ETag: resp.Header.Get(eTagHeader),
|
ETag: resp.Header.Get(eTagHeader),
|
||||||
MimeType: resp.Header.Get(contentTypeHeader),
|
MimeType: mimeType,
|
||||||
Body: body,
|
Body: body,
|
||||||
}, fileCacheTimeout); err != nil {
|
}, fileCacheTimeout); err != nil {
|
||||||
log.Error().Err(err).Msg("could not save content in cache")
|
log.Error().Err(err).Msg("could not save content in cache")
|
||||||
|
@ -224,3 +250,12 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
|
||||||
}
|
}
|
||||||
return branch, nil
|
return branch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *Client) getMimeTypeByExtension(resource string) string {
|
||||||
|
mimeType := mime.TypeByExtension(path.Ext(resource))
|
||||||
|
mimeTypeSplit := strings.SplitN(mimeType, ";", 2)
|
||||||
|
if client.forbiddenMimeTypes[mimeTypeSplit[0]] || mimeType == "" {
|
||||||
|
mimeType = client.defaultMimeType
|
||||||
|
}
|
||||||
|
return mimeType
|
||||||
|
}
|
||||||
|
|
|
@ -134,11 +134,7 @@ func Handler(mainDomainSuffix, rawDomain string,
|
||||||
log.Debug().Msg("raw domain")
|
log.Debug().Msg("raw domain")
|
||||||
|
|
||||||
targetOptions.TryIndexPages = false
|
targetOptions.TryIndexPages = false
|
||||||
if targetOptions.ForbiddenMimeTypes == nil {
|
targetOptions.ServeRaw = true
|
||||||
targetOptions.ForbiddenMimeTypes = make(map[string]bool)
|
|
||||||
}
|
|
||||||
targetOptions.ForbiddenMimeTypes["text/html"] = true
|
|
||||||
targetOptions.DefaultMimeType = "text/plain; charset=utf-8"
|
|
||||||
|
|
||||||
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
|
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
|
||||||
if len(pathElements) < 2 {
|
if len(pathElements) < 2 {
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
package upstream
|
package upstream
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"mime"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"codeberg.org/codeberg/pages/server/gitea"
|
"codeberg.org/codeberg/pages/server/gitea"
|
||||||
|
@ -34,23 +30,3 @@ func GetBranchTimestamp(giteaClient *gitea.Client, owner, repo, branch string) *
|
||||||
log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)
|
log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)
|
||||||
return timestamp
|
return timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Options) getMimeTypeByExtension() string {
|
|
||||||
if o.ForbiddenMimeTypes == nil {
|
|
||||||
o.ForbiddenMimeTypes = make(map[string]bool)
|
|
||||||
}
|
|
||||||
mimeType := mime.TypeByExtension(path.Ext(o.TargetPath))
|
|
||||||
mimeTypeSplit := strings.SplitN(mimeType, ";", 2)
|
|
||||||
if o.ForbiddenMimeTypes[mimeTypeSplit[0]] || mimeType == "" {
|
|
||||||
if o.DefaultMimeType != "" {
|
|
||||||
mimeType = o.DefaultMimeType
|
|
||||||
} else {
|
|
||||||
mimeType = "application/octet-stream"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mimeType
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o *Options) generateUriClientArgs() (targetOwner, targetRepo, ref, resource string) {
|
|
||||||
return o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath
|
|
||||||
}
|
|
||||||
|
|
|
@ -19,6 +19,8 @@ const (
|
||||||
headerETag = "ETag"
|
headerETag = "ETag"
|
||||||
headerLastModified = "Last-Modified"
|
headerLastModified = "Last-Modified"
|
||||||
headerIfModifiedSince = "If-Modified-Since"
|
headerIfModifiedSince = "If-Modified-Since"
|
||||||
|
|
||||||
|
rawMime = "text/plain; charset=utf-8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// upstreamIndexPages lists pages that may be considered as index pages for directories.
|
// upstreamIndexPages lists pages that may be considered as index pages for directories.
|
||||||
|
@ -41,13 +43,13 @@ type Options struct {
|
||||||
// Used for debugging purposes.
|
// Used for debugging purposes.
|
||||||
Host string
|
Host string
|
||||||
|
|
||||||
DefaultMimeType string
|
TryIndexPages bool
|
||||||
ForbiddenMimeTypes map[string]bool
|
BranchTimestamp time.Time
|
||||||
TryIndexPages bool
|
|
||||||
BranchTimestamp time.Time
|
|
||||||
// internal
|
// internal
|
||||||
appendTrailingSlash bool
|
appendTrailingSlash bool
|
||||||
redirectIfExists string
|
redirectIfExists string
|
||||||
|
|
||||||
|
ServeRaw bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
||||||
|
@ -80,7 +82,10 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
}
|
}
|
||||||
log.Debug().Msg("preparations")
|
log.Debug().Msg("preparations")
|
||||||
|
|
||||||
reader, res, err := giteaClient.ServeRawContent(o.generateUriClientArgs())
|
reader, res, err := giteaClient.ServeRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath)
|
||||||
|
if reader != nil {
|
||||||
|
defer reader.Close()
|
||||||
|
}
|
||||||
log.Debug().Msg("acquisition")
|
log.Debug().Msg("acquisition")
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
|
@ -142,13 +147,14 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client) (fin
|
||||||
}
|
}
|
||||||
log.Debug().Msg("error handling")
|
log.Debug().Msg("error handling")
|
||||||
|
|
||||||
// Set the MIME type
|
// Set ETag & MIME
|
||||||
mimeType := o.getMimeTypeByExtension()
|
|
||||||
ctx.Response().Header.Set(headerContentType, mimeType)
|
|
||||||
|
|
||||||
// Set ETag
|
|
||||||
if res != nil {
|
if res != nil {
|
||||||
ctx.Response().Header.Set(headerETag, res.Header.Get(headerETag))
|
ctx.Response().Header.Set(headerETag, res.Header.Get(headerETag))
|
||||||
|
if o.ServeRaw {
|
||||||
|
ctx.Response().Header.Set(headerContentType, res.Header.Get(headerContentType))
|
||||||
|
} else {
|
||||||
|
ctx.Response().Header.Set(headerContentType, rawMime)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Response().StatusCode != http.StatusNotFound {
|
if ctx.Response().StatusCode != http.StatusNotFound {
|
||||||
|
|
Loading…
Reference in a new issue