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 ...@@ -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
} }
...@@ -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)
} }
......
...@@ -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)
} }
......
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