luci-go: Index | Examples | Files

package httpmitm

import ""


package main

import (

func getBody(s string) (body []string) {
    pastHeaders := false
    for _, part := range strings.Split(s, "\r\n") {
        if pastHeaders {
            body = append(body, part)
        } else if part == "" {
            pastHeaders = true

func main() {
    // Setup test server.
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello, Client!"))
    defer ts.Close()

    // Setup instrumented client.
    type record struct {
        o   Origin
        d   string
    var records []*record
    client := http.Client{
        Transport: &Transport{
            Callback: func(o Origin, data []byte, err error) {
                records = append(records, &record{o, string(data)})

    _, err := client.Post(ts.URL, "test", bytes.NewBufferString("Hail, Server!"))
    if err != nil {

    // There should be two records: request and response.
    for idx, r := range records {
        fmt.Printf("%d) %s\n", idx, r.o)
        for _, line := range getBody(r.d) {




Package Files


type Callback Uses

type Callback func(Origin, []byte, error)

Callback is a callback method that is invoked during HTTP communications to forward captured data.

type Origin Uses

type Origin uint

Origin is an enumeration used to annotate which type of data is being fed to the callback.

const (
    Request Origin = iota

Log transport types.

func (Origin) String Uses

func (t Origin) String() string

String converts a Origin to a user-friendly string.

type Transport Uses

type Transport struct {
    // Underlying RoundTripper; uses http.DefaultTransport if nil.

    Callback Callback // Output callback.

Transport is an implementation of http.RoundTripper that logs outgoing requests and incoming responses.

func (*Transport) RoundTrip Uses

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface.

Package httpmitm imports 4 packages (graph). Updated 2018-12-21. Refresh now. Tools for package owners.