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
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-workhorse
Commits
d89b378e
Commit
d89b378e
authored
Dec 19, 2015
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create internal/staticpages package
parent
beb6dfe7
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
119 additions
and
89 deletions
+119
-89
internal/staticpages/deploy_page.go
internal/staticpages/deploy_page.go
+5
-5
internal/staticpages/deploy_page_test.go
internal/staticpages/deploy_page_test.go
+7
-5
internal/staticpages/error_pages.go
internal/staticpages/error_pages.go
+5
-5
internal/staticpages/error_pages_test.go
internal/staticpages/error_pages_test.go
+5
-3
internal/staticpages/servefile.go
internal/staticpages/servefile.go
+5
-4
internal/staticpages/servefile_test.go
internal/staticpages/servefile_test.go
+13
-7
internal/staticpages/static.go
internal/staticpages/static.go
+5
-0
internal/upstream/routes.go
internal/upstream/routes.go
+6
-5
internal/upstream/upstream.go
internal/upstream/upstream.go
+33
-23
internal/upstream/urlprefix.go
internal/upstream/urlprefix.go
+0
-32
internal/urlprefix/urlprefix.go
internal/urlprefix/urlprefix.go
+35
-0
No files found.
internal/
upstream
/deploy_page.go
→
internal/
staticpages
/deploy_page.go
View file @
d89b378e
package
upstream
package
staticpages
import
(
"../helper"
...
...
@@ -7,10 +7,10 @@ import (
"path/filepath"
)
func
handleDeployPage
(
documentRoot
string
,
handler
http
.
Handler
)
http
.
HandlerFunc
{
deployPage
:=
filepath
.
Join
(
d
ocumentRoot
,
"index.html"
)
func
(
s
*
Static
)
DeployPage
(
handler
http
.
Handler
)
http
.
Handler
{
deployPage
:=
filepath
.
Join
(
s
.
D
ocumentRoot
,
"index.html"
)
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
data
,
err
:=
ioutil
.
ReadFile
(
deployPage
)
if
err
!=
nil
{
handler
.
ServeHTTP
(
w
,
r
)
...
...
@@ -21,5 +21,5 @@ func handleDeployPage(documentRoot string, handler http.Handler) http.HandlerFun
w
.
Header
()
.
Set
(
"Content-Type"
,
"text/html; charset=utf-8"
)
w
.
WriteHeader
(
http
.
StatusOK
)
w
.
Write
(
data
)
}
}
)
}
internal/
upstream
/deploy_page_test.go
→
internal/
staticpages
/deploy_page_test.go
View file @
d89b378e
package
upstream
package
staticpages
import
(
"../helper"
...
...
@@ -20,9 +20,10 @@ func TestIfNoDeployPageExist(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
executed
:=
false
handleDeployPage
(
dir
,
http
.
HandlerFunc
(
func
(
_
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
st
:=
&
Static
{
dir
}
st
.
DeployPage
(
http
.
HandlerFunc
(
func
(
_
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
executed
=
true
}))(
w
,
nil
)
}))
.
ServeHTTP
(
w
,
nil
)
if
!
executed
{
t
.
Error
(
"The handler should get executed"
)
}
...
...
@@ -41,9 +42,10 @@ func TestIfDeployPageExist(t *testing.T) {
w
:=
httptest
.
NewRecorder
()
executed
:=
false
handleDeployPage
(
dir
,
http
.
HandlerFunc
(
func
(
_
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
st
:=
&
Static
{
dir
}
st
.
DeployPage
(
http
.
HandlerFunc
(
func
(
_
http
.
ResponseWriter
,
_
*
http
.
Request
)
{
executed
=
true
}))(
w
,
nil
)
}))
.
ServeHTTP
(
w
,
nil
)
if
executed
{
t
.
Error
(
"The handler should not get executed"
)
}
...
...
internal/
errorpage
/error_pages.go
→
internal/
staticpages
/error_pages.go
View file @
d89b378e
package
errorpage
package
staticpages
import
(
"../helper"
...
...
@@ -60,13 +60,13 @@ func (s *errorPageResponseWriter) Flush() {
s
.
WriteHeader
(
http
.
StatusOK
)
}
func
Inject
(
documentRoot
string
,
handler
http
.
Handler
)
http
.
HandlerFunc
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
st
*
Static
)
ErrorPages
(
handler
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
rw
:=
errorPageResponseWriter
{
rw
:
w
,
path
:
d
ocumentRoot
,
path
:
st
.
D
ocumentRoot
,
}
defer
rw
.
Flush
()
handler
.
ServeHTTP
(
&
rw
,
r
)
}
}
)
}
internal/
errorpage
/error_pages_test.go
→
internal/
staticpages
/error_pages_test.go
View file @
d89b378e
package
errorpage
package
staticpages
import
(
"../helper"
...
...
@@ -26,7 +26,8 @@ func TestIfErrorPageIsPresented(t *testing.T) {
w
.
WriteHeader
(
404
)
fmt
.
Fprint
(
w
,
"Not Found"
)
})
Inject
(
dir
,
h
)(
w
,
nil
)
st
:=
&
Static
{
dir
}
st
.
ErrorPages
(
h
)
.
ServeHTTP
(
w
,
nil
)
w
.
Flush
()
helper
.
AssertResponseCode
(
t
,
w
,
404
)
...
...
@@ -46,7 +47,8 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
w
.
WriteHeader
(
404
)
fmt
.
Fprint
(
w
,
errorResponse
)
})
Inject
(
dir
,
h
)(
w
,
nil
)
st
:=
&
Static
{
dir
}
st
.
ErrorPages
(
h
)
.
ServeHTTP
(
w
,
nil
)
w
.
Flush
()
helper
.
AssertResponseCode
(
t
,
w
,
404
)
...
...
internal/
upstream
/servefile.go
→
internal/
staticpages
/servefile.go
View file @
d89b378e
package
upstream
package
staticpages
import
(
"../helper"
"../urlprefix"
"log"
"net/http"
"os"
...
...
@@ -17,12 +18,12 @@ const (
CacheExpireMax
)
func
handleServeFile
(
documentRoot
string
,
prefix
url
Prefix
,
cache
CacheMode
,
notFoundHandler
http
.
Handler
)
http
.
Handler
{
func
(
s
*
Static
)
ServeExisting
(
prefix
urlprefix
.
Prefix
,
cache
CacheMode
,
notFoundHandler
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
file
:=
filepath
.
Join
(
documentRoot
,
prefix
.
s
trip
(
r
.
URL
.
Path
))
file
:=
filepath
.
Join
(
s
.
DocumentRoot
,
prefix
.
S
trip
(
r
.
URL
.
Path
))
// The filepath.Join does Clean traversing directories up
if
!
strings
.
HasPrefix
(
file
,
d
ocumentRoot
)
{
if
!
strings
.
HasPrefix
(
file
,
s
.
D
ocumentRoot
)
{
helper
.
Fail500
(
w
,
&
os
.
PathError
{
Op
:
"open"
,
Path
:
file
,
...
...
internal/
upstream
/servefile_test.go
→
internal/
staticpages
/servefile_test.go
View file @
d89b378e
package
upstream
package
staticpages
import
(
"../helper"
...
...
@@ -17,7 +17,8 @@ func TestServingNonExistingFile(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
st
:=
&
Static
{
dir
}
st
.
ServeExisting
(
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
404
)
}
...
...
@@ -30,7 +31,8 @@ func TestServingDirectory(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
st
:=
&
Static
{
dir
}
st
.
ServeExisting
(
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
404
)
}
...
...
@@ -39,7 +41,8 @@ func TestServingMalformedUri(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/../../../static/file"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
st
:=
&
Static
{
dir
}
st
.
ServeExisting
(
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
404
)
}
...
...
@@ -48,7 +51,8 @@ func TestExecutingHandlerWhenNoFileFound(t *testing.T) {
httpRequest
,
_
:=
http
.
NewRequest
(
"GET"
,
"/file"
,
nil
)
executed
:=
false
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
http
.
HandlerFunc
(
func
(
_
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
st
:=
&
Static
{
dir
}
st
.
ServeExisting
(
"/"
,
CacheDisabled
,
http
.
HandlerFunc
(
func
(
_
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
executed
=
(
r
==
httpRequest
)
}))
.
ServeHTTP
(
nil
,
httpRequest
)
if
!
executed
{
...
...
@@ -69,7 +73,8 @@ func TestServingTheActualFile(t *testing.T) {
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
st
:=
&
Static
{
dir
}
st
.
ServeExisting
(
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
200
)
if
w
.
Body
.
String
()
!=
fileContent
{
t
.
Error
(
"We should serve the file: "
,
w
.
Body
.
String
())
...
...
@@ -100,7 +105,8 @@ func testServingThePregzippedFile(t *testing.T, enableGzip bool) {
ioutil
.
WriteFile
(
filepath
.
Join
(
dir
,
"file"
),
[]
byte
(
fileContent
),
0600
)
w
:=
httptest
.
NewRecorder
()
handleServeFile
(
dir
,
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
st
:=
&
Static
{
dir
}
st
.
ServeExisting
(
"/"
,
CacheDisabled
,
nil
)
.
ServeHTTP
(
w
,
httpRequest
)
helper
.
AssertResponseCode
(
t
,
w
,
200
)
if
enableGzip
{
helper
.
AssertResponseHeader
(
t
,
w
,
"Content-Encoding"
,
"gzip"
)
...
...
internal/staticpages/static.go
0 → 100644
View file @
d89b378e
package
staticpages
type
Static
struct
{
DocumentRoot
string
}
internal/upstream/routes.go
View file @
d89b378e
package
upstream
import
(
"../errorpage"
"../git"
"../lfs"
"../staticpages"
"../upload"
"net/http"
"regexp"
...
...
@@ -33,6 +33,7 @@ func (u *Upstream) Routes() []route {
}
func
(
u
*
Upstream
)
configureRoutes
()
{
static
:=
&
staticpages
.
Static
{
u
.
DocumentRoot
}
u
.
routes
=
[]
route
{
// Git Clone
route
{
"GET"
,
regexp
.
MustCompile
(
gitProjectPattern
+
`info/refs\z`
),
git
.
GetInfoRefs
(
u
.
API
())},
...
...
@@ -63,7 +64,7 @@ func (u *Upstream) configureRoutes() {
// Serve assets
route
{
""
,
regexp
.
MustCompile
(
`^/assets/`
),
handleServeFile
(
u
.
DocumentRoot
,
u
.
URLPrefix
(),
CacheExpireMax
,
static
.
ServeExisting
(
u
.
URLPrefix
(),
staticpages
.
CacheExpireMax
,
NotFoundUnless
(
u
.
DevelopmentMode
,
u
.
Proxy
(),
),
...
...
@@ -72,9 +73,9 @@ func (u *Upstream) configureRoutes() {
// Serve static files or forward the requests
route
{
""
,
nil
,
handleServeFile
(
u
.
DocumentRoot
,
u
.
URLPrefix
(),
CacheDisabled
,
handleDeployPage
(
u
.
DocumentRoot
,
errorpage
.
Inject
(
u
.
DocumentRoot
,
static
.
ServeExisting
(
u
.
URLPrefix
(),
staticpages
.
CacheDisabled
,
static
.
DeployPage
(
static
.
ErrorPages
(
u
.
Proxy
(),
),
),
...
...
internal/upstream/upstream.go
View file @
d89b378e
...
...
@@ -10,10 +10,12 @@ import (
"../api"
"../helper"
"../proxy"
"../staticpages"
"../urlprefix"
"fmt"
"net/http"
"net/url"
"
path
"
"
strings
"
"sync"
"time"
)
...
...
@@ -34,7 +36,7 @@ type Upstream struct {
_proxy
*
proxy
.
Proxy
configureProxyOnce
sync
.
Once
urlPrefix
urlPrefix
urlPrefix
url
prefix
.
Prefix
configureURLPrefixOnce
sync
.
Once
routes
[]
route
...
...
@@ -42,6 +44,9 @@ type Upstream struct {
transport
http
.
RoundTripper
configureTransportOnce
sync
.
Once
_static
*
staticpages
.
Static
configureStaticOnce
sync
.
Once
}
func
(
u
*
Upstream
)
Proxy
()
*
proxy
.
Proxy
{
...
...
@@ -66,6 +71,29 @@ func (u *Upstream) configureAPI() {
}
}
func
(
u
*
Upstream
)
URLPrefix
()
urlprefix
.
Prefix
{
u
.
configureURLPrefixOnce
.
Do
(
u
.
configureURLPrefix
)
return
u
.
urlPrefix
}
func
(
u
*
Upstream
)
configureURLPrefix
()
{
if
u
.
Backend
==
nil
{
u
.
Backend
=
DefaultBackend
}
relativeURLRoot
:=
u
.
Backend
.
Path
if
!
strings
.
HasSuffix
(
relativeURLRoot
,
"/"
)
{
relativeURLRoot
+=
"/"
}
u
.
urlPrefix
=
urlprefix
.
Prefix
(
relativeURLRoot
)
}
// func (u *Upstream) Static() *static.Static {
// u.configureStaticOnce.Do(func() {
// u._static = &static.Static{u.DocumentRoot}
// })
// return u._static
// }
func
(
u
*
Upstream
)
ServeHTTP
(
ow
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
:=
newLoggingResponseWriter
(
ow
)
defer
w
.
Log
(
r
)
...
...
@@ -83,9 +111,9 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
}
// Check URL Root
URIPath
:=
c
leanURIPath
(
r
.
URL
.
Path
)
URIPath
:=
urlprefix
.
C
leanURIPath
(
r
.
URL
.
Path
)
prefix
:=
u
.
URLPrefix
()
if
!
prefix
.
m
atch
(
URIPath
)
{
if
!
prefix
.
M
atch
(
URIPath
)
{
httpError
(
&
w
,
r
,
fmt
.
Sprintf
(
"Not found %q"
,
URIPath
),
http
.
StatusNotFound
)
return
}
...
...
@@ -98,7 +126,7 @@ func (u *Upstream) ServeHTTP(ow http.ResponseWriter, r *http.Request) {
continue
}
if
ro
.
regex
==
nil
||
ro
.
regex
.
MatchString
(
prefix
.
s
trip
(
URIPath
))
{
if
ro
.
regex
==
nil
||
ro
.
regex
.
MatchString
(
prefix
.
S
trip
(
URIPath
))
{
foundService
=
true
break
}
...
...
@@ -121,21 +149,3 @@ func httpError(w http.ResponseWriter, r *http.Request, error string, code int) {
http
.
Error
(
w
,
error
,
code
)
}
// Borrowed from: net/http/server.go
// Return the canonical path for p, eliminating . and .. elements.
func
cleanURIPath
(
p
string
)
string
{
if
p
==
""
{
return
"/"
}
if
p
[
0
]
!=
'/'
{
p
=
"/"
+
p
}
np
:=
path
.
Clean
(
p
)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if
p
[
len
(
p
)
-
1
]
==
'/'
&&
np
!=
"/"
{
np
+=
"/"
}
return
np
}
internal/upstream/urlprefix.go
deleted
100644 → 0
View file @
beb6dfe7
package
upstream
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
}
func
(
u
*
Upstream
)
URLPrefix
()
urlPrefix
{
u
.
configureURLPrefixOnce
.
Do
(
u
.
configureURLPrefix
)
return
u
.
urlPrefix
}
func
(
u
*
Upstream
)
configureURLPrefix
()
{
if
u
.
Backend
==
nil
{
u
.
Backend
=
DefaultBackend
}
relativeURLRoot
:=
u
.
Backend
.
Path
if
!
strings
.
HasSuffix
(
relativeURLRoot
,
"/"
)
{
relativeURLRoot
+=
"/"
}
u
.
urlPrefix
=
urlPrefix
(
relativeURLRoot
)
}
internal/urlprefix/urlprefix.go
0 → 100644
View file @
d89b378e
package
urlprefix
import
(
"path"
"strings"
)
type
Prefix
string
func
(
p
Prefix
)
Strip
(
path
string
)
string
{
return
CleanURIPath
(
strings
.
TrimPrefix
(
path
,
string
(
p
)))
}
func
(
p
Prefix
)
Match
(
path
string
)
bool
{
pre
:=
string
(
p
)
return
strings
.
HasPrefix
(
path
,
pre
)
||
path
+
"/"
==
pre
}
// Borrowed from: net/http/server.go
// Return the canonical path for p, eliminating . and .. elements.
func
CleanURIPath
(
p
string
)
string
{
if
p
==
""
{
return
"/"
}
if
p
[
0
]
!=
'/'
{
p
=
"/"
+
p
}
np
:=
path
.
Clean
(
p
)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if
p
[
len
(
p
)
-
1
]
==
'/'
&&
np
!=
"/"
{
np
+=
"/"
}
return
np
}
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