2016-11-29 09:26:36 -07:00
// Copyright 2016 The Gitea Authors. All rights reserved.
2022-11-27 11:20:29 -07:00
// SPDX-License-Identifier: MIT
2016-11-29 09:26:36 -07:00
package public
2017-01-28 15:14:56 -07:00
import (
2023-04-12 04:16:45 -06:00
"bytes"
"io"
2018-02-03 15:37:05 -07:00
"net/http"
2021-05-30 04:25:11 -06:00
"os"
2023-04-12 04:16:45 -06:00
"path"
2018-02-03 15:37:05 -07:00
"strings"
2023-04-12 04:16:45 -06:00
"time"
2017-01-28 15:14:56 -07:00
2023-04-12 04:16:45 -06:00
"code.gitea.io/gitea/modules/assetfs"
2022-10-11 23:18:26 -06:00
"code.gitea.io/gitea/modules/container"
2020-11-17 15:44:52 -07:00
"code.gitea.io/gitea/modules/httpcache"
2021-05-30 04:25:11 -06:00
"code.gitea.io/gitea/modules/log"
2017-01-28 15:14:56 -07:00
"code.gitea.io/gitea/modules/setting"
2023-03-08 05:17:39 -07:00
"code.gitea.io/gitea/modules/util"
2017-01-28 15:14:56 -07:00
)
2023-04-12 04:16:45 -06:00
func CustomAssets ( ) * assetfs . Layer {
return assetfs . Local ( "custom" , setting . CustomPath , "public" )
2016-11-29 09:26:36 -07:00
}
2017-01-28 15:14:56 -07:00
2023-04-12 04:16:45 -06:00
func AssetFS ( ) * assetfs . LayeredFS {
return assetfs . Layered ( CustomAssets ( ) , BuiltinAssets ( ) )
}
2022-01-20 04:41:25 -07:00
2023-07-21 06:14:20 -06:00
// FileHandlerFunc implements the static handler for serving files in "public" assets
func FileHandlerFunc ( ) http . HandlerFunc {
2023-04-12 04:16:45 -06:00
assetFS := AssetFS ( )
2022-01-20 04:41:25 -07:00
return func ( resp http . ResponseWriter , req * http . Request ) {
2023-04-12 04:16:45 -06:00
if req . Method != "GET" && req . Method != "HEAD" {
2023-12-25 06:01:24 -07:00
resp . WriteHeader ( http . StatusMethodNotAllowed )
2022-01-20 04:41:25 -07:00
return
}
2023-07-21 06:14:20 -06:00
handleRequest ( resp , req , assetFS , req . URL . Path )
2018-02-03 15:37:05 -07:00
}
}
2020-12-23 21:25:17 -07:00
// parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
2022-10-11 23:18:26 -06:00
func parseAcceptEncoding ( val string ) container . Set [ string ] {
2020-12-23 21:25:17 -07:00
parts := strings . Split ( val , ";" )
2022-10-11 23:18:26 -06:00
types := make ( container . Set [ string ] )
2020-12-23 21:25:17 -07:00
for _ , v := range strings . Split ( parts [ 0 ] , "," ) {
2022-10-11 23:18:26 -06:00
types . Add ( strings . TrimSpace ( v ) )
2020-12-23 21:25:17 -07:00
}
return types
}
2022-01-23 05:19:49 -07:00
// setWellKnownContentType will set the Content-Type if the file is a well-known type.
// See the comments of detectWellKnownMimeType
func setWellKnownContentType ( w http . ResponseWriter , file string ) {
2023-04-12 04:16:45 -06:00
mimeType := detectWellKnownMimeType ( path . Ext ( file ) )
2022-01-23 05:19:49 -07:00
if mimeType != "" {
w . Header ( ) . Set ( "Content-Type" , mimeType )
}
}
2023-07-21 06:14:20 -06:00
func handleRequest ( w http . ResponseWriter , req * http . Request , fs http . FileSystem , file string ) {
2023-03-21 14:02:49 -06:00
// actually, fs (http.FileSystem) is designed to be a safe interface, relative paths won't bypass its parent directory, it's also fine to do a clean here
2023-07-21 06:14:20 -06:00
f , err := fs . Open ( util . PathJoinRelX ( file ) )
2018-02-03 15:37:05 -07:00
if err != nil {
2021-05-30 04:25:11 -06:00
if os . IsNotExist ( err ) {
2023-07-21 06:14:20 -06:00
w . WriteHeader ( http . StatusNotFound )
return
2020-04-18 15:01:06 -06:00
}
2021-05-30 04:25:11 -06:00
w . WriteHeader ( http . StatusInternalServerError )
log . Error ( "[Static] Open %q failed: %v" , file , err )
2023-07-21 06:14:20 -06:00
return
2018-02-03 15:37:05 -07:00
}
defer f . Close ( )
fi , err := f . Stat ( )
if err != nil {
2021-05-30 04:25:11 -06:00
w . WriteHeader ( http . StatusInternalServerError )
log . Error ( "[Static] %q exists, but fails to open: %v" , file , err )
2023-07-21 06:14:20 -06:00
return
2018-02-03 15:37:05 -07:00
}
2023-07-21 06:14:20 -06:00
// need to serve index file? (no at the moment)
2018-02-03 15:37:05 -07:00
if fi . IsDir ( ) {
2021-05-30 04:25:11 -06:00
w . WriteHeader ( http . StatusNotFound )
2023-07-21 06:14:20 -06:00
return
2018-02-03 15:37:05 -07:00
}
2021-05-30 04:25:11 -06:00
serveContent ( w , req , fi , fi . ModTime ( ) , f )
2018-02-03 15:37:05 -07:00
}
2023-04-12 04:16:45 -06:00
type GzipBytesProvider interface {
GzipBytes ( ) [ ] byte
}
// serveContent serve http content
func serveContent ( w http . ResponseWriter , req * http . Request , fi os . FileInfo , modtime time . Time , content io . ReadSeeker ) {
setWellKnownContentType ( w , fi . Name ( ) )
encodings := parseAcceptEncoding ( req . Header . Get ( "Accept-Encoding" ) )
if encodings . Contains ( "gzip" ) {
// try to provide gzip content directly from bindata (provided by vfsgen۰ CompressedFileInfo)
if compressed , ok := fi . ( GzipBytesProvider ) ; ok {
rdGzip := bytes . NewReader ( compressed . GzipBytes ( ) )
// all gzipped static files (from bindata) are managed by Gitea, so we can make sure every file has the correct ext name
// then we can get the correct Content-Type, we do not need to do http.DetectContentType on the decompressed data
if w . Header ( ) . Get ( "Content-Type" ) == "" {
w . Header ( ) . Set ( "Content-Type" , "application/octet-stream" )
}
w . Header ( ) . Set ( "Content-Encoding" , "gzip" )
2023-05-13 08:04:57 -06:00
httpcache . ServeContentWithCacheControl ( w , req , fi . Name ( ) , modtime , rdGzip )
2023-04-12 04:16:45 -06:00
return
}
}
2023-05-13 08:04:57 -06:00
httpcache . ServeContentWithCacheControl ( w , req , fi . Name ( ) , modtime , content )
2023-04-12 04:16:45 -06:00
return
}