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 (
|
2014-07-08 04:23:01 +02:00
|
|
|
"encoding/json"
|
2014-07-08 22:55:24 +02:00
|
|
|
"io/ioutil"
|
2014-07-08 03:59:41 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2014-07-08 04:23:01 +02:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/drivers/tracker"
|
2014-07-08 22:55:24 +02:00
|
|
|
"github.com/chihaya/chihaya/models"
|
2014-07-08 03:59:41 +02:00
|
|
|
)
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) getTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 04:23:01 +02:00
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
torrent, err := conn.FindTorrent(p.ByName("infohash"))
|
|
|
|
if err == tracker.ErrTorrentDNE {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusNotFound, err
|
2014-07-08 04:23:01 +02:00
|
|
|
} else if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e := json.NewEncoder(w)
|
|
|
|
err = e.Encode(torrent)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) 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
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.PutTorrent(&torrent)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) delTorrent(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 04:44:27 +02:00
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:44:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 11:58:00 +02:00
|
|
|
err = conn.DeleteTorrent(p.ByName("infohash"))
|
2014-07-08 04:44:27 +02:00
|
|
|
if err == tracker.ErrTorrentDNE {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusNotFound, err
|
2014-07-08 04:44:27 +02:00
|
|
|
} else if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:44:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) getUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 04:23:01 +02:00
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := conn.FindUser(p.ByName("passkey"))
|
|
|
|
if err == tracker.ErrUserDNE {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusNotFound, err
|
2014-07-08 04:23:01 +02:00
|
|
|
} else if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e := json.NewEncoder(w)
|
|
|
|
err = e.Encode(user)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) 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
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.PutUser(&user)
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 22:55:24 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) delUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 04:44:27 +02:00
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:44:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 11:58:00 +02:00
|
|
|
err = conn.DeleteUser(p.ByName("passkey"))
|
2014-07-08 04:44:27 +02:00
|
|
|
if err == tracker.ErrUserDNE {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusNotFound, err
|
2014-07-08 04:44:27 +02:00
|
|
|
} else if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:44:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) putClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 04:23:01 +02:00
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 11:58:00 +02:00
|
|
|
err = conn.PutClient(p.ByName("clientID"))
|
2014-07-08 04:23:01 +02:00
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
func (t *Tracker) delClient(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
2014-07-08 04:23:01 +02:00
|
|
|
conn, err := t.tp.Get()
|
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 11:58:00 +02:00
|
|
|
err = conn.DeleteClient(p.ByName("clientID"))
|
2014-07-08 04:23:01 +02:00
|
|
|
if err != nil {
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusInternalServerError, err
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|
2014-07-08 03:59:41 +02:00
|
|
|
|
2014-07-09 06:53:57 +02:00
|
|
|
return http.StatusOK, nil
|
2014-07-08 04:23:01 +02:00
|
|
|
}
|