Browse Source

Fix echo's URI routing race condition that caused random 404s.

Kailash Nadh 4 years ago
parent
commit
1e8b533d45
4 changed files with 29 additions and 9 deletions
  1. 3 3
      cmd/campaigns.go
  2. 20 0
      cmd/handlers.go
  3. 3 3
      cmd/lists.go
  4. 3 3
      cmd/templates.go

+ 3 - 3
cmd/campaigns.go

@@ -267,9 +267,9 @@ func handleCreateCampaign(c echo.Context) error {
 	}
 	}
 
 
 	// Hand over to the GET handler to return the last insertion.
 	// Hand over to the GET handler to return the last insertion.
-	c.SetParamNames("id")
-	c.SetParamValues(fmt.Sprintf("%d", newID))
-	return handleGetCampaigns(c)
+	return handleGetCampaigns(copyEchoCtx(c, map[string]string{
+		"id": fmt.Sprintf("%d", newID),
+	}))
 }
 }
 
 
 // handleUpdateCampaign handles campaign modification.
 // handleUpdateCampaign handles campaign modification.

+ 20 - 0
cmd/handlers.go

@@ -238,3 +238,23 @@ func getPagination(q url.Values, perPage, maxPerPage int) pagination {
 		Limit:   perPage,
 		Limit:   perPage,
 	}
 	}
 }
 }
+
+// copyEchoCtx returns a copy of the the current echo.Context in a request
+// with the given params set for the active handler to proxy the request
+// to another handler without mutating its context.
+func copyEchoCtx(c echo.Context, params map[string]string) echo.Context {
+	var (
+		keys = make([]string, 0, len(params))
+		vals = make([]string, 0, len(params))
+	)
+	for k, v := range params {
+		keys = append(keys, k)
+		vals = append(vals, v)
+	}
+
+	b := c.Echo().NewContext(c.Request(), c.Response())
+	b.Set("app", c.Get("app").(*App))
+	b.SetParamNames(keys...)
+	b.SetParamValues(vals...)
+	return b
+}

+ 3 - 3
cmd/lists.go

@@ -118,9 +118,9 @@ func handleCreateList(c echo.Context) error {
 	}
 	}
 
 
 	// Hand over to the GET handler to return the last insertion.
 	// Hand over to the GET handler to return the last insertion.
-	c.SetParamNames("id")
-	c.SetParamValues(fmt.Sprintf("%d", newID))
-	return handleGetLists(c)
+	return handleGetLists(copyEchoCtx(c, map[string]string{
+		"id": fmt.Sprintf("%d", newID),
+	}))
 }
 }
 
 
 // handleUpdateList handles list modification.
 // handleUpdateList handles list modification.

+ 3 - 3
cmd/templates.go

@@ -147,9 +147,9 @@ func handleCreateTemplate(c echo.Context) error {
 	}
 	}
 
 
 	// Hand over to the GET handler to return the last insertion.
 	// Hand over to the GET handler to return the last insertion.
-	c.SetParamNames("id")
-	c.SetParamValues(fmt.Sprintf("%d", newID))
-	return handleGetTemplates(c)
+	return handleGetTemplates(copyEchoCtx(c, map[string]string{
+		"id": fmt.Sprintf("%d", newID),
+	}))
 }
 }
 
 
 // handleUpdateTemplate handles template modification.
 // handleUpdateTemplate handles template modification.