From 45f184d061fe2cd73d1b3d46d7ddc3ce75a54f66 Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Fri, 9 Feb 2024 19:26:39 -0800 Subject: [PATCH] List file names in log when request/response bodies are pk zip archives --- README.org | 30 ++++++++++++++++++++++++++++-- main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/README.org b/README.org index bf9415e..b951ed4 100644 --- a/README.org +++ b/README.org @@ -195,7 +195,7 @@ ./gh-mrva -h # In log-submit-the-mrva-job.log after edit - SN=14 + SN=11 ./gh-mrva submit --language cpp --session mirva-session-$SN \ --list mirva-list \ --query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql >& log-$SN.out & @@ -208,7 +208,8 @@ ./gh-mrva download --session mirva-session-$SN \ --download-dbs \ --output-dir mirva-session-$SN-sarif \ - |& tee log-download.log + >& log-download-$SN.log & + echo log-download-$SN.log # 2024/02/08 15:33:39 >> Response body is # Zip archive data, at least v1.0 to extract, compression method=deflate # 0:$ unzip -v foo @@ -238,4 +239,29 @@ # Type 'help' for list of commands. # (dlv) c + dlv debug -- download --session mirva-session-$SN \ + --download-dbs \ + --output-dir mirva-session-$SN-sarif \ + #+END_SRC +** VS Code Debugger Configuration + launch.json + #+begin_src javascript + { + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "buildFlags": [], + "args": ["download", "--session", "mirva-session-11", "--download-dbs", "--output-dir","mirva-session-11-sarif"] + } + ] + } + #+end_src diff --git a/main.go b/main.go index 2811ceb..f8cd1fe 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ THE SOFTWARE. package main import ( + "archive/zip" "bytes" "io" "log" @@ -48,15 +49,11 @@ func main() { func LogRequestDump(req *http.Request) { log.Printf(">> %s %s", req.Method, req.URL) - - // TODO: show index for pk zip archives - // TODO: show json ?toc? for - // 2024/02/08 14:54:14 >> Request body: {"repositories":["google/flatbuffers"], - // "language":"cpp","query_pack":"H4sIAAAA...","action_repo_ref":"main"} req.Body = LogBody(req.Body, "request") } func LogBody(body io.ReadCloser, from string) io.ReadCloser { + if body != nil { buf, err := io.ReadAll(body) if err != nil { @@ -65,7 +62,50 @@ func LogBody(body io.ReadCloser, from string) io.ReadCloser { http.Error(w, err.Error(), http.StatusInternalServerError) return nil } - log.Printf(">> %s body: %v", from, string(buf)) + + IsZipFile := func() 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 + } + } + + 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 false { + // TODO: show json ?toc? for + // 2024/02/08 14:54:14 >> Request body: {"repositories":["google/flatbuffers"], + // "language":"cpp","query_pack":"H4sIAAAA...","action_repo_ref":"main"} + // 54:0:$ base64 -d < foo1 | gunzip | tar t|head -20 + // base64 has no magic number. + // We have to decode enough to check the gzip magic number, 0x1f 0x8b + // 4 chars in base64 yield 3 bytes. + } else { + log.Printf(">> %s body: %v", from, string(buf)) + } reader := io.NopCloser(bytes.NewBuffer(buf)) return reader