Skip to content

Commit

Permalink
guestbook: fix for when referer contains path
Browse files Browse the repository at this point in the history
Signed-off-by: x-t <xdasht@pm.me>
  • Loading branch information
x-t committed Nov 1, 2023
1 parent 78c463f commit c6c19a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
15 changes: 12 additions & 3 deletions apps/guestbook3/src/controllers/post/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@
package post

import (
goaway "github.com/TwiN/go-away"
"github.com/gin-gonic/gin"
"fmt"
"log"
"net/http"
"time"
"x-t/guestbook3/src/models"
"x-t/guestbook3/src/providers"
"x-t/guestbook3/src/settings"

goaway "github.com/TwiN/go-away"
"github.com/gin-gonic/gin"
)

func Post(c *gin.Context) {
postRequest := &models.PostRequest{}
referer := c.Request.Referer() + settings.RedirectHTML

baseReferer, err := providers.RemovePathSegments(c.Request.Referer())
if err != nil {
fmt.Println("Error:", err)
panic(err)
}

referer := baseReferer + settings.RedirectHTML

if err := c.Bind(postRequest); err != nil {
c.HTML(http.StatusBadRequest,
Expand Down
25 changes: 25 additions & 0 deletions apps/guestbook3/src/providers/uri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// x-t.github.io (c) 2023
// This work is licensed under the Creative Commons
// Attribution-NonCommercial-ShareAlike 4.0 International
// License. To view a copy of this license, visit
// http://creativecommons.org/licenses/by-nc-sa/4.0/ or
// send a letter to Creative Commons, PO Box 1866,
// Mountain View, CA 94042, USA.

package providers

import (
"net/url"
"fmt"
)

func RemovePathSegments(uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}

baseURI := fmt.Sprintf("%s://%s", u.Scheme, u.Host)

return baseURI, nil
}
8 changes: 0 additions & 8 deletions apps/guestbook3/src/settings/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,4 @@ package settings
// RedirectHTML Name of HTML document (endpoint) where
// the post controller (/api/post) will redirect/point to
// in a concat of Headers[Referer] + RedirectHTML.
//
// See:
//
// - src/controllers/post/post.go#54
//
// - src/controllers/post/post.go#59
//
// - src/views/post/validation_errors.html.tmpl#9
const RedirectHTML = "guestbook.html"

0 comments on commit c6c19a9

Please sign in to comment.