Skip to content

Commit 082f09e

Browse files
authored
Fix invalid json v3 (#258)
* webserver: return on error and check if location is not nil, and fix sending invalid json payloads
1 parent 4da968e commit 082f09e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/webserver.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"os/signal"
11+
"reflect"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -20,10 +21,8 @@ import (
2021
"github.com/go-resty/resty/v2"
2122
)
2223

23-
var (
24-
// TibiaData app resty vars
25-
TibiaDataUserAgent, TibiaDataProxyDomain string
26-
)
24+
// TibiaData app resty vars
25+
var TibiaDataUserAgent, TibiaDataProxyDomain string
2726

2827
// Information - child of JSONData
2928
type Information struct {
@@ -101,8 +100,8 @@ func runWebServer() {
101100

102101
// Tibia guilds
103102
v3.GET("/guild/:name", tibiaGuildsGuildV3)
104-
//v3.GET("/guild/:name/events",TibiaGuildsGuildEventsV3)
105-
//v3.GET("/guild/:name/wars",TibiaGuildsGuildWarsV3)
103+
// v3.GET("/guild/:name/events",TibiaGuildsGuildEventsV3)
104+
// v3.GET("/guild/:name/wars",TibiaGuildsGuildWarsV3)
106105
v3.GET("/guilds/:world", tibiaGuildsOverviewV3)
107106

108107
// Tibia highscores
@@ -466,6 +465,10 @@ func tibiaHousesOverviewV3(c *gin.Context) {
466465
town = TibiaDataStringWorldFormatToTitleV3(town)
467466

468467
jsonData := TibiaHousesOverviewV3Impl(c, world, town, TibiaDataHTMLDataCollectorV3)
468+
// An error occured, prevent sending an invalid json
469+
if reflect.DeepEqual(jsonData, HousesOverviewResponse{}) {
470+
return
471+
}
469472

470473
// return jsonData
471474
TibiaDataAPIHandleResponse(c, http.StatusOK, "TibiaHousesOverviewV3", jsonData)
@@ -757,10 +760,10 @@ func tibiaWorldsWorldV3(c *gin.Context) {
757760

758761
func tibiaDataRequestHandler(c *gin.Context, tibiaDataRequest TibiaDataRequestStruct, requestHandler func(string) (interface{}, int), handlerName string) {
759762
BoxContentHTML, err := TibiaDataHTMLDataCollectorV3(tibiaDataRequest)
760-
761763
// return error (e.g. for maintenance mode)
762764
if err != nil {
763765
TibiaDataAPIHandleResponse(c, http.StatusBadGateway, handlerName, gin.H{"error": err.Error()})
766+
return
764767
}
765768

766769
jsonData, httpStatusCode := requestHandler(BoxContentHTML)
@@ -936,6 +939,6 @@ func readyz(c *gin.Context) {
936939
c.JSON(http.StatusServiceUnavailable, gin.H{"error": http.StatusText(http.StatusServiceUnavailable)})
937940
return
938941
}
939-
//c.JSON(http.StatusOK, gin.H{"status": http.StatusText(http.StatusOK)})
942+
// c.JSON(http.StatusOK, gin.H{"status": http.StatusText(http.StatusOK)})
940943
TibiaDataAPIHandleResponse(c, http.StatusOK, "readyz", gin.H{"status": http.StatusText(http.StatusOK)})
941944
}

0 commit comments

Comments
 (0)