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
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
...
@@ -5,15 +5,12 @@ Miscellaneous helpers: logging, errors, subprocesses
package
main
package
main
import
(
import
(
"errors"
"fmt"
"fmt"
"io"
"io/ioutil"
"log"
"log"
"net/http"
"net/http"
"net/url"
"os"
"os"
"os/exec"
"os/exec"
"strings"
"syscall"
"syscall"
)
)
...
@@ -63,26 +60,38 @@ func cleanUpProcessGroup(cmd *exec.Cmd) {
...
@@ -63,26 +60,38 @@ func cleanUpProcessGroup(cmd *exec.Cmd) {
cmd
.
Wait
()
cmd
.
Wait
()
}
}
func
forwardResponseToClient
(
w
http
.
ResponseWriter
,
r
*
http
.
Response
)
{
func
setNoCacheHeaders
(
header
http
.
Header
)
{
log
.
Printf
(
"PROXY:%s %q %d"
,
r
.
Request
.
Method
,
r
.
Request
.
URL
,
r
.
StatusCode
)
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
{
func
openFile
(
path
string
)
(
file
*
os
.
File
,
fi
os
.
FileInfo
,
err
error
)
{
w
.
Header
()[
k
]
=
v
file
,
err
=
os
.
Open
(
path
)
if
err
!=
nil
{
return
}
}
w
.
WriteHeader
(
r
.
StatusCode
)
defer
func
()
{
io
.
Copy
(
w
,
r
.
Body
)
if
err
!=
nil
{
}
file
.
Close
()
}
}()
func
setHttpPostForm
(
r
*
http
.
Request
,
values
url
.
Values
)
{
fi
,
err
=
file
.
Stat
()
dataBuffer
:=
strings
.
NewReader
(
values
.
Encode
())
if
err
!=
nil
{
r
.
Body
=
ioutil
.
NopCloser
(
dataBuffer
)
return
r
.
ContentLength
=
int64
(
dataBuffer
.
Len
())
}
r
.
Header
.
Set
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
}
func
setNoCacheHeaders
(
header
http
.
Header
)
{
// The os.Open can also open directories
header
.
Set
(
"Cache-Control"
,
"no-cache, no-store, max-age=0, must-revalidate"
)
if
fi
.
IsDir
()
{
header
.
Set
(
"Pragma"
,
"no-cache"
)
err
=
&
os
.
PathError
{
header
.
Set
(
"Expires"
,
"Fri, 01 Jan 1990 00:00:00 GMT"
)
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
...
@@ -7,10 +7,8 @@ via the X-Sendfile mechanism. All that is needed in the Rails code is the
package
main
package
main
import
(
import
(
"fmt"
"log"
"log"
"net/http"
"net/http"
"os"
)
)
type
sendFileResponseWriter
struct
{
type
sendFileResponseWriter
struct
{
...
@@ -65,19 +63,13 @@ func (s *sendFileResponseWriter) WriteHeader(status int) {
...
@@ -65,19 +63,13 @@ func (s *sendFileResponseWriter) WriteHeader(status int) {
// Serve the file
// Serve the file
log
.
Printf
(
"SendFile: serving %q"
,
file
)
log
.
Printf
(
"SendFile: serving %q"
,
file
)
content
,
err
:=
os
.
Open
(
file
)
content
,
fi
,
err
:=
openFile
(
file
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
NotFound
(
s
.
rw
,
s
.
req
)
http
.
NotFound
(
s
.
rw
,
s
.
req
)
return
return
}
}
defer
content
.
Close
()
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
)
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
...
@@ -19,7 +19,7 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
return
return
}
}
content
,
err
:=
os
.
Open
(
file
)
content
,
fi
,
err
:=
openFile
(
file
)
if
err
!=
nil
{
if
err
!=
nil
{
if
notFoundHandler
!=
nil
{
if
notFoundHandler
!=
nil
{
notFoundHandler
(
w
,
r
)
notFoundHandler
(
w
,
r
)
...
@@ -30,21 +30,6 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
...
@@ -30,21 +30,6 @@ func handleServeFile(documentRoot *string, notFoundHandler serviceHandleFunc) se
}
}
defer
content
.
Close
()
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
)
log
.
Printf
(
"StaticFile: serving %q"
,
file
)
http
.
ServeContent
(
w
,
r
.
Request
,
filepath
.
Base
(
file
),
fi
.
ModTime
(),
content
)
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