http.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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"
  24. "github.com/coreos/etcd/etcdserver/auth"
  25. "github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
  26. )
  27. const (
  28. // time to wait for response from EtcdServer requests
  29. // 5s for disk and network delay + 10*heartbeat for commit and possible
  30. // leader switch
  31. // TODO: use heartbeat set in etcdserver
  32. defaultServerTimeout = 5*time.Second + 10*(100*time.Millisecond)
  33. // time to wait for a Watch request
  34. defaultWatchTimeout = time.Duration(math.MaxInt64)
  35. )
  36. var (
  37. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcdhttp")
  38. errClosed = errors.New("etcdhttp: client closed connection")
  39. )
  40. // writeError logs and writes the given Error to the ResponseWriter
  41. // If Error is an etcdErr, it is rendered to the ResponseWriter
  42. // Otherwise, it is assumed to be an InternalServerError
  43. func writeError(w http.ResponseWriter, err error) {
  44. if err == nil {
  45. return
  46. }
  47. switch e := err.(type) {
  48. case *etcdErr.Error:
  49. e.WriteTo(w)
  50. case *httptypes.HTTPError:
  51. e.WriteTo(w)
  52. case auth.Error:
  53. herr := httptypes.NewHTTPError(e.HTTPStatus(), e.Error())
  54. herr.WriteTo(w)
  55. default:
  56. if err == etcdserver.ErrTimeoutDueToLeaderFail {
  57. plog.Error(err)
  58. } else {
  59. plog.Errorf("got unexpected response error (%v)", err)
  60. }
  61. herr := httptypes.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
  62. herr.WriteTo(w)
  63. }
  64. }
  65. // allowMethod verifies that the given method is one of the allowed methods,
  66. // and if not, it writes an error to w. A boolean is returned indicating
  67. // whether or not the method is allowed.
  68. func allowMethod(w http.ResponseWriter, m string, ms ...string) bool {
  69. for _, meth := range ms {
  70. if m == meth {
  71. return true
  72. }
  73. }
  74. w.Header().Set("Allow", strings.Join(ms, ","))
  75. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  76. return false
  77. }
  78. func requestLogger(handler http.Handler) http.Handler {
  79. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  80. plog.Debugf("[%s] %s remote:%s", r.Method, r.RequestURI, r.RemoteAddr)
  81. handler.ServeHTTP(w, r)
  82. })
  83. }