convert http req directly to PB

This commit is contained in:
Jeff Carr 2025-09-06 18:43:23 -05:00
parent f8b2ff5ace
commit 4e5a34b772
3 changed files with 32 additions and 25 deletions

View File

@ -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}"

View File

@ -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

46
http.go
View File

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