Exemple #1
0
func extractCommand(reader *textproto.Reader) (command, error) {
	line, err := reader.ReadLine()
	if err != nil {
		return getStats, err
	}

	for index, commandName := range commandNames {
		if commandName == line {
			return command(index), nil
		}
	}
	return getStats, errInvalidCommand
}
Exemple #2
0
func (s *Service) reader(r *textproto.Reader) {
	for {
		str, e := r.ReadLine()
		if e != nil {
			break
		}
		req := rxreq.FindStringSubmatch(str)
		if req == nil {
			break
		}
		if req[1] == "QUIT" {
			break
		}
		switch req[1] {
		case "PULL":
			if s.Debug {
				fmt.Println("got pull", req[2])
			}
			k, e := base64.StdEncoding.DecodeString(req[2])
			if e == nil {
				s.rpull <- k
			}
		case "PUSH":
			_, e = io.ReadFull(r.R, s.rblock)
			if e == nil {
				h := sha512.New()
				h.Write(s.rblock)
				hash := h.Sum([]byte{})
				if s.acc.Accept(hash) {
					if s.Debug {
						fmt.Println("got push", base64.StdEncoding.EncodeToString(hash), "accept")
					}
					s.bck.Store(hash, s.rblock)
				} else {
					if s.Debug {
						fmt.Println("got push", base64.StdEncoding.EncodeToString(hash), "refuse")
					}
				}
			}
		}
	}
}