forked from krum110487/zipfs
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphpcgi.go
More file actions
51 lines (43 loc) · 1.36 KB
/
phpcgi.go
File metadata and controls
51 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package zipfs
import (
"io"
"net/http"
"net/http/cgi"
"os"
"strings"
)
func Cgi(w http.ResponseWriter, r *http.Request, phpBin string, scriptFileName string, htdocsPath string) {
handler := new(cgi.Handler)
handler.Path = phpBin
handler.Env = append(handler.Env, "REDIRECT_STATUS=CGI")
handler.Env = append(handler.Env, "SCRIPT_FILENAME="+strings.ReplaceAll(scriptFileName, "\\", "/"))
// Unchunk the request body when required
if len(r.TransferEncoding) > 0 && r.TransferEncoding[0] == "chunked" {
// Remove the chunked marker
r.TransferEncoding = r.TransferEncoding[1:]
}
if r.Body != nil { // Only do for > 10MB responses
// Save body into temp file (don't want large request bodies eating memory)
tmpfile, err := os.CreateTemp("", "fp-cgi-")
if err != nil {
http.Error(w, "Error creating temp file for request body", http.StatusInternalServerError)
return
}
defer os.Remove(tmpfile.Name())
// Write the body to the temp file
size, err := io.Copy(tmpfile, r.Body)
if err != nil {
http.Error(w, "Error writing request body to temp file", http.StatusInternalServerError)
return
}
// Move seek to start of file
_, err = tmpfile.Seek(0, 0)
if err != nil {
http.Error(w, "Error seeking to start of temp file", http.StatusInternalServerError)
return
}
r.Body = tmpfile
r.ContentLength = size
}
handler.ServeHTTP(w, r)
}