Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-workhorse
Commits
eba05966
Commit
eba05966
authored
Dec 18, 2015
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move 'relative URL' string stuff into urlPrefix
parent
0a5c156c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
23 deletions
+36
-23
servefile.go
servefile.go
+2
-2
servefile_test.go
servefile_test.go
+6
-6
upstream.go
upstream.go
+12
-15
urlprefix.go
urlprefix.go
+16
-0
No files found.
servefile.go
View file @
eba05966
...
...
@@ -17,9 +17,9 @@ const (
CacheExpireMax
)
func
(
u
*
upstream
)
handleServeFile
(
documentRoot
string
,
cache
CacheMode
,
notFoundHandler
http
.
HandlerFunc
)
http
.
HandlerFunc
{
func
handleServeFile
(
documentRoot
string
,
prefix
urlPrefix
,
cache
CacheMode
,
notFoundHandler
http
.
HandlerFunc
)
http
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
file
:=
filepath
.
Join
(
documentRoot
,
u
.
relativeURIPath
(
cleanURIPath
(
r
.
URL
.
Path
)
))
file
:=
filepath
.
Join
(
documentRoot
,
prefix
.
strip
(
r
.
URL
.
Path
))
// The filepath.Join does Clean traversing directories up
if
!
strings
.
HasPrefix
(
file
,
documentRoot
)
{
...
...
servefile_test.go
View file @
eba05966
...
...
@@ -19,7 +19,7 @@ func TestServingNonExistingFile(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
w
:=
httptest
.
NewRecorder
()
dummyUpstream
.
handleServeFile
(
dir
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
404
)
}
...
...
@@ -32,7 +32,7 @@ func TestServingDirectory(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
w
:=
httptest
.
NewRecorder
()
dummyUpstream
.
handleServeFile
(
dir
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
404
)
}
...
...
@@ -41,7 +41,7 @@ func TestServingMalformedUri(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/../../../static/file"
,
nil
)
w
:=
httptest
.
NewRecorder
()
dummyUpstream
.
handleServeFile
(
dir
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
404
)
}
...
...
@@ -50,7 +50,7 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
executed
:=
false
dummyUpstream
.
handleServeFile
(
dir
,
CacheDisabled
,
func
(
_
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
func
(
_
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
executed
=
(
r
==
httpRequest
)
})(
nil
,
httpRequest
)
if
!
executed
{
...
...
@@ -71,7 +71,7 @@ func TestServingTheActualFile(t *testing.T) {
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
w
:=
httptest
.
NewRecorder
()
dummyUpstream
.
handleServeFile
(
dir
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
200
)
if
w
.
Body
.
String
()
!=
fileContent
{
t
.
Error
(
"We should serve the file: "
,
w
.
Body
.
String
())
...
...
@@ -102,7 +102,7 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
w
:=
httptest
.
NewRecorder
()
dummyUpstream
.
handleServeFile
(
dir
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
200
)
if
enableGzip
{
helper
.
AssertResponseHeader
(
t
,
w
,
"Content-Encoding"
,
"gzip"
)
...
...
upstream.go
View file @
eba05966
...
...
@@ -22,11 +22,11 @@ import (
)
type
upstream
struct
{
API
*
api
.
API
Proxy
*
proxy
.
Proxy
DocumentRoot
string
relativeURLRoot
string
routes
[]
route
API
*
api
.
API
Proxy
*
proxy
.
Proxy
DocumentRoot
string
urlPrefix
urlPrefix
routes
[]
route
}
type
route
struct
{
...
...
@@ -79,7 +79,7 @@ func (u *upstream) compileRoutes() {
// Serve assets
route
{
""
,
regexp
.
MustCompile
(
`^/assets/`
),
u
.
handleServeFile
(
u
.
DocumentRoot
,
CacheExpireMax
,
handleServeFile
(
u
.
DocumentRoot
,
u
.
urlPrefix
,
CacheExpireMax
,
handleDevelopmentMode
(
developmentMode
,
handleDeployPage
(
u
.
DocumentRoot
,
errorpage
.
Inject
(
u
.
DocumentRoot
,
...
...
@@ -92,7 +92,7 @@ func (u *upstream) compileRoutes() {
// Serve static files or forward the requests
route
{
""
,
nil
,
u
.
handleServeFile
(
u
.
DocumentRoot
,
CacheDisabled
,
handleServeFile
(
u
.
DocumentRoot
,
u
.
urlPrefix
,
CacheDisabled
,
handleDeployPage
(
u
.
DocumentRoot
,
errorpage
.
Inject
(
u
.
DocumentRoot
,
u
.
Proxy
,
...
...
@@ -137,17 +137,13 @@ func newUpstream(authBackend string, authSocket string) *upstream {
URL
:
parsedURL
,
Version
:
Version
,
},
Proxy
:
proxy
.
NewProxy
(
parsedURL
,
proxyTransport
,
Version
),
relativeURLRoot
:
relativeURLRoot
,
Proxy
:
proxy
.
NewProxy
(
parsedURL
,
proxyTransport
,
Version
),
urlPrefix
:
urlPrefix
(
relativeURLRoot
)
,
}
up
.
compileRoutes
()
return
up
}
func
(
u
*
upstream
)
relativeURIPath
(
p
string
)
string
{
return
cleanURIPath
(
strings
.
TrimPrefix
(
p
,
u
.
relativeURLRoot
))
}
func
(
u
*
upstream
)
ServeHTTP
(
ow
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
var
g
route
...
...
@@ -168,7 +164,8 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
// Check URL Root
URIPath
:=
cleanURIPath
(
r
.
URL
.
Path
)
if
!
strings
.
HasPrefix
(
URIPath
,
u
.
relativeURLRoot
)
&&
URIPath
+
"/"
!=
u
.
relativeURLRoot
{
prefix
:=
u
.
urlPrefix
if
!
prefix
.
match
(
URIPath
)
{
httpError
(
&
w
,
r
,
fmt
.
Sprintf
(
"Not found %q"
,
URIPath
),
http
.
StatusNotFound
)
return
}
...
...
@@ -180,7 +177,7 @@ func (u *upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue
}
if
g
.
regex
==
nil
||
g
.
regex
.
MatchString
(
u
.
relativeURIPath
(
URIPath
))
{
if
g
.
regex
==
nil
||
g
.
regex
.
MatchString
(
prefix
.
strip
(
URIPath
))
{
foundService
=
true
break
}
...
...
urlprefix.go
0 → 100644
View file @
eba05966
package
main
import
(
"strings"
)
type
urlPrefix
string
func
(
p
urlPrefix
)
strip
(
path
string
)
string
{
return
cleanURIPath
(
strings
.
TrimPrefix
(
path
,
string
(
p
)))
}
func
(
p
urlPrefix
)
match
(
path
string
)
bool
{
pre
:=
string
(
p
)
return
strings
.
HasPrefix
(
path
,
pre
)
||
path
+
"/"
==
pre
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment