package upstream import ( "errors" "fmt" "io" "net/http" "strings" "time" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "pages-server/html" "pages-server/server/cache" "pages-server/server/context" "pages-server/server/gitea" ) const ( headerLastModified = "Last-Modified" headerIfModifiedSince = "If-Modified-Since" rawMime = "text/plain; charset=utf-8" ) var upstreamIndexPages = []string{ "index.html", } var upstreamNotFoundPages = []string{ "404.html", } type Options struct { TargetOwner string TargetRepo string TargetBranch string TargetPath string Host string TryIndexPages bool BranchTimestamp time.Time appendTrailingSlash bool redirectIfExists string ServeRaw bool } func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redirectsCache cache.ICache) bool { log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger() log.Debug().Msg("Start") if o.TargetOwner == "" || o.TargetRepo == "" { html.ReturnErrorPage(ctx, "forge client: either repo owner or name info is missing", http.StatusBadRequest) return false } if o.BranchTimestamp.IsZero() { branchExist, err := o.GetBranchTimestamp(giteaClient) if err != nil && errors.Is(err, gitea.ErrorNotFound) || !branchExist { html.ReturnErrorPage(ctx, fmt.Sprintf("branch %q for %s/%s not found", o.TargetBranch, o.TargetOwner, o.TargetRepo), http.StatusNotFound) return false } if err != nil { html.ReturnErrorPage(ctx, fmt.Sprintf("could not get timestamp of branch %q: '%v'", o.TargetBranch, err), http.StatusFailedDependency) return false } } if ctx.Response() != nil { ifModifiedSince, err := time.Parse(time.RFC1123, ctx.Response().Header.Get(headerIfModifiedSince)) if err == nil && ifModifiedSince.After(o.BranchTimestamp) { ctx.RespWriter.WriteHeader(http.StatusNotModified) log.Trace().Msg("check response against last modified: valid") return true } log.Trace().Msg("check response against last modified: outdated") } reader, header, statusCode, err := giteaClient.ServeRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath) if err != nil { handleGiteaError(ctx, log, err, statusCode) return false } defer func() { if reader != nil { reader.Close() } }() if errors.Is(err, gitea.ErrorNotFound) { handleNotFound(ctx, log, giteaClient, redirectsCache, o) return false } if err != nil || reader == nil || statusCode != http.StatusOK { handleUnexpectedError(ctx, log, err, statusCode) return false } handleRedirects(ctx, log, o, redirectsCache) setHeaders(ctx, header) writeResponse(ctx, reader) return true } func handleGiteaError(ctx *context.Context, log zerolog.Logger, err error, statusCode int) { var msg string if err != nil { msg = "forge client: returned unexpected error" log.Error().Err(err).Msg(msg) msg = fmt.Sprintf("%s: '%v'", msg, err) } if statusCode != http.StatusOK { msg = fmt.Sprintf("forge client: couldn't fetch contents: %d - %s", statusCode, http.StatusText(statusCode)) log.Error().Msg(msg) } html.ReturnErrorPage(ctx, msg, http.StatusInternalServerError) } func handleNotFound(ctx *context.Context, log zerolog.Logger, giteaClient *gitea.Client, redirectsCache cache.ICache, o *Options) { log.Debug().Msg("Handling not found error") redirects := o.getRedirects(giteaClient, redirectsCache) if o.matchRedirects(ctx, giteaClient, redirects, redirectsCache) { log.Trace().Msg("redirect") return } if o.TryIndexPages { log.Trace().Msg("try index page") optionsForIndexPages := *o optionsForIndexPages.TryIndexPages = false optionsForIndexPages.appendTrailingSlash = true for _, indexPage := range upstreamIndexPages { optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) { return } } log.Trace().Msg("try html file with path name") optionsForIndexPages.appendTrailingSlash = false optionsForIndexPages.redirectIfExists = strings.TrimSuffix(ctx.Path(), "/") + ".html" optionsForIndexPages.TargetPath = o.TargetPath + ".html" if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) { return } } log.Trace().Msg("not found") ctx.StatusCode = http.StatusNotFound if o.TryIndexPages { log.Trace().Msg("try not found page") optionsForNotFoundPages := *o optionsForNotFoundPages.TryIndexPages = false optionsForNotFoundPages.appendTrailingSlash = false for _, notFoundPage := range upstreamNotFoundPages { optionsForNotFoundPages.TargetPath = "/" + notFoundPage if optionsForNotFoundPages.Upstream(ctx, giteaClient, redirectsCache) { return } } log.Trace().Msg("not found page missing") } } func handleUnexpectedError(ctx *context.Context, log zerolog.Logger, err error, statusCode int) { var msg string if err != nil { msg = "forge client: returned unexpected error" log.Error().Err(err).Msg(msg) msg = fmt.Sprintf("%s: '%v'", msg, err) } if statusCode != http.StatusOK { msg = fmt.Sprintf("forge client: couldn't fetch contents: %d - %s", statusCode, http.StatusText(statusCode)) log.Error().Msg(msg) } html.ReturnErrorPage(ctx, msg, http.StatusInternalServerError) } func handleRedirects(ctx *context.Context, log zerolog.Logger, o *Options, redirectsCache cache.ICache) { if o.appendTrailingSlash && !strings.HasSuffix(ctx.Path(), "/") { log.Trace().Msg("append trailing slash and redirect") ctx.Redirect(ctx.Path()+"/", http.StatusTemporaryRedirect) return } if strings.HasSuffix(ctx.Path(), "/index.html") && !o.ServeRaw { log.Trace().Msg("remove index.html from path and redirect") ctx.Redirect(strings.TrimSuffix(ctx.Path(), "index.html"), http.StatusTemporaryRedirect) return } if o.redirectIfExists != "" { ctx.Redirect(o.redirectIfExists, http.StatusTemporaryRedirect) return } } func setHeaders(ctx *context.Context, header http.Header) { ctx.RespWriter.Header().Set("ETag", header.Get("ETag")) ctx.RespWriter.Header().Set("Content-Type", header.Get("Content-Type")) } func writeResponse(ctx *context.Context, reader io.Reader) { ctx.RespWriter.WriteHeader(ctx.StatusCode) if reader != nil { _, err := io.Copy(ctx.RespWriter, reader) if err != nil { log.Error().Err(err).Msgf("Couldn't write body for %q", ctx.Path()) html.ReturnErrorPage(ctx, "", http.StatusInternalServerError) } } }