http.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "github.com/julienschmidt/httprouter"
  21. )
  22. type HttpAuthWraper struct {
  23. h http.Handler
  24. user string
  25. passwd string
  26. }
  27. func NewHttpBasicAuthWraper(h http.Handler, user, passwd string) http.Handler {
  28. return &HttpAuthWraper{
  29. h: h,
  30. user: user,
  31. passwd: passwd,
  32. }
  33. }
  34. func (aw *HttpAuthWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. user, passwd, hasAuth := r.BasicAuth()
  36. if (aw.user == "" && aw.passwd == "") || (hasAuth && user == aw.user && passwd == aw.passwd) {
  37. aw.h.ServeHTTP(w, r)
  38. } else {
  39. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  40. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  41. }
  42. }
  43. func HttpBasicAuth(h http.HandlerFunc, user, passwd string) http.HandlerFunc {
  44. return func(w http.ResponseWriter, r *http.Request) {
  45. reqUser, reqPasswd, hasAuth := r.BasicAuth()
  46. if (user == "" && passwd == "") ||
  47. (hasAuth && reqUser == user && reqPasswd == passwd) {
  48. h.ServeHTTP(w, r)
  49. } else {
  50. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  51. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  52. }
  53. }
  54. }
  55. func HttprouterBasicAuth(h httprouter.Handle, user, passwd string) httprouter.Handle {
  56. return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  57. reqUser, reqPasswd, hasAuth := r.BasicAuth()
  58. if (user == "" && passwd == "") ||
  59. (hasAuth && reqUser == user && reqPasswd == passwd) {
  60. h(w, r, ps)
  61. } else {
  62. w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
  63. http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
  64. }
  65. }
  66. }
  67. type HttpGzipWraper struct {
  68. h http.Handler
  69. }
  70. func (gw *HttpGzipWraper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  71. if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  72. gw.h.ServeHTTP(w, r)
  73. return
  74. }
  75. w.Header().Set("Content-Encoding", "gzip")
  76. gz := gzip.NewWriter(w)
  77. defer gz.Close()
  78. gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
  79. gw.h.ServeHTTP(gzr, r)
  80. }
  81. func MakeHttpGzipHandler(h http.Handler) http.Handler {
  82. return &HttpGzipWraper{
  83. h: h,
  84. }
  85. }
  86. type gzipResponseWriter struct {
  87. io.Writer
  88. http.ResponseWriter
  89. }
  90. func (w gzipResponseWriter) Write(b []byte) (int, error) {
  91. return w.Writer.Write(b)
  92. }