2015-01-01 18:02:25 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2014-07-02 03:40:29 +02:00
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2015-10-11 08:27:08 +02:00
|
|
|
// Package http implements a BitTorrent tracker over the HTTP protocol as per
|
|
|
|
// BEP 3.
|
2014-07-02 03:40:29 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2014-07-22 07:38:07 +02:00
|
|
|
"net"
|
2014-07-02 03:40:29 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2015-04-29 02:32:19 +02:00
|
|
|
"github.com/tylerb/graceful"
|
2014-07-02 03:40:29 +02:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-22 07:38:07 +02:00
|
|
|
"github.com/chihaya/chihaya/stats"
|
2014-07-17 06:09:56 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker"
|
2014-07-02 03:40:29 +02:00
|
|
|
)
|
|
|
|
|
2015-02-03 08:53:20 +01:00
|
|
|
// ResponseHandler is an HTTP handler that returns a status code.
|
2014-07-17 06:09:56 +02:00
|
|
|
type ResponseHandler func(http.ResponseWriter, *http.Request, httprouter.Params) (int, error)
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2015-02-03 08:53:20 +01:00
|
|
|
// Server represents an HTTP serving torrent tracker.
|
2014-07-17 06:09:56 +02:00
|
|
|
type Server struct {
|
2015-02-20 19:39:19 +01:00
|
|
|
config *config.Config
|
|
|
|
tracker *tracker.Tracker
|
|
|
|
grace *graceful.Server
|
|
|
|
stopping bool
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
|
2015-02-03 08:53:20 +01:00
|
|
|
// makeHandler wraps our ResponseHandlers while timing requests, collecting,
|
|
|
|
// stats, logging, and handling errors.
|
2014-07-02 03:40:29 +02:00
|
|
|
func makeHandler(handler ResponseHandler) httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
start := time.Now()
|
2014-07-16 17:52:59 +02:00
|
|
|
httpCode, err := handler(w, r, p)
|
2014-07-23 07:20:48 +02:00
|
|
|
duration := time.Since(start)
|
2014-07-22 07:40:52 +02:00
|
|
|
|
2015-06-27 03:07:29 +02:00
|
|
|
var msg string
|
2014-07-23 07:24:31 +02:00
|
|
|
if err != nil {
|
2014-07-25 09:39:02 +02:00
|
|
|
msg = err.Error()
|
|
|
|
} else if httpCode != http.StatusOK {
|
|
|
|
msg = http.StatusText(httpCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(msg) > 0 {
|
|
|
|
http.Error(w, msg, httpCode)
|
2014-07-22 07:40:52 +02:00
|
|
|
stats.RecordEvent(stats.ErroredRequest)
|
2014-07-25 09:39:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(msg) > 0 || glog.V(2) {
|
2014-07-25 09:43:06 +02:00
|
|
|
reqString := r.URL.Path + " " + r.RemoteAddr
|
2014-07-23 08:25:15 +02:00
|
|
|
if glog.V(3) {
|
2014-07-25 09:39:02 +02:00
|
|
|
reqString = r.URL.RequestURI() + " " + r.RemoteAddr
|
2014-07-23 08:25:15 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 09:39:02 +02:00
|
|
|
if len(msg) > 0 {
|
2015-06-27 03:07:29 +02:00
|
|
|
glog.Errorf("[HTTP - %9s] %s (%d - %s)", duration, reqString, httpCode, msg)
|
2014-07-25 09:39:02 +02:00
|
|
|
} else {
|
2015-06-27 03:07:29 +02:00
|
|
|
glog.Infof("[HTTP - %9s] %s (%d)", duration, reqString, httpCode)
|
2014-07-25 09:39:02 +02:00
|
|
|
}
|
2014-07-16 17:52:59 +02:00
|
|
|
}
|
2014-07-23 07:31:22 +02:00
|
|
|
|
|
|
|
stats.RecordEvent(stats.HandledRequest)
|
|
|
|
stats.RecordTiming(stats.ResponseTime, duration)
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-03 08:53:20 +01:00
|
|
|
// newRouter returns a router with all the routes.
|
2014-07-17 07:00:26 +02:00
|
|
|
func newRouter(s *Server) *httprouter.Router {
|
2014-07-02 03:40:29 +02:00
|
|
|
r := httprouter.New()
|
2014-07-09 06:53:57 +02:00
|
|
|
|
2014-07-25 22:58:26 +02:00
|
|
|
if s.config.PrivateEnabled {
|
2014-07-17 06:09:56 +02:00
|
|
|
r.GET("/users/:passkey/announce", makeHandler(s.serveAnnounce))
|
|
|
|
r.GET("/users/:passkey/scrape", makeHandler(s.serveScrape))
|
2014-07-02 03:40:29 +02:00
|
|
|
} else {
|
2014-07-17 06:09:56 +02:00
|
|
|
r.GET("/announce", makeHandler(s.serveAnnounce))
|
|
|
|
r.GET("/scrape", makeHandler(s.serveScrape))
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-02-03 08:53:20 +01:00
|
|
|
// connState is used by graceful in order to gracefully shutdown. It also
|
|
|
|
// keeps track of connection stats.
|
2014-07-22 07:38:07 +02:00
|
|
|
func (s *Server) connState(conn net.Conn, state http.ConnState) {
|
|
|
|
switch state {
|
|
|
|
case http.StateNew:
|
|
|
|
stats.RecordEvent(stats.AcceptedConnection)
|
|
|
|
|
|
|
|
case http.StateClosed:
|
|
|
|
stats.RecordEvent(stats.ClosedConnection)
|
|
|
|
|
|
|
|
case http.StateHijacked:
|
|
|
|
panic("connection impossibly hijacked")
|
|
|
|
|
2015-02-03 08:53:20 +01:00
|
|
|
// Ignore the following cases.
|
|
|
|
case http.StateActive, http.StateIdle:
|
2014-07-22 07:38:07 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
glog.Errorf("Connection transitioned to unknown state %s (%d)", state, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 20:08:46 +01:00
|
|
|
// Serve runs an HTTP server, blocking until the server has shut down.
|
2015-10-11 08:27:08 +02:00
|
|
|
func (s *Server) Serve() {
|
|
|
|
glog.V(0).Info("Starting HTTP on ", s.config.HTTPConfig.ListenAddr)
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2015-10-11 08:27:08 +02:00
|
|
|
if s.config.HTTPConfig.ListenLimit != 0 {
|
|
|
|
glog.V(0).Info("Limiting connections to ", s.config.HTTPConfig.ListenLimit)
|
2014-09-25 19:58:07 +02:00
|
|
|
}
|
2014-07-22 07:38:07 +02:00
|
|
|
|
2014-09-03 07:15:21 +02:00
|
|
|
grace := &graceful.Server{
|
2015-10-11 08:27:08 +02:00
|
|
|
Timeout: s.config.HTTPConfig.RequestTimeout.Duration,
|
2015-02-20 19:39:19 +01:00
|
|
|
ConnState: s.connState,
|
2015-10-11 08:27:08 +02:00
|
|
|
ListenLimit: s.config.HTTPConfig.ListenLimit,
|
2015-04-29 02:27:39 +02:00
|
|
|
|
|
|
|
NoSignalHandling: true,
|
2014-07-22 07:38:07 +02:00
|
|
|
Server: &http.Server{
|
2015-10-11 08:27:08 +02:00
|
|
|
Addr: s.config.HTTPConfig.ListenAddr,
|
2015-02-20 19:39:19 +01:00
|
|
|
Handler: newRouter(s),
|
2015-10-11 08:27:08 +02:00
|
|
|
ReadTimeout: s.config.HTTPConfig.ReadTimeout.Duration,
|
|
|
|
WriteTimeout: s.config.HTTPConfig.WriteTimeout.Duration,
|
2014-07-22 07:38:07 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-02-20 19:39:19 +01:00
|
|
|
s.grace = grace
|
2014-08-27 00:03:44 +02:00
|
|
|
grace.SetKeepAlivesEnabled(false)
|
2015-02-20 19:39:19 +01:00
|
|
|
grace.ShutdownInitiated = func() { s.stopping = true }
|
2014-08-27 00:03:44 +02:00
|
|
|
|
2014-07-25 09:43:06 +02:00
|
|
|
if err := grace.ListenAndServe(); err != nil {
|
2014-08-05 21:00:52 +02:00
|
|
|
if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") {
|
|
|
|
glog.Errorf("Failed to gracefully run HTTP server: %s", err.Error())
|
2015-02-20 19:39:19 +01:00
|
|
|
return
|
2014-08-05 21:00:52 +02:00
|
|
|
}
|
2014-07-25 09:43:06 +02:00
|
|
|
}
|
2015-02-20 19:39:19 +01:00
|
|
|
|
|
|
|
glog.Info("HTTP server shut down cleanly")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop cleanly shuts down the server.
|
|
|
|
func (s *Server) Stop() {
|
|
|
|
if !s.stopping {
|
|
|
|
s.grace.Stop(s.grace.Timeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a new HTTP server for a given configuration and tracker.
|
|
|
|
func NewServer(cfg *config.Config, tkr *tracker.Tracker) *Server {
|
|
|
|
return &Server{
|
|
|
|
config: cfg,
|
|
|
|
tracker: tkr,
|
|
|
|
}
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|