diff --git a/assets/go-licenses.json b/assets/go-licenses.json index 593d6af5c..516c07933 100644 --- a/assets/go-licenses.json +++ b/assets/go-licenses.json @@ -924,11 +924,6 @@ "path": "github.com/unknwon/com/LICENSE", "licenseText": "Apache License\nVersion 2.0, January 2004\nhttp://www.apache.org/licenses/\n\nTERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n1. Definitions.\n\n\"License\" shall mean the terms and conditions for use, reproduction, and\ndistribution as defined by Sections 1 through 9 of this document.\n\n\"Licensor\" shall mean the copyright owner or entity authorized by the copyright\nowner that is granting the License.\n\n\"Legal Entity\" shall mean the union of the acting entity and all other entities\nthat control, are controlled by, or are under common control with that entity.\nFor the purposes of this definition, \"control\" means (i) the power, direct or\nindirect, to cause the direction or management of such entity, whether by\ncontract or otherwise, or (ii) ownership of fifty percent (50%) or more of the\noutstanding shares, or (iii) beneficial ownership of such entity.\n\n\"You\" (or \"Your\") shall mean an individual or Legal Entity exercising\npermissions granted by this License.\n\n\"Source\" form shall mean the preferred form for making modifications, including\nbut not limited to software source code, documentation source, and configuration\nfiles.\n\n\"Object\" form shall mean any form resulting from mechanical transformation or\ntranslation of a Source form, including but not limited to compiled object code,\ngenerated documentation, and conversions to other media types.\n\n\"Work\" shall mean the work of authorship, whether in Source or Object form, made\navailable under the License, as indicated by a copyright notice that is included\nin or attached to the work (an example is provided in the Appendix below).\n\n\"Derivative Works\" shall mean any work, whether in Source or Object form, that\nis based on (or derived from) the Work and for which the editorial revisions,\nannotations, elaborations, or other modifications represent, as a whole, an\noriginal work of authorship. For the purposes of this License, Derivative Works\nshall not include works that remain separable from, or merely link (or bind by\nname) to the interfaces of, the Work and Derivative Works thereof.\n\n\"Contribution\" shall mean any work of authorship, including the original version\nof the Work and any modifications or additions to that Work or Derivative Works\nthereof, that is intentionally submitted to Licensor for inclusion in the Work\nby the copyright owner or by an individual or Legal Entity authorized to submit\non behalf of the copyright owner. For the purposes of this definition,\n\"submitted\" means any form of electronic, verbal, or written communication sent\nto the Licensor or its representatives, including but not limited to\ncommunication on electronic mailing lists, source code control systems, and\nissue tracking systems that are managed by, or on behalf of, the Licensor for\nthe purpose of discussing and improving the Work, but excluding communication\nthat is conspicuously marked or otherwise designated in writing by the copyright\nowner as \"Not a Contribution.\"\n\n\"Contributor\" shall mean Licensor and any individual or Legal Entity on behalf\nof whom a Contribution has been received by Licensor and subsequently\nincorporated within the Work.\n\n2. Grant of Copyright License.\n\nSubject to the terms and conditions of this License, each Contributor hereby\ngrants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,\nirrevocable copyright license to reproduce, prepare Derivative Works of,\npublicly display, publicly perform, sublicense, and distribute the Work and such\nDerivative Works in Source or Object form.\n\n3. Grant of Patent License.\n\nSubject to the terms and conditions of this License, each Contributor hereby\ngrants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,\nirrevocable (except as stated in this section) patent license to make, have\nmade, use, offer to sell, sell, import, and otherwise transfer the Work, where\nsuch license applies only to those patent claims licensable by such Contributor\nthat are necessarily infringed by their Contribution(s) alone or by combination\nof their Contribution(s) with the Work to which such Contribution(s) was\nsubmitted. If You institute patent litigation against any entity (including a\ncross-claim or counterclaim in a lawsuit) alleging that the Work or a\nContribution incorporated within the Work constitutes direct or contributory\npatent infringement, then any patent licenses granted to You under this License\nfor that Work shall terminate as of the date such litigation is filed.\n\n4. Redistribution.\n\nYou may reproduce and distribute copies of the Work or Derivative Works thereof\nin any medium, with or without modifications, and in Source or Object form,\nprovided that You meet the following conditions:\n\nYou must give any other recipients of the Work or Derivative Works a copy of\nthis License; and\nYou must cause any modified files to carry prominent notices stating that You\nchanged the files; and\nYou must retain, in the Source form of any Derivative Works that You distribute,\nall copyright, patent, trademark, and attribution notices from the Source form\nof the Work, excluding those notices that do not pertain to any part of the\nDerivative Works; and\nIf the Work includes a \"NOTICE\" text file as part of its distribution, then any\nDerivative Works that You distribute must include a readable copy of the\nattribution notices contained within such NOTICE file, excluding those notices\nthat do not pertain to any part of the Derivative Works, in at least one of the\nfollowing places: within a NOTICE text file distributed as part of the\nDerivative Works; within the Source form or documentation, if provided along\nwith the Derivative Works; or, within a display generated by the Derivative\nWorks, if and wherever such third-party notices normally appear. The contents of\nthe NOTICE file are for informational purposes only and do not modify the\nLicense. You may add Your own attribution notices within Derivative Works that\nYou distribute, alongside or as an addendum to the NOTICE text from the Work,\nprovided that such additional attribution notices cannot be construed as\nmodifying the License.\nYou may add Your own copyright statement to Your modifications and may provide\nadditional or different license terms and conditions for use, reproduction, or\ndistribution of Your modifications, or for any such Derivative Works as a whole,\nprovided Your use, reproduction, and distribution of the Work otherwise complies\nwith the conditions stated in this License.\n\n5. Submission of Contributions.\n\nUnless You explicitly state otherwise, any Contribution intentionally submitted\nfor inclusion in the Work by You to the Licensor shall be under the terms and\nconditions of this License, without any additional terms or conditions.\nNotwithstanding the above, nothing herein shall supersede or modify the terms of\nany separate license agreement you may have executed with Licensor regarding\nsuch Contributions.\n\n6. Trademarks.\n\nThis License does not grant permission to use the trade names, trademarks,\nservice marks, or product names of the Licensor, except as required for\nreasonable and customary use in describing the origin of the Work and\nreproducing the content of the NOTICE file.\n\n7. Disclaimer of Warranty.\n\nUnless required by applicable law or agreed to in writing, Licensor provides the\nWork (and each Contributor provides its Contributions) on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,\nincluding, without limitation, any warranties or conditions of TITLE,\nNON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are\nsolely responsible for determining the appropriateness of using or\nredistributing the Work and assume any risks associated with Your exercise of\npermissions under this License.\n\n8. Limitation of Liability.\n\nIn no event and under no legal theory, whether in tort (including negligence),\ncontract, or otherwise, unless required by applicable law (such as deliberate\nand grossly negligent acts) or agreed to in writing, shall any Contributor be\nliable to You for damages, including any direct, indirect, special, incidental,\nor consequential damages of any character arising as a result of this License or\nout of the use or inability to use the Work (including but not limited to\ndamages for loss of goodwill, work stoppage, computer failure or malfunction, or\nany and all other commercial damages or losses), even if such Contributor has\nbeen advised of the possibility of such damages.\n\n9. Accepting Warranty or Additional Liability.\n\nWhile redistributing the Work or Derivative Works thereof, You may choose to\noffer, and charge a fee for, acceptance of support, warranty, indemnity, or\nother liability obligations and/or rights consistent with this License. However,\nin accepting such obligations, You may act only on Your own behalf and on Your\nsole responsibility, not on behalf of any other Contributor, and only if You\nagree to indemnify, defend, and hold each Contributor harmless for any liability\nincurred by, or claims asserted against, such Contributor by reason of your\naccepting any such warranty or additional liability.\n\nEND OF TERMS AND CONDITIONS\n\nAPPENDIX: How to apply the Apache License to your work\n\nTo apply the Apache License to your work, attach the following boilerplate\nnotice, with the fields enclosed by brackets \"[]\" replaced with your own\nidentifying information. (Don't include the brackets!) The text should be\nenclosed in the appropriate comment syntax for the file format. We also\nrecommend that a file or class name and description of purpose be included on\nthe same \"printed page\" as the copyright notice for easier identification within\nthird-party archives.\n\n Copyright [yyyy] [name of copyright owner]\n\n Licensed under the Apache License, Version 2.0 (the \"License\");\n you may not use this file except in compliance with the License.\n You may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an \"AS IS\" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n See the License for the specific language governing permissions and\n limitations under the License." }, - { - "name": "github.com/unrolled/render", - "path": "github.com/unrolled/render/LICENSE", - "licenseText": "The MIT License (MIT)\n\nCopyright (c) 2014 Cory Jacobsen\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n" - }, { "name": "github.com/urfave/cli", "path": "github.com/urfave/cli/LICENSE", diff --git a/build/generate-go-licenses.go b/build/generate-go-licenses.go index 7dd1ba38b..bdac51e71 100644 --- a/build/generate-go-licenses.go +++ b/build/generate-go-licenses.go @@ -7,9 +7,10 @@ package main import ( "encoding/json" + "fmt" "io/fs" "os" - goPath "path" + "path" "path/filepath" "regexp" "sort" @@ -27,9 +28,14 @@ type LicenseEntry struct { } func main() { + if len(os.Args) != 3 { + fmt.Println("usage: go run generate-go-licenses.go ") + os.Exit(1) + } + base, out := os.Args[1], os.Args[2] - paths := []string{} + var paths []string err := filepath.WalkDir(base, func(path string, entry fs.DirEntry, err error) error { if err != nil { return err @@ -46,28 +52,27 @@ func main() { sort.Strings(paths) - entries := []LicenseEntry{} - for _, path := range paths { - path := filepath.ToSlash(path) - - licenseText, err := os.ReadFile(path) + var entries []LicenseEntry + for _, filePath := range paths { + licenseText, err := os.ReadFile(filePath) if err != nil { panic(err) } - path = strings.Replace(path, base+"/", "", 1) - name := goPath.Dir(path) + pkgPath := filepath.ToSlash(filePath) + pkgPath = strings.TrimPrefix(pkgPath, base+"/") + pkgName := path.Dir(pkgPath) // There might be a bug somewhere in go-licenses that sometimes interprets the // root package as "." and sometimes as "code.gitea.io/gitea". Workaround by // removing both of them for the sake of stable output. - if name == "." || name == "code.gitea.io/gitea" { + if pkgName == "." || pkgName == "code.gitea.io/gitea" { continue } entries = append(entries, LicenseEntry{ - Name: name, - Path: path, + Name: pkgName, + Path: pkgPath, LicenseText: string(licenseText), }) } diff --git a/go.mod b/go.mod index 4485c641f..837ff555c 100644 --- a/go.mod +++ b/go.mod @@ -96,7 +96,6 @@ require ( github.com/stretchr/testify v1.8.1 github.com/syndtr/goleveldb v1.0.0 github.com/tstranex/u2f v1.0.0 - github.com/unrolled/render v1.5.0 github.com/urfave/cli v1.22.12 github.com/xanzy/go-gitlab v0.80.2 github.com/xeipuuv/gojsonschema v1.2.0 diff --git a/go.sum b/go.sum index 25463143f..753a9d884 100644 --- a/go.sum +++ b/go.sum @@ -1182,8 +1182,6 @@ github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o github.com/unknwon/com v0.0.0-20190804042917-757f69c95f3e/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM= github.com/unknwon/com v1.0.1 h1:3d1LTxD+Lnf3soQiD4Cp/0BRB+Rsa/+RTvz8GMMzIXs= github.com/unknwon/com v1.0.1/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnlCXM= -github.com/unrolled/render v1.5.0 h1:uNTHMvVoI9pyyXfgoDHHycIqFONNY2p4eQR9ty+NsxM= -github.com/unrolled/render v1.5.0/go.mod h1:eLTosBkQqEPEk7pRfkCRApXd++lm++nCsVlFOHpeedw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8= diff --git a/modules/context/context.go b/modules/context/context.go index 1eff1459a..04f8a9bd3 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -42,14 +42,13 @@ import ( "gitea.com/go-chi/session" chi "github.com/go-chi/chi/v5" "github.com/minio/sha256-simd" - "github.com/unrolled/render" "golang.org/x/crypto/pbkdf2" ) // Render represents a template render type Render interface { TemplateLookup(tmpl string) *template.Template - HTML(w io.Writer, status int, name string, binding interface{}, htmlOpt ...render.HTMLOptions) error + HTML(w io.Writer, status int, name string, data interface{}) error } // Context represents context of a request. diff --git a/modules/templates/htmlrenderer.go b/modules/templates/htmlrenderer.go index 96dc01079..fd985edc6 100644 --- a/modules/templates/htmlrenderer.go +++ b/modules/templates/htmlrenderer.go @@ -7,15 +7,18 @@ import ( "bytes" "context" "fmt" + "html/template" + "io" + "net/http" + "path/filepath" "regexp" "strconv" "strings" + "sync/atomic" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/watcher" - - "github.com/unrolled/render" ) var ( @@ -27,14 +30,50 @@ var ( expectedEndError = regexp.MustCompile(`^template: (.*):([0-9]+): expected end; found (.*)`) ) -// HTMLRenderer returns the current html renderer for the context or creates and stores one within the context for future use -func HTMLRenderer(ctx context.Context) (context.Context, *render.Render) { - rendererInterface := ctx.Value(rendererKey) - if rendererInterface != nil { - renderer, ok := rendererInterface.(*render.Render) - if ok { - return ctx, renderer +type HTMLRender struct { + templates atomic.Pointer[template.Template] +} + +func (h *HTMLRender) HTML(w io.Writer, status int, name string, data interface{}) error { + if respWriter, ok := w.(http.ResponseWriter); ok { + if respWriter.Header().Get("Content-Type") == "" { + respWriter.Header().Set("Content-Type", "text/html; charset=utf-8") } + respWriter.WriteHeader(status) + } + return h.templates.Load().ExecuteTemplate(w, name, data) +} + +func (h *HTMLRender) TemplateLookup(t string) *template.Template { + return h.templates.Load().Lookup(t) +} + +func (h *HTMLRender) CompileTemplates() error { + dirPrefix := "templates/" + tmpls := template.New("") + for _, path := range GetTemplateAssetNames() { + name := path[len(dirPrefix):] + name = strings.TrimSuffix(name, ".tmpl") + tmpl := tmpls.New(filepath.ToSlash(name)) + for _, fm := range NewFuncMap() { + tmpl.Funcs(fm) + } + buf, err := GetAsset(path) + if err != nil { + return err + } + if _, err = tmpl.Parse(string(buf)); err != nil { + return err + } + } + h.templates.Store(tmpls) + return nil +} + +// HTMLRenderer returns the current html renderer for the context or creates and stores one within the context for future use +func HTMLRenderer(ctx context.Context) (context.Context, *HTMLRender) { + if renderer, ok := ctx.Value(rendererKey).(*HTMLRender); ok { + return ctx, renderer } rendererType := "static" @@ -43,53 +82,24 @@ func HTMLRenderer(ctx context.Context) (context.Context, *render.Render) { } log.Log(1, log.DEBUG, "Creating "+rendererType+" HTML Renderer") - compilingTemplates := true - defer func() { - if !compilingTemplates { - return - } - - panicked := recover() - if panicked == nil { - return - } - - // OK try to handle the panic... - err, ok := panicked.(error) - if ok { - handlePanicError(err) - } - log.Fatal("PANIC: Unable to compile templates!\n%v\n\nStacktrace:\n%s", panicked, log.Stack(2)) - }() - - renderer := render.New(render.Options{ - Extensions: []string{".tmpl"}, - Directory: "templates", - Funcs: NewFuncMap(), - Asset: GetAsset, - AssetNames: GetTemplateAssetNames, - UseMutexLock: !setting.IsProd, - IsDevelopment: false, - DisableHTTPErrorRendering: true, - }) - compilingTemplates = false + renderer := &HTMLRender{} + if err := renderer.CompileTemplates(); err != nil { + handleFatalError(err) + } if !setting.IsProd { watcher.CreateWatcher(ctx, "HTML Templates", &watcher.CreateWatcherOpts{ PathsCallback: walkTemplateFiles, BetweenCallback: func() { - defer func() { - if err := recover(); err != nil { - log.Error("PANIC: %v\n%s", err, log.Stack(2)) - } - }() - renderer.CompileTemplates() + if err := renderer.CompileTemplates(); err != nil { + log.Error("Template error: %v\n%s", err, log.Stack(2)) + } }, }) } return context.WithValue(ctx, rendererKey, renderer), renderer } -func handlePanicError(err error) { +func handleFatalError(err error) { wrapFatal(handleNotDefinedPanicError(err)) wrapFatal(handleUnexpected(err)) wrapFatal(handleExpectedEnd(err)) diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index 6c434c201..5e6604953 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -22,7 +22,6 @@ import ( chi "github.com/go-chi/chi/v5" "github.com/stretchr/testify/assert" - "github.com/unrolled/render" ) // MockContext mock context for unit tests @@ -138,7 +137,7 @@ func (tr *mockRender) TemplateLookup(tmpl string) *template.Template { return nil } -func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ interface{}, _ ...render.HTMLOptions) error { +func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ interface{}) error { if resp, ok := w.(http.ResponseWriter); ok { resp.WriteHeader(status) } diff --git a/services/auth/sspi_windows.go b/services/auth/sspi_windows.go index 045834b69..b6e8d4298 100644 --- a/services/auth/sspi_windows.go +++ b/services/auth/sspi_windows.go @@ -23,7 +23,6 @@ import ( gouuid "github.com/google/uuid" "github.com/quasoft/websspi" - "github.com/unrolled/render" ) const ( @@ -48,7 +47,7 @@ var ( // On successful authentication returns a valid user object. // Returns nil if authentication fails. type SSPI struct { - rnd *render.Render + rnd *templates.HTMLRender } // Init creates a new global websspi.Authenticator object diff --git a/tests/integration/markup_external_test.go b/tests/integration/markup_external_test.go index c0e08a4f4..5f102f8d6 100644 --- a/tests/integration/markup_external_test.go +++ b/tests/integration/markup_external_test.go @@ -27,7 +27,7 @@ func TestExternalMarkupRenderer(t *testing.T) { const repoURL = "user30/renderer" req := NewRequest(t, "GET", repoURL+"/src/branch/master/README.html") resp := MakeRequest(t, req, http.StatusOK) - assert.EqualValues(t, "text/html; charset=UTF-8", resp.Header()["Content-Type"][0]) + assert.EqualValues(t, "text/html; charset=utf-8", resp.Header()["Content-Type"][0]) bs, err := io.ReadAll(resp.Body) assert.NoError(t, err)