From 4866ad0f7e809e8c7e7a94fd98d83cf85ca3735b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 1 Nov 2016 15:56:15 -0400 Subject: posthack --- src/edit/dir.go | 1 + src/edit/posthack.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/edit/posthack.go diff --git a/src/edit/dir.go b/src/edit/dir.go index 2be52d5..bc6ef6c 100644 --- a/src/edit/dir.go +++ b/src/edit/dir.go @@ -10,6 +10,7 @@ import ( ) func ServeGit(out http.ResponseWriter, in *http.Request) { + PostHack(in) upath := in.URL.Path if strings.HasPrefix(upath, "/") { upath = "/" + upath diff --git a/src/edit/posthack.go b/src/edit/posthack.go new file mode 100644 index 0000000..c190399 --- /dev/null +++ b/src/edit/posthack.go @@ -0,0 +1,59 @@ +package main + +import ( + "io" + "io/ioutil" + "strconv" + "net/http" + "strings" +) + +func SeekerLen(s io.Seeker) int64 { + cur, _ := s.Seek(0, io.SeekCurrent) + end, _ := s.Seek(0, io.SeekEnd) + s.Seek(cur, io.SeekStart) + return end +} + +func PostHack(req *http.Request) { + if req.Method == http.MethodPost { + // _method + if method := req.PostFormValue("_method"); method != "" { + req.Method = method + req.PostForm.Del("_method") + } + // _message + if msg := req.PostFormValue("_message"); msg != "" { + req.Header.Set("X-Commit-Message", msg) + req.PostForm.Del("_message") + } + // _body + if file, header, err := req.FormFile("_body"); err != nil { + req.Body = file + for k, v := range header.Header { + req.Header[k] = v + } + if header.Header.Get("Content-Length") == "" { + req.Header.Del("Content-Length") + } + req.ContentLength = SeekerLen(file) + + // Clear caches + req.Form = nil + req.PostForm = nil + req.MultipartForm = nil + } else if bodies := req.PostForm["_body"]; len(bodies) > 0 { + body := bodies[0] + req.Body = ioutil.NopCloser(strings.NewReader(body)) + if req.Header.Get("Content-Length") != "" { + req.Header.Set("Content-Length", strconv.Itoa(len(body))) + } + req.ContentLength = int64(len(body)) + + // Clear caches + req.Form = nil + req.PostForm = nil + req.MultipartForm = nil + } + } +} -- cgit v1.2.3