[performance] http response encoding / writing improvements (#2374)

This commit is contained in:
kim
2023-11-27 14:00:57 +00:00
committed by GitHub
parent d7e35f6bc9
commit 74700cc803
104 changed files with 526 additions and 267 deletions

View File

@@ -18,7 +18,6 @@
package emoji
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -36,7 +35,7 @@ func (m *Module) EmojiGetHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
@@ -48,11 +47,12 @@ func (m *Module) EmojiGetHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
// Encode JSON HTTP response.
apiutil.EncodeJSONResponse(
c.Writer,
c.Request,
http.StatusOK,
contentType,
resp,
)
}

View File

@@ -18,7 +18,6 @@
package publickey
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -42,13 +41,13 @@ func (m *Module) PublicKeyGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// redirect to the user's profile
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
return
@@ -60,11 +59,12 @@ func (m *Module) PublicKeyGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
// Encode JSON HTTP response.
apiutil.EncodeJSONResponse(
c.Writer,
c.Request,
http.StatusOK,
contentType,
resp,
)
}

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -67,13 +66,13 @@ func (m *Module) FeaturedCollectionGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// This isn't an ActivityPub request;
// redirect to the user's profile.
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
@@ -86,11 +85,5 @@ func (m *Module) FeaturedCollectionGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -39,13 +38,13 @@ func (m *Module) FollowersGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// This isn't an ActivityPub request;
// redirect to the user's profile.
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
@@ -68,11 +67,5 @@ func (m *Module) FollowersGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -39,13 +38,13 @@ func (m *Module) FollowingGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// This isn't an ActivityPub request;
// redirect to the user's profile.
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
@@ -68,11 +67,5 @@ func (m *Module) FollowingGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}

View File

@@ -47,6 +47,5 @@ func (m *Module) InboxPOSTHandler(c *gin.Context) {
return
}
// Inbox POST body was Accepted for processing.
c.JSON(http.StatusAccepted, gin.H{"status": http.StatusText(http.StatusAccepted)})
apiutil.Data(c, http.StatusAccepted, apiutil.AppJSON, apiutil.StatusAcceptedJSON)
}

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -93,13 +92,13 @@ func (m *Module) OutboxGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// This isn't an ActivityPub request;
// redirect to the user's profile.
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
@@ -135,11 +134,5 @@ func (m *Module) OutboxGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}

View File

@@ -209,7 +209,7 @@ func (suite *OutboxGetTestSuite) TestGetOutboxNextPage() {
suite.NoError(err)
suite.Equal(`{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "http://localhost:8080/users/the_mighty_zork/outbox?page=true\u0026maxID=01F8MHAMCHF6Y650WCRSCP4WMY",
"id": "http://localhost:8080/users/the_mighty_zork/outbox?page=true&maxID=01F8MHAMCHF6Y650WCRSCP4WMY",
"orderedItems": [],
"partOf": "http://localhost:8080/users/the_mighty_zork/outbox",
"type": "OrderedCollectionPage"

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -107,13 +106,13 @@ func (m *Module) StatusRepliesGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// redirect to the status
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername+"/statuses/"+requestedStatusID)
return
@@ -161,12 +160,5 @@ func (m *Module) StatusRepliesGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
errWithCode := gtserror.NewErrorInternalError(err)
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}

View File

@@ -266,11 +266,16 @@ func toJSON(a any) string {
}
a = m
}
b, err := json.MarshalIndent(a, "", " ")
var dst bytes.Buffer
enc := json.NewEncoder(&dst)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
err := enc.Encode(a)
if err != nil {
panic(err)
}
return string(b)
dst.Truncate(dst.Len() - 1) // drop new-line
return dst.String()
}
// indentJSON will return indented JSON from raw provided JSON.

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -46,13 +45,13 @@ func (m *Module) StatusGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// redirect to the status
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername+"/statuses/"+requestedStatusID)
return
@@ -64,11 +63,5 @@ func (m *Module) StatusGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}

View File

@@ -18,7 +18,6 @@
package users
import (
"encoding/json"
"errors"
"net/http"
"strings"
@@ -46,13 +45,13 @@ func (m *Module) UsersGETHandler(c *gin.Context) {
return
}
format, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
contentType, err := apiutil.NegotiateAccept(c, apiutil.ActivityPubOrHTMLHeaders...)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
return
}
if format == string(apiutil.TextHTML) {
if contentType == string(apiutil.TextHTML) {
// redirect to the user's profile
c.Redirect(http.StatusSeeOther, "/@"+requestedUsername)
return
@@ -64,11 +63,5 @@ func (m *Module) UsersGETHandler(c *gin.Context) {
return
}
b, err := json.Marshal(resp)
if err != nil {
apiutil.ErrorHandler(c, gtserror.NewErrorInternalError(err), m.processor.InstanceGetV1)
return
}
c.Data(http.StatusOK, format, b)
apiutil.JSONType(c, http.StatusOK, contentType, resp)
}