2015-01-01 12:02:25 -05:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2014-07-07 21:59:41 -04:00
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2014-07-07 22:23:01 -04:00
|
|
|
|
2014-07-21 20:24:51 -04:00
|
|
|
"github.com/chihaya/chihaya/stats"
|
2014-07-17 00:09:56 -04:00
|
|
|
"github.com/chihaya/chihaya/tracker/models"
|
2014-07-07 21:59:41 -04:00
|
|
|
)
|
|
|
|
|
2014-07-25 03:01:26 -04:00
|
|
|
func handleTorrentError(err error, w *Writer) (int, error) {
|
|
|
|
if err == nil {
|
|
|
|
return http.StatusOK, nil
|
2015-02-21 14:35:21 -05:00
|
|
|
} else if models.IsPublicError(err) {
|
2014-07-24 17:57:44 -04:00
|
|
|
w.WriteError(err)
|
2014-07-24 18:01:26 -04:00
|
|
|
stats.RecordEvent(stats.ClientError)
|
2014-07-17 00:09:56 -04:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2014-08-13 17:45:34 -04:00
|
|
|
|
2014-07-24 17:57:44 -04:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2014-07-23 01:34:40 -04:00
|
|
|
|
2014-07-24 17:57:44 -04:00
|
|
|
func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
|
|
|
writer := &Writer{w}
|
2015-02-20 02:06:44 -05:00
|
|
|
ann, err := s.newAnnounce(r, p)
|
2014-07-24 17:57:44 -04:00
|
|
|
if err != nil {
|
2014-07-25 03:01:26 -04:00
|
|
|
return handleTorrentError(err, writer)
|
2014-07-24 17:57:44 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 03:01:26 -04:00
|
|
|
return handleTorrentError(s.tracker.HandleAnnounce(ann, writer), writer)
|
2014-07-17 00:09:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request, p httprouter.Params) (int, error) {
|
|
|
|
writer := &Writer{w}
|
2015-02-20 02:06:44 -05:00
|
|
|
scrape, err := s.newScrape(r, p)
|
2014-07-24 17:57:44 -04:00
|
|
|
if err != nil {
|
2014-07-25 03:01:26 -04:00
|
|
|
return handleTorrentError(err, writer)
|
2014-07-17 00:09:56 -04:00
|
|
|
}
|
|
|
|
|
2014-07-25 03:01:26 -04:00
|
|
|
return handleTorrentError(s.tracker.HandleScrape(scrape, writer), writer)
|
2014-07-17 00:09:56 -04:00
|
|
|
}
|