Skip to content

Commit

Permalink
fixes influxdata#182, add Content-Type json to all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Feb 5, 2016
1 parent 00eb7b4 commit ee39186
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 118 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ There was a breaking change to the `define` command, see [#173](https://github.c
- [#195](https://github.com/influxdata/kapacitor/issues/195): Fix panic in where node.
- [#208](https://github.com/influxdata/kapacitor/issues/208): Add default stats dbrp to default subscription excludes.
- [#203](https://github.com/influxdata/kapacitor/issues/203): Fix hang when deleteing invalid batch task.
- [#182](https://github.com/influxdata/kapacitor/issues/182): Fix missing/incorrect Content-Type headers for various HTTP endpoints.

## v0.10.0 [2016-01-26]

Expand Down
2 changes: 0 additions & 2 deletions http_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ func (h *HTTPOutNode) runOut([]byte) error {
Name: h.Name(),
Method: "GET",
Pattern: p,
Gzipped: true,
Log: true,
HandlerFunc: hndl,
}}

Expand Down
114 changes: 64 additions & 50 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type Route struct {
Name string
Method string
Pattern string
Gzipped bool
Log bool
HandlerFunc interface{}
}

Expand Down Expand Up @@ -75,45 +73,75 @@ func NewHandler(requireAuthentication, loggingEnabled, writeTrace bool, statMap
}

h.AddRoutes([]Route{
Route{ // Ping
"ping",
"GET", "/ping", true, true, h.servePing,
Route{
// Ping
Name: "ping",
Method: "GET",
Pattern: "/ping",
HandlerFunc: h.servePing,
},
Route{ // Ping
"ping-head",
"HEAD", "/ping", true, true, h.servePing,
Route{
// Ping
Name: "ping-head",
Method: "HEAD",
Pattern: "/ping",
HandlerFunc: h.servePing,
},
Route{
"write", // Satisfy CORS checks.
"OPTIONS", "/write", true, true, h.serveOptions,
// Satisfy CORS checks.
Name: "write",
Method: "OPTIONS",
Pattern: "/write",
HandlerFunc: h.serveOptions,
},
Route{
"write", // Data-ingest route.
"POST", "/write", true, true, h.serveWrite,
// Data-ingest route.
Name: "write",
Method: "POST",
Pattern: "/write",
HandlerFunc: h.serveWrite,
},
Route{
"routes", // Display current API routes
"GET", "/:routes", true, true, h.serveRoutes,
// Display current API routes
Name: "routes",
Method: "GET",
Pattern: "/:routes",
HandlerFunc: h.serveRoutes,
},
Route{
"log-level", // Display current API routes
"POST", "/loglevel", true, true, h.serveLogLevel,
// Display current log level
Name: "log-level",
Method: "POST",
Pattern: "/loglevel",
HandlerFunc: h.serveLogLevel,
},
Route{
"404", // Catch all 404
"GET", "/", true, true, h.serve404,
// Catch all 404
Name: "404",
Method: "GET",
Pattern: "/",
HandlerFunc: h.serve404,
},
Route{
"404", // Catch all 404
"POST", "/", true, true, h.serve404,
// Catch all 404
Name: "404",
Method: "POST",
Pattern: "/",
HandlerFunc: h.serve404,
},
Route{
"404", // Catch all 404
"DELETE", "/", true, true, h.serve404,
// Catch all 404
Name: "404",
Method: "DELETE",
Pattern: "/",
HandlerFunc: h.serve404,
},
Route{
"404", // Catch all 404
"HEAD", "/", true, true, h.serve404,
// Catch all 404
Name: "404",
Method: "HEAD",
Pattern: "/",
HandlerFunc: h.serve404,
},
})

Expand Down Expand Up @@ -141,13 +169,14 @@ func (h *Handler) AddRoute(r Route) error {
handler = http.HandlerFunc(hf)
}

if r.Gzipped {
handler = gzipFilter(handler)
}
// Set basic handlers for all requests
handler = jsonContent(handler)
handler = gzipFilter(handler)
handler = versionHeader(handler, h)
handler = cors(handler)
handler = requestID(handler)
if h.loggingEnabled && r.Log {

if h.loggingEnabled {
handler = logging(handler, r.Name, h.Logger)
}
handler = recovery(handler, r.Name, h.Logger) // make sure recovery is always last
Expand Down Expand Up @@ -281,25 +310,6 @@ func (h *Handler) serveWrite(w http.ResponseWriter, r *http.Request, user *meta.

// serveWriteLine receives incoming series data in line protocol format and writes it to the database.
func (h *Handler) serveWriteLine(w http.ResponseWriter, r *http.Request, body []byte, user *meta.UserInfo) {
// Some clients may not set the content-type header appropriately and send JSON with a non-json
// content-type. If the body looks JSON, try to handle it as as JSON instead
if len(body) > 0 {
var i int
for {
// JSON requests must start w/ an opening bracket
if body[i] == '{' {
h.writeError(w, influxql.Result{Err: fmt.Errorf("kapacitor does not support the JSON write protocol")}, http.StatusBadRequest)
return
}

// check that the byte is in the standard ascii code range
if body[i] > 32 {
break
}
i += 1
}
}

precision := r.FormValue("precision")
if precision == "" {
precision = "n"
Expand Down Expand Up @@ -390,7 +400,6 @@ func MarshalJSON(v interface{}, pretty bool) []byte {

// serveExpvar serves registered expvar information over HTTP.
func serveExpvar(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
Expand All @@ -405,7 +414,6 @@ func serveExpvar(w http.ResponseWriter, r *http.Request) {

// HttpError writes an error to the client in a standard format.
func HttpError(w http.ResponseWriter, err string, pretty bool, code int) {
w.Header().Add("content-type", "application/json")
w.WriteHeader(code)

type errResponse struct {
Expand All @@ -423,7 +431,6 @@ func HttpError(w http.ResponseWriter, err string, pretty bool, code int) {
}

func resultError(w http.ResponseWriter, result influxql.Result, code int) {
w.Header().Add("content-type", "application/json")
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(&result)
}
Expand Down Expand Up @@ -489,6 +496,13 @@ func gzipFilter(inner http.Handler) http.Handler {
})
}

func jsonContent(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
inner.ServeHTTP(w, r)
})
}

// versionHeader takes a HTTP handler and returns a HTTP handler
// and adds the X-KAPACITOR-VERSION header to outgoing responses.
func versionHeader(inner http.Handler, h *Handler) http.Handler {
Expand Down
50 changes: 20 additions & 30 deletions services/replay/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,34 @@ func NewService(conf Config, l *log.Logger) *Service {
func (r *Service) Open() error {
r.routes = []httpd.Route{
{
"recordings",
"GET",
"/recordings",
true,
true,
r.handleList,
Name: "recordings",
Method: "GET",
Pattern: "/recordings",
HandlerFunc: r.handleList,
},
{
"recording-delete",
"DELETE",
"/recording",
true,
true,
r.handleDelete,
Name: "recording-delete",
Method: "DELETE",
Pattern: "/recording",
HandlerFunc: r.handleDelete,
},
{
"record",
"POST",
"/record",
true,
true,
r.handleRecord,
Name: "record",
Method: "POST",
Pattern: "/record",
HandlerFunc: r.handleRecord,
},
{
"record",
"GET",
"/record",
true,
true,
r.handleGetRecording,
Name: "record",
Method: "GET",
Pattern: "/record",
HandlerFunc: r.handleGetRecording,
},
{
"replay",
"POST",
"/replay",
true,
true,
r.handleReplay,
Name: "replay",
Method: "POST",
Pattern: "/replay",
HandlerFunc: r.handleReplay,
},
}

Expand Down
60 changes: 24 additions & 36 deletions services/task_store/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,52 +84,40 @@ func (ts *Service) Open() error {
// Define API routes
ts.routes = []httpd.Route{
{
"task-show",
"GET",
"/task",
true,
true,
ts.handleTask,
Name: "task-show",
Method: "GET",
Pattern: "/task",
HandlerFunc: ts.handleTask,
},
{
"task-list",
"GET",
"/tasks",
true,
true,
ts.handleTasks,
Name: "task-list",
Method: "GET",
Pattern: "/tasks",
HandlerFunc: ts.handleTasks,
},
{
"task-save",
"POST",
"/task",
true,
true,
ts.handleSave,
Name: "task-save",
Method: "POST",
Pattern: "/task",
HandlerFunc: ts.handleSave,
},
{
"task-delete",
"DELETE",
"/task",
true,
true,
ts.handleDelete,
Name: "task-delete",
Method: "DELETE",
Pattern: "/task",
HandlerFunc: ts.handleDelete,
},
{
"task-enable",
"POST",
"/enable",
true,
true,
ts.handleEnable,
Name: "task-enable",
Method: "POST",
Pattern: "/enable",
HandlerFunc: ts.handleEnable,
},
{
"task-disable",
"POST",
"/disable",
true,
true,
ts.handleDisable,
Name: "task-disable",
Method: "POST",
Pattern: "/disable",
HandlerFunc: ts.handleDisable,
},
}
err = ts.HTTPDService.AddRoutes(ts.routes)
Expand Down

0 comments on commit ee39186

Please sign in to comment.