Contains the Concourse pipeline definition for building a line-server container
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.

73 lines
1.5 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "path"
  6. "strings"
  7. "github.com/zenazn/goji/web"
  8. )
  9. func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  10. fileName := c.URLParams["name"]
  11. filePath := path.Join(Config.filesDir, fileName)
  12. if !fileExistsAndNotExpired(fileName) {
  13. notFoundHandler(c, w, r)
  14. return
  15. }
  16. if !Config.allowHotlink {
  17. referer := r.Header.Get("Referer")
  18. if referer != "" && !strings.HasPrefix(referer, Config.siteURL) {
  19. w.WriteHeader(403)
  20. return
  21. }
  22. }
  23. w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
  24. http.ServeFile(w, r, filePath)
  25. }
  26. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  27. path := r.URL.Path
  28. if path[len(path)-1:] == "/" {
  29. notFoundHandler(c, w, r)
  30. return
  31. } else {
  32. if path == "/favicon.ico" {
  33. path = "/static/images/favicon.gif"
  34. }
  35. filePath := strings.TrimPrefix(path, "/static/")
  36. file, err := staticBox.Open(filePath)
  37. if err != nil {
  38. notFoundHandler(c, w, r)
  39. return
  40. }
  41. w.Header().Set("Etag", timeStartedStr)
  42. w.Header().Set("Cache-Control", "max-age=86400")
  43. http.ServeContent(w, r, filePath, timeStarted, file)
  44. return
  45. }
  46. }
  47. func fileExistsAndNotExpired(filename string) bool {
  48. filePath := path.Join(Config.filesDir, filename)
  49. _, err := os.Stat(filePath)
  50. if err != nil {
  51. return false
  52. }
  53. if isFileExpired(filename) {
  54. os.Remove(path.Join(Config.filesDir, filename))
  55. os.Remove(path.Join(Config.metaDir, filename))
  56. return false
  57. }
  58. return true
  59. }