client-inspect

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2020 License: MIT

README

client-inspect

GoDoc Go Report Card

Allows inspection of a http.Client connections

Heavily based on github.com/j0hnsmith/connspy

http package

A http.Client suitable for debugging, writes all http data to stdout.

import (
  "bytes"
  "fmt"
  "io/ioutil"
  "os"
  "regexp"
  "strings"

  clientInspect "github.com/petems/client-inspect/http"
)

func main() { 

  client := clientInspect.NewClient(nil, nil)

  resp, _ := client.Get("http://example.com/")
  // ensure all of the body is read
  ioutil.ReadAll(resp.Body)
  resp.Body.Close()

  resp, _ = client.Get("https://example.com/")
  ioutil.ReadAll(resp.Body)
  resp.Body.Close()

}

image

You can also specify the writer with http.NewClientWriter, which can be used to do things like redact certain fields:

import (
  "bytes"
  "fmt"
  "io/ioutil"
  "os"
  "regexp"
  "strings"

  clientInspect "github.com/petems/client-inspect/http"
)

func main() { 

  buf := new(bytes.Buffer)

  client := http.NewClientWriter(nil, nil, buf)

  resp, _ := client.Get("http://example.com/")
  // ensure all of the body is read
  ioutil.ReadAll(resp.Body)
  resp.Body.Close()

  resp, _ = client.Get("https://example.com/")
  ioutil.ReadAll(resp.Body)
  resp.Body.Close()

  httpLog := buf.String()

  s := strings.Split(httpLog, "\n")

  for count, line := range s {
    rgx := regexp.MustCompile(`^(Host: )(.+)$`)
    line = rgx.ReplaceAllString(line, `$1[REDACTED]`)
    s[count] = line
  }

  fmt.Println(s)

}

image

Background info

https://medium.com/@j0hnsmith/eavesdrop-on-a-golang-http-client-c4dc49af9d5e

Directories

Path Synopsis
Package http provides a http client that outputs raw http to stdout.
Package http provides a http client that outputs raw http to stdout.
Package net provides tools for eavesdropping on a net.Conn.
Package net provides tools for eavesdropping on a net.Conn.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL