main.mx raw

   1  // The classic ServeMux's matching, one assertion per registration shape.
   2  //
   3  // This is its own fixture because the phase-8 smoke server serves through the
   4  // CHANNEL path, whose router is a plain table: a broken pattern tree could pass
   5  // every network unit and still answer 404 to every real application. The bug
   6  // this pins was not in the matching logic but in whether the tree still held
   7  // what registration wrote: every shape below answered pattern=[] because the
   8  // node a registration call returned was a COPY of the node the tree stored
   9  // (src/runtime/codec.mx, return-value stability: the root arena was treated as
  10  // the callee's transient top frame).
  11  //
  12  // usage: muxmatch            exit 0 when every shape matches, 1 otherwise
  13  package main
  14  
  15  import (
  16  	"fmt"
  17  	"net/http"
  18  	"os"
  19  	"runtime"
  20  )
  21  
  22  // rec is the smallest stand-in for a routing node: a mutating method that
  23  // allocates a child of its own arena and stores it, with the ROOT arena made
  24  // current exactly the way mux registration does it.
  25  type rec struct {
  26  	key   string
  27  	child *rec
  28  }
  29  
  30  func (x *rec) add(key string) (c *rec) {
  31  	c = &rec{key: key}
  32  	x.child = c
  33  	return c
  34  }
  35  
  36  // tally counts failures. A package global may not be written outside init, so
  37  // the counter is a sovereign type reached through its initialiser.
  38  type tally struct {
  39  	n int32
  40  }
  41  
  42  func (c *tally) fail() {
  43  	c.n = c.n + 1
  44  }
  45  
  46  var fails = &tally{}
  47  
  48  func body(w http.ResponseWriter, r *http.Request) {
  49  	w.Write([]byte("ok\n"))
  50  }
  51  
  52  // probe asks the mux to match a request and compares the pattern it reports.
  53  func probe(label string, mux *http.ServeMux, method, rawurl, wantPat string) {
  54  	req, err := http.NewRequest(method, rawurl, nil)
  55  	if err != nil {
  56  		fmt.Printf("FAIL %-16s request: %v\n", label, err)
  57  		fails.fail()
  58  		return
  59  	}
  60  	h, pat := mux.Handler(req)
  61  	if h == nil {
  62  		fmt.Printf("FAIL %-16s %s %s: no handler, pattern=[%s]\n", label, method, req.URL.Path, pat)
  63  		fails.fail()
  64  		return
  65  	}
  66  	if pat != wantPat {
  67  		fmt.Printf("FAIL %-16s %s %s: pattern=[%s] want [%s]\n", label, method, req.URL.Path, pat, wantPat)
  68  		fails.fail()
  69  		return
  70  	}
  71  	fmt.Printf("ok   %-16s %s %s -> [%s]\n", label, method, req.URL.Path, pat)
  72  }
  73  
  74  // identity pins the property the tree depends on: a child built while the root
  75  // arena is current is the SAME node the receiver stored, so the node the caller
  76  // mutates is the node the structure holds. This is what the ServeMux bug was:
  77  // the two diverged and every route vanished.
  78  func identity() {
  79  	a := &rec{key: "a"}
  80  	saved := runtime.CurrentArena()
  81  	runtime.SetCurrentArena(runtime.RootArena())
  82  	b := a.add("b")
  83  	runtime.SetCurrentArena(saved)
  84  	if a.child != b {
  85  		fmt.Printf("FAIL %-16s stored child and returned child differ\n", "identity")
  86  		fails.fail()
  87  		return
  88  	}
  89  	fmt.Printf("ok   %-16s stored child is the returned child\n", "identity")
  90  }
  91  
  92  func main() {
  93  	// Termination: a batch program must still be stoppable, so the entry
  94  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  95  	select {
  96  	case <-shutdown:
  97  		return
  98  	default:
  99  	}
 100  
 101  	identity()
 102  
 103  	// Exact path.
 104  	exact := http.NewServeMux()
 105  	exact.HandleFunc("/smoke", body)
 106  	probe("exact", exact, "GET", "http://host/smoke", "/smoke")
 107  	probe("exact-miss", exact, "GET", "http://host/other", "")
 108  
 109  	// Subtree, both the root subtree and a named one.
 110  	root := http.NewServeMux()
 111  	root.HandleFunc("/", body)
 112  	probe("subtree-root", root, "GET", "http://host/", "/")
 113  	probe("subtree-root-deep", root, "GET", "http://host/a/b/c", "/")
 114  
 115  	api := http.NewServeMux()
 116  	api.HandleFunc("/api/", body)
 117  	probe("subtree-api", api, "GET", "http://host/api/v1/x", "/api/")
 118  
 119  	// Method pattern, and HEAD falling back to GET.
 120  	meth := http.NewServeMux()
 121  	meth.HandleFunc("GET /x", body)
 122  	probe("method", meth, "GET", "http://host/x", "GET /x")
 123  	probe("method-head", meth, "HEAD", "http://host/x", "GET /x")
 124  	probe("method-miss", meth, "POST", "http://host/x", "")
 125  
 126  	// Host pattern.
 127  	host := http.NewServeMux()
 128  	host.HandleFunc("example.com/y", body)
 129  	probe("host", host, "GET", "http://example.com/y", "example.com/y")
 130  
 131  	// Host + method + exact path, the fully specified shape.
 132  	all := http.NewServeMux()
 133  	all.HandleFunc("POST example.com/z/deep", body)
 134  	probe("host-method-path", all, "POST", "http://example.com/z/deep", "POST example.com/z/deep")
 135  
 136  	// The package-level mux, the shape every main() uses.
 137  	http.HandleFunc("/pkg", body)
 138  	probe("pkg-default", http.DefaultServeMux, "GET", "http://host/pkg", "/pkg")
 139  
 140  	if fails.n != 0 {
 141  		fmt.Printf("muxmatch: %d shape(s) did not match\n", fails.n)
 142  		os.Exit(1)
 143  	}
 144  	fmt.Printf("muxmatch ok\n")
 145  }
 146