@@ -23,6 +23,7 @@ import (
2323 "net/url"
2424 "os"
2525 "path/filepath"
26+ "strconv"
2627 "strings"
2728 "time"
2829)
@@ -129,6 +130,8 @@ type Request struct {
129130 gzip * teaconfigs.GzipConfig
130131 debug bool
131132
133+ locationContext * teaconfigs.LocationConfig // 当前变量的上下文 *Location ...
134+
132135 hasForwardHeader bool
133136}
134137
@@ -219,6 +222,8 @@ func (this *Request) reset(rawRequest *http.Request) {
219222
220223 this .accessLog = nil
221224
225+ this .locationContext = nil
226+
222227 this .gzip = nil
223228 this .debug = false
224229
@@ -331,6 +336,7 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
331336 if ! location .On {
332337 continue
333338 }
339+ this .locationContext = location
334340 if locationMatches , ok := location .Match (rawPath , this .Format ); ok {
335341 this .addVarMapping (locationMatches )
336342
@@ -363,6 +369,7 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
363369 }
364370 if location .RedirectToHttps && this .rawScheme == "http" {
365371 this .redirectToHttps = true
372+ this .locationContext = nil
366373 return nil
367374 }
368375
@@ -428,6 +435,7 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
428435 this .rewriteIsExternal = true
429436 this .rewriteRedirectMode = rule .RedirectMode ()
430437 this .rewriteProxyHost = rule .ProxyHost
438+ this .locationContext = nil
431439 return nil
432440 }
433441
@@ -436,12 +444,14 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
436444 this .rewriteReplace = replace
437445 this .rewriteIsExternal = false
438446 this .rewriteRedirectMode = teaconfigs .RewriteFlagRedirect
447+ this .locationContext = nil
439448 return nil
440449 }
441450
442451 newURI , err := url .ParseRequestURI (replace )
443452 if err != nil {
444453 this .uri = replace
454+ this .locationContext = nil
445455 return nil
446456 }
447457 if len (newURI .RawQuery ) > 0 {
@@ -458,18 +468,23 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
458468
459469 switch rule .TargetType () {
460470 case teaconfigs .RewriteTargetURL :
471+ this .locationContext = nil
461472 return this .configure (server , redirects , rule .IsBreak )
462473 case teaconfigs .RewriteTargetProxy :
463474 proxyId := rule .TargetProxy ()
464475 server := SharedManager .FindServer (proxyId )
465476 if server == nil {
477+ this .locationContext = nil
466478 return errors .New ("server with '" + proxyId + "' not found" )
467479 }
468480 if ! server .On {
481+ this .locationContext = nil
469482 return errors .New ("server with '" + proxyId + "' not available now" )
470483 }
484+ this .locationContext = nil
471485 return this .configure (server , redirects , rule .IsBreak )
472486 }
487+ this .locationContext = nil
473488 return nil
474489 }
475490 }
@@ -497,18 +512,22 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
497512 if len (location .Proxy ) > 0 {
498513 server := SharedManager .FindServer (location .Proxy )
499514 if server == nil {
515+ this .locationContext = nil
500516 return errors .New ("server with '" + location .Proxy + "' not found" )
501517 }
502518 if ! server .On {
519+ this .locationContext = nil
503520 return errors .New ("server with '" + location .Proxy + "' not available now" )
504521 }
522+ this .locationContext = nil
505523 return this .configure (server , redirects , breakRewrite )
506524 }
507525
508526 // backends
509527 if len (location .Backends ) > 0 {
510528 backend := location .NextBackend (this .backendCall )
511529 if backend == nil {
530+ this .locationContext = nil
512531 return errors .New ("no backends available" )
513532 }
514533 if len (this .backendCall .ResponseCallbacks ) > 0 {
@@ -536,9 +555,11 @@ func (this *Request) configure(server *teaconfigs.ServerConfig, redirects int, b
536555 if location .Websocket != nil && location .Websocket .On {
537556 this .backend = location .Websocket .NextBackend (this .backendCall )
538557 this .websocket = location .Websocket
558+ this .locationContext = nil
539559 return nil
540560 }
541561 }
562+ this .locationContext = nil
542563 }
543564
544565 // 如果经过location找到了相关配置,就终止
@@ -1137,7 +1158,7 @@ func (this *Request) Format(source string) string {
11371158 }
11381159 return addr
11391160 case "remotePort" :
1140- return fmt . Sprintf ( "%d" , this .requestRemotePort ())
1161+ return strconv . Itoa ( this .requestRemotePort ())
11411162 case "remoteUser" :
11421163 return this .requestRemoteUser ()
11431164 case "requestURI" , "requestUri" :
@@ -1151,7 +1172,20 @@ func (this *Request) Format(source string) string {
11511172 case "requestMethod" :
11521173 return this .requestMethod ()
11531174 case "requestFilename" :
1154- return this .requestFilename ()
1175+ filename := this .requestFilename ()
1176+ if len (filename ) > 0 {
1177+ return filename
1178+ }
1179+
1180+ if this .locationContext != nil && len (this .locationContext .Root ) > 0 {
1181+ return filepath .Clean (this .locationContext .Root + this .requestPath ())
1182+ }
1183+
1184+ if len (this .root ) > 0 {
1185+ return filepath .Clean (this .root + this .requestPath ())
1186+ }
1187+
1188+ return ""
11551189 case "scheme" :
11561190 return this .rawScheme
11571191 case "serverProtocol" , "proto" :
@@ -1161,7 +1195,7 @@ func (this *Request) Format(source string) string {
11611195 case "bodyBytesSent" :
11621196 return fmt .Sprintf ("%d" , this .responseWriter .SentBodyBytes ())
11631197 case "status" :
1164- return fmt . Sprintf ( "%d" , this .responseWriter .StatusCode ())
1198+ return strconv . Itoa ( this .responseWriter .StatusCode ())
11651199 case "statusMessage" :
11661200 return http .StatusText (this .responseWriter .StatusCode ())
11671201 case "timeISO8601" :
@@ -1191,7 +1225,12 @@ func (this *Request) Format(source string) string {
11911225 case "serverName" :
11921226 return this .serverName
11931227 case "serverPort" :
1194- return fmt .Sprintf ("%d" , this .requestServerPort ())
1228+ return strconv .Itoa (this .requestServerPort ())
1229+ case "documentRoot" :
1230+ if this .locationContext != nil && len (this .locationContext .Root ) > 0 {
1231+ return this .locationContext .Root
1232+ }
1233+ return this .root
11951234 }
11961235
11971236 dotIndex := strings .Index (varName , "." )
0 commit comments