Skip to content
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

WIP: w.Write: replace to http.ResponseWriter.Write #801

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cache/buntdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func BuntGet(key string, w http.ResponseWriter) (cacheExist bool) {
val, err := tx.Get(key)
if err == nil {
cacheExist = true
w.Header().Set("Cache-Server", "prestd")
w.WriteHeader(http.StatusOK)
w.Write([]byte(val))
http.ResponseWriter.Header(w).Set("Cache-Server", "prestd")
http.ResponseWriter.WriteHeader(w, http.StatusOK)
http.ResponseWriter.Write(w, []byte(val))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the 'http.ResponseWriter' is an interface not defined here, I believe that there is a chance that we get nil panics, isn't it?

}
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion controllers/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func GetDatabases(w http.ResponseWriter, r *http.Request) {
return
}
//nolint
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}
4 changes: 2 additions & 2 deletions controllers/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func WrappedHealthCheck(checks CheckList) http.HandlerFunc {
for _, check := range checks {
if err := check(ctx); err != nil {
log.Errorf("could not check DB connection: %v\n", err)
w.WriteHeader(http.StatusServiceUnavailable)
http.ResponseWriter.WriteHeader(w, http.StatusServiceUnavailable)
return
}
}
w.WriteHeader(http.StatusOK)
http.ResponseWriter.WriteHeader(w, http.StatusOK)
}
}
2 changes: 1 addition & 1 deletion controllers/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func GetSchemas(w http.ResponseWriter, r *http.Request) {
return
}
//nolint
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}
2 changes: 1 addition & 1 deletion controllers/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func ExecuteFromScripts(w http.ResponseWriter, r *http.Request) {
cache.BuntSet(r.URL.String(), string(result))
}
//nolint
w.Write(result)
http.ResponseWriter.Write(w, result)
}

// extractHeaders gets from the given request the headers and populate the provided templateData accordingly.
Expand Down
4 changes: 2 additions & 2 deletions controllers/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestExecuteScriptQuery(t *testing.T) {
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
w.Write(resp)
http.ResponseWriter.Write(w, resp)
}))

r.HandleFunc("/testing/script-post/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resp, err := ExecuteScriptQuery(r, "fulltable", "write_all")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
w.Write(resp)
http.ResponseWriter.Write(w, resp)
}))

ts := httptest.NewServer(r)
Expand Down
20 changes: 10 additions & 10 deletions controllers/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func GetTables(w http.ResponseWriter, r *http.Request) {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}

// GetTablesByDatabaseAndSchema list all (or filter) tables based on database and schema
Expand Down Expand Up @@ -111,7 +111,7 @@ func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}

// SelectFromTables perform select in database
Expand Down Expand Up @@ -254,7 +254,7 @@ func SelectFromTables(w http.ResponseWriter, r *http.Request) {

// Cache arrow if enabled
cache.BuntSet(r.URL.String(), string(sc.Bytes()))
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}

// InsertInTables perform insert in specific table
Expand Down Expand Up @@ -296,8 +296,8 @@ func InsertInTables(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
w.Write(sc.Bytes())
http.ResponseWriter.WriteHeader(w, http.StatusCreated)
http.ResponseWriter.Write(w, sc.Bytes())
}

// BatchInsertInTables perform insert in specific table from a batch request
Expand Down Expand Up @@ -344,8 +344,8 @@ func BatchInsertInTables(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
w.Write(sc.Bytes())
http.ResponseWriter.WriteHeader(w, http.StatusCreated)
http.ResponseWriter.Write(w, sc.Bytes())
}

// DeleteFromTable perform delete sql
Expand Down Expand Up @@ -403,7 +403,7 @@ func DeleteFromTable(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}

// UpdateTable perform update table
Expand Down Expand Up @@ -472,7 +472,7 @@ func UpdateTable(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}

// ShowTable show information from table
Expand Down Expand Up @@ -501,5 +501,5 @@ func ShowTable(w http.ResponseWriter, r *http.Request) {
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
http.ResponseWriter.Write(w, sc.Bytes())
}
14 changes: 7 additions & 7 deletions middlewares/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Test_Middleware_DoesntBlock_CustomRoutes(t *testing.T) {
postgres.Load()
app = nil
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("custom route")) })
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ResponseWriter.Write(w, []byte("custom route")) })
crudRoutes := mux.NewRouter().PathPrefix("/").Subrouter().StrictSlash(true)
crudRoutes.HandleFunc("/{database}/{schema}/{table}", controllers.SelectFromTables).Methods("GET")

Expand Down Expand Up @@ -127,8 +127,8 @@ func customMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerF
m["msg"] = "Calling custom middleware"
b, _ := json.Marshal(m)

w.Header().Set("Content-Type", "application/json")
w.Write(b)
http.ResponseWriter.Header(w).Set("Content-Type", "application/json")
http.ResponseWriter.Write(w, b)

next(w, r)
}
Expand Down Expand Up @@ -224,11 +224,11 @@ func appTest() *negroni.Negroni {
r := mux.NewRouter()
if !config.PrestConf.Debug && !config.PrestConf.EnableDefaultJWT {
n.UseHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
http.ResponseWriter.WriteHeader(w, http.StatusNotImplemented)
})
}
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test app"))
http.ResponseWriter.Write(w, []byte("test app"))
}).Methods("GET")

n.UseHandler(r)
Expand All @@ -240,7 +240,7 @@ func appTestWithJwt() *negroni.Negroni {
r := mux.NewRouter()

r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test app"))
http.ResponseWriter.Write(w, []byte("test app"))
}).Methods("GET")

n.UseHandler(r)
Expand All @@ -255,7 +255,7 @@ func Test_CORS_Middleware(t *testing.T) {
config.Load()
app = nil
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("custom route")) })
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ResponseWriter.Write(w, []byte("custom route")) })
n := GetApp()
n.UseHandler(r)
server := httptest.NewServer(n)
Expand Down
12 changes: 6 additions & 6 deletions middlewares/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ func JwtMiddleware(key string, algo string) negroni.Handler {
// Deprecated: we'll use github.com/rs/cors instead
func Cors(origin []string, headers []string) negroni.Handler {
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Set(headerAllowOrigin, strings.Join(origin, ","))
w.Header().Set(headerAllowCredentials, strconv.FormatBool(true))
http.ResponseWriter.Header(w).Set(headerAllowOrigin, strings.Join(origin, ","))
http.ResponseWriter.Header(w).Set(headerAllowCredentials, strconv.FormatBool(true))
if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
w.Header().Set(headerAllowMethods, strings.Join(defaultAllowMethods, ","))
w.Header().Set(headerAllowHeaders, strings.Join(headers, ","))
http.ResponseWriter.Header(w).Set(headerAllowMethods, strings.Join(defaultAllowMethods, ","))
http.ResponseWriter.Header(w).Set(headerAllowHeaders, strings.Join(headers, ","))
if allowed := checkCors(r, origin); !allowed {
w.WriteHeader(http.StatusForbidden)
http.ResponseWriter.WriteHeader(w, http.StatusForbidden)
return
}
w.WriteHeader(http.StatusOK)
http.ResponseWriter.WriteHeader(w, http.StatusOK)
return
}
next(w, r)
Expand Down
13 changes: 6 additions & 7 deletions middlewares/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func permissionByMethod(method string) (permission string) {

func renderFormat(w http.ResponseWriter, recorder *httptest.ResponseRecorder, format string) {
for key := range recorder.Header() {
w.Header().Set(key, recorder.Header().Get(key))
http.ResponseWriter.Header(w).Set(key, recorder.Header().Get(key))
}
byt, _ := ioutil.ReadAll(recorder.Body)
if recorder.Code >= 400 {
m := make(map[string]string)
m["error"] = strings.TrimSpace(string(byt))
byt, _ = json.MarshalIndent(m, "", "\t")
}
http.ResponseWriter.WriteHeader(w, recorder.Code)
switch format {
case "xml":
xmldata, err := j2x.JsonToXml(byt)
Expand All @@ -63,13 +64,11 @@ func renderFormat(w http.ResponseWriter, recorder *httptest.ResponseRecorder, fo
return
}
xmlStr := fmt.Sprintf("<objects>%s</objects>", string(xmldata))
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(recorder.Code)
w.Write([]byte(xmlStr))
http.ResponseWriter.Header(w).Set("Content-Type", "application/xml")
http.ResponseWriter.Write(w, []byte(xmlStr))
default:
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(recorder.Code)
w.Write(byt)
http.ResponseWriter.Header(w).Set("Content-Type", "application/json")
http.ResponseWriter.Write(w, byt)
}
}

Expand Down
5 changes: 2 additions & 3 deletions plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ func HandlerPlugin(w http.ResponseWriter, r *http.Request) {

//nolint
if ret.StatusCode != -1 {
w.WriteHeader(ret.StatusCode)
http.ResponseWriter.WriteHeader(w, ret.StatusCode)
}

w.Write([]byte(ret.ReturnJson))
http.ResponseWriter.Write(w, []byte(ret.ReturnJson))
}
2 changes: 1 addition & 1 deletion testutils/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestDoRequest(t *testing.T) {
bodyResponse := `{"key": "value"}`
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//nolint
w.Write([]byte(bodyResponse))
http.ResponseWriter.Write(w, []byte(bodyResponse))
}).Methods("GET")
server := httptest.NewServer(router)
defer server.Close()
Expand Down