From 3b7f41f9f7bbd3d19ce5a6b667b5c1f56c2b1b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Silva=20Sim=C3=B5es?= Date: Tue, 5 Feb 2019 20:58:55 -0500 Subject: [PATCH] Fix serving of raw wiki files other than .md (#5814) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix serving of raw wiki files other than .md Closes #4690. Closes #4395. Signed-off-by: Gabriel Silva Simões * Simplify code at routers/repo/wiki.go Signed-off-by: Gabriel Silva Simões * Add more files to user2/repo1.wiki for testing Signed-off-by: Gabriel Silva Simões * Update macaron to v1.3.2 Signed-off-by: Gabriel Silva Simões * Add tests for WikiRaw Signed-off-by: Gabriel Silva Simões * Fix NewResponseWriter usage due to macaron update Signed-off-by: Gabriel Silva Simões * Add raw to reserved wiki names Signed-off-by: Gabriel Silva Simões --- Gopkg.lock | 6 +-- .../0c/f15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 | 2 + .../32/5dc4f8e9344e6668f21536a69d5f1d4ed53ca3 | Bin 0 -> 63 bytes .../71/911bf48766c7181518c1070911019fbb00b1fc | 1 + .../72/fc6251cc648e914c10009d31431fa2e38b9a20 | Bin 0 -> 94 bytes .../7c/d7c8fa852973c72c66eb120a6677c54a8697f7 | Bin 0 -> 95 bytes .../c1/0d10b7e655b3dab1f53176db57c8219a5488d6 | 2 + .../f5/05ec9b5c7a45a10259c1dda7f18434e5d55940 | Bin 0 -> 157 bytes .../user2/repo1.wiki.git/refs/heads/master | 2 +- models/wiki.go | 2 +- routers/api/v1/misc/markdown_test.go | 2 +- routers/repo/wiki.go | 41 ++++++++++++------ routers/repo/wiki_test.go | 24 +++++++++- vendor/gopkg.in/macaron.v1/context.go | 4 +- vendor/gopkg.in/macaron.v1/macaron.go | 4 +- vendor/gopkg.in/macaron.v1/response_writer.go | 13 +++--- vendor/gopkg.in/macaron.v1/router.go | 5 ++- 17 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0c/f15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/32/5dc4f8e9344e6668f21536a69d5f1d4ed53ca3 create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/71/911bf48766c7181518c1070911019fbb00b1fc create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/72/fc6251cc648e914c10009d31431fa2e38b9a20 create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/7c/d7c8fa852973c72c66eb120a6677c54a8697f7 create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/c1/0d10b7e655b3dab1f53176db57c8219a5488d6 create mode 100644 integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/f5/05ec9b5c7a45a10259c1dda7f18434e5d55940 diff --git a/Gopkg.lock b/Gopkg.lock index 0df0cf57d0..8eb150bc9b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1108,12 +1108,12 @@ version = "v2.5.1" [[projects]] - digest = "1:cfe1730a152ff033ad7d9c115d22e36b19eec6d5928c06146b9119be45d39dc0" + digest = "1:de2e7294c9bd0b7d07ada8e98ad02cbbaabacff90eedebe7454ebdbab50d0d19" name = "gopkg.in/macaron.v1" packages = ["."] pruneopts = "NUT" - revision = "75f2e9b42e99652f0d82b28ccb73648f44615faa" - version = "v1.2.4" + revision = "dfcb80ca86e8534962c62812efd93209c7e600e7" + version = "v1.3.2" [[projects]] digest = "1:00126f697efdcab42f07c89ac8bf0095fb2328aef6464e070055154088cea859" diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0c/f15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0c/f15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 new file mode 100644 index 0000000000..c0314c5584 --- /dev/null +++ b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/0c/f15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 @@ -0,0 +1,2 @@ +xm DMY(J`5ɜ-K*Ki,Hi!?K_sq/]09MHpѤk_d-%풇۞ v_]^/I[t \ No newline at end of file diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/32/5dc4f8e9344e6668f21536a69d5f1d4ed53ca3 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/32/5dc4f8e9344e6668f21536a69d5f1d4ed53ca3 new file mode 100644 index 0000000000000000000000000000000000000000..d52aa8e1ff18454d3079d249bfb7900779fff7fc GIT binary patch literal 63 zcmV-F0Korv0ZYosPf{?kWKdQJNK8*v2+u6ZQ1HwJGP$@Equn!0QWF(?^3(HUHT3k0 V63g|o3R2VcvI^2Qxd87p5qfeO9YO#A literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/71/911bf48766c7181518c1070911019fbb00b1fc b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/71/911bf48766c7181518c1070911019fbb00b1fc new file mode 100644 index 0000000000..84ade81980 --- /dev/null +++ b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/71/911bf48766c7181518c1070911019fbb00b1fc @@ -0,0 +1 @@ +xM@ Mr›6&&&9Leśwt<#͡mv-0wbjy̖ڗ~݋[=H ."ǁ= \ No newline at end of file diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/72/fc6251cc648e914c10009d31431fa2e38b9a20 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/72/fc6251cc648e914c10009d31431fa2e38b9a20 new file mode 100644 index 0000000000000000000000000000000000000000..052fdf35a5187b8394a864cdfc77302a393de45e GIT binary patch literal 94 zcmV-k0HObQ0ZYosPg1b7U{F>FNK8*v2+u6ZPzWwaOioQv@Jq~1<>Cs-$ShU>qC|xP zkb-iMf<%Qppd5r<3{qUIpi!QYnVbO>%E?hkN>wOIEyw{I0#>iCtFFlf03OpKVH4IR AzyJUM literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/7c/d7c8fa852973c72c66eb120a6677c54a8697f7 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/7c/d7c8fa852973c72c66eb120a6677c54a8697f7 new file mode 100644 index 0000000000000000000000000000000000000000..9c264956058d3ff89dbc06f2827f281603892050 GIT binary patch literal 95 zcmV-l0HFVP0V^p=O;xZkXD~D{Ff%bx@W{_i)yqv`c-8c$y?l{3`=M+r?=O!^beN0n zRzsBpB&Mh8hG&*!=mr-gCa0$8`Xxg26#YpGJd@Hl(MNz`uA#I1qQ~8{6aX>eB7Gjp BEph+= literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/c1/0d10b7e655b3dab1f53176db57c8219a5488d6 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/c1/0d10b7e655b3dab1f53176db57c8219a5488d6 new file mode 100644 index 0000000000..8a6345dfa5 --- /dev/null +++ b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/c1/0d10b7e655b3dab1f53176db57c8219a5488d6 @@ -0,0 +1,2 @@ +xm0)nt2S`ņe,VY/H#[)E@Nq툎r2)D0jCLaC&4Bv]$EIӑePrIsez˳~_ +[yvWV=헛˘H vZ~s@݉%?TZH \ No newline at end of file diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/f5/05ec9b5c7a45a10259c1dda7f18434e5d55940 b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/objects/f5/05ec9b5c7a45a10259c1dda7f18434e5d55940 new file mode 100644 index 0000000000000000000000000000000000000000..eaeadaeaeed58b613a4e57382398712273e1fbfe GIT binary patch literal 157 zcmV;O0Al}m0V^p=O;s>7vt%$dFfcPQQSivmP1VayVR+T_r@efUH~XP%EAKClN_3cu z?N&pT1SF=X>V{{QWaxV40+}GyMzKeJyfpDk%lIT}wrp;^tlw3e#TcrC3lfu4Q*`|j zAvP5KNeVoZ(l^mZfMKqov;3mR-Ln*+da?>q)Ah0n(isXTN`GljJ1!wAagd!;ka7NQ LhK+v!Aca6I!9qzZ literal 0 HcmV?d00001 diff --git a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master index b22bbf31f0..86de6490a2 100644 --- a/integrations/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master +++ b/integrations/gitea-repositories-meta/user2/repo1.wiki.git/refs/heads/master @@ -1 +1 @@ -2c54faec6c45d31c1abfaecdab471eac6633738a +0cf15c3f66ec8384480ed9c3cf87c9e97fbb0ec3 diff --git a/models/wiki.go b/models/wiki.go index d61593609f..65cf26a44c 100644 --- a/models/wiki.go +++ b/models/wiki.go @@ -22,7 +22,7 @@ import ( ) var ( - reservedWikiNames = []string{"_pages", "_new", "_edit"} + reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"} wikiWorkingPool = sync.NewExclusivePool() ) diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index f9503bc639..32d2f0730e 100644 --- a/routers/api/v1/misc/markdown_test.go +++ b/routers/api/v1/misc/markdown_test.go @@ -27,7 +27,7 @@ func createContext(req *http.Request) (*macaron.Context, *httptest.ResponseRecor c := &macaron.Context{ Injector: inject.New(), Req: macaron.Request{Request: req}, - Resp: macaron.NewResponseWriter(resp), + Resp: macaron.NewResponseWriter(req.Method, resp), Render: &macaron.DummyRender{ResponseWriter: resp}, Data: make(map[string]interface{}), } diff --git a/routers/repo/wiki.go b/routers/repo/wiki.go index 6a25d9ffea..8426406c31 100644 --- a/routers/repo/wiki.go +++ b/routers/repo/wiki.go @@ -295,26 +295,41 @@ func WikiRaw(ctx *context.Context) { return } } + providedPath := ctx.Params("*") - if strings.HasSuffix(providedPath, ".md") { - providedPath = providedPath[:len(providedPath)-3] - } - wikiPath := models.WikiNameToFilename(providedPath) + var entry *git.TreeEntry if commit != nil { - entry, err = findEntryForFile(commit, wikiPath) + // Try to find a file with that name + entry, err = findEntryForFile(commit, providedPath) + if err != nil { + ctx.ServerError("findFile", err) + return + } + + if entry == nil { + // Try to find a wiki page with that name + if strings.HasSuffix(providedPath, ".md") { + providedPath = providedPath[:len(providedPath)-3] + } + + wikiPath := models.WikiNameToFilename(providedPath) + entry, err = findEntryForFile(commit, wikiPath) + if err != nil { + ctx.ServerError("findFile", err) + return + } + } } - if err != nil { - ctx.ServerError("findFile", err) - return - } else if entry == nil { - ctx.NotFound("findEntryForFile", nil) + + if entry != nil { + if err = ServeBlob(ctx, entry.Blob()); err != nil { + ctx.ServerError("ServeBlob", err) + } return } - if err = ServeBlob(ctx, entry.Blob()); err != nil { - ctx.ServerError("ServeBlob", err) - } + ctx.NotFound("findEntryForFile", nil) } // NewWiki render wiki create page diff --git a/routers/repo/wiki_test.go b/routers/repo/wiki_test.go index d433a86292..99812cab47 100644 --- a/routers/repo/wiki_test.go +++ b/routers/repo/wiki_test.go @@ -77,7 +77,7 @@ func TestWiki(t *testing.T) { Wiki(ctx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) assert.EqualValues(t, "Home", ctx.Data["Title"]) - assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"]) + assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"]) } func TestWikiPages(t *testing.T) { @@ -87,7 +87,7 @@ func TestWikiPages(t *testing.T) { test.LoadRepo(t, ctx, 1) WikiPages(ctx) assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) - assertPagesMetas(t, []string{"Home"}, ctx.Data["Pages"]) + assertPagesMetas(t, []string{"Home", "Page With Image", "Page With Spaced Name"}, ctx.Data["Pages"]) } func TestNewWiki(t *testing.T) { @@ -185,3 +185,23 @@ func TestDeleteWikiPagePost(t *testing.T) { assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) assertWikiNotExists(t, ctx.Repo.Repository, "Home") } + +func TestWikiRaw(t *testing.T) { + for filepath, filetype := range map[string]string{ + "jpeg.jpg": "image/jpeg", + "Page With Spaced Name": "text/plain; charset=utf-8", + "Page-With-Spaced-Name": "text/plain; charset=utf-8", + "Page With Spaced Name.md": "text/plain; charset=utf-8", + "Page-With-Spaced-Name.md": "text/plain; charset=utf-8", + } { + models.PrepareTestEnv(t) + + ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+filepath) + ctx.SetParams("*", filepath) + test.LoadUser(t, ctx, 2) + test.LoadRepo(t, ctx, 1) + WikiRaw(ctx) + assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) + assert.EqualValues(t, filetype, ctx.Resp.Header().Get("Content-Type")) + } +} diff --git a/vendor/gopkg.in/macaron.v1/context.go b/vendor/gopkg.in/macaron.v1/context.go index 94a8c45d7d..063f9e0148 100644 --- a/vendor/gopkg.in/macaron.v1/context.go +++ b/vendor/gopkg.in/macaron.v1/context.go @@ -262,7 +262,7 @@ func (ctx *Context) Params(name string) string { // SetParams sets value of param with given name. func (ctx *Context) SetParams(name, val string) { - if !strings.HasPrefix(name, ":") { + if name != "*" && !strings.HasPrefix(name, ":") { name = ":" + name } ctx.params[name] = val @@ -270,7 +270,7 @@ func (ctx *Context) SetParams(name, val string) { // ReplaceAllParams replace all current params with given params func (ctx *Context) ReplaceAllParams(params Params) { - ctx.params = params; + ctx.params = params } // ParamsEscape returns escapred params result. diff --git a/vendor/gopkg.in/macaron.v1/macaron.go b/vendor/gopkg.in/macaron.v1/macaron.go index 5926e61368..715076ac13 100644 --- a/vendor/gopkg.in/macaron.v1/macaron.go +++ b/vendor/gopkg.in/macaron.v1/macaron.go @@ -32,7 +32,7 @@ import ( "github.com/go-macaron/inject" ) -const _VERSION = "1.2.4.1123" +const _VERSION = "1.3.2.1216" func Version() string { return _VERSION @@ -194,7 +194,7 @@ func (m *Macaron) createContext(rw http.ResponseWriter, req *http.Request) *Cont index: 0, Router: m.Router, Req: Request{req}, - Resp: NewResponseWriter(rw), + Resp: NewResponseWriter(req.Method, rw), Render: &DummyRender{rw}, Data: make(map[string]interface{}), } diff --git a/vendor/gopkg.in/macaron.v1/response_writer.go b/vendor/gopkg.in/macaron.v1/response_writer.go index ab54f56c03..9133948f9b 100644 --- a/vendor/gopkg.in/macaron.v1/response_writer.go +++ b/vendor/gopkg.in/macaron.v1/response_writer.go @@ -42,11 +42,12 @@ type ResponseWriter interface { type BeforeFunc func(ResponseWriter) // NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter -func NewResponseWriter(rw http.ResponseWriter) ResponseWriter { - return &responseWriter{rw, 0, 0, nil} +func NewResponseWriter(method string, rw http.ResponseWriter) ResponseWriter { + return &responseWriter{method, rw, 0, 0, nil} } type responseWriter struct { + method string http.ResponseWriter status int size int @@ -59,13 +60,15 @@ func (rw *responseWriter) WriteHeader(s int) { rw.status = s } -func (rw *responseWriter) Write(b []byte) (int, error) { +func (rw *responseWriter) Write(b []byte) (size int, err error) { if !rw.Written() { // The status will be StatusOK if WriteHeader has not been called yet rw.WriteHeader(http.StatusOK) } - size, err := rw.ResponseWriter.Write(b) - rw.size += size + if rw.method != "HEAD" { + size, err = rw.ResponseWriter.Write(b) + rw.size += size + } return size, err } diff --git a/vendor/gopkg.in/macaron.v1/router.go b/vendor/gopkg.in/macaron.v1/router.go index 950c5bcb09..df593d669a 100644 --- a/vendor/gopkg.in/macaron.v1/router.go +++ b/vendor/gopkg.in/macaron.v1/router.go @@ -96,7 +96,7 @@ func NewRouter() *Router { } // SetAutoHead sets the value who determines whether add HEAD method automatically -// when GET method is added. Combo router will not be affected by this value. +// when GET method is added. func (r *Router) SetAutoHead(v bool) { r.autoHead = v } @@ -341,6 +341,9 @@ func (cr *ComboRouter) route(fn func(string, ...Handler) *Route, method string, } func (cr *ComboRouter) Get(h ...Handler) *ComboRouter { + if cr.router.autoHead { + cr.Head(h...) + } return cr.route(cr.router.Get, "GET", h...) }