http.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  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 net
  15. import (
  16. "compress/gzip"
  17. "io"
  18. "net/http"
  19. "strings"
  20. )
  21. type HTTPAuthWraper struct {
  22. h http.Handler
  23. user string
  24. passwd string
  25. }
  26. func NewHTTPBasicAuthWraper(h http.Handler, user, passwd string) http.Handler {
  27. return &HTTPAuthWraper{
  28. h: h,
  29. user: user,
  30. passwd: passwd,
  31. }
  32. }
  33. func (aw *HTTPAuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. user, passwd, hasAuth := r.BasicAuth()
  35. if (aw.user == "" && aw.passwd == "") || (hasAuth && user == aw.user && passwd == aw.passwd) {
  36. aw.h.ServeHTTP(w, r)
  37. } else {
  38. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  39. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  40. }
  41. }
  42. type HTTPAuthMiddleware struct {
  43. user string
  44. passwd string
  45. }
  46. func NewHTTPAuthMiddleware(user, passwd string) *HTTPAuthMiddleware {
  47. return &HTTPAuthMiddleware{
  48. user: user,
  49. passwd: passwd,
  50. }
  51. }
  52. func (authMid *HTTPAuthMiddleware) Middleware(next http.Handler) http.Handler {
  53. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  54. reqUser, reqPasswd, hasAuth := r.BasicAuth()
  55. if (authMid.user == "" && authMid.passwd == "") ||
  56. (hasAuth && reqUser == authMid.user && reqPasswd == authMid.passwd) {
  57. next.ServeHTTP(w, r)
  58. } else {
  59. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  60. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  61. }
  62. })
  63. }
  64. func HTTPBasicAuth(h http.HandlerFunc, user, passwd string) http.HandlerFunc {
  65. return func(w http.ResponseWriter, r *http.Request) {
  66. reqUser, reqPasswd, hasAuth := r.BasicAuth()
  67. if (user == "" && passwd == "") ||
  68. (hasAuth && reqUser == user && reqPasswd == passwd) {
  69. h.ServeHTTP(w, r)
  70. } else {
  71. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  72. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  73. }
  74. }
  75. }
  76. type HTTPGzipWraper struct {
  77. h http.Handler
  78. }
  79. func (gw *HTTPGzipWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  80. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  81. gw.h.ServeHTTP(w, r)
  82. return
  83. }
  84. w.Header().Set("Content-Encoding", "gzip")
  85. gz := gzip.NewWriter(w)
  86. defer gz.Close()
  87. gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
  88. gw.h.ServeHTTP(gzr, r)
  89. }
  90. func MakeHTTPGzipHandler(h http.Handler) http.Handler {
  91. return &HTTPGzipWraper{
  92. h: h,
  93. }
  94. }
  95. type gzipResponseWriter struct {
  96. io.Writer
  97. http.ResponseWriter
  98. }
  99. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  100. return w.Writer.Write(b)
  101. }