示例#1
0
文件: conn.go 项目: federomero/pq.go
func OpenRaw(uarel string) (*Conn, os.Error) {
	u, err := url.Parse(uarel)
	if err != nil {
		return nil, err
	}

	nc, err := net.Dial("tcp", u.Host)
	if err != nil {
		return nil, err
	}

	// TODO: use pass
	user, _, err := url.UnescapeUserinfo(u.RawUserinfo)
	if err != nil {
		return nil, err
	}

	params := make(proto.Values)
	params.Set("user", user)
	if u.Path != "" {
		params.Set("database", path.Base(u.Path))
	}

	return New(nc, params)
}
示例#2
0
文件: ncd.go 项目: pjjw/ncd
func root(w http.ResponseWriter, r *http.Request) {
	// check header
	if *flagPassword != "" && *flagUsername != "" {
		auth, ok := r.Header["Authorization"]
		if ok && strings.HasPrefix(auth[0], "Basic ") {
			str := strings.TrimLeft(auth[0], "Basic ")
			decode, err := base64.StdEncoding.DecodeString(str)
			if err != nil {
				log.Print("cannot decode auth string: ", err)
				return
			}
			user, pass, err := url.UnescapeUserinfo(string(decode))
			if err != nil {
				log.Print("auth: couldn't decode user/pass: "******"auth: wrong user/pass: "******"/"+pass, *r)
				return
			}
			/* log.Printf("auth: %#v, user: %s, pass: %s", auth, user, pass)*/
		} else {
			log.Print("auth: no authorization")
			return
		}
	}

	checkpb := new(CheckResultSet)
	if r.Method == "POST" {
		cout := new(bytes.Buffer)
		if _, err := cout.ReadFrom(r.Body); err != nil {
			log.Print("error! ", err)
			return
		}
		switch r.Header["Content-Type"][0] {
		case "application/x-protobuf":
			err := proto.Unmarshal(cout.Bytes(), checkpb)
			if err != nil {
				log.Printf("unmarshalling error: ", err)
			}
		case "application/json", "text/plain", "application/x-www-form-urlencoded", "multipart/form-data":
			err := json.Unmarshal(cout.Bytes(), checkpb)
			if err != nil {
				log.Printf("unmarshalling error: ", err)
			}
		}
		logger.Printf("check returned! %s", proto.CompactTextString(checkpb))
		for _, v := range checkpb.Results {
			_, err := WriteCheck(v, *flagSpoolDir)
			if err != nil {
				logger.Print("writecheck failed: ", err)
			}
		}
	} else {
		/* logger.Printf("NOT POST!! %s", r.Method)*/
	}
	templ.Execute(w, nil)
}