post_target.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package ui
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "strings"
  8. "github.com/gorilla/sessions"
  9. "code.osinet.fr/fgm/kurz/domain"
  10. "github.com/gorilla/mux"
  11. )
  12. // handlePostTarget handles form POST requests to /
  13. func handlePostTarget(w http.ResponseWriter, r *http.Request, router *mux.Router) {
  14. var sess *sessions.Session
  15. sess, storeErr := store.Get(r, globals.SessionName)
  16. if storeErr != nil {
  17. log.Fatal()
  18. w.WriteHeader(http.StatusInternalServerError)
  19. return
  20. }
  21. r.ParseForm()
  22. defer r.Body.Close()
  23. rawTarget := r.PostForm.Get(rootInputName)
  24. target, err := validateTarget(rawTarget)
  25. if err != nil {
  26. sess.AddFlash(err.Error())
  27. sess.Save(r, w)
  28. location, err := URLFromRoute(router, RouteGetRoot, nil)
  29. if err != nil {
  30. w.WriteHeader(http.StatusInternalServerError)
  31. return
  32. }
  33. w.Header().Set("Location", location)
  34. w.WriteHeader(http.StatusSeeOther)
  35. return
  36. }
  37. short, isNew, err := domain.GetShortURL(target)
  38. if err != nil {
  39. w.WriteHeader(http.StatusInternalServerError)
  40. return
  41. }
  42. fqsu, err := URLFromRoute(router, RouteGetShort, map[string]string{"short": short})
  43. if err != nil {
  44. w.WriteHeader(http.StatusInternalServerError)
  45. return
  46. }
  47. sw := &strings.Builder{}
  48. var templateName string
  49. if isNew {
  50. templateName = "201"
  51. } else {
  52. templateName = "409"
  53. }
  54. defer sess.Save(r, w)
  55. data := struct {
  56. Flashes []interface{}
  57. FullyQualifiedShortURL string
  58. FullyQualifiedTargetURL string
  59. Globals
  60. }{
  61. sess.Flashes(),
  62. fqsu,
  63. target,
  64. globals,
  65. }
  66. err = tmpl.ExecuteTemplate(sw, templateName, data)
  67. if err != nil {
  68. fmt.Println(err)
  69. w.WriteHeader(http.StatusInternalServerError)
  70. return
  71. }
  72. if isNew {
  73. w.WriteHeader(http.StatusCreated)
  74. } else {
  75. w.WriteHeader(http.StatusConflict)
  76. }
  77. io.Copy(w, strings.NewReader(sw.String()))
  78. }
  79. func validateTarget(raw string) (string, error) {
  80. if raw == "" {
  81. return "", domain.MakeError(domain.TargetInvalidError, "empty target")
  82. }
  83. // BUG(fgm): needs much more validation, starting with XSS.
  84. return raw, nil
  85. }