From 4e5a34b772db9df404b0b927f3ee9b9b54f65cdd Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 6 Sep 2025 18:43:23 -0500 Subject: [PATCH] convert http req directly to PB --- Makefile | 4 ++-- handlePatches.go | 7 +++++++ http.go | 46 +++++++++++++++++++++++----------------------- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Makefile b/Makefile index 5a29ece..a58aff9 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,14 @@ VERSION = $(shell git describe --tags) BUILDTIME = $(shell date +%Y.%m.%d_%H%M) -all: build +all: build-verbose ./forged merge build: goimports GO111MODULE=off go build \ -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -verbose: +build-verbose: GO111MODULE=off go build -v -x \ -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" diff --git a/handlePatches.go b/handlePatches.go index 3a0317c..f270d74 100644 --- a/handlePatches.go +++ b/handlePatches.go @@ -5,6 +5,7 @@ import ( "strings" "go.wit.com/lib/protobuf/forgepb" + "go.wit.com/lib/protobuf/httppb" "go.wit.com/log" ) @@ -33,6 +34,12 @@ func handlePatches(w http.ResponseWriter, r *http.Request, data []byte) error { return nil } +func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) { + pb := forgepb.NewPatches() + err := pb.Unmarshal(reqPB.Body) + return pb, err +} + func sendPatchesError(w http.ResponseWriter, r *forgepb.Patches, err error) error { log.Info("send error back to user", err) return nil diff --git a/http.go b/http.go index fce515a..a88d6c3 100644 --- a/http.go +++ b/http.go @@ -2,13 +2,13 @@ package main import ( "fmt" - "io/ioutil" "net" "net/http" "strings" "time" "go.wit.com/lib/gui/shell" + "go.wit.com/lib/protobuf/httppb" "go.wit.com/log" ) @@ -49,15 +49,14 @@ func whoSent(r *http.Request) string { } func okHandler(w http.ResponseWriter, r *http.Request) { - // something appears to be buggy. always get this first I guess - msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte - r.Body.Close() - log.Info("TRYING TO MARSHAL bytes:", len(msg), err) + reqPB, err := httppb.ReqToPB(r) + if err != nil { + log.Info("something crazy err", err) + } who := whoSent(r) - var route string - route = cleanURL(r.URL.Path) + route := reqPB.Route parts := strings.Split(route, "?") // log.Info("client sent url =", route, parts) requrl := parts[0] @@ -78,7 +77,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { log.Warn("forged REQUEST URL =", requrl, "from =", who) if strings.HasPrefix(route, "/patches/") { - handlePatches(w, r, msg) + pb, err := makePatchesPB(reqPB) + log.Info("err", err, "len", pb.Len()) return } @@ -88,7 +88,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if route == "/patchset" { - if err := savePatchset(w, msg); err != nil { + if err := savePatchset(w, reqPB.Body); err != nil { log.Warn("forged /patchset error", err) return } @@ -100,8 +100,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if route == "/lookup" { - log.Info("doing lookup len(msg) =", len(msg)) - found, err := lookupRepos(msg) + log.Info("doing lookup len(reqPB.Body) =", len(reqPB.Body)) + found, err := lookupRepos(reqPB.Body) if err != nil { return } @@ -117,12 +117,12 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if strings.HasPrefix(route, "/patches/") { - handlePatches(w, r, msg) + handlePatches(w, r, reqPB.Body) return } if route == "/patchset" { - if err := savePatchset(w, msg); err != nil { + if err := savePatchset(w, reqPB.Body); err != nil { log.Warn("forged /patchset error", err) return } @@ -134,8 +134,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if route == "/lookup" { - log.Info("doing lookup len(msg) =", len(msg)) - found, err := lookupRepos(msg) + log.Info("doing lookup len(reqPB.Body) =", len(reqPB.Body)) + found, err := lookupRepos(reqPB.Body) if err != nil { return } @@ -151,12 +151,12 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if strings.HasPrefix(route, "/patches/") { - handlePatches(w, r, msg) + handlePatches(w, r, reqPB.Body) return } if route == "/patchset" { - if err := savePatchset(w, msg); err != nil { + if err := savePatchset(w, reqPB.Body); err != nil { log.Warn("forged /patchset error", err) return } @@ -168,8 +168,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if route == "/lookup" { - log.Info("doing lookup len(msg) =", len(msg)) - found, err := lookupRepos(msg) + log.Info("doing lookup len(reqPB.Body) =", len(reqPB.Body)) + found, err := lookupRepos(reqPB.Body) if err != nil { return } @@ -185,8 +185,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } if route == "/update" { - log.Info("doing update len(msg) =", len(msg)) - found, err := updateRepos(msg) + log.Info("doing update len(reqPB.Body) =", len(reqPB.Body)) + found, err := updateRepos(reqPB.Body) if err != nil { return } @@ -208,8 +208,8 @@ func okHandler(w http.ResponseWriter, r *http.Request) { /* start := time.Now() - log.Info("going to w.Write(msg) with len", len(msg)) - w.Write(msg) + log.Info("going to w.Write(reqPB.Body) with len", len(reqPB.Body)) + w.Write(reqPB.Body) */ age := shell.FormatDuration(time.Since(start)) log.Printf("Done with xfer in (%s). happy hacking!\n", age)