2022-07-14 18:07:07 +01:00
|
|
|
package pageHandler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.captainalm.com/cityuni-webserver/conf"
|
2022-07-15 12:18:33 +01:00
|
|
|
"golang.captainalm.com/cityuni-webserver/pageHandler/utils"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2022-07-14 18:07:07 +01:00
|
|
|
"net/http"
|
2022-07-15 12:18:33 +01:00
|
|
|
"net/textproto"
|
|
|
|
"strconv"
|
2022-07-14 23:25:43 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-07-15 12:18:33 +01:00
|
|
|
"time"
|
2022-07-14 18:07:07 +01:00
|
|
|
)
|
|
|
|
|
2022-07-28 01:56:00 +01:00
|
|
|
const indexName = "index.go"
|
|
|
|
|
2022-07-14 23:25:43 +01:00
|
|
|
type PageHandler struct {
|
2022-07-15 12:18:33 +01:00
|
|
|
PageContentsCache map[string]*CachedPage
|
2022-07-14 23:25:43 +01:00
|
|
|
PageProviders map[string]PageProvider
|
|
|
|
pageContentsCacheRWMutex *sync.RWMutex
|
|
|
|
RangeSupported bool
|
|
|
|
CacheSettings conf.CacheSettingsYaml
|
|
|
|
}
|
|
|
|
|
2022-07-15 12:18:33 +01:00
|
|
|
type CachedPage struct {
|
|
|
|
Content []byte
|
|
|
|
ContentType string
|
|
|
|
LastMod time.Time
|
|
|
|
}
|
|
|
|
|
2022-07-14 23:25:43 +01:00
|
|
|
func NewPageHandler(config conf.ServeYaml) *PageHandler {
|
2022-07-15 12:18:33 +01:00
|
|
|
var thePCCMap map[string]*CachedPage
|
2022-07-14 23:25:43 +01:00
|
|
|
var theMutex *sync.RWMutex
|
|
|
|
if config.CacheSettings.EnableContentsCaching {
|
2022-07-15 12:18:33 +01:00
|
|
|
thePCCMap = make(map[string]*CachedPage)
|
2022-07-14 23:25:43 +01:00
|
|
|
theMutex = &sync.RWMutex{}
|
|
|
|
}
|
2022-07-16 00:53:50 +01:00
|
|
|
toReturn := &PageHandler{
|
2022-07-14 23:25:43 +01:00
|
|
|
PageContentsCache: thePCCMap,
|
|
|
|
pageContentsCacheRWMutex: theMutex,
|
|
|
|
RangeSupported: config.RangeSupported,
|
|
|
|
CacheSettings: config.CacheSettings,
|
|
|
|
}
|
2022-07-16 00:53:50 +01:00
|
|
|
if config.EnableGoInfoPage {
|
2023-09-19 23:27:32 +01:00
|
|
|
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.GetDataStoragePath(), toReturn, config.GetTemplateStoragePath(), config.PageSettings, config.YmlDataFallback)
|
2022-07-16 00:53:50 +01:00
|
|
|
} else {
|
2023-09-19 23:27:32 +01:00
|
|
|
toReturn.PageProviders = GetProviders(config.CacheSettings.EnableTemplateCaching, config.GetDataStoragePath(), nil, config.GetTemplateStoragePath(), config.PageSettings, config.YmlDataFallback)
|
2022-07-16 00:53:50 +01:00
|
|
|
}
|
|
|
|
return toReturn
|
2022-07-14 23:25:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ph *PageHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
2022-07-28 01:56:00 +01:00
|
|
|
actualPagePath := ""
|
|
|
|
if strings.HasSuffix(request.URL.Path, "/") {
|
|
|
|
if strings.HasSuffix(request.URL.Path, ".go/") {
|
|
|
|
actualPagePath = strings.TrimRight(request.URL.Path, "/")
|
|
|
|
} else {
|
|
|
|
actualPagePath = request.URL.Path + indexName
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
actualPagePath = request.URL.Path
|
|
|
|
}
|
2022-07-15 12:18:33 +01:00
|
|
|
|
2022-07-16 11:44:38 +01:00
|
|
|
var currentProvider PageProvider
|
|
|
|
canCache := false
|
|
|
|
actualQueries := ""
|
|
|
|
queryValues := request.URL.Query()
|
2022-07-15 12:18:33 +01:00
|
|
|
var pageContent []byte
|
2022-07-16 11:44:38 +01:00
|
|
|
pageContentType := ""
|
2022-07-15 12:18:33 +01:00
|
|
|
var lastMod time.Time
|
|
|
|
|
2022-07-16 11:44:38 +01:00
|
|
|
if currentProvider = ph.PageProviders[actualPagePath]; currentProvider != nil {
|
|
|
|
actualQueries = currentProvider.GetCacheIDExtension(queryValues)
|
|
|
|
|
|
|
|
if ph.CacheSettings.EnableContentsCaching {
|
2022-07-28 01:56:00 +01:00
|
|
|
cached := ph.getPageFromCache(request.URL.Path, actualQueries)
|
2022-07-16 11:44:38 +01:00
|
|
|
if cached != nil {
|
|
|
|
pageContent = cached.Content
|
|
|
|
pageContentType = cached.ContentType
|
|
|
|
lastMod = cached.LastMod
|
|
|
|
}
|
2022-07-15 12:18:33 +01:00
|
|
|
}
|
|
|
|
|
2022-07-16 11:44:38 +01:00
|
|
|
if pageContentType == "" {
|
|
|
|
pageContentType, pageContent, canCache = currentProvider.GetContents(queryValues)
|
|
|
|
lastMod = currentProvider.GetLastModified()
|
2022-07-15 15:37:59 +01:00
|
|
|
if pageContentType != "" && canCache && ph.CacheSettings.EnableContentsCaching {
|
2022-07-28 01:56:00 +01:00
|
|
|
ph.setPageToCache(request.URL.Path, actualQueries, &CachedPage{
|
2022-07-15 12:18:33 +01:00
|
|
|
Content: pageContent,
|
|
|
|
ContentType: pageContentType,
|
|
|
|
LastMod: lastMod,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedMethods := ph.getAllowedMethodsForPath(request.URL.Path)
|
|
|
|
allowed := false
|
|
|
|
if request.Method != http.MethodOptions {
|
|
|
|
for _, method := range allowedMethods {
|
|
|
|
if method == request.Method {
|
|
|
|
allowed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if allowed {
|
|
|
|
|
|
|
|
if pageContentType == "" {
|
|
|
|
utils.WriteResponseHeaderCanWriteBody(request.Method, writer, http.StatusNotFound, "Page Not Found")
|
|
|
|
} else {
|
|
|
|
|
|
|
|
switch request.Method {
|
|
|
|
case http.MethodGet, http.MethodHead:
|
|
|
|
|
|
|
|
writer.Header().Set("Content-Type", pageContentType)
|
|
|
|
writer.Header().Set("Content-Length", strconv.Itoa(len(pageContent)))
|
|
|
|
utils.SetLastModifiedHeader(writer.Header(), lastMod)
|
|
|
|
utils.SetCacheHeaderWithAge(writer.Header(), ph.CacheSettings.MaxAge, lastMod)
|
|
|
|
theETag := utils.GetValueForETagUsingByteArray(pageContent)
|
|
|
|
writer.Header().Set("ETag", theETag)
|
|
|
|
|
|
|
|
if utils.ProcessSupportedPreconditionsForNext(writer, request, lastMod, theETag, ph.CacheSettings.NotModifiedResponseUsingLastModified, ph.CacheSettings.NotModifiedResponseUsingETags) {
|
|
|
|
|
|
|
|
httpRangeParts := utils.ProcessRangePreconditions(int64(len(pageContent)), writer, request, lastMod, theETag, ph.RangeSupported)
|
|
|
|
if httpRangeParts != nil {
|
|
|
|
if len(httpRangeParts) <= 1 {
|
|
|
|
var theWriter io.Writer = writer
|
|
|
|
if len(httpRangeParts) == 1 {
|
|
|
|
theWriter = utils.NewPartialRangeWriter(theWriter, httpRangeParts[0])
|
|
|
|
}
|
|
|
|
_, _ = theWriter.Write(pageContent)
|
|
|
|
} else {
|
|
|
|
multWriter := multipart.NewWriter(writer)
|
|
|
|
writer.Header().Set("Content-Type", "multipart/byteranges; boundary="+multWriter.Boundary())
|
|
|
|
for _, currentPart := range httpRangeParts {
|
|
|
|
mimePart, err := multWriter.CreatePart(textproto.MIMEHeader{
|
|
|
|
"Content-Range": {currentPart.ToField(int64(len(pageContent)))},
|
|
|
|
"Content-Type": {"text/plain; charset=utf-8"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err = mimePart.Write(pageContent[currentPart.Start : currentPart.Start+currentPart.Length])
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = multWriter.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case http.MethodDelete:
|
2022-07-28 01:56:00 +01:00
|
|
|
ph.PurgeTemplateCache(actualPagePath, request.URL.Path == "/")
|
2022-07-15 12:18:33 +01:00
|
|
|
ph.PurgeContentsCache(request.URL.Path, actualQueries)
|
|
|
|
utils.SetNeverCacheHeader(writer.Header())
|
|
|
|
utils.WriteResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
theAllowHeaderContents := ""
|
|
|
|
for _, method := range allowedMethods {
|
|
|
|
theAllowHeaderContents += method + ", "
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Allow", strings.TrimSuffix(theAllowHeaderContents, ", "))
|
|
|
|
if request.Method == http.MethodOptions {
|
|
|
|
utils.WriteResponseHeaderCanWriteBody(request.Method, writer, http.StatusOK, "")
|
|
|
|
} else {
|
|
|
|
utils.WriteResponseHeaderCanWriteBody(request.Method, writer, http.StatusMethodNotAllowed, "")
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 23:25:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ph *PageHandler) PurgeContentsCache(path string, query string) {
|
2022-07-15 12:18:33 +01:00
|
|
|
if ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge {
|
2022-07-28 01:56:00 +01:00
|
|
|
if path == "/" {
|
2022-07-14 23:25:43 +01:00
|
|
|
ph.pageContentsCacheRWMutex.Lock()
|
2022-07-15 12:18:33 +01:00
|
|
|
ph.PageContentsCache = make(map[string]*CachedPage)
|
2022-07-14 23:25:43 +01:00
|
|
|
ph.pageContentsCacheRWMutex.Unlock()
|
|
|
|
} else {
|
2022-07-28 01:56:00 +01:00
|
|
|
if strings.HasSuffix(path, ".go/") {
|
2022-07-14 23:25:43 +01:00
|
|
|
ph.pageContentsCacheRWMutex.RLock()
|
|
|
|
toDelete := make([]string, len(ph.PageContentsCache))
|
|
|
|
theSize := 0
|
|
|
|
for cPath := range ph.PageContentsCache {
|
|
|
|
dPath := strings.Split(cPath, "?")[0]
|
|
|
|
if dPath == path || dPath == path[:len(path)-1] {
|
|
|
|
toDelete[theSize] = cPath
|
|
|
|
theSize++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ph.pageContentsCacheRWMutex.RUnlock()
|
|
|
|
ph.pageContentsCacheRWMutex.Lock()
|
|
|
|
for i := 0; i < theSize; i++ {
|
|
|
|
delete(ph.PageContentsCache, toDelete[i])
|
|
|
|
}
|
|
|
|
ph.pageContentsCacheRWMutex.Unlock()
|
2022-07-28 01:56:00 +01:00
|
|
|
return
|
|
|
|
} else if strings.HasSuffix(path, "/") {
|
|
|
|
path += indexName
|
|
|
|
}
|
|
|
|
ph.pageContentsCacheRWMutex.Lock()
|
|
|
|
if query == "" {
|
|
|
|
delete(ph.PageContentsCache, path)
|
2022-07-14 23:25:43 +01:00
|
|
|
} else {
|
2022-07-28 01:56:00 +01:00
|
|
|
delete(ph.PageContentsCache, path+"?"+query)
|
2022-07-14 23:25:43 +01:00
|
|
|
}
|
2022-07-28 01:56:00 +01:00
|
|
|
ph.pageContentsCacheRWMutex.Unlock()
|
2022-07-14 23:25:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 18:07:07 +01:00
|
|
|
|
2022-07-28 01:56:00 +01:00
|
|
|
func (ph *PageHandler) PurgeTemplateCache(path string, all bool) {
|
2022-07-14 23:25:43 +01:00
|
|
|
if ph.CacheSettings.EnableTemplateCaching && ph.CacheSettings.EnableTemplateCachePurge {
|
2022-07-28 01:56:00 +01:00
|
|
|
if all {
|
2022-07-14 23:25:43 +01:00
|
|
|
for _, pageProvider := range ph.PageProviders {
|
|
|
|
pageProvider.PurgeTemplate()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if pageProvider, ok := ph.PageProviders[path]; ok {
|
|
|
|
pageProvider.PurgeTemplate()
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 18:07:07 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-28 01:56:00 +01:00
|
|
|
func (ph *PageHandler) getPageFromCache(pathIn string, cleanedQueries string) *CachedPage {
|
2022-07-15 12:18:33 +01:00
|
|
|
ph.pageContentsCacheRWMutex.RLock()
|
|
|
|
defer ph.pageContentsCacheRWMutex.RUnlock()
|
2022-07-28 01:56:00 +01:00
|
|
|
if strings.HasSuffix(pathIn, ".go/") {
|
|
|
|
return ph.PageContentsCache[strings.TrimRight(pathIn, "/")]
|
|
|
|
} else if strings.HasSuffix(pathIn, "/") {
|
|
|
|
pathIn += indexName
|
|
|
|
}
|
|
|
|
if cleanedQueries == "" {
|
|
|
|
return ph.PageContentsCache[pathIn]
|
2022-07-15 12:18:33 +01:00
|
|
|
} else {
|
2022-07-28 01:56:00 +01:00
|
|
|
return ph.PageContentsCache[pathIn+"?"+cleanedQueries]
|
2022-07-15 12:18:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 01:56:00 +01:00
|
|
|
func (ph *PageHandler) setPageToCache(pathIn string, cleanedQueries string, newPage *CachedPage) {
|
2022-07-15 12:18:33 +01:00
|
|
|
ph.pageContentsCacheRWMutex.Lock()
|
|
|
|
defer ph.pageContentsCacheRWMutex.Unlock()
|
2022-07-28 01:56:00 +01:00
|
|
|
if strings.HasSuffix(pathIn, ".go/") {
|
|
|
|
ph.PageContentsCache[strings.TrimRight(pathIn, "/")] = newPage
|
|
|
|
return
|
|
|
|
} else if strings.HasSuffix(pathIn, "/") {
|
|
|
|
pathIn += indexName
|
|
|
|
}
|
|
|
|
if cleanedQueries == "" {
|
|
|
|
ph.PageContentsCache[pathIn] = newPage
|
2022-07-15 12:18:33 +01:00
|
|
|
} else {
|
2022-07-28 01:56:00 +01:00
|
|
|
ph.PageContentsCache[pathIn+"?"+cleanedQueries] = newPage
|
2022-07-15 12:18:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ph *PageHandler) getAllowedMethodsForPath(pathIn string) []string {
|
2022-07-28 01:56:00 +01:00
|
|
|
if pathIn == "/" || strings.HasSuffix(pathIn, ".go/") {
|
2022-07-15 12:18:33 +01:00
|
|
|
if (ph.CacheSettings.EnableTemplateCaching && ph.CacheSettings.EnableTemplateCachePurge) ||
|
|
|
|
(ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge) {
|
|
|
|
return []string{http.MethodHead, http.MethodGet, http.MethodOptions, http.MethodDelete}
|
|
|
|
} else {
|
|
|
|
return []string{http.MethodHead, http.MethodGet, http.MethodOptions}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ph.CacheSettings.EnableContentsCaching && ph.CacheSettings.EnableContentsCachePurge {
|
|
|
|
return []string{http.MethodHead, http.MethodGet, http.MethodOptions, http.MethodDelete}
|
|
|
|
} else {
|
|
|
|
return []string{http.MethodHead, http.MethodGet, http.MethodOptions}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-16 00:53:50 +01:00
|
|
|
|
|
|
|
func (ph *PageHandler) GetRegisteredPages() []string {
|
|
|
|
pages := make([]string, len(ph.PageProviders))
|
|
|
|
index := 0
|
|
|
|
for s := range ph.PageProviders {
|
|
|
|
pages[index] = s
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ph *PageHandler) GetCachedPages() []string {
|
2023-09-19 23:27:32 +01:00
|
|
|
if ph.pageContentsCacheRWMutex == nil {
|
|
|
|
return make([]string, 0)
|
|
|
|
}
|
2022-07-16 11:44:38 +01:00
|
|
|
ph.pageContentsCacheRWMutex.RLock()
|
|
|
|
defer ph.pageContentsCacheRWMutex.RUnlock()
|
2022-07-16 00:53:50 +01:00
|
|
|
pages := make([]string, len(ph.PageContentsCache))
|
|
|
|
index := 0
|
|
|
|
for s := range ph.PageContentsCache {
|
|
|
|
pages[index] = s
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
return pages
|
|
|
|
}
|
2022-07-16 11:44:38 +01:00
|
|
|
|
|
|
|
func (ph *PageHandler) GetNumberOfCachedPages() int {
|
2023-09-19 23:27:32 +01:00
|
|
|
if ph.pageContentsCacheRWMutex == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2022-07-16 11:44:38 +01:00
|
|
|
ph.pageContentsCacheRWMutex.RLock()
|
|
|
|
defer ph.pageContentsCacheRWMutex.RUnlock()
|
|
|
|
return len(ph.PageContentsCache)
|
|
|
|
}
|