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
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
iv
gitlab-workhorse
Commits
e9616610
Commit
e9616610
authored
Dec 10, 2015
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use helper to open file (not directory)
parent
9f3966b8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
46 deletions
+32
-46
helpers.go
helpers.go
+30
-21
sendfile.go
sendfile.go
+1
-9
servefile.go
servefile.go
+1
-16
No files found.
helpers.go
View file @
e9616610
...
...
@@ -5,15 +5,12 @@ Miscellaneous helpers: logging, errors, subprocesses
package
main
import
(
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"syscall"
)
...
...
@@ -63,26 +60,38 @@ func cleanUpProcessGroup(cmd *exec.Cmd) {
cmd
.
Wait
()
}
func
forwardResponseToClient
(
w
http
.
ResponseWriter
,
r
*
http
.
Response
)
{
log
.
Printf
(
"PROXY:%s %q %d"
,
r
.
Request
.
Method
,
r
.
Request
.
URL
,
r
.
StatusCode
)
func
setNoCacheHeaders
(
header
http
.
Header
)
{
header
.
Set
(
"Cache-Control"
,
"no-cache, no-store, max-age=0, must-revalidate"
)
header
.
Set
(
"Pragma"
,
"no-cache"
)
header
.
Set
(
"Expires"
,
"Fri, 01 Jan 1990 00:00:00 GMT"
)
}
for
k
,
v
:=
range
r
.
Header
{
w
.
Header
()[
k
]
=
v
func
openFile
(
path
string
)
(
file
*
os
.
File
,
fi
os
.
FileInfo
,
err
error
)
{
file
,
err
=
os
.
Open
(
path
)
if
err
!=
nil
{
return
}
w
.
WriteHeader
(
r
.
StatusCode
)
io
.
Copy
(
w
,
r
.
Body
)
}
defer
func
()
{
if
err
!=
nil
{
file
.
Close
()
}
}()
func
setHttpPostForm
(
r
*
http
.
Request
,
values
url
.
Values
)
{
dataBuffer
:=
strings
.
NewReader
(
values
.
Encode
())
r
.
Body
=
ioutil
.
NopCloser
(
dataBuffer
)
r
.
ContentLength
=
int64
(
dataBuffer
.
Len
())
r
.
Header
.
Set
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
}
fi
,
err
=
file
.
Stat
()
if
err
!=
nil
{
return
}
func
setNoCacheHeaders
(
header
http
.
Header
)
{
header
.
Set
(
"Cache-Control"
,
"no-cache, no-store, max-age=0, must-revalidate"
)
header
.
Set
(
"Pragma"
,
"no-cache"
)
header
.
Set
(
"Expires"
,
"Fri, 01 Jan 1990 00:00:00 GMT"
)
// The os.Open can also open directories
if
fi
.
IsDir
()
{
err
=
&
os
.
PathError
{
Op
:
"open"
,
Path
:
path
,
Err
:
errors
.
New
(
"path is directory"
),
}
return
}
return
}
sendfile.go
View file @
e9616610
...
...
@@ -7,10 +7,8 @@ via the X-Sendfile mechanism. All that is needed in the Rails code is the
package
main
import
(
"fmt"
"log"
"net/http"
"os"
)
type
sendFileResponseWriter
struct
{
...
...
@@ -65,19 +63,13 @@ func (s *sendFileResponseWriter) WriteHeader(status int) {
// Serve the file
log
.
Printf
(
"SendFile: serving %q"
,
file
)
content
,
err
:=
os
.
Open
(
file
)
content
,
fi
,
err
:=
openFile
(
file
)
if
err
!=
nil
{
http
.
NotFound
(
s
.
rw
,
s
.
req
)
return
}
defer
content
.
Close
()
fi
,
err
:=
content
.
Stat
()
if
err
!=
nil
||
fi
.
IsDir
()
{
fail500
(
s
.
rw
,
fmt
.
Errorf
(
"handleSendfile: get mtime: %v"
,
err
))
return
}
http
.
ServeContent
(
s
.
rw
,
s
.
req
,
""
,
fi
.
ModTime
(),
content
)
}
...
...
servefile.go
View file @
e9616610
...
...
@@ -19,7 +19,7 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
return
}
content
,
err
:=
os
.
Open
(
file
)
content
,
fi
,
err
:=
openFile
(
file
)
if
err
!=
nil
{
if
notFoundHandler
!=
nil
{
notFoundHandler
(
w
,
r
)
...
...
@@ -30,21 +30,6 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
}
defer
content
.
Close
()
fi
,
err
:=
content
.
Stat
()
if
err
!=
nil
{
fail500
(
w
,
fmt
.
Errorf
(
"handleServeFileHandler"
,
err
))
return
}
if
fi
.
IsDir
()
{
if
notFoundHandler
!=
nil
{
notFoundHandler
(
w
,
r
)
}
else
{
http
.
NotFound
(
w
,
r
.
Request
)
}
return
}
log
.
Printf
(
"StaticFile: serving %q"
,
file
)
http
.
ServeContent
(
w
,
r
.
Request
,
filepath
.
Base
(
file
),
fi
.
ModTime
(),
content
)
}
...
...
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