/* Copyright © 2026 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package cfg import ( "bytes" "encoding/json" "fmt" "log/slog" "net/http" ) // used to print uri, path and body of a request made by the go-client type DebugTransport struct { Transport http.RoundTripper } func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) { content := "" contentline := "" if req.ContentLength > 0 { buf := new(bytes.Buffer) body, _ := req.GetBody() _, err := buf.ReadFrom(body) if err != nil { return nil, err } var pretty bytes.Buffer err = json.Indent(&pretty, buf.Bytes(), "", "\t") if err != nil { return nil, fmt.Errorf("json parse error: %w", err) } content = pretty.String() contentline = buf.String() } if req.Header.Get("Accept") != "" { req.Header.Del("Accept") req.Header.Add("Accept", "application/json") } slog.Info("req", "host", req.URL.Host, "uri", req.URL.Path, "body", content, "bodyline", contentline, "headers", req.Header, ) return t.Transport.RoundTrip(req) } // Fixes https://codeberg.org/scip/esctl/issues/79: // the API client has this Accept header hardcoded everywhere: // req.Header.Set("Accept", "application/vnd.elasticsearch+json;compatible-with=9") // While this works pretty well with ES9, it doesn't with ES8. So, we replace // this header with a new one without the compatibility part. type CompatibilityTransport struct { Transport http.RoundTripper } func (t *CompatibilityTransport) RoundTrip(req *http.Request) (*http.Response, error) { if req.Header.Get("Accept") != "" { req.Header.Del("Accept") req.Header.Add("Accept", "application/json") } return t.Transport.RoundTrip(req) }