Commit e9616610 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Use helper to open file (not directory)

parent 9f3966b8
......@@ -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
}
......@@ -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)
}
......
......@@ -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)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment