Clean up and fix custom logging code
Unrecognized json caused duplicate output.
Specifically:
2024/02/12 18:58:54 WARNING: json decode error: json: unknown field "id"
This commit is contained in:
committed by
=Michael Hohn
parent
2877835899
commit
ff94bfa9f5
@@ -190,12 +190,12 @@
|
|||||||
|
|
||||||
# Build it
|
# Build it
|
||||||
go clean
|
go clean
|
||||||
go build -gcflags="all=-N -l" . # go build .
|
go build . # go build -gcflags="all=-N -l" .
|
||||||
|
|
||||||
./gh-mrva -h
|
./gh-mrva -h
|
||||||
|
|
||||||
# In log-submit-the-mrva-job.log after edit
|
# In log-submit-the-mrva-job.log after edit
|
||||||
SN=41
|
SN=54
|
||||||
./gh-mrva submit --language cpp --session mirva-session-$SN \
|
./gh-mrva submit --language cpp --session mirva-session-$SN \
|
||||||
--list mirva-list \
|
--list mirva-list \
|
||||||
--query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out &
|
--query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out &
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
238
main.go
238
main.go
@@ -51,11 +51,70 @@ func main() {
|
|||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsBase64Gzip(val []byte) bool {
|
||||||
|
// Some important payloads can be listed via
|
||||||
|
// base64 -d < foo1 | gunzip | tar t|head -20
|
||||||
|
//
|
||||||
|
// This function checks the request body up to the `gunzip` part.
|
||||||
|
//
|
||||||
|
if len(val) >= 4 {
|
||||||
|
// Extract header
|
||||||
|
hdr := make([]byte, base64.StdEncoding.DecodedLen(4))
|
||||||
|
_, err := base64.StdEncoding.Decode(hdr, []byte(val[0:4]))
|
||||||
|
if err != nil {
|
||||||
|
log.Println("WARNING: IsBase64Gzip decode error:", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Check for gzip heading
|
||||||
|
magic := []byte{0x1f, 0x8b}
|
||||||
|
if bytes.Equal(hdr[0:2], magic) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func LogRequestDump(req *http.Request) {
|
func LogRequestDump(req *http.Request) {
|
||||||
log.Printf(">> %s %s", req.Method, req.URL)
|
log.Printf(">> %s %s", req.Method, req.URL)
|
||||||
req.Body = LogBody(req.Body, "request")
|
req.Body = LogBody(req.Body, "request")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsZipFile(buf []byte) bool {
|
||||||
|
if len(buf) >= 4 {
|
||||||
|
// The header is []byte{ 0x50, 0x4b, 0x03, 0x04 }
|
||||||
|
magic := []byte{0x50, 0x4b, 0x03, 0x04}
|
||||||
|
if bytes.Equal(buf[0:4], magic) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaybeJSON(buf []byte) bool {
|
||||||
|
if len(buf) >= 4 { // {""} is 4 characters
|
||||||
|
if bytes.Equal(buf[0:2], []byte("{\"")) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type SubmitMsg struct {
|
||||||
|
ActionRepoRef string `json:"action_repo_ref"`
|
||||||
|
Language string `json:"language"`
|
||||||
|
QueryPack string `json:"query_pack"`
|
||||||
|
Repositories []string `json:"repositories"`
|
||||||
|
}
|
||||||
|
|
||||||
func LogBody(body io.ReadCloser, from string) io.ReadCloser {
|
func LogBody(body io.ReadCloser, from string) io.ReadCloser {
|
||||||
|
|
||||||
if body != nil {
|
if body != nil {
|
||||||
@@ -67,99 +126,24 @@ func LogBody(body io.ReadCloser, from string) io.ReadCloser {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
IsZipFile := func() bool {
|
if IsZipFile(buf) {
|
||||||
if len(buf) >= 4 {
|
ShowZipIndex(buf, from)
|
||||||
// The header is []byte{ 0x50, 0x4b, 0x03, 0x04 }
|
} else if MaybeJSON(buf) {
|
||||||
magic := []byte{0x50, 0x4b, 0x03, 0x04}
|
// See if the json contains a known message
|
||||||
if bytes.Equal(buf[0:4], magic) {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IsBase64Gzip := func(val []byte) bool {
|
|
||||||
// Some important payloads can be listed via
|
|
||||||
// base64 -d < foo1 | gunzip | tar t|head -20
|
|
||||||
//
|
|
||||||
// This function checks the request body up to the `gunzip` part.
|
|
||||||
//
|
|
||||||
if len(val) >= 4 {
|
|
||||||
// Extract header
|
|
||||||
hdr := make([]byte, base64.StdEncoding.DecodedLen(4))
|
|
||||||
_, err := base64.StdEncoding.Decode(hdr, []byte(val[0:4]))
|
|
||||||
if err != nil {
|
|
||||||
log.Println("WARNING: IsBase64Gzip decode error:", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// Check for gzip heading
|
|
||||||
magic := []byte{0x1f, 0x8b}
|
|
||||||
if bytes.Equal(hdr[0:2], magic) {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MaybeJSON := func() bool {
|
|
||||||
if len(buf) >= 4 { // {""} is 4 characters
|
|
||||||
if bytes.Equal(buf[0:2], []byte("{\"")) {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if IsZipFile() {
|
|
||||||
// Show index for pk zip archives
|
|
||||||
buf1 := make([]byte, len(buf))
|
|
||||||
copy(buf1, buf)
|
|
||||||
|
|
||||||
r, err := zip.NewReader(bytes.NewReader(buf1), int64(len(buf1)))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// defer r.Close()
|
|
||||||
|
|
||||||
// Print the archive index
|
|
||||||
log.Printf(">> %s body:\n", from)
|
|
||||||
log.Printf("zip file, contents:\n")
|
|
||||||
|
|
||||||
for _, f := range r.File {
|
|
||||||
log.Printf("\t%s\n", f.Name)
|
|
||||||
}
|
|
||||||
} else if MaybeJSON() {
|
|
||||||
// TODO: show index for encoded query packs in the json <value>:
|
|
||||||
// {..."query_pack": <value>,...}
|
|
||||||
//
|
|
||||||
type Message struct {
|
|
||||||
// FIXME: exact structure
|
|
||||||
ActionRepoRef string `json:"action_repo_ref"`
|
|
||||||
Language string `json:"language"`
|
|
||||||
QueryPack string `json:"query_pack"`
|
|
||||||
Repositories []string `json:"repositories"`
|
|
||||||
}
|
|
||||||
buf1 := make([]byte, len(buf))
|
buf1 := make([]byte, len(buf))
|
||||||
copy(buf1, buf)
|
copy(buf1, buf)
|
||||||
dec := json.NewDecoder(bytes.NewReader(buf1))
|
dec := json.NewDecoder(bytes.NewReader(buf1))
|
||||||
dec.DisallowUnknownFields()
|
dec.DisallowUnknownFields()
|
||||||
|
var m SubmitMsg
|
||||||
|
err := dec.Decode(&m)
|
||||||
|
|
||||||
var m Message
|
if err != nil {
|
||||||
if err := dec.Decode(&m); err == io.EOF {
|
|
||||||
log.Printf(">> %s body: %v", from, string(buf))
|
|
||||||
} else if err != nil {
|
|
||||||
log.Printf("WARNING: json decode error: %s\n", err)
|
|
||||||
log.Printf(">> %s body: %v", from, string(buf))
|
log.Printf(">> %s body: %v", from, string(buf))
|
||||||
|
goto BodyDone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print index for encoded query packs in the json <value>:
|
||||||
|
// {..."query_pack": <value>,...}
|
||||||
log.Printf(">> %s body:\n", from)
|
log.Printf(">> %s body:\n", from)
|
||||||
log.Printf(" \"%s\": \"%s\"\n", "action_repo_ref", m.ActionRepoRef)
|
log.Printf(" \"%s\": \"%s\"\n", "action_repo_ref", m.ActionRepoRef)
|
||||||
log.Printf(" \"%s\": \"%s\"\n", "language", m.Language)
|
log.Printf(" \"%s\": \"%s\"\n", "language", m.Language)
|
||||||
@@ -167,36 +151,7 @@ func LogBody(body io.ReadCloser, from string) io.ReadCloser {
|
|||||||
|
|
||||||
// Provide custom logging for encoded, compressed tar file
|
// Provide custom logging for encoded, compressed tar file
|
||||||
if IsBase64Gzip([]byte(m.QueryPack)) {
|
if IsBase64Gzip([]byte(m.QueryPack)) {
|
||||||
// These are decoded manually via
|
LogBase64GzippedTar(m)
|
||||||
// base64 -d < foo1 | gunzip | tar t | head -20
|
|
||||||
// but we need complete logs for inspection and testing.
|
|
||||||
// base64 decode the body
|
|
||||||
data, err := base64.StdEncoding.DecodeString(m.QueryPack)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("body decoding error:", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// gunzip the decoded body
|
|
||||||
gzb := bytes.NewBuffer(data)
|
|
||||||
gzr, err := gzip.NewReader(gzb)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
// tar t the gunzipped body
|
|
||||||
log.Printf(" \"%s\": \n", "query_pack")
|
|
||||||
log.Printf(" base64 encoded gzipped tar file, contents:\n")
|
|
||||||
tr := tar.NewReader(gzr)
|
|
||||||
for {
|
|
||||||
hdr, err := tr.Next()
|
|
||||||
if err == io.EOF {
|
|
||||||
break // End of archive
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln("Tar listing failure:", err)
|
|
||||||
}
|
|
||||||
// TODO: head / tail the listing
|
|
||||||
log.Printf(" %s\n", hdr.Name)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf(" \"%s\": \"%s\"\n", "query_pack", m.QueryPack)
|
log.Printf(" \"%s\": \"%s\"\n", "query_pack", m.QueryPack)
|
||||||
}
|
}
|
||||||
@@ -204,12 +159,63 @@ func LogBody(body io.ReadCloser, from string) io.ReadCloser {
|
|||||||
log.Printf(">> %s body: %v", from, string(buf))
|
log.Printf(">> %s body: %v", from, string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BodyDone:
|
||||||
reader := io.NopCloser(bytes.NewBuffer(buf))
|
reader := io.NopCloser(bytes.NewBuffer(buf))
|
||||||
return reader
|
return reader
|
||||||
}
|
}
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LogBase64GzippedTar(m SubmitMsg) {
|
||||||
|
// These are decoded manually via
|
||||||
|
// base64 -d < foo1 | gunzip | tar t | head -20
|
||||||
|
// but we need complete logs for inspection and testing.
|
||||||
|
// base64 decode the body
|
||||||
|
data, err := base64.StdEncoding.DecodeString(m.QueryPack)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("body decoding error:", err)
|
||||||
|
}
|
||||||
|
// gunzip the decoded body
|
||||||
|
gzb := bytes.NewBuffer(data)
|
||||||
|
gzr, err := gzip.NewReader(gzb)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
// tar t the gunzipped body
|
||||||
|
log.Printf(" \"%s\": \n", "query_pack")
|
||||||
|
log.Printf(" base64 encoded gzipped tar file, contents:\n")
|
||||||
|
tr := tar.NewReader(gzr)
|
||||||
|
for {
|
||||||
|
hdr, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
break // End of archive
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Tar listing failure:", err)
|
||||||
|
}
|
||||||
|
// TODO: head / tail the listing
|
||||||
|
log.Printf(" %s\n", hdr.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShowZipIndex(buf []byte, from string) {
|
||||||
|
buf1 := make([]byte, len(buf))
|
||||||
|
copy(buf1, buf)
|
||||||
|
|
||||||
|
r, err := zip.NewReader(bytes.NewReader(buf1), int64(len(buf1)))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the archive index
|
||||||
|
log.Printf(">> %s body:\n", from)
|
||||||
|
log.Printf("zip file, contents:\n")
|
||||||
|
|
||||||
|
for _, f := range r.File {
|
||||||
|
log.Printf("\t%s\n", f.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type contextKey struct {
|
type contextKey struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user