mirror of
https://github.com/simongregorebner/gitea-pages.git
synced 2025-04-20 01:40:03 +02:00
improve path handling if path query parameters are present
This commit is contained in:
parent
02fae34477
commit
ec8227b98d
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
caddy
|
caddy
|
||||||
Caddyfile
|
Caddyfile
|
||||||
|
Caddyfile_gitea-test.psi.ch
|
@ -6,6 +6,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
@ -103,12 +104,18 @@ func (module *GiteaPagesModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
|
|||||||
func (module GiteaPagesModule) ServeHTTP(writer http.ResponseWriter, request *http.Request, _ caddyhttp.Handler) error {
|
func (module GiteaPagesModule) ServeHTTP(writer http.ResponseWriter, request *http.Request, _ caddyhttp.Handler) error {
|
||||||
|
|
||||||
var organization, repository, path string
|
var organization, repository, path string
|
||||||
|
|
||||||
|
// the path might have url query parameters e.g. "?h=vpn" - this need to be stripped of
|
||||||
|
parsedUrl, _ := url.Parse(request.URL.Path)
|
||||||
|
parsedUrl.RawQuery = "" // Clear the query parameters
|
||||||
|
path = parsedUrl.String()
|
||||||
|
|
||||||
if module.URLScheme == "simple" {
|
if module.URLScheme == "simple" {
|
||||||
// "Simple" URL case - we expect the organization and repository in the URL
|
// "Simple" URL case - we expect the organization and repository in the URL
|
||||||
// The URL/path looks like http(s)://<giteaserver>[:<port>]/<organization>/<repository>[/<filepath>]
|
// The URL/path looks like http(s)://<giteaserver>[:<port>]/<organization>/<repository>[/<filepath>]
|
||||||
|
|
||||||
// Remove a potential "/" prefix and trailing "/" - then split up the path
|
// Remove a potential "/" prefix and trailing "/" - then split up the path
|
||||||
parts := strings.Split(strings.TrimSuffix(strings.TrimPrefix(request.URL.Path, "/"), "/"), "/")
|
parts := strings.Split(strings.TrimSuffix(strings.TrimPrefix(path, "/"), "/"), "/")
|
||||||
|
|
||||||
length := len(parts)
|
length := len(parts)
|
||||||
if length <= 1 {
|
if length <= 1 {
|
||||||
@ -130,7 +137,7 @@ func (module GiteaPagesModule) ServeHTTP(writer http.ResponseWriter, request *ht
|
|||||||
organization = strings.Split(request.Host, ".")[0]
|
organization = strings.Split(request.Host, ".")[0]
|
||||||
|
|
||||||
// Remove a potential "/" prefix and trailing "/" - then split up the path
|
// Remove a potential "/" prefix and trailing "/" - then split up the path
|
||||||
path = strings.TrimSuffix(strings.TrimPrefix(request.URL.Path, "/"), "/")
|
path = strings.TrimSuffix(strings.TrimPrefix(path, "/"), "/")
|
||||||
|
|
||||||
if path == "" {
|
if path == "" {
|
||||||
// Case http(s)://<organization>.<giteaserver>[:<port>]
|
// Case http(s)://<organization>.<giteaserver>[:<port>]
|
||||||
@ -177,12 +184,20 @@ func (module GiteaPagesModule) ServeHTTP(writer http.ResponseWriter, request *ht
|
|||||||
// Handle request
|
// Handle request
|
||||||
content, err := module.getFile(organization, repository, module.PagesBranch, path)
|
content, err := module.getFile(organization, repository, module.PagesBranch, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
module.Logger.Error("Unable to retrieve file - error: " + err.Error())
|
module.Logger.Error("Unable to retrieve file: " + path + " - error: " + err.Error())
|
||||||
return caddyhttp.Error(http.StatusNotFound, err)
|
|
||||||
|
// append an index.html and retry
|
||||||
|
path = path + "/index.html"
|
||||||
|
content, err = module.getFile(organization, repository, module.PagesBranch, path)
|
||||||
|
if err != nil {
|
||||||
|
module.Logger.Error("Unable to retrieve file: " + path + " - error: " + err.Error())
|
||||||
|
return caddyhttp.Error(http.StatusNotFound, err)
|
||||||
|
}
|
||||||
|
// return caddyhttp.Error(http.StatusNotFound, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to determine mime type based on extenstion of file
|
// Try to determine mime type based on extenstion of file
|
||||||
parts := strings.Split(request.URL.Path, ".")
|
parts := strings.Split(path, ".")
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
extension := parts[len(parts)-1] // get file extension
|
extension := parts[len(parts)-1] // get file extension
|
||||||
writer.Header().Add("Content-Type", mime.TypeByExtension("."+extension))
|
writer.Header().Add("Content-Type", mime.TypeByExtension("."+extension))
|
||||||
@ -210,21 +225,6 @@ func (module GiteaPagesModule) repoBranchExists(organization, repository, branch
|
|||||||
return branchInfo.Name == branch
|
return branchInfo.Name == branch
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a repo has a specific topic assigned
|
|
||||||
func (module GiteaPagesModule) topicExists(organization, repository, topic string) bool {
|
|
||||||
topics, _, err := module.GiteaClient.ListRepoTopics(organization, repository, gitea.ListRepoTopicsOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, topicName := range topics {
|
|
||||||
if topicName == topic {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interface guards
|
// Interface guards
|
||||||
var (
|
var (
|
||||||
_ caddy.Provisioner = (*GiteaPagesModule)(nil)
|
_ caddy.Provisioner = (*GiteaPagesModule)(nil)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user