2026-05-08 07:27:06 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"apigo.cc/go/file"
|
|
|
|
|
"apigo.cc/go/log"
|
|
|
|
|
"mime"
|
|
|
|
|
"net/http"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Static 注册静态文件目录
|
2026-06-02 13:22:23 +08:00
|
|
|
func (hc *HostContext) Static(path, rootPath string) *HostContext {
|
|
|
|
|
host := hc.host
|
|
|
|
|
if host == "*" {
|
|
|
|
|
host = ""
|
|
|
|
|
}
|
2026-06-04 18:16:46 +08:00
|
|
|
hc.ws.StaticByHost(path, rootPath, host)
|
2026-06-02 13:22:23 +08:00
|
|
|
return hc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Static 注册静态文件目录 (使用默认 Host "*")
|
2026-05-08 07:27:06 +08:00
|
|
|
func Static(path, rootPath string) {
|
2026-06-04 18:16:46 +08:00
|
|
|
DefaultServer.Static(path, rootPath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ws *webServer) Static(path, rootPath string) {
|
|
|
|
|
ws.Host("*").Static(path, rootPath)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StaticByHost 为指定域名注册静态文件目录
|
|
|
|
|
func StaticByHost(path, rootPath, host string) {
|
2026-06-04 18:16:46 +08:00
|
|
|
DefaultServer.StaticByHost(path, rootPath, host)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ws *webServer) StaticByHost(path, rootPath, host string) {
|
2026-05-08 07:27:06 +08:00
|
|
|
if !filepath.IsAbs(rootPath) {
|
|
|
|
|
if absPath, err := filepath.Abs(rootPath); err == nil {
|
|
|
|
|
rootPath = absPath
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
ws.staticsByHostLock.Lock()
|
|
|
|
|
defer ws.staticsByHostLock.Unlock()
|
2026-05-08 07:27:06 +08:00
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
if ws.codeStatics[host] == nil {
|
|
|
|
|
ws.codeStatics[host] = make(map[string]*string)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|
2026-06-04 18:16:46 +08:00
|
|
|
ws.codeStatics[host][path] = &rootPath
|
|
|
|
|
ws.rebuildStaticsUnderLock(host)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|
2026-05-12 23:18:31 +08:00
|
|
|
|
2026-05-12 23:53:16 +08:00
|
|
|
// ReplaceStatics 使用 Copy-on-Write 机制原子地替换指定 host 下的动态静态目录规则
|
2026-05-12 23:18:31 +08:00
|
|
|
func ReplaceStatics(host string, config map[string]string) {
|
2026-06-04 18:16:46 +08:00
|
|
|
DefaultServer.ReplaceStatics(host, config)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ws *webServer) ReplaceStatics(host string, config map[string]string) {
|
2026-05-12 23:18:31 +08:00
|
|
|
newStatics := make(map[string]*string, len(config))
|
|
|
|
|
for path, rootPath := range config {
|
|
|
|
|
rp := rootPath
|
|
|
|
|
if !filepath.IsAbs(rp) {
|
|
|
|
|
if absPath, err := filepath.Abs(rp); err == nil {
|
|
|
|
|
rp = absPath
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
newStatics[path] = &rp
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
ws.staticsByHostLock.Lock()
|
|
|
|
|
defer ws.staticsByHostLock.Unlock()
|
2026-05-12 23:53:16 +08:00
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
ws.dynamicStatics[host] = newStatics
|
|
|
|
|
ws.rebuildStaticsUnderLock(host)
|
2026-05-12 23:18:31 +08:00
|
|
|
}
|
2026-05-08 07:27:06 +08:00
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
func (ws *webServer) getStaticFilePath(requestPath, host string) string {
|
|
|
|
|
ws.staticsByHostLock.RLock()
|
|
|
|
|
defer ws.staticsByHostLock.RUnlock()
|
2026-05-08 07:27:06 +08:00
|
|
|
|
|
|
|
|
// 优先匹配指定域名的配置
|
2026-06-04 18:16:46 +08:00
|
|
|
if hostConfig, exists := ws.staticsByHost[host]; exists {
|
|
|
|
|
if filePath := ws.findMatchedPath(hostConfig, requestPath); filePath != "" {
|
2026-05-08 07:27:06 +08:00
|
|
|
return filePath
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 匹配全局配置
|
2026-06-04 18:16:46 +08:00
|
|
|
return ws.findMatchedPath(ws.statics, requestPath)
|
2026-05-08 07:27:06 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
func (ws *webServer) findMatchedPath(config map[string]*string, requestPath string) string {
|
2026-05-08 07:27:06 +08:00
|
|
|
for urlPath, rootPath := range config {
|
|
|
|
|
if strings.HasPrefix(requestPath, urlPath) {
|
|
|
|
|
return filepath.Join(*rootPath, requestPath[len(urlPath):])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-04 18:16:46 +08:00
|
|
|
func (ws *webServer) processStatic(requestPath string, request *Request, response *Response, logger *log.Logger) bool {
|
|
|
|
|
filePath := ws.getStaticFilePath(requestPath, request.Host)
|
2026-05-08 07:27:06 +08:00
|
|
|
if filePath == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 16:39:20 +08:00
|
|
|
info := file.GetFileInfo(filePath)
|
|
|
|
|
if info == nil {
|
2026-05-08 07:27:06 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 16:39:20 +08:00
|
|
|
if info.IsDir {
|
2026-05-08 07:27:06 +08:00
|
|
|
// 自动查找索引文件
|
2026-06-04 18:16:46 +08:00
|
|
|
indexFiles := ws.Config.IndexFiles
|
|
|
|
|
if len(indexFiles) == 0 {
|
|
|
|
|
indexFiles = []string{"index.html", "index.htm"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, indexFile := range indexFiles {
|
2026-05-08 07:27:06 +08:00
|
|
|
f := filepath.Join(filePath, indexFile)
|
2026-05-09 16:39:20 +08:00
|
|
|
if i := file.GetFileInfo(f); i != nil && !i.IsDir {
|
2026-05-08 07:27:06 +08:00
|
|
|
filePath = f
|
|
|
|
|
info = i
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 16:39:20 +08:00
|
|
|
if info.IsDir {
|
2026-05-08 07:27:06 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查 304
|
|
|
|
|
if ifModifiedSince := request.Header.Get("If-Modified-Since"); ifModifiedSince != "" {
|
|
|
|
|
if t, err := time.Parse(http.TimeFormat, ifModifiedSince); err == nil {
|
2026-05-09 16:39:20 +08:00
|
|
|
if time.Unix(info.ModTime, 0).Truncate(time.Second).Before(t.Truncate(time.Second)) ||
|
|
|
|
|
time.Unix(info.ModTime, 0).Truncate(time.Second).Equal(t.Truncate(time.Second)) {
|
2026-05-08 07:27:06 +08:00
|
|
|
response.WriteHeader(http.StatusNotModified)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送文件
|
|
|
|
|
contentType := mime.TypeByExtension(filepath.Ext(filePath))
|
|
|
|
|
if contentType == "" {
|
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
|
}
|
|
|
|
|
response.Header().Set("Content-Type", contentType)
|
2026-05-09 16:39:20 +08:00
|
|
|
response.Header().Set("Last-Modified", time.Unix(info.ModTime, 0).UTC().Format(http.TimeFormat))
|
2026-05-08 07:27:06 +08:00
|
|
|
|
|
|
|
|
data, err := file.ReadBytes(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, _ = response.Write(data)
|
|
|
|
|
return true
|
|
|
|
|
}
|