Skip to content

Commit 070c065

Browse files
committed
fix
1 parent c2fa157 commit 070c065

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+454
-241
lines changed

eslint.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,6 @@ export default defineConfig([
575575
'no-restricted-imports': [2, {paths: [
576576
{name: 'jquery', message: 'Use the global $ instead', allowTypeImports: true},
577577
{name: 'htmx.org', message: 'Use the global htmx instead', allowTypeImports: true},
578-
{name: 'idiomorph/htmx', message: 'Loaded in globals.ts', allowTypeImports: true},
579578
]}],
580579
'no-restricted-syntax': [2, 'WithStatement', 'ForInStatement', 'LabeledStatement', 'SequenceExpression'],
581580
'no-return-assign': [0],

modules/web/middleware/cookie.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func DeleteRedirectToCookie(resp http.ResponseWriter) {
3535
}
3636

3737
func RedirectLinkUserLogin(req *http.Request) string {
38+
if req.Header.Get("X-Gitea-Fetch-Action") != "" {
39+
// when building the redirect link for a fetch request, the current link might be a partial page,
40+
// so we only redirect to the login page without redirect_to parameter
41+
return setting.AppSubURL + "/user/login"
42+
}
3843
return setting.AppSubURL + "/user/login?redirect_to=" + url.QueryEscape(setting.AppSubURL+req.URL.RequestURI())
3944
}
4045

routers/common/redirect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ func FetchRedirectDelegate(resp http.ResponseWriter, req *http.Request) {
1616
// 2. when use "window.reload()", the hash is not respected, the newly loaded page won't scroll to the hash target.
1717
// The typical page is "issue comment" page. The backend responds "/owner/repo/issues/1#comment-2",
1818
// then frontend needs this delegate to redirect to the new location with hash correctly.
19-
redirect := req.PostFormValue("redirect")
20-
if !httplib.IsCurrentGiteaSiteURL(req.Context(), redirect) {
19+
redirect := req.FormValue("redirect")
20+
if req.Method != http.MethodPost || !httplib.IsCurrentGiteaSiteURL(req.Context(), redirect) {
2121
resp.WriteHeader(http.StatusBadRequest)
2222
return
2323
}

routers/web/devtest/devtest.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func List(ctx *context.Context) {
4545

4646
func FetchActionTest(ctx *context.Context) {
4747
_ = ctx.Req.ParseForm()
48-
ctx.Flash.Info("fetch-action: " + ctx.Req.Method + " " + ctx.Req.RequestURI + "\n" +
48+
ctx.Flash.Info("fetch action: " + ctx.Req.Method + " " + ctx.Req.RequestURI + "\n" +
4949
"Form: " + ctx.Req.Form.Encode() + "\n" +
5050
"PostForm: " + ctx.Req.PostForm.Encode(),
5151
)
@@ -241,9 +241,8 @@ func prepareMockDataUnicodeEscape(ctx *context.Context) {
241241

242242
func TmplCommon(ctx *context.Context) {
243243
prepareMockData(ctx)
244-
if ctx.Req.Method == http.MethodPost {
245-
_ = ctx.Req.ParseForm()
246-
ctx.Flash.Info("form: "+ctx.Req.Method+" "+ctx.Req.RequestURI+"\n"+
244+
if ctx.Req.Method == http.MethodPost && ctx.FormBool("mock_response_delay") {
245+
ctx.Flash.Info("form submit: "+ctx.Req.Method+" "+ctx.Req.RequestURI+"\n"+
247246
"Form: "+ctx.Req.Form.Encode()+"\n"+
248247
"PostForm: "+ctx.Req.PostForm.Encode(),
249248
true,

routers/web/repo/star.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ func ActionStar(ctx *context.Context) {
2626
ctx.ServerError("GetRepositoryByName", err)
2727
return
2828
}
29-
ctx.RespHeader().Add("hx-trigger", "refreshUserCards") // see the `hx-trigger="refreshUserCards ..."` comments in tmpl
3029
ctx.HTML(http.StatusOK, tplStarUnstar)
3130
}

routers/web/repo/view.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,15 @@ func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entri
310310
return nil
311311
}
312312

313-
{
313+
{ // this block is for testing purpose only
314314
if timeout != 0 && !setting.IsProd && !setting.IsInTesting {
315315
log.Debug("first call to get directory file commit info")
316316
clearFilesCommitInfo := func() {
317317
log.Warn("clear directory file commit info to force async loading on frontend")
318318
for i := range files {
319-
files[i].Commit = nil
319+
if i%2 == 0 { // for testing purpose, only clear half of the files' commit info
320+
files[i].Commit = nil
321+
}
320322
}
321323
}
322324
_ = clearFilesCommitInfo

routers/web/repo/watch.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ func ActionWatch(ctx *context.Context) {
2626
ctx.ServerError("GetRepositoryByName", err)
2727
return
2828
}
29-
ctx.RespHeader().Add("hx-trigger", "refreshUserCards") // see the `hx-trigger="refreshUserCards ..."` comments in tmpl
3029
ctx.HTML(http.StatusOK, tplWatchUnwatch)
3130
}

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) {
17101710

17111711
m.Get("/forks", repo.Forks)
17121712
m.Get("/commit/{sha:([a-f0-9]{7,64})}.{ext:patch|diff}", repo.MustBeNotEmpty, repo.RawDiff)
1713-
m.Post("/lastcommit/*", context.RepoRefByType(git.RefTypeCommit), repo.LastCommit)
1713+
m.Get("/lastcommit/*", context.RepoRefByType(git.RefTypeCommit), repo.LastCommit)
17141714
}, optSignIn, context.RepoAssignment, reqUnitCodeReader)
17151715
// end "/{username}/{reponame}": repo code
17161716

services/context/base.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,10 @@ func (b *Base) Redirect(location string, status ...int) {
159159
// So in this case, we should remove the session cookie from the response header
160160
removeSessionCookieHeader(b.Resp)
161161
}
162-
// in case the request is made by htmx, have it redirect the browser instead of trying to follow the redirect inside htmx
163-
if b.Req.Header.Get("HX-Request") == "true" {
164-
b.Resp.Header().Set("HX-Redirect", location)
165-
// we have to return a non-redirect status code so XMLHTTPRequest will not immediately follow the redirect
166-
// so as to give htmx redirect logic a chance to run
167-
b.Status(http.StatusNoContent)
162+
// In case the request is made by "fetch-action" module, make JS redirect to the new location
163+
// Otherwise, the JS fetch will follow the redirection and read a "login" page, embed it to the current page, which is not expected.
164+
if b.Req.Header.Get("X-Gitea-Fetch-Action") != "" {
165+
b.JSON(http.StatusOK, map[string]any{"redirect": location})
168166
return
169167
}
170168
http.Redirect(b.Resp, b.Req, location, code)

services/context/base_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ func TestRedirect(t *testing.T) {
3838

3939
req, _ = http.NewRequest(http.MethodGet, "/", nil)
4040
resp := httptest.NewRecorder()
41-
req.Header.Add("HX-Request", "true")
41+
req.Header.Add("X-Gitea-Fetch-Action", "1")
4242
b := NewBaseContextForTest(resp, req)
4343
b.Redirect("/other")
44-
assert.Equal(t, "/other", resp.Header().Get("HX-Redirect"))
45-
assert.Equal(t, http.StatusNoContent, resp.Code)
44+
assert.Contains(t, resp.Header().Get("Content-Type"), "application/json")
45+
assert.JSONEq(t, `{"redirect":"/other"}`, resp.Body.String())
46+
assert.Equal(t, http.StatusOK, resp.Code)
4647
}

0 commit comments

Comments
 (0)