Ejemplo n.º 1
0
func considerTrids(q *http.Request) fttp.Responder {
	switch q.Method {
	case "GET":
		// let the range check panic indicate the problem
		trid, rx := checkTrid(q, q.URL.Path[len(tridPath):])
		if rx != nil {
			return rx
		}

		tags, err := getTags(trid)
		if err != nil {
			return fttp.ServerError(err)
		}

		jason, err := json.Marshal(tags)
		if err != nil {
			return fttp.ServerError(err)
		}

		r := &fttp.Response{}
		r.JSON()
		r.Set(jason)
		return r
	case "DELETE":
		// let the range check panic indicate the problem
		suffix := q.URL.Path[len(tridPath):]
		sep := strings.IndexByte(suffix, '/')
		if sep < 1 {
			// BUG: bad request
			return fttp.NotFound(q, errors.New("trid or tag not specified"))
		}
		trid, rx := checkTrid(q, suffix[:sep])
		if rx != nil {
			return rx
		}
		suffix = suffix[sep:]
		if !strings.HasPrefix(suffix, "/tag/") {
			// BUG: bad request
			return fttp.NotFound(q, errors.New("tag not specified"))
		}
		tag := suffix[len("/tag/"):]

		err := deleteTag(trid, tag)
		if err != nil {
			return fttp.ServerError(err)
		}

		r := &fttp.Response{}
		r.JSON()
		return r
	default:
		return fttp.BadMethod(q, "GET", "DELETE")
	}
}
Ejemplo n.º 2
0
func checkTrid(q *http.Request, maybeTrid string) (string, fttp.Responder) {
	for i := 0; i < len(maybeTrid); i++ {
		c := maybeTrid[i]
		if c < '0' || c > '9' {
			return "", fttp.NotFound(q, NotTridError(maybeTrid))
		}
	}
	return maybeTrid, nil
}