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
0b10eb3a
Commit
0b10eb3a
authored
Aug 23, 2015
by
Jacob Vosmaer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Put gitHandler config in a struct
This lets us drop a few global variables.
parent
46e1ae4e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
10 deletions
+18
-10
main.go
main.go
+18
-10
No files found.
main.go
View file @
0b10eb3a
...
@@ -28,6 +28,12 @@ import (
...
@@ -28,6 +28,12 @@ import (
"syscall"
"syscall"
)
)
type
gitHandler
struct
{
httpClient
*
http
.
Client
repoRoot
string
authBackend
string
}
type
gitService
struct
{
type
gitService
struct
{
method
string
method
string
suffix
string
suffix
string
...
@@ -40,10 +46,8 @@ type gitEnv struct {
...
@@ -40,10 +46,8 @@ type gitEnv struct {
}
}
var
Version
string
// Set at build time in the Makefile
var
Version
string
// Set at build time in the Makefile
var
httpClient
=
&
http
.
Client
{}
// Command-line options
// Command-line options
var
repoRoot
string
var
printVersion
=
flag
.
Bool
(
"version"
,
false
,
"Print version and exit"
)
var
printVersion
=
flag
.
Bool
(
"version"
,
false
,
"Print version and exit"
)
var
listenAddr
=
flag
.
String
(
"listenAddr"
,
"localhost:8181"
,
"Listen address for HTTP server"
)
var
listenAddr
=
flag
.
String
(
"listenAddr"
,
"localhost:8181"
,
"Listen address for HTTP server"
)
var
listenNetwork
=
flag
.
String
(
"listenNetwork"
,
"tcp"
,
"Listen 'network' (protocol)"
)
var
listenNetwork
=
flag
.
String
(
"listenNetwork"
,
"tcp"
,
"Listen 'network' (protocol)"
)
...
@@ -69,7 +73,7 @@ func main() {
...
@@ -69,7 +73,7 @@ func main() {
fmt
.
Printf
(
"gitlab-git-http-server %s
\n
"
,
Version
)
fmt
.
Printf
(
"gitlab-git-http-server %s
\n
"
,
Version
)
os
.
Exit
(
0
)
os
.
Exit
(
0
)
}
}
repoRoot
=
flag
.
Arg
(
0
)
repoRoot
:
=
flag
.
Arg
(
0
)
if
repoRoot
==
""
{
if
repoRoot
==
""
{
flag
.
Usage
()
flag
.
Usage
()
os
.
Exit
(
1
)
os
.
Exit
(
1
)
...
@@ -91,11 +95,15 @@ func main() {
...
@@ -91,11 +95,15 @@ func main() {
log
.
Fatal
(
err
)
log
.
Fatal
(
err
)
}
}
http
.
Handle
Func
(
"/"
,
gitHandler
)
http
.
Handle
(
"/"
,
newGitHandler
(
repoRoot
,
*
authBackend
)
)
log
.
Fatal
(
http
.
Serve
(
listener
,
nil
))
log
.
Fatal
(
http
.
Serve
(
listener
,
nil
))
}
}
func
gitHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
newGitHandler
(
repoRoot
,
authBackend
string
)
*
gitHandler
{
return
&
gitHandler
{
&
http
.
Client
{},
repoRoot
,
authBackend
}
}
func
(
h
*
gitHandler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
var
env
gitEnv
var
env
gitEnv
var
g
gitService
var
g
gitService
...
@@ -118,7 +126,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -118,7 +126,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
// Ask the auth backend if the request is allowed, and what the
// Ask the auth backend if the request is allowed, and what the
// user ID (GL_ID) is.
// user ID (GL_ID) is.
authResponse
,
err
:=
doAuthRequest
(
r
)
authResponse
,
err
:=
h
.
doAuthRequest
(
r
)
if
err
!=
nil
{
if
err
!=
nil
{
fail500
(
w
,
err
)
fail500
(
w
,
err
)
return
return
...
@@ -157,7 +165,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -157,7 +165,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
// .- and ..-free URL.". In other words, we may assume that
// .- and ..-free URL.". In other words, we may assume that
// r.URL.Path does not contain '/../', so there is no possibility
// r.URL.Path does not contain '/../', so there is no possibility
// of path traversal here.
// of path traversal here.
repoPath
:=
path
.
Join
(
repoRoot
,
strings
.
TrimSuffix
(
r
.
URL
.
Path
,
g
.
suffix
))
repoPath
:=
path
.
Join
(
h
.
repoRoot
,
strings
.
TrimSuffix
(
r
.
URL
.
Path
,
g
.
suffix
))
if
!
looksLikeRepo
(
repoPath
)
{
if
!
looksLikeRepo
(
repoPath
)
{
http
.
Error
(
w
,
"Not Found"
,
404
)
http
.
Error
(
w
,
"Not Found"
,
404
)
return
return
...
@@ -176,8 +184,8 @@ func looksLikeRepo(p string) bool {
...
@@ -176,8 +184,8 @@ func looksLikeRepo(p string) bool {
return
true
return
true
}
}
func
doAuthRequest
(
r
*
http
.
Request
)
(
result
*
http
.
Response
,
err
error
)
{
func
(
h
*
gitHandler
)
doAuthRequest
(
r
*
http
.
Request
)
(
result
*
http
.
Response
,
err
error
)
{
url
:=
fmt
.
Sprintf
(
"%s%s"
,
*
authBackend
,
r
.
URL
.
RequestURI
()
)
url
:=
h
.
authBackend
+
r
.
URL
.
RequestURI
(
)
authReq
,
err
:=
http
.
NewRequest
(
r
.
Method
,
url
,
nil
)
authReq
,
err
:=
http
.
NewRequest
(
r
.
Method
,
url
,
nil
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -187,7 +195,7 @@ func doAuthRequest(r *http.Request) (result *http.Response, err error) {
...
@@ -187,7 +195,7 @@ func doAuthRequest(r *http.Request) (result *http.Response, err error) {
for
k
,
v
:=
range
r
.
Header
{
for
k
,
v
:=
range
r
.
Header
{
authReq
.
Header
[
k
]
=
v
authReq
.
Header
[
k
]
=
v
}
}
return
httpClient
.
Do
(
authReq
)
return
h
.
h
ttpClient
.
Do
(
authReq
)
}
}
func
handleGetInfoRefs
(
env
gitEnv
,
_
string
,
path
string
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
handleGetInfoRefs
(
env
gitEnv
,
_
string
,
path
string
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
...
...
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