Skip to content

Commit e5bb544

Browse files
authored
Merge branch 'master' into feature/dismissing_reviews
2 parents f323120 + a7cfb9f commit e5bb544

36 files changed

+262
-161
lines changed

build/generate-svg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function main() {
6565
await Promise.all([
6666
...processFiles('../node_modules/@primer/octicons/build/svg/*-16.svg', {prefix: 'octicon'}),
6767
...processFiles('../web_src/svg/*.svg'),
68-
...processFiles('../assets/logo.svg', {fullName: 'gitea-gitea'}),
68+
...processFiles('../public/img/gitea.svg', {fullName: 'gitea-gitea'}),
6969
]);
7070
}
7171

modules/auth/sso/oauth2.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/middlewares"
1516
"code.gitea.io/gitea/modules/timeutil"
1617
)
1718

@@ -121,7 +122,7 @@ func (o *OAuth2) VerifyAuthData(req *http.Request, w http.ResponseWriter, store
121122
return nil
122123
}
123124

124-
if isInternalPath(req) || !isAPIPath(req) && !isAttachmentDownload(req) {
125+
if middlewares.IsInternalPath(req) || !middlewares.IsAPIPath(req) && !isAttachmentDownload(req) {
125126
return nil
126127
}
127128

modules/auth/sso/sso.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ func SessionUser(sess SessionStore) *models.User {
9494
return user
9595
}
9696

97-
// isAPIPath returns true if the specified URL is an API path
98-
func isAPIPath(req *http.Request) bool {
99-
return strings.HasPrefix(req.URL.Path, "/api/")
100-
}
101-
102-
// isInternalPath returns true if the specified URL is an internal API path
103-
func isInternalPath(req *http.Request) bool {
104-
return strings.HasPrefix(req.URL.Path, "/api/internal/")
105-
}
106-
10797
// isAttachmentDownload check if request is a file download (GET) with URL to an attachment
10898
func isAttachmentDownload(req *http.Request) bool {
10999
return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"

modules/auth/sso/sspi_windows.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/base"
1414
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/middlewares"
1516
"code.gitea.io/gitea/modules/setting"
1617
"code.gitea.io/gitea/modules/templates"
1718

@@ -135,7 +136,7 @@ func (s *SSPI) VerifyAuthData(req *http.Request, w http.ResponseWriter, store Da
135136
}
136137

137138
// Make sure requests to API paths and PWA resources do not create a new session
138-
if !isAPIPath(req) && !isAttachmentDownload(req) {
139+
if !middlewares.IsAPIPath(req) && !isAttachmentDownload(req) {
139140
handleSignIn(w, req, sess, user)
140141
}
141142

@@ -166,9 +167,9 @@ func (s *SSPI) shouldAuthenticate(req *http.Request) (shouldAuth bool) {
166167
} else if req.FormValue("auth_with_sspi") == "1" {
167168
shouldAuth = true
168169
}
169-
} else if isInternalPath(req) {
170+
} else if middlewares.IsInternalPath(req) {
170171
shouldAuth = false
171-
} else if isAPIPath(req) || isAttachmentDownload(req) {
172+
} else if middlewares.IsAPIPath(req) || isAttachmentDownload(req) {
172173
shouldAuth = true
173174
}
174175
return

modules/cache/cache.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,6 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) {
2727
})
2828
}
2929

30-
// Cache is the interface that operates the cache data.
31-
type Cache interface {
32-
// Put puts value into cache with key and expire time.
33-
Put(key string, val interface{}, timeout int64) error
34-
// Get gets cached value by given key.
35-
Get(key string) interface{}
36-
// Delete deletes cached value by given key.
37-
Delete(key string) error
38-
// Incr increases cached int-type value by given key as a counter.
39-
Incr(key string) error
40-
// Decr decreases cached int-type value by given key as a counter.
41-
Decr(key string) error
42-
// IsExist returns true if cached value exists.
43-
IsExist(key string) bool
44-
// Flush deletes all cached data.
45-
Flush() error
46-
}
47-
4830
// NewContext start cache service
4931
func NewContext() error {
5032
var err error
@@ -59,7 +41,7 @@ func NewContext() error {
5941
}
6042

6143
// GetCache returns the currently configured cache
62-
func GetCache() Cache {
44+
func GetCache() mc.Cache {
6345
return conn
6446
}
6547

modules/context/access_log.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package context
6+
7+
import (
8+
"bytes"
9+
"html/template"
10+
"net/http"
11+
"time"
12+
13+
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/setting"
15+
)
16+
17+
type routerLoggerOptions struct {
18+
req *http.Request
19+
Identity *string
20+
Start *time.Time
21+
ResponseWriter http.ResponseWriter
22+
Ctx map[string]interface{}
23+
}
24+
25+
// AccessLogger returns a middleware to log access logger
26+
func AccessLogger() func(http.Handler) http.Handler {
27+
logger := log.GetLogger("access")
28+
logTemplate, _ := template.New("log").Parse(setting.AccessLogTemplate)
29+
return func(next http.Handler) http.Handler {
30+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
31+
start := time.Now()
32+
next.ServeHTTP(w, req)
33+
identity := "-"
34+
if val := SignedUserName(req); val != "" {
35+
identity = val
36+
}
37+
rw := w.(ResponseWriter)
38+
39+
buf := bytes.NewBuffer([]byte{})
40+
err := logTemplate.Execute(buf, routerLoggerOptions{
41+
req: req,
42+
Identity: &identity,
43+
Start: &start,
44+
ResponseWriter: rw,
45+
Ctx: map[string]interface{}{
46+
"RemoteAddr": req.RemoteAddr,
47+
"Req": req,
48+
},
49+
})
50+
if err != nil {
51+
log.Error("Could not set up chi access logger: %v", err.Error())
52+
}
53+
54+
err = logger.SendLog(log.INFO, "", "", 0, buf.String(), "")
55+
if err != nil {
56+
log.Error("Could not set up chi access logger: %v", err.Error())
57+
}
58+
})
59+
}
60+
}

modules/context/captcha.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package context
77
import (
88
"sync"
99

10+
"code.gitea.io/gitea/modules/cache"
1011
"code.gitea.io/gitea/modules/setting"
1112

1213
"gitea.com/go-chi/captcha"
@@ -21,6 +22,7 @@ func GetImageCaptcha() *captcha.Captcha {
2122
cpt = captcha.NewCaptcha(captcha.Options{
2223
SubURL: setting.AppSubURL,
2324
})
25+
cpt.Store = cache.GetCache()
2426
})
2527
return cpt
2628
}

modules/context/context.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/models"
2424
"code.gitea.io/gitea/modules/auth/sso"
2525
"code.gitea.io/gitea/modules/base"
26+
mc "code.gitea.io/gitea/modules/cache"
2627
"code.gitea.io/gitea/modules/log"
2728
"code.gitea.io/gitea/modules/middlewares"
2829
"code.gitea.io/gitea/modules/setting"
@@ -355,8 +356,8 @@ func (ctx *Context) Error(status int, contents ...string) {
355356

356357
// JSON render content as JSON
357358
func (ctx *Context) JSON(status int, content interface{}) {
358-
ctx.Resp.WriteHeader(status)
359359
ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf8")
360+
ctx.Resp.WriteHeader(status)
360361
if err := json.NewEncoder(ctx.Resp).Encode(content); err != nil {
361362
ctx.ServerError("Render JSON failed", err)
362363
}
@@ -484,6 +485,31 @@ func GetContext(req *http.Request) *Context {
484485
return req.Context().Value(contextKey).(*Context)
485486
}
486487

488+
// SignedUserName returns signed user's name via context
489+
func SignedUserName(req *http.Request) string {
490+
if middlewares.IsInternalPath(req) {
491+
return ""
492+
}
493+
if middlewares.IsAPIPath(req) {
494+
ctx, ok := req.Context().Value(apiContextKey).(*APIContext)
495+
if ok {
496+
v := ctx.Data["SignedUserName"]
497+
if res, ok := v.(string); ok {
498+
return res
499+
}
500+
}
501+
} else {
502+
ctx, ok := req.Context().Value(contextKey).(*Context)
503+
if ok {
504+
v := ctx.Data["SignedUserName"]
505+
if res, ok := v.(string); ok {
506+
return res
507+
}
508+
}
509+
}
510+
return ""
511+
}
512+
487513
func getCsrfOpts() CsrfOptions {
488514
return CsrfOptions{
489515
Secret: setting.SecretKey,
@@ -499,23 +525,8 @@ func getCsrfOpts() CsrfOptions {
499525

500526
// Contexter initializes a classic context for a request.
501527
func Contexter() func(next http.Handler) http.Handler {
502-
rnd := templates.HTMLRenderer()
503-
504-
var c cache.Cache
505-
var err error
506-
if setting.CacheService.Enabled {
507-
c, err = cache.NewCacher(cache.Options{
508-
Adapter: setting.CacheService.Adapter,
509-
AdapterConfig: setting.CacheService.Conn,
510-
Interval: setting.CacheService.Interval,
511-
})
512-
if err != nil {
513-
panic(err)
514-
}
515-
}
516-
528+
var rnd = templates.HTMLRenderer()
517529
var csrfOpts = getCsrfOpts()
518-
//var flashEncryptionKey, _ = NewSecret()
519530

520531
return func(next http.Handler) http.Handler {
521532
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
@@ -524,7 +535,7 @@ func Contexter() func(next http.Handler) http.Handler {
524535
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
525536
var ctx = Context{
526537
Resp: NewResponse(resp),
527-
Cache: c,
538+
Cache: mc.GetCache(),
528539
Locale: locale,
529540
Link: link,
530541
Render: rnd,
@@ -571,16 +582,14 @@ func Contexter() func(next http.Handler) http.Handler {
571582
}
572583
ctx.Resp.Before(func(resp ResponseWriter) {
573584
if flash := f.Encode(); len(flash) > 0 {
574-
if err == nil {
575-
middlewares.SetCookie(resp, "macaron_flash", flash, 0,
576-
setting.SessionConfig.CookiePath,
577-
middlewares.Domain(setting.SessionConfig.Domain),
578-
middlewares.HTTPOnly(true),
579-
middlewares.Secure(setting.SessionConfig.Secure),
580-
//middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
581-
)
582-
return
583-
}
585+
middlewares.SetCookie(resp, "macaron_flash", flash, 0,
586+
setting.SessionConfig.CookiePath,
587+
middlewares.Domain(setting.SessionConfig.Domain),
588+
middlewares.HTTPOnly(true),
589+
middlewares.Secure(setting.SessionConfig.Secure),
590+
//middlewares.SameSite(opt.SameSite), FIXME: we need a samesite config
591+
)
592+
return
584593
}
585594

586595
ctx.SetCookie("macaron_flash", "", -1,

modules/context/pagination.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ func (p *Pagination) SetDefaultParams(ctx *Context) {
5353
p.AddParam(ctx, "sort", "SortType")
5454
p.AddParam(ctx, "q", "Keyword")
5555
p.AddParam(ctx, "tab", "TabName")
56+
p.AddParam(ctx, "t", "queryType")
5657
}

modules/context/response.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type ResponseWriter interface {
1212
Flush()
1313
Status() int
1414
Before(func(ResponseWriter))
15+
Size() int
1516
}
1617

1718
var (
@@ -21,11 +22,17 @@ var (
2122
// Response represents a response
2223
type Response struct {
2324
http.ResponseWriter
25+
written int
2426
status int
2527
befores []func(ResponseWriter)
2628
beforeExecuted bool
2729
}
2830

31+
// Size return written size
32+
func (r *Response) Size() int {
33+
return r.written
34+
}
35+
2936
// Write writes bytes to HTTP endpoint
3037
func (r *Response) Write(bs []byte) (int, error) {
3138
if !r.beforeExecuted {
@@ -35,8 +42,9 @@ func (r *Response) Write(bs []byte) (int, error) {
3542
r.beforeExecuted = true
3643
}
3744
size, err := r.ResponseWriter.Write(bs)
45+
r.written += size
3846
if err != nil {
39-
return 0, err
47+
return size, err
4048
}
4149
if r.status == 0 {
4250
r.WriteHeader(200)

0 commit comments

Comments
 (0)