update IsJSON to check for Accept header
this changes the helper IsJSON to take a request instead of a string, allowing to check multiple headers. In this case both Content-Type and Accept.
This commit is contained in:
parent
66974dcbff
commit
f87371b594
16
account.go
16
account.go
|
@ -85,7 +85,7 @@ func apiSignup(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func signup(app *App, w http.ResponseWriter, r *http.Request) (*AuthUser, error) {
|
func signup(app *App, w http.ResponseWriter, r *http.Request) (*AuthUser, error) {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
|
|
||||||
// Get params
|
// Get params
|
||||||
var ur userRegistration
|
var ur userRegistration
|
||||||
|
@ -120,7 +120,7 @@ func signup(app *App, w http.ResponseWriter, r *http.Request) (*AuthUser, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func signupWithRegistration(app *App, signup userRegistration, w http.ResponseWriter, r *http.Request) (*AuthUser, error) {
|
func signupWithRegistration(app *App, signup userRegistration, w http.ResponseWriter, r *http.Request) (*AuthUser, error) {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
|
|
||||||
// Validate required params (alias)
|
// Validate required params (alias)
|
||||||
if signup.Alias == "" {
|
if signup.Alias == "" {
|
||||||
|
@ -377,7 +377,7 @@ func webLogin(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
var loginAttemptUsers = sync.Map{}
|
var loginAttemptUsers = sync.Map{}
|
||||||
|
|
||||||
func login(app *App, w http.ResponseWriter, r *http.Request) error {
|
func login(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
oneTimeToken := r.FormValue("with")
|
oneTimeToken := r.FormValue("with")
|
||||||
verbose := r.FormValue("all") == "true" || r.FormValue("verbose") == "1" || r.FormValue("verbose") == "true" || (reqJSON && oneTimeToken != "")
|
verbose := r.FormValue("all") == "true" || r.FormValue("verbose") == "1" || r.FormValue("verbose") == "true" || (reqJSON && oneTimeToken != "")
|
||||||
|
|
||||||
|
@ -580,7 +580,7 @@ func viewExportOptions(app *App, u *User, w http.ResponseWriter, r *http.Request
|
||||||
func viewExportPosts(app *App, w http.ResponseWriter, r *http.Request) ([]byte, string, error) {
|
func viewExportPosts(app *App, w http.ResponseWriter, r *http.Request) ([]byte, string, error) {
|
||||||
var filename string
|
var filename string
|
||||||
var u = &User{}
|
var u = &User{}
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
if reqJSON {
|
if reqJSON {
|
||||||
// Use given Authorization header
|
// Use given Authorization header
|
||||||
accessToken := r.Header.Get("Authorization")
|
accessToken := r.Header.Get("Authorization")
|
||||||
|
@ -662,7 +662,7 @@ func viewExportFull(app *App, w http.ResponseWriter, r *http.Request) ([]byte, s
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewMeAPI(app *App, w http.ResponseWriter, r *http.Request) error {
|
func viewMeAPI(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
uObj := struct {
|
uObj := struct {
|
||||||
ID int64 `json:"id,omitempty"`
|
ID int64 `json:"id,omitempty"`
|
||||||
Username string `json:"username,omitempty"`
|
Username string `json:"username,omitempty"`
|
||||||
|
@ -686,7 +686,7 @@ func viewMeAPI(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewMyPostsAPI(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
func viewMyPostsAPI(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
if !reqJSON {
|
if !reqJSON {
|
||||||
return ErrBadRequestedType
|
return ErrBadRequestedType
|
||||||
}
|
}
|
||||||
|
@ -717,7 +717,7 @@ func viewMyPostsAPI(app *App, u *User, w http.ResponseWriter, r *http.Request) e
|
||||||
}
|
}
|
||||||
|
|
||||||
func viewMyCollectionsAPI(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
func viewMyCollectionsAPI(app *App, u *User, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
if !reqJSON {
|
if !reqJSON {
|
||||||
return ErrBadRequestedType
|
return ErrBadRequestedType
|
||||||
}
|
}
|
||||||
|
@ -822,7 +822,7 @@ func viewEditCollection(app *App, u *User, w http.ResponseWriter, r *http.Reques
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateSettings(app *App, w http.ResponseWriter, r *http.Request) error {
|
func updateSettings(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
|
|
||||||
var s userSettings
|
var s userSettings
|
||||||
var u *User
|
var u *User
|
||||||
|
|
|
@ -338,7 +338,7 @@ func (c *Collection) RenderMathJax() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCollection(app *App, w http.ResponseWriter, r *http.Request) error {
|
func newCollection(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
alias := r.FormValue("alias")
|
alias := r.FormValue("alias")
|
||||||
title := r.FormValue("title")
|
title := r.FormValue("title")
|
||||||
|
|
||||||
|
@ -454,7 +454,7 @@ func fetchCollection(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
c.hostName = app.cfg.App.Host
|
c.hostName = app.cfg.App.Host
|
||||||
|
|
||||||
// Redirect users who aren't requesting JSON
|
// Redirect users who aren't requesting JSON
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
if !reqJSON {
|
if !reqJSON {
|
||||||
return impart.HTTPError{http.StatusFound, c.CanonicalURL()}
|
return impart.HTTPError{http.StatusFound, c.CanonicalURL()}
|
||||||
}
|
}
|
||||||
|
@ -919,7 +919,7 @@ func handleCollectionPostRedirect(app *App, w http.ResponseWriter, r *http.Reque
|
||||||
}
|
}
|
||||||
|
|
||||||
func existingCollection(app *App, w http.ResponseWriter, r *http.Request) error {
|
func existingCollection(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
collAlias := vars["alias"]
|
collAlias := vars["alias"]
|
||||||
isWeb := r.FormValue("web") == "1"
|
isWeb := r.FormValue("web") == "1"
|
||||||
|
|
|
@ -772,7 +772,7 @@ func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if IsJSON(r.Header.Get("Content-Type")) {
|
if IsJSON(r) {
|
||||||
impart.WriteError(w, impart.HTTPError{http.StatusInternalServerError, "This is an unhelpful error message for a miscellaneous internal error."})
|
impart.WriteError(w, impart.HTTPError{http.StatusInternalServerError, "This is an unhelpful error message for a miscellaneous internal error."})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
4
posts.go
4
posts.go
|
@ -472,7 +472,7 @@ func handleViewPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
// /posts?collection={alias}
|
// /posts?collection={alias}
|
||||||
// ? /collections/{alias}/posts
|
// ? /collections/{alias}/posts
|
||||||
func newPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
func newPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
collAlias := vars["alias"]
|
collAlias := vars["alias"]
|
||||||
if collAlias == "" {
|
if collAlias == "" {
|
||||||
|
@ -598,7 +598,7 @@ func newPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func existingPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
func existingPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
postID := vars["post"]
|
postID := vars["post"]
|
||||||
|
|
||||||
|
|
12
request.go
12
request.go
|
@ -10,9 +10,13 @@
|
||||||
|
|
||||||
package writefreely
|
package writefreely
|
||||||
|
|
||||||
import "mime"
|
import (
|
||||||
|
"mime"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
func IsJSON(h string) bool {
|
func IsJSON(r *http.Request) bool {
|
||||||
ct, _, _ := mime.ParseMediaType(h)
|
ct, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||||
return ct == "application/json"
|
accept := r.Header.Get("Accept")
|
||||||
|
return ct == "application/json" || accept == "application/json"
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,14 @@ package writefreely
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/writeas/impart"
|
"github.com/writeas/impart"
|
||||||
"github.com/writeas/web-core/log"
|
"github.com/writeas/web-core/log"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleWebSignup(app *App, w http.ResponseWriter, r *http.Request) error {
|
func handleWebSignup(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
|
|
||||||
// Get params
|
// Get params
|
||||||
var ur userRegistration
|
var ur userRegistration
|
||||||
|
@ -71,7 +72,7 @@ func handleWebSignup(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
// { "username": "asdf" }
|
// { "username": "asdf" }
|
||||||
// result: { code: 204 }
|
// result: { code: 204 }
|
||||||
func handleUsernameCheck(app *App, w http.ResponseWriter, r *http.Request) error {
|
func handleUsernameCheck(app *App, w http.ResponseWriter, r *http.Request) error {
|
||||||
reqJSON := IsJSON(r.Header.Get("Content-Type"))
|
reqJSON := IsJSON(r)
|
||||||
|
|
||||||
// Get params
|
// Get params
|
||||||
var d struct {
|
var d struct {
|
||||||
|
|
Loading…
Reference in New Issue