Skip to content

Commit

Permalink
Fix link mixing
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrafrancyz committed Apr 11, 2020
1 parent 4418e14 commit 0b4d5e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backend/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ func (s *Mysql) TryClickLink(slug string) *Link {

func (s *Mysql) GetAllLinks() []*Link {
rows, err := s.db.Query("SELECT slug, url, clicks, created FROM links")
list := make([]*Link, 0)
list := make([]*Link, 0, 16)
if err != nil {
log.Println("mysql error: ", err)
return list
}
defer rows.Close()
for rows.Next() {
link := &internalLink{
Link: &Link{},
Expand Down
13 changes: 12 additions & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ func (w *web) handleRoot(ctx *fasthttp.RequestCtx) {

func (w *web) handleSlug(ctx *fasthttp.RequestCtx) {
log.Print(string(ctx.URI().Path()))
full := w.backend.TryClickLink(ctx.UserValue("slug").(string))

// Make a copy of a slug. fasthttp/router's UserValues become invalid after the request is completed.
slug := copyString(ctx.UserValue("slug").(string))

full := w.backend.TryClickLink(slug)

if full != nil {
ctx.Redirect(full.Url, fasthttp.StatusFound)
Expand Down Expand Up @@ -148,6 +152,13 @@ func (w *web) handleEdit(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
}

func copyString(in string) string {
var b strings.Builder
b.Grow(len(in))
b.WriteString(in)
return b.String()
}

type OperationError struct {
Error bool `json:"error"`
Message string `json:"message"`
Expand Down

0 comments on commit 0b4d5e8

Please sign in to comment.