tracker/server/scrape.go

77 lines
1.7 KiB
Go
Raw Normal View History

2013-06-23 12:21:59 +02:00
// Copyright 2013 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.
2013-06-23 09:56:28 +02:00
package server
import (
2013-11-24 06:49:20 +01:00
"errors"
"io"
"log"
"net/http"
"path"
2013-06-23 09:56:28 +02:00
2013-12-01 04:39:02 +01:00
"github.com/chihaya/chihaya/storage"
2013-06-23 09:56:28 +02:00
)
func (s *Server) serveScrape(w http.ResponseWriter, r *http.Request) {
2013-11-24 06:49:20 +01:00
// Parse the query
pq, err := parseQuery(r.URL.RawQuery)
if err != nil {
fail(errors.New("Error parsing query"), w, r)
return
}
2013-06-23 09:56:28 +02:00
2013-11-24 06:49:20 +01:00
// Get a connection to the tracker db
2014-02-23 05:47:11 +01:00
conn, err := s.trackerPool.Get()
2013-11-24 06:49:20 +01:00
if err != nil {
log.Fatal(err)
}
2013-11-24 06:49:20 +01:00
// Find and validate the user
passkey, _ := path.Split(r.URL.Path)
_, err = validateUser(conn, passkey)
if err != nil {
fail(err, w, r)
return
}
2013-06-23 09:56:28 +02:00
2013-11-24 06:49:20 +01:00
io.WriteString(w, "d")
writeBencoded(w, "files")
if pq.Infohashes != nil {
for _, infohash := range pq.Infohashes {
torrent, exists, err := conn.FindTorrent(infohash)
if err != nil {
log.Panicf("server: %s", err)
}
if exists {
writeBencoded(w, infohash)
writeScrapeInfo(w, torrent)
}
}
} else if infohash, exists := pq.Params["info_hash"]; exists {
torrent, exists, err := conn.FindTorrent(infohash)
if err != nil {
log.Panicf("server: %s", err)
}
if exists {
writeBencoded(w, infohash)
writeScrapeInfo(w, torrent)
}
}
io.WriteString(w, "e")
2013-11-24 06:49:20 +01:00
w.(http.Flusher).Flush()
2013-06-23 09:56:28 +02:00
}
func writeScrapeInfo(w io.Writer, torrent *storage.Torrent) {
2013-11-24 06:49:20 +01:00
io.WriteString(w, "d")
writeBencoded(w, "complete")
writeBencoded(w, len(torrent.Seeders))
writeBencoded(w, "downloaded")
writeBencoded(w, torrent.Snatches)
writeBencoded(w, "incomplete")
writeBencoded(w, len(torrent.Leechers))
io.WriteString(w, "e")
2013-06-23 09:56:28 +02:00
}