-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3577 sec fix #3597
Closed
Closed
3577 sec fix #3597
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
package gin | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"log" | ||
|
@@ -15,6 +16,7 @@ | |
"net/url" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
@@ -956,15 +958,54 @@ | |
// JSONP serializes the given struct as JSON into the response body. | ||
// It adds padding to response body to request data from a server residing in a different domain than the client. | ||
// It also sets the Content-Type as "application/javascript". | ||
func (c *Context) JSONP(code int, obj any) { | ||
callback := c.DefaultQuery("callback", "") | ||
if callback == "" { | ||
c.Render(code, render.JSON{Data: obj}) | ||
return | ||
} | ||
c.Render(code, render.JsonpJSON{Callback: callback, Data: obj}) | ||
func (c *Context) JSONP(code int, obj interface{}) { | ||
// Get the callback query parameter from the request or use an empty string as the default value | ||
callback := c.DefaultQuery("callback", "") | ||
|
||
// If the callback query parameter is empty, respond with a JSON object | ||
if callback == "" { | ||
c.Render(code, render.JSON{Data: obj}) | ||
return | ||
} | ||
|
||
// Add type checking for the callback function name | ||
// Use a Unicode-aware pattern for alphanumeric characters and underscores | ||
callbackPattern := `^[\p{L}\p{N}_]+$` | ||
isValidCallback := regexp.MustCompile(callbackPattern).MatchString(callback) | ||
|
||
// If the callback function name is not valid, respond with an error message | ||
if !isValidCallback { | ||
c.JSON(http.StatusBadRequest, H{"error": "Invalid callback function name"}) | ||
return | ||
} | ||
|
||
// Convert the input object to a slice of H (map[string]interface{}) values | ||
var data []H | ||
if d, ok := obj.([]H); ok { | ||
data = d | ||
} else if d, ok := obj.(H); ok { | ||
data = []H{d} | ||
} else { | ||
data = []H{{"message": obj}} | ||
} | ||
|
||
// Convert the H slice to a slice of empty interface values | ||
var anyData []interface{} | ||
for _, item := range data { | ||
anyData = append(anyData, item) | ||
} | ||
|
||
// Marshal the anyData slice to a JSON string | ||
jsonString, _ := json.Marshal(anyData) | ||
|
||
// Respond with a JavaScript callback function call that includes the JSON data | ||
c.Render(code, render.String{Format: "/**/ typeof " + callback + " === 'function' && " + callback + "(%s);", Data: []interface{}{string(jsonString)}}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The render logic should probably be moved to the |
||
} | ||
|
||
|
||
|
||
|
||
|
||
// JSON serializes the given struct as JSON into the response body. | ||
// It also sets the Content-Type as "application/json". | ||
func (c *Context) JSON(code int, obj any) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should move the
callbackPattern
and the regexMustCompile
in a package level variable to avoid the cost of rebuilding it everytime.