http.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdhttp
  15. import (
  16. "errors"
  17. "math"
  18. "net/http"
  19. "strings"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/coreos/pkg/capnslog"
  22. etcdErr "github.com/coreos/etcd/error"
  23. "github.com/coreos/etcd/etcdserver/auth"
  24. "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
  25. )
  26. const (
  27. // time to wait for response from EtcdServer requests
  28. // 5s for disk and network delay + 10*heartbeat for commit and possible
  29. // leader switch
  30. // TODO: use heartbeat set in etcdserver
  31. defaultServerTimeout = 5*time.Second + 10*(100*time.Millisecond)
  32. // time to wait for a Watch request
  33. defaultWatchTimeout = time.Duration(math.MaxInt64)
  34. )
  35. var (
  36. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdhttp")
  37. errClosed = errors.New("etcdhttp: client closed connection")
  38. )
  39. // writeError logs and writes the given Error to the ResponseWriter
  40. // If Error is an etcdErr, it is rendered to the ResponseWriter
  41. // Otherwise, it is assumed to be an InternalServerError
  42. func writeError(w http.ResponseWriter, err error) {
  43. if err == nil {
  44. return
  45. }
  46. switch e := err.(type) {
  47. case *etcdErr.Error:
  48. e.WriteTo(w)
  49. case *httptypes.HTTPError:
  50. e.WriteTo(w)
  51. case auth.Error:
  52. herr := httptypes.NewHTTPError(http.StatusBadRequest, e.Error())
  53. herr.WriteTo(w)
  54. default:
  55. plog.Errorf("got unexpected response error (%v)", err)
  56. herr := httptypes.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
  57. herr.WriteTo(w)
  58. }
  59. }
  60. // allowMethod verifies that the given method is one of the allowed methods,
  61. // and if not, it writes an error to w. A boolean is returned indicating
  62. // whether or not the method is allowed.
  63. func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
  64. for _, meth := range ms {
  65. if m == meth {
  66. return true
  67. }
  68. }
  69. w.Header().Set("Allow", strings.Join(ms, ","))
  70. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  71. return false
  72. }
  73. func requestLogger(handler http.Handler) http.Handler {
  74. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  75. plog.Debugf("[%s] %s remote:%s", r.Method, r.RequestURI, r.RemoteAddr)
  76. handler.ServeHTTP(w, r)
  77. })
  78. }