You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2KB

  1. /* ----------------------------------------------------------------------
  2. * ______ ___ __
  3. * / ____/___ / | ____ __ ___ __/ /_ ___ ________
  4. * / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
  5. * / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
  6. * \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
  7. * /____/
  8. *
  9. * (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
  10. * ----------------------------------------------------------------------
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. * ----------------------------------------------------------------------*/
  23. package livereload
  24. import (
  25. "bytes"
  26. "compress/gzip"
  27. "compress/zlib"
  28. "fmt"
  29. "io"
  30. "net/http"
  31. "regexp"
  32. "strings"
  33. "github.com/goanywhere/x/env"
  34. )
  35. type writer struct {
  36. http.ResponseWriter
  37. host string
  38. }
  39. func (self *writer) addJavaScript(data []byte) []byte {
  40. javascript := fmt.Sprintf(`<script src="//%s%s"></script>
  41. </head>`, self.host, URL.JavaScript)
  42. return regexp.MustCompile(`</head>`).ReplaceAll(data, []byte(javascript))
  43. }
  44. func (self *writer) Write(data []byte) (size int, e error) {
  45. if strings.Contains(self.Header().Get("Content-Type"), "html") {
  46. var encoding = self.Header().Get("Content-Encoding")
  47. if encoding == "" {
  48. data = self.addJavaScript(data)
  49. } else {
  50. var reader io.ReadCloser
  51. var buffer *bytes.Buffer = new(bytes.Buffer)
  52. if encoding == "gzip" {
  53. // decode to add javascript reference.
  54. reader, _ = gzip.NewReader(bytes.NewReader(data))
  55. io.Copy(buffer, reader)
  56. output := self.addJavaScript(buffer.Bytes())
  57. reader.Close()
  58. buffer.Reset()
  59. // encode back to HTML with added javascript reference.
  60. writer := gzip.NewWriter(buffer)
  61. writer.Write(output)
  62. writer.Close()
  63. data = buffer.Bytes()
  64. } else if encoding == "deflate" {
  65. // decode to add javascript reference.
  66. reader, _ = zlib.NewReader(bytes.NewReader(data))
  67. io.Copy(buffer, reader)
  68. output := self.addJavaScript(buffer.Bytes())
  69. reader.Close()
  70. buffer.Reset()
  71. // encode back to HTML with added javascript reference.
  72. writer := zlib.NewWriter(buffer)
  73. writer.Write(output)
  74. writer.Close()
  75. data = buffer.Bytes()
  76. }
  77. }
  78. }
  79. return self.ResponseWriter.Write(data)
  80. }
  81. func Module(next http.Handler) http.Handler {
  82. // ONLY run this under debug mode.
  83. if !env.Bool("DEBUG", true) {
  84. return next
  85. }
  86. Start()
  87. fn := func(w http.ResponseWriter, r *http.Request) {
  88. if r.URL.Path == URL.WebSocket {
  89. ServeWebSocket(w, r)
  90. } else if r.URL.Path == URL.JavaScript {
  91. ServeJavaScript(w, r)
  92. } else {
  93. writer := &writer{w, r.Host}
  94. next.ServeHTTP(writer, r)
  95. }
  96. }
  97. return http.HandlerFunc(fn)
  98. }