Skip to content

Commit 547b9bc

Browse files
committed
Support POST for those endpoints that may use it
1 parent b120300 commit 547b9bc

1 file changed

Lines changed: 49 additions & 33 deletions

File tree

lokiproxy.go

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,16 @@ func getRequiredLabelsForUser(res http.ResponseWriter, req *http.Request) (map[s
241241

242242
func respondWithProxy(
243243
path string,
244-
req *http.Request,
244+
args url.Values,
245245
res http.ResponseWriter,
246246
setArgs map[string][]string,
247247
passthroughArgs []string,
248248
ctx context.Context,
249249
) {
250250
// Assemble query parameters
251-
origQuery := req.URL.Query()
252251
proxyQuery := make(url.Values)
253252
for _, key := range passthroughArgs {
254-
values, ok := origQuery[key]
253+
values, ok := args[key]
255254
if ok {
256255
proxyQuery[key] = values
257256
}
@@ -291,12 +290,16 @@ func respondWithProxy(
291290

292291
func handleQuery(res http.ResponseWriter, req *http.Request) {
293292
if req.Method == "GET" {
294-
args := req.URL.Query()
295-
if len(args["query"]) != 1 {
293+
if err := req.ParseForm(); err != nil {
294+
sendError(res, "invalid form data")
295+
return
296+
}
297+
298+
if len(req.Form["query"]) != 1 {
296299
sendError(res, "one query expected")
297300
return
298301
}
299-
query := args["query"][0]
302+
query := req.Form["query"][0]
300303

301304
// Find user, get required labels
302305
requiredLabels, ok := getRequiredLabelsForUser(res, req)
@@ -314,7 +317,7 @@ func handleQuery(res http.ResponseWriter, req *http.Request) {
314317
// Proxy
315318
respondWithProxy(
316319
"/loki/api/v1/query",
317-
req,
320+
req.Form,
318321
res,
319322
map[string][]string{"query": []string{query}},
320323
[]string{"limit", "time", "direction"},
@@ -329,12 +332,16 @@ func handleQuery(res http.ResponseWriter, req *http.Request) {
329332

330333
func handleQueryRange(res http.ResponseWriter, req *http.Request) {
331334
if req.Method == "GET" {
332-
args := req.URL.Query()
333-
if len(args["query"]) != 1 {
335+
if err := req.ParseForm(); err != nil {
336+
sendError(res, "invalid form data")
337+
return
338+
}
339+
340+
if len(req.Form["query"]) != 1 {
334341
sendError(res, "one query expected")
335342
return
336343
}
337-
query := args["query"][0]
344+
query := req.Form["query"][0]
338345

339346
// Find user, get allowed namespaces
340347
requiredLabels, ok := getRequiredLabelsForUser(res, req)
@@ -352,7 +359,7 @@ func handleQueryRange(res http.ResponseWriter, req *http.Request) {
352359
// Proxy
353360
respondWithProxy(
354361
"/loki/api/v1/query_range",
355-
req,
362+
req.Form,
356363
res,
357364
map[string][]string{"query": []string{query}},
358365
[]string{"limit", "start", "end", "since", "step", "interval", "direction"},
@@ -366,9 +373,13 @@ func handleQueryRange(res http.ResponseWriter, req *http.Request) {
366373
}
367374

368375
func handleSeries(res http.ResponseWriter, req *http.Request) {
369-
if req.Method == "GET" {
370-
args := req.URL.Query()
371-
queries := args["match[]"]
376+
if req.Method == "GET" || req.Method == "POST" {
377+
if err := req.ParseForm(); err != nil {
378+
sendError(res, "invalid form data")
379+
return
380+
}
381+
382+
queries := req.Form["match[]"]
372383

373384
// Find user, get allowed namespaces
374385
requiredLabels, ok := getRequiredLabelsForUser(res, req)
@@ -389,16 +400,12 @@ func handleSeries(res http.ResponseWriter, req *http.Request) {
389400
// Proxy
390401
respondWithProxy(
391402
"/loki/api/v1/series",
392-
req,
403+
req.Form,
393404
res,
394405
map[string][]string{"query": queries},
395406
[]string{"limit", "start", "end", "since", "step", "interval", "direction"},
396407
req.Context(),
397408
)
398-
} else if req.Method == "POST" {
399-
// TODO
400-
sendError(res, "not yet implemented")
401-
return
402409
} else {
403410
log.Printf("got %s to series", req.Method)
404411
sendError(res, "use methods GET or POST")
@@ -408,12 +415,16 @@ func handleSeries(res http.ResponseWriter, req *http.Request) {
408415

409416
func handleTail(res http.ResponseWriter, req *http.Request) {
410417
if req.Method == "GET" {
411-
args := req.URL.Query()
412-
if len(args["query"]) != 1 {
418+
if err := req.ParseForm(); err != nil {
419+
sendError(res, "invalid form data")
420+
return
421+
}
422+
423+
if len(req.Form["query"]) != 1 {
413424
sendError(res, "one query expected")
414425
return
415426
}
416-
query := args["query"][0]
427+
query := req.Form["query"][0]
417428

418429
// Find user, get allowed namespaces
419430
requiredLabels, ok := getRequiredLabelsForUser(res, req)
@@ -431,7 +442,7 @@ func handleTail(res http.ResponseWriter, req *http.Request) {
431442
// Proxy
432443
respondWithProxy(
433444
"/loki/api/v1/tail",
434-
req,
445+
req.Form,
435446
res,
436447
map[string][]string{"query": []string{query}},
437448
[]string{"delay_for", "limit", "start"},
@@ -446,10 +457,15 @@ func handleTail(res http.ResponseWriter, req *http.Request) {
446457

447458
func handleLabels(res http.ResponseWriter, req *http.Request) {
448459
if req.Method == "GET" {
460+
if err := req.ParseForm(); err != nil {
461+
sendError(res, "invalid form data")
462+
return
463+
}
464+
449465
// Proxy
450466
respondWithProxy(
451467
"/loki/api/v1/labels",
452-
req,
468+
req.Form,
453469
res,
454470
map[string][]string{},
455471
[]string{"start", "end", "since"},
@@ -468,13 +484,17 @@ func handleLabelValues(res http.ResponseWriter, _ *http.Request) {
468484
}
469485

470486
func handleIndexStats(res http.ResponseWriter, req *http.Request) {
471-
if req.Method == "GET" {
472-
args := req.URL.Query()
473-
if len(args["query"]) != 1 {
487+
if req.Method == "GET" || req.Method == "POST" {
488+
if err := req.ParseForm(); err != nil {
489+
sendError(res, "invalid form data")
490+
return
491+
}
492+
493+
if len(req.Form["query"]) != 1 {
474494
sendError(res, "one query expected")
475495
return
476496
}
477-
query := args["query"][0]
497+
query := req.Form["query"][0]
478498

479499
// Find user, get allowed namespaces
480500
requiredLabels, ok := getRequiredLabelsForUser(res, req)
@@ -492,16 +512,12 @@ func handleIndexStats(res http.ResponseWriter, req *http.Request) {
492512
// Proxy
493513
respondWithProxy(
494514
"/loki/api/v1/index/stats",
495-
req,
515+
req.Form,
496516
res,
497517
map[string][]string{"query": []string{query}},
498518
[]string{"start", "end"},
499519
req.Context(),
500520
)
501-
} else if req.Method == "POST" {
502-
// TODO
503-
sendError(res, "not yet implemented")
504-
return
505521
} else {
506522
log.Printf("got %s to index/stats", req.Method)
507523
sendError(res, "use methods GET or POST")

0 commit comments

Comments
 (0)