Skip to content

Commit

Permalink
Fix serving of raw wiki files other than .md (#5814)
Browse files Browse the repository at this point in the history
* Fix serving of raw wiki files other than .md

Closes #4690.
Closes #4395.

Signed-off-by: Gabriel Silva Simões <[email protected]>

* Simplify code at routers/repo/wiki.go

Signed-off-by: Gabriel Silva Simões <[email protected]>

* Add more files to user2/repo1.wiki for testing

Signed-off-by: Gabriel Silva Simões <[email protected]>

* Update macaron to v1.3.2

Signed-off-by: Gabriel Silva Simões <[email protected]>

* Add tests for WikiRaw

Signed-off-by: Gabriel Silva Simões <[email protected]>

* Fix NewResponseWriter usage due to macaron update

Signed-off-by: Gabriel Silva Simões <[email protected]>

* Add raw to reserved wiki names

Signed-off-by: Gabriel Silva Simões <[email protected]>
  • Loading branch information
gabrielsimoes authored and techknowlogick committed Feb 6, 2019
1 parent 4a747ae commit 3b7f41f
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x���m� D�M��Y����(�J�`�5�ɜ-�K*Ki,Hi!?��<�i�Vki0Z��XH�D(Z6ĨG�Sb��3�JD�h��!�uB��DaJp� ���F�Lƹ4+~��v�;���
e����[Nx>K�����_s�q�/�]09MHpѤ��k���_d�-%�풇۞�� v�_�]��^�/�I[t
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x�M�@ ��M����r�›�6�&&&��9Le�św����t<#���͡�mv-��0w�b��jy̖��ڗ~݋[�����=H� �.�"������ǁ=
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x���m�0���)n���t2�S����`ņ���e�,VY�/H�#�[)��E��@N�q��툎�r2�)D��0�j�C���L��aC��&�4B�v]$E����Iӑe����P�r�I�s�e�z�˳~_
���[y��v��W��V=헛�˘�H vZ~s�@݉%����?T�ZH
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2c54faec6c45d31c1abfaecdab471eac6633738a
0cf15c3f66ec8384480ed9c3cf87c9e97fbb0ec3
2 changes: 1 addition & 1 deletion models/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

var (
reservedWikiNames = []string{"_pages", "_new", "_edit"}
reservedWikiNames = []string{"_pages", "_new", "_edit", "raw"}
wikiWorkingPool = sync.NewExclusivePool()
)

Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/misc/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
}
Expand Down
41 changes: 28 additions & 13 deletions routers/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions routers/repo/wiki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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"))
}
}
4 changes: 2 additions & 2 deletions vendor/gopkg.in/macaron.v1/context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/gopkg.in/macaron.v1/macaron.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions vendor/gopkg.in/macaron.v1/response_writer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion vendor/gopkg.in/macaron.v1/router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3b7f41f

Please sign in to comment.