tracker/http/routes.go

187 lines
4.8 KiB
Go
Raw Normal View History

2014-07-08 03:59:41 +02:00
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
"encoding/json"
2014-07-08 22:55:24 +02:00
"io/ioutil"
2014-07-08 03:59:41 +02:00
"net/http"
2014-07-16 11:46:33 +02:00
"net/url"
2014-07-08 03:59:41 +02:00
"github.com/julienschmidt/httprouter"
2014-07-22 02:24:51 +02:00
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker/models"
2014-07-08 03:59:41 +02:00
)
const jsonContentType = "application/json; charset=UTF-8"
func handleError(err error) (int, error) {
if err == nil {
return http.StatusOK, nil
2014-07-25 09:39:02 +02:00
} else if _, ok := err.(models.NotFoundError); ok {
stats.RecordEvent(stats.ClientError)
return http.StatusNotFound, nil
} else if _, ok := err.(models.ClientError); ok {
stats.RecordEvent(stats.ClientError)
return http.StatusBadRequest, nil
2014-07-22 07:19:09 +02:00
}
return http.StatusInternalServerError, err
}
2014-07-22 07:19:09 +02:00
func (s *Server) check(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
_, err := w.Write([]byte("STILL-ALIVE"))
return handleError(err)
2014-07-22 07:19:09 +02:00
}
func (s *Server) stats(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-22 07:12:41 +02:00
w.Header().Set("Content-Type", jsonContentType)
2014-07-25 01:35:15 +02:00
var err error
var val interface{}
query := r.URL.Query()
2014-07-25 01:35:15 +02:00
if _, flatten := query["flatten"]; flatten {
val = stats.DefaultStats.Flattened()
2014-07-25 01:35:15 +02:00
} else {
val = stats.DefaultStats
2014-07-25 01:35:15 +02:00
}
if _, pretty := query["pretty"]; pretty {
var buf []byte
buf, err = json.MarshalIndent(val, "", " ")
if err == nil {
_, err = w.Write(buf)
}
} else {
err = json.NewEncoder(w).Encode(val)
2014-07-16 11:44:26 +02:00
}
return handleError(err)
2014-07-16 11:44:26 +02:00
}
func handleTorrentError(err error, w *Writer) (int, error) {
if err == nil {
return http.StatusOK, nil
} else if _, ok := err.(models.ClientError); ok {
w.WriteError(err)
stats.RecordEvent(stats.ClientError)
return http.StatusOK, nil
}
return http.StatusInternalServerError, err
}
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
stats.RecordEvent(stats.Announce)
writer := &Writer{w}
ann, err := NewAnnounce(s.config, r, p)
if err != nil {
return handleTorrentError(err, writer)
}
return handleTorrentError(s.tracker.HandleAnnounce(ann, writer), writer)
}
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
stats.RecordEvent(stats.Scrape)
writer := &Writer{w}
scrape, err := NewScrape(s.config, r, p)
if err != nil {
return handleTorrentError(err, writer)
}
return handleTorrentError(s.tracker.HandleScrape(scrape, writer), writer)
}
func (s *Server) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-16 11:46:33 +02:00
infohash, err := url.QueryUnescape(p.ByName("infohash"))
if err != nil {
return http.StatusNotFound, err
}
torrent, err := s.tracker.FindTorrent(infohash)
2014-07-25 09:05:43 +02:00
if err != nil {
return handleError(err)
}
w.Header().Set("Content-Type", jsonContentType)
e := json.NewEncoder(w)
return handleError(e.Encode(torrent))
}
func (s *Server) putTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-08 22:55:24 +02:00
body, err := ioutil.ReadAll(r.Body)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
var torrent models.Torrent
err = json.Unmarshal(body, &torrent)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusBadRequest, err
2014-07-08 22:55:24 +02:00
}
s.tracker.PutTorrent(&torrent)
return http.StatusOK, nil
}
func (s *Server) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-16 11:46:33 +02:00
infohash, err := url.QueryUnescape(p.ByName("infohash"))
if err != nil {
return http.StatusNotFound, err
}
s.tracker.DeleteTorrent(infohash)
return http.StatusOK, nil
}
func (s *Server) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
user, err := s.tracker.FindUser(p.ByName("passkey"))
2014-07-23 06:37:30 +02:00
if err == models.ErrUserDNE {
2014-07-09 06:53:57 +02:00
return http.StatusNotFound, err
} else if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
}
w.Header().Set("Content-Type", jsonContentType)
e := json.NewEncoder(w)
return handleError(e.Encode(user))
}
func (s *Server) putUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
2014-07-08 22:55:24 +02:00
body, err := ioutil.ReadAll(r.Body)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusInternalServerError, err
2014-07-08 22:55:24 +02:00
}
var user models.User
err = json.Unmarshal(body, &user)
if err != nil {
2014-07-09 06:53:57 +02:00
return http.StatusBadRequest, err
2014-07-08 22:55:24 +02:00
}
s.tracker.PutUser(&user)
return http.StatusOK, nil
}
func (s *Server) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
s.tracker.DeleteUser(p.ByName("passkey"))
return http.StatusOK, nil
}
func (s *Server) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
s.tracker.PutClient(p.ByName("clientID"))
return http.StatusOK, nil
}
func (s *Server) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
s.tracker.DeleteClient(p.ByName("clientID"))
return http.StatusOK, nil
}