func flagParse() { var err os.Error flag.Parse() (&options).nmcURL, err = http.ParseRequestURL(*flagNmcUrl) if err != nil { usage(err) } LOG.Println("Namecoind @", options.nmcURL) (&options).udpListen, err = net.ResolveUDPAddr("udp", *flagUdpListen) if err != nil { usage(err) } LOG.Println("Serving DNS @", options.udpListen) (&options).dnsProxy, err = net.ResolveUDPAddr("udp", *flagDnsProxy) if err != nil { usage(err) } LOG.Println("Proxy DNS @", options.dnsProxy) }
func pageHandler(w http.ResponseWriter, r *http.Request) { url, _ := http.ParseRequestURL(r.URL.String()) body, err := getFile(url.Path) if err != nil { http.Error(w, fmt.Sprintf("Could not find file: %s", url.Path), 404) } fmt.Fprintf(w, "%s", string(body)) }
// NewClient creates a new connection to a host given as "hostname" or "hostname:port". // If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID. // Default the port to 5222. func NewClient(host, user, passwd string) (*Client, os.Error) { addr := host if strings.TrimSpace(host) == "" { a := strings.Split(user, "@", 2) if len(a) == 2 { host = a[1] } } a := strings.Split(host, ":", 2) if len(a) == 1 { host += ":5222" } proxy := os.Getenv("HTTP_PROXY") if proxy == "" { proxy = os.Getenv("http_proxy") } if proxy != "" { url, err := http.ParseRequestURL(proxy) if err == nil { addr = url.Host } } c, err := net.Dial("tcp", addr) if err != nil { return nil, err } if proxy != "" { fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\n", host) fmt.Fprintf(c, "Host: %s\r\n", host) fmt.Fprintf(c, "\r\n") br := bufio.NewReader(c) req, _ := http.NewRequest("CONNECT", host, nil) resp, err := http.ReadResponse(br, req) if err != nil { return nil, err } if resp.StatusCode != 200 { f := strings.Split(resp.Status, " ", 2) return nil, os.ErrorString(f[1]) } } tlsconn := tls.Client(c, &DefaultConfig) if err = tlsconn.Handshake(); err != nil { return nil, err } if strings.LastIndex(host, ":") > 0 { host = host[:strings.LastIndex(host, ":")] } if err = tlsconn.VerifyHostname(host); err != nil { return nil, err } client := new(Client) client.tls = tlsconn if err := client.init(user, passwd); err != nil { client.Close() return nil, err } return client, nil }
func (w *widgetwrapper) Handle(evt string) { fmt.Println("Got event:", evt) evts := strings.Split(evt, ":") switch evts[0] { case "path": if ph, ok := w.w.(PathHandler); ok { rawurl := evt[len(evts[0])+1:] url, err := http.ParseRequestURL(rawurl) if err == nil { rawurl = url.Path } else { fmt.Println("got bad url:", err) } ph.pathChangedTo(rawurl) } else { fmt.Printf("Type of non-HasPath widget is %T\n", w.w) } case "onclick": clicked := Locate(Id(evts[1]), w.w) if clicked != nil { if clicked, ok := clicked.(Clickable); ok { r := clicked.HandleClick() fmt.Println("HandleClick gave", r) } } case "onchange": if len(evts) < 1 { fmt.Println("A broken onchange!") break } changed := Locate(Id(evts[1]), w.w) switch changed := changed.(type) { case interface { Changeable Bool }: if len(evts) == 2 { fmt.Println("I got a nice event to toggle") changed.Toggle() r := changed.HandleChange() fmt.Println("HandleChange gave", r) } else { fmt.Println("Ignoring a strange onchange text event with", len(evts), "events!") } case interface { Changeable String }: if len(evts) == 4 { changed.SetString(evts[3]) r := changed.HandleChange() fmt.Println("HandleChange gave", r) } else { fmt.Println("Ignoring a strange onchange text event with", len(evts), "events!") } case nil: fmt.Println("There is no event with id", evts[1]) default: fmt.Printf("I don't understand this event of type %t", changed) } } // if evt == "First time" { // dat.Write("read-cookie") // return // } else if strings.HasPrefix(evt, "cookie is ") { // fmt.Println("got cookie:", evt) // dat.Cookie = readCookie(evt[len("cookie is "):]) // dat.WriteCookie() // return // } out := `<h3> Event is ` + evt + "</h3>\n" html, ecs := w.w.Private__html() out += html for _, send := range w.sends { for _, ec := range ecs { fmt.Println("sending extra command:", ec) send(string(ec)) } send(out) } }