head 1.35; access; symbols; locks strick:1.35; strict; comment @# @; 1.35 date 2012.10.01.01.45.40; author strick; state Exp; branches; next 1.34; 1.34 date 2012.10.01.01.44.59; author strick; state Exp; branches; next 1.33; 1.33 date 2012.10.01.01.44.16; author strick; state Exp; branches; next 1.32; 1.32 date 2012.10.01.01.44.04; author strick; state Exp; branches; next 1.31; 1.31 date 2012.10.01.01.43.25; author strick; state Exp; branches; next 1.30; 1.30 date 2012.10.01.01.42.54; author strick; state Exp; branches; next 1.29; 1.29 date 2012.10.01.01.36.01; author strick; state Exp; branches; next 1.28; 1.28 date 2012.10.01.01.32.43; author strick; state Exp; branches; next 1.27; 1.27 date 2012.10.01.01.32.21; author strick; state Exp; branches; next 1.26; 1.26 date 2012.10.01.01.29.52; author strick; state Exp; branches; next 1.25; 1.25 date 2012.10.01.01.29.26; author strick; state Exp; branches; next 1.24; 1.24 date 2012.10.01.01.28.41; author strick; state Exp; branches; next 1.23; 1.23 date 2012.10.01.00.41.33; author strick; state Exp; branches; next 1.22; 1.22 date 2012.10.01.00.38.55; author strick; state Exp; branches; next 1.21; 1.21 date 2012.10.01.00.38.30; author strick; state Exp; branches; next 1.20; 1.20 date 2012.10.01.00.35.07; author strick; state Exp; branches; next 1.19; 1.19 date 2012.10.01.00.34.42; author strick; state Exp; branches; next 1.18; 1.18 date 2012.10.01.00.33.55; author strick; state Exp; branches; next 1.17; 1.17 date 2012.10.01.00.33.38; author strick; state Exp; branches; next 1.16; 1.16 date 2012.10.01.00.33.26; author strick; state Exp; branches; next 1.15; 1.15 date 2012.10.01.00.32.55; author strick; state Exp; branches; next 1.14; 1.14 date 2012.10.01.00.30.47; author strick; state Exp; branches; next 1.13; 1.13 date 2012.10.01.00.30.23; author strick; state Exp; branches; next 1.12; 1.12 date 2012.10.01.00.30.08; author strick; state Exp; branches; next 1.11; 1.11 date 2012.10.01.00.29.38; author strick; state Exp; branches; next 1.10; 1.10 date 2012.10.01.00.21.19; author strick; state Exp; branches; next 1.9; 1.9 date 2012.10.01.00.21.09; author strick; state Exp; branches; next 1.8; 1.8 date 2012.10.01.00.20.38; author strick; state Exp; branches; next 1.7; 1.7 date 2012.09.30.23.58.35; author strick; state Exp; branches; next 1.6; 1.6 date 2012.09.30.23.57.17; author strick; state Exp; branches; next 1.5; 1.5 date 2012.09.30.23.56.07; author strick; state Exp; branches; next 1.4; 1.4 date 2012.09.30.23.55.59; author strick; state Exp; branches; next 1.3; 1.3 date 2012.09.30.23.55.41; author strick; state Exp; branches; next 1.2; 1.2 date 2012.09.30.23.50.46; author strick; state Exp; branches; next 1.1; 1.1 date 2012.09.30.23.15.04; author strick; state Exp; branches; next ; desc @@ 1.35 log @/dev/null @ text @// package inspect package main import ( . "fmt" . "reflect" // "log" ) import "bytes" import "go/parser" import "go/ast" import "go/token" import "net/http" import "net/url" import "html" import "strconv" var _ = html.EscapeString var _ = html.UnescapeString type any interface{} func pr(x ...any) { for _, e := range x { Printf("## %#v ", e) } Println() } // The Registry of numbered objects. func Load() *ast.File { fset := token.NewFileSet() // positions are relative to fset // Parse the file containing this very example // but stop after processing the imports. root, err := parser.ParseFile(fset, "a.go", nil, 0 /*parser.Trace | parser.ParseComments */) if err != nil { panic(err) } pr("root", root) // Print the imports from the file's AST. for _, s := range root.Imports { Println(s.Path.Value) pr(s) pr(s.Path) pr(s.Path.Value) } return root } type Registry struct { next int m map[int]any } type Reply struct { w http.ResponseWriter r *http.Request buf *bytes.Buffer registry *Registry query url.Values } func NewReply(w http.ResponseWriter, r *http.Request, registry *Registry) *Reply { z := &Reply{w: w, r: r, buf: bytes.NewBuffer(nil), registry: registry, query: r.URL.Query()} return z } func NewInspector(root any) *Registry { z := new(Registry) z.m = make(map[int]any) return z } func (p *Registry) Register(x any) int { z := p.next p.next++ p.m[z] = x return z } func (p *Registry) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) // z := bytes.NewBuffer(nil) // z.WriteString("EHLO\r\n") reply := NewReply(w, r, p) reply.Emit() w.Write(reply.buf.Bytes()) } func (p *Reply) Emit() { pr(p.query) s := p.query.Get("n") n, err := strconv.Atoi(s) if err != nil { panic("'n' should be a number: " + s) } obj := p.registry.m[n] p.EmitObject(obj) } func (p *Reply) Html(s string) { p.buf.WriteString(s) } func (p *Reply) Ascii(s string) { p.buf.WriteString(html.EscapeString(s)) } func (p *Reply) EmitObject(x any) { p.Ascii(Sprintf("%#v", x)) } func Serve(root *ast.File) { http.Handle("/", NewInspector(root)) http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { Fprintf(w, "Hello, %q ;;; %q", html.EscapeString(r.URL.Path), html.EscapeString(Sprintf("%#v", r.URL.Query()))) }) panic(http.ListenAndServe(":8080", nil)) } func main() { var x float64 = 3.4 Println("type:", TypeOf(x)) var v = ValueOf(x) Println("value:", v) Println("value:", v.Kind()) Println("value:", v.Float()) root := Load() Serve(root) } @ 1.34 log @/dev/null @ text @d25 1 a25 1 func p(x ...any) { d44 1 a44 1 p("root", root) d49 3 a51 3 p(s) p(s.Path) p(s.Path.Value) d100 1 a100 1 p(p.query) @ 1.33 log @/dev/null @ text @d100 1 @ 1.32 log @/dev/null @ text @d100 1 a100 1 s := query.Get("n") @ 1.31 log @/dev/null @ text @d71 1 a71 3 z := &Reply{w, r, bytes.NewBuffer(nil), registry} z.query = r.URL.Query() @ 1.30 log @/dev/null @ text @d16 1 @ 1.29 log @/dev/null @ text @a62 1 query map[string]string d65 2 d70 3 a72 1 z := &Reply{w, r, make(map[string]string), bytes.NewBuffer(nil), registry} d101 1 a101 1 s := p.r.URL.Query().Get("n") @ 1.28 log @/dev/null @ text @a31 2 type RegMap map[int]any d55 1 a55 1 type Inspector struct { d57 1 a57 1 Reg RegMap d63 3 a65 3 query map[string]string buf *bytes.Buffer inspector *Inspector d68 2 a69 2 func NewReply(w http.ResponseWriter, r *http.Request, inspector *Inspector) *Reply { z := &Reply{w, r, make(map[string]string), bytes.NewBuffer(nil), inspector} d74 3 a76 3 func NewInspector(root any) *Inspector { z := new(Inspector) z.Reg = make(RegMap) d80 1 a80 1 func (p *Inspector) Register(x any) int { d83 1 a83 1 p.Reg[z] = x d86 1 a86 1 func (p *Inspector) ServeHTTP(w http.ResponseWriter, r *http.Request) { d104 1 a104 1 obj := p.inspector.Reg[n] @ 1.27 log @/dev/null @ text @d17 1 @ 1.26 log @/dev/null @ text @d64 3 a66 2 query map[string]string buf *bytes.Buffer d69 2 a70 2 func NewReply(w http.ResponseWriter, r *http.Request) *Reply { z := &Reply{w, r, make(map[string]string), bytes.NewBuffer(nil)} d93 1 a93 1 reply := NewReply(w, r) d99 2 a100 2 nStr := p.r.URL.Query().Get("n") n, err := strconv.Atoi(nStr) d102 1 a102 1 panic("'n' should be a number: " + nstr) d105 1 a105 1 obj := p.Reg[n] @ 1.25 log @/dev/null @ text @d65 1 a65 1 buf bytes.Buffer @ 1.24 log @/dev/null @ text @d65 1 a65 1 buf bytes.Buffer d67 1 d69 1 a69 1 z := &Reply{w, r, make(map[string]string, bytes.NewBuffer(nil)} d93 1 a93 1 reply.Emit() d97 9 a105 7 func (p* Reply) Emit() { nStr := p.r.URL.Query().Get("n") n, err := strconv.Atoi(nStr) if err != nil {panic("'n' should be a number: " + nstr)} obj := p.Reg[n] p.EmitObject(obj) d107 1 a107 1 func (p* Reply) Html(s string) { d110 1 a110 1 func (p* Reply) Ascii(s string) { d113 1 a113 1 func (p* Reply) EmitObject(x any) { @ 1.23 log @/dev/null @ text @d60 12 a73 1 // z := &Inspector{} d88 2 a89 1 z := bytes.NewBuffer(nil) d91 2 a92 1 z.WriteString("EHLO\r\n") d94 18 a111 1 w.Write(z.Bytes()) @ 1.22 log @/dev/null @ text @d21 3 a23 1 func p(x ...interface{}) { a30 1 type any interface{} d61 1 a61 1 func NewInspector(root interface{}) *Inspector { @ 1.21 log @/dev/null @ text @d61 2 a62 1 z = &Inspector{} @ 1.20 log @/dev/null @ text @d17 1 a27 1 a31 1 d38 1 a38 1 tree, err := parser.ParseFile(fset, "a.go", nil, 0 /*parser.Trace | parser.ParseComments */) d43 1 a43 1 p("tree", tree) d46 1 a46 1 for _, s := range tree.Imports { d52 1 a52 1 return tree d56 2 a57 2 next int Reg RegMap d59 5 a63 4 func NewInspector(root interface{})(z *Inspector){ z = &Inspector{} z.Reg = make(RegMap) return d66 1 a66 1 func (p*Inspector)Register(x any) int { d72 1 a72 1 func (p*Inspector) ServeHTTP(w http.ResponseWriter, r *http.Request) { d82 2 a83 2 func Serve(tree *ast.File) { http.Handle("/", NewInspector(tree)) d86 2 a87 2 Fprintf(w, "Hello, %q ;;; %q", html.EscapeString(r.URL.Path), html.EscapeString(Sprintf("%#v",r.URL.Query()))) }) d102 2 a103 2 tree := Load() Serve(tree) @ 1.19 log @/dev/null @ text @d83 1 a83 2 insp := NewInspector(tree) http.Handle("/", &Inspector{}) @ 1.18 log @/dev/null @ text @d83 1 a83 2 insp := &Inspector{} _ = insp.Register(tree) @ 1.17 log @/dev/null @ text @d63 1 @ 1.16 log @/dev/null @ text @d60 1 a60 1 func NewInspector(root interface{})(z Inspector){ @ 1.15 log @/dev/null @ text @d60 1 a60 1 func NewInspector(root interface{}) z Inspector { @ 1.14 log @/dev/null @ text @d60 4 @ 1.13 log @/dev/null @ text @d78 1 a78 1 insp = &Inspector{} @ 1.12 log @/dev/null @ text @d12 1 a12 1 import "go/parser/ast" @ 1.11 log @/dev/null @ text @a9 1 import "ast" d12 1 @ 1.10 log @/dev/null @ text @d10 1 d33 1 a33 1 func Load() { d39 1 a39 1 f, err := parser.ParseFile(fset, "a.go", nil, 0 /*parser.Trace | parser.ParseComments */) d44 1 a44 1 p("f", f) d47 1 a47 1 for _, s := range f.Imports { d53 1 d59 1 a60 1 } d77 3 a79 1 func Serve() { d99 2 a100 2 Load() Serve() @ 1.9 log @/dev/null @ text @d60 1 a60 1 z := next @ 1.8 log @/dev/null @ text @d55 2 a56 2 var next int = 101 var Reg RegMap @ 1.7 log @/dev/null @ text @a25 1 var next int = 101 a30 8 var Reg RegMap func Register(x any) int { z := next next++ Reg[z] = x return z } d55 9 d75 2 a76 2 func Server() { http.Handle("/foo", &Inspector{}) d96 1 a96 1 Server() @ 1.6 log @/dev/null @ text @d79 1 a79 1 Fprintf(w, "Hello, %q ;;; %#v", html.EscapeString(r.URL.Path), html.EscapeString(Sprintf("%#v",r.URL.Query()))) @ 1.5 log @/dev/null @ text @d79 1 a79 1 Fprintf(w, "Hello, %q ;;; %#v", html.EscapeString(r.URL.Path), html.EscapeString(r.URL.Query())) @ 1.4 log @/dev/null @ text @d79 1 a79 1 Fprintf(w, "Hello, %q ;;; %#v", html.EscapeString(r.URL.Path), html.EscapeString(r.URL.Query)) @ 1.3 log @/dev/null @ text @d79 1 a79 1 Fprintf(w, "Hello, %q ;;; %#v", html.EscapeString(r.URL.Path), html.EscapeStgring(r.URL.Query)) @ 1.2 log @/dev/null @ text @a66 3 //w.write("HTTP/1.1 200 OK\r\n") //w.write("Content-Type: text/html\r\n") //w.write("\r\n") d79 1 a79 1 Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) @ 1.1 log @/dev/null @ text @d7 1 d10 1 d14 5 d63 15 d79 7 a85 8 s := &http.Server{ Addr: ":8080", Handler: myHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } log.Fatal(s.ListenAndServe()) d89 1 d99 1 @