server.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. /*
  17. A simple http server interface to Swarm
  18. */
  19. package http
  20. import (
  21. "bytes"
  22. "io"
  23. "net/http"
  24. "regexp"
  25. "sync"
  26. "time"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/logger"
  29. "github.com/ethereum/go-ethereum/logger/glog"
  30. "github.com/ethereum/go-ethereum/swarm/api"
  31. )
  32. const (
  33. rawType = "application/octet-stream"
  34. )
  35. var (
  36. // accepted protocols: bzz (traditional), bzzi (immutable) and bzzr (raw)
  37. bzzPrefix = regexp.MustCompile("^/+bzz[ir]?:/+")
  38. trailingSlashes = regexp.MustCompile("/+$")
  39. rootDocumentUri = regexp.MustCompile("^/+bzz[i]?:/+[^/]+$")
  40. // forever = func() time.Time { return time.Unix(0, 0) }
  41. forever = time.Now
  42. )
  43. type sequentialReader struct {
  44. reader io.Reader
  45. pos int64
  46. ahead map[int64](chan bool)
  47. lock sync.Mutex
  48. }
  49. // browser API for registering bzz url scheme handlers:
  50. // https://developer.mozilla.org/en/docs/Web-based_protocol_handlers
  51. // electron (chromium) api for registering bzz url scheme handlers:
  52. // https://github.com/atom/electron/blob/master/docs/api/protocol.md
  53. // starts up http server
  54. func StartHttpServer(api *api.Api, port string) {
  55. serveMux := http.NewServeMux()
  56. serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  57. handler(w, r, api)
  58. })
  59. go http.ListenAndServe(":"+port, serveMux)
  60. glog.V(logger.Info).Infof("Swarm HTTP proxy started on localhost:%s", port)
  61. }
  62. func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
  63. requestURL := r.URL
  64. // This is wrong
  65. // if requestURL.Host == "" {
  66. // var err error
  67. // requestURL, err = url.Parse(r.Referer() + requestURL.String())
  68. // if err != nil {
  69. // http.Error(w, err.Error(), http.StatusBadRequest)
  70. // return
  71. // }
  72. // }
  73. glog.V(logger.Debug).Infof("HTTP %s request URL: '%s', Host: '%s', Path: '%s', Referer: '%s', Accept: '%s'", r.Method, r.RequestURI, requestURL.Host, requestURL.Path, r.Referer(), r.Header.Get("Accept"))
  74. uri := requestURL.Path
  75. var raw, nameresolver bool
  76. var proto string
  77. // HTTP-based URL protocol handler
  78. glog.V(logger.Debug).Infof("BZZ request URI: '%s'", uri)
  79. path := bzzPrefix.ReplaceAllStringFunc(uri, func(p string) string {
  80. proto = p
  81. return ""
  82. })
  83. // protocol identification (ugly)
  84. if proto == "" {
  85. if glog.V(logger.Error) {
  86. glog.Errorf(
  87. "[BZZ] Swarm: Protocol error in request `%s`.",
  88. uri,
  89. )
  90. http.Error(w, "BZZ protocol error", http.StatusBadRequest)
  91. return
  92. }
  93. }
  94. if len(proto) > 4 {
  95. raw = proto[1:5] == "bzzr"
  96. nameresolver = proto[1:5] != "bzzi"
  97. }
  98. glog.V(logger.Debug).Infof(
  99. "[BZZ] Swarm: %s request over protocol %s '%s' received.",
  100. r.Method, proto, path,
  101. )
  102. switch {
  103. case r.Method == "POST" || r.Method == "PUT":
  104. if r.Header.Get("content-length") == "" {
  105. http.Error(w, "Missing Content-Length header in request.", http.StatusBadRequest)
  106. return
  107. }
  108. key, err := a.Store(io.LimitReader(r.Body, r.ContentLength), r.ContentLength, nil)
  109. if err == nil {
  110. glog.V(logger.Debug).Infof("Content for %v stored", key.Log())
  111. } else {
  112. http.Error(w, err.Error(), http.StatusBadRequest)
  113. return
  114. }
  115. if r.Method == "POST" {
  116. if raw {
  117. w.Header().Set("Content-Type", "text/plain")
  118. http.ServeContent(w, r, "", time.Now(), bytes.NewReader([]byte(common.Bytes2Hex(key))))
  119. } else {
  120. http.Error(w, "No POST to "+uri+" allowed.", http.StatusBadRequest)
  121. return
  122. }
  123. } else {
  124. // PUT
  125. if raw {
  126. http.Error(w, "No PUT to /raw allowed.", http.StatusBadRequest)
  127. return
  128. } else {
  129. path = api.RegularSlashes(path)
  130. mime := r.Header.Get("Content-Type")
  131. // TODO proper root hash separation
  132. glog.V(logger.Debug).Infof("Modify '%s' to store %v as '%s'.", path, key.Log(), mime)
  133. newKey, err := a.Modify(path, common.Bytes2Hex(key), mime, nameresolver)
  134. if err == nil {
  135. glog.V(logger.Debug).Infof("Swarm replaced manifest by '%s'", newKey)
  136. w.Header().Set("Content-Type", "text/plain")
  137. http.ServeContent(w, r, "", time.Now(), bytes.NewReader([]byte(newKey)))
  138. } else {
  139. http.Error(w, "PUT to "+path+"failed.", http.StatusBadRequest)
  140. return
  141. }
  142. }
  143. }
  144. case r.Method == "DELETE":
  145. if raw {
  146. http.Error(w, "No DELETE to /raw allowed.", http.StatusBadRequest)
  147. return
  148. } else {
  149. path = api.RegularSlashes(path)
  150. glog.V(logger.Debug).Infof("Delete '%s'.", path)
  151. newKey, err := a.Modify(path, "", "", nameresolver)
  152. if err == nil {
  153. glog.V(logger.Debug).Infof("Swarm replaced manifest by '%s'", newKey)
  154. w.Header().Set("Content-Type", "text/plain")
  155. http.ServeContent(w, r, "", time.Now(), bytes.NewReader([]byte(newKey)))
  156. } else {
  157. http.Error(w, "DELETE to "+path+"failed.", http.StatusBadRequest)
  158. return
  159. }
  160. }
  161. case r.Method == "GET" || r.Method == "HEAD":
  162. path = trailingSlashes.ReplaceAllString(path, "")
  163. if raw {
  164. // resolving host
  165. key, err := a.Resolve(path, nameresolver)
  166. if err != nil {
  167. glog.V(logger.Error).Infof("%v", err)
  168. http.Error(w, err.Error(), http.StatusBadRequest)
  169. return
  170. }
  171. // retrieving content
  172. reader := a.Retrieve(key)
  173. quitC := make(chan bool)
  174. size, err := reader.Size(quitC)
  175. glog.V(logger.Debug).Infof("Reading %d bytes.", size)
  176. // setting mime type
  177. qv := requestURL.Query()
  178. mimeType := qv.Get("content_type")
  179. if mimeType == "" {
  180. mimeType = rawType
  181. }
  182. w.Header().Set("Content-Type", mimeType)
  183. http.ServeContent(w, r, uri, forever(), reader)
  184. glog.V(logger.Debug).Infof("Serve raw content '%s' (%d bytes) as '%s'", uri, size, mimeType)
  185. // retrieve path via manifest
  186. } else {
  187. glog.V(logger.Debug).Infof("Structured GET request '%s' received.", uri)
  188. // add trailing slash, if missing
  189. if rootDocumentUri.MatchString(uri) {
  190. http.Redirect(w, r, path+"/", http.StatusFound)
  191. return
  192. }
  193. reader, mimeType, status, err := a.Get(path, nameresolver)
  194. if err != nil {
  195. if _, ok := err.(api.ErrResolve); ok {
  196. glog.V(logger.Debug).Infof("%v", err)
  197. status = http.StatusBadRequest
  198. } else {
  199. glog.V(logger.Debug).Infof("error retrieving '%s': %v", uri, err)
  200. status = http.StatusNotFound
  201. }
  202. http.Error(w, err.Error(), status)
  203. return
  204. }
  205. // set mime type and status headers
  206. w.Header().Set("Content-Type", mimeType)
  207. if status > 0 {
  208. w.WriteHeader(status)
  209. } else {
  210. status = 200
  211. }
  212. quitC := make(chan bool)
  213. size, err := reader.Size(quitC)
  214. glog.V(logger.Debug).Infof("Served '%s' (%d bytes) as '%s' (status code: %v)", uri, size, mimeType, status)
  215. http.ServeContent(w, r, path, forever(), reader)
  216. }
  217. default:
  218. http.Error(w, "Method "+r.Method+" is not supported.", http.StatusMethodNotAllowed)
  219. }
  220. }
  221. func (self *sequentialReader) ReadAt(target []byte, off int64) (n int, err error) {
  222. self.lock.Lock()
  223. // assert self.pos <= off
  224. if self.pos > off {
  225. glog.V(logger.Error).Infof("non-sequential read attempted from sequentialReader; %d > %d",
  226. self.pos, off)
  227. panic("Non-sequential read attempt")
  228. }
  229. if self.pos != off {
  230. glog.V(logger.Debug).Infof("deferred read in POST at position %d, offset %d.",
  231. self.pos, off)
  232. wait := make(chan bool)
  233. self.ahead[off] = wait
  234. self.lock.Unlock()
  235. if <-wait {
  236. // failed read behind
  237. n = 0
  238. err = io.ErrUnexpectedEOF
  239. return
  240. }
  241. self.lock.Lock()
  242. }
  243. localPos := 0
  244. for localPos < len(target) {
  245. n, err = self.reader.Read(target[localPos:])
  246. localPos += n
  247. glog.V(logger.Debug).Infof("Read %d bytes into buffer size %d from POST, error %v.",
  248. n, len(target), err)
  249. if err != nil {
  250. glog.V(logger.Debug).Infof("POST stream's reading terminated with %v.", err)
  251. for i := range self.ahead {
  252. self.ahead[i] <- true
  253. delete(self.ahead, i)
  254. }
  255. self.lock.Unlock()
  256. return localPos, err
  257. }
  258. self.pos += int64(n)
  259. }
  260. wait := self.ahead[self.pos]
  261. if wait != nil {
  262. glog.V(logger.Debug).Infof("deferred read in POST at position %d triggered.",
  263. self.pos)
  264. delete(self.ahead, self.pos)
  265. close(wait)
  266. }
  267. self.lock.Unlock()
  268. return localPos, err
  269. }