func mustParseMemberIDFromKey(key string) types.ID { id, err := types.IDFromString(path.Base(key)) if err != nil { log.Panicf("unexpected parse member id error: %v", err) } return id }
// If logerr is true, it prints out more error messages. func getClusterFromRemotePeers(urls []string, logerr bool, tr *http.Transport) (*Cluster, error) { cc := &http.Client{ Transport: tr, Timeout: time.Second, } for _, u := range urls { resp, err := cc.Get(u + "/members") if err != nil { if logerr { log.Printf("etcdserver: could not get cluster response from %s: %v", u, err) } continue } b, err := ioutil.ReadAll(resp.Body) if err != nil { if logerr { log.Printf("etcdserver: could not read the body of cluster response: %v", err) } continue } var membs []*Member if err := json.Unmarshal(b, &membs); err != nil { if logerr { log.Printf("etcdserver: could not unmarshal cluster response: %v", err) } continue } id, err := types.IDFromString(resp.Header.Get("X-Etcd-Cluster-ID")) if err != nil { if logerr { log.Printf("etcdserver: could not parse the cluster ID from cluster res: %v", err) } continue } var index uint64 // The header at or before v2.0.3 doesn't have this field. For backward // compatibility, it checks whether the field exists. if indexStr := resp.Header.Get("X-Raft-Index"); indexStr != "" { index, err = strconv.ParseUint(indexStr, 10, 64) if err != nil { if logerr { log.Printf("etcdserver: could not parse raft index: %v", err) } continue } } cl := NewClusterFromMembers("", id, membs) cl.UpdateIndex(index) return cl, nil } return nil, fmt.Errorf("etcdserver: could not retrieve cluster information from the given urls") }
func getID(p string, w http.ResponseWriter) (types.ID, bool) { idStr := trimPrefix(p, membersPrefix) if idStr == "" { http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) return 0, false } id, err := types.IDFromString(idStr) if err != nil { writeError(w, httptypes.NewHTTPError(http.StatusNotFound, fmt.Sprintf("No such member: %s", idStr))) return 0, false } return id, true }
func (h *streamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { w.Header().Set("Allow", "GET") http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) return } var t streamType switch path.Dir(r.URL.Path) { case path.Join(RaftStreamPrefix, string(streamTypeMsgApp)): t = streamTypeMsgApp case path.Join(RaftStreamPrefix, string(streamTypeMessage)): t = streamTypeMessage // backward compatibility case RaftStreamPrefix: t = streamTypeMsgApp default: log.Printf("rafthttp: ignored unexpected streaming request path %s", r.URL.Path) http.Error(w, "invalid path", http.StatusNotFound) return } fromStr := path.Base(r.URL.Path) from, err := types.IDFromString(fromStr) if err != nil { log.Printf("rafthttp: failed to parse from %s into ID", fromStr) http.Error(w, "invalid from", http.StatusNotFound) return } p := h.peerGetter.Get(from) if p == nil { log.Printf("rafthttp: fail to find sender %s", from) http.Error(w, "error sender not found", http.StatusNotFound) return } wcid := h.cid.String() if gcid := r.Header.Get("X-Etcd-Cluster-ID"); gcid != wcid { log.Printf("rafthttp: streaming request ignored due to cluster ID mismatch got %s want %s", gcid, wcid) http.Error(w, "clusterID mismatch", http.StatusPreconditionFailed) return } wto := h.id.String() if gto := r.Header.Get("X-Raft-To"); gto != wto { log.Printf("rafthttp: streaming request ignored due to ID mismatch got %s want %s", gto, wto) http.Error(w, "to field mismatch", http.StatusPreconditionFailed) return } w.WriteHeader(http.StatusOK) w.(http.Flusher).Flush() c := newCloseNotifier() conn := &outgoingConn{ t: t, termStr: r.Header.Get("X-Raft-Term"), Writer: w, Flusher: w.(http.Flusher), Closer: c, } p.attachOutgoingConn(conn) <-c.closeNotify() }