From aa548cf901d1a8acc7794c80a33012d26dcd9069 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 27 May 2015 14:10:00 -0700 Subject: [PATCH] http: support raw HTTP output --- http/logical.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/http/logical.go b/http/logical.go index c9e842e62d..70d0535ab1 100644 --- a/http/logical.go +++ b/http/logical.go @@ -96,6 +96,12 @@ func respondLogical(w http.ResponseWriter, r *http.Request, path string, resp *l return } + // Check if this is a raw response + if _, ok := resp.Data[logical.HTTPContentType]; ok { + respondRaw(w, r, path, resp) + return + } + logicalResp := &LogicalResponse{Data: resp.Data} if resp.Secret != nil { logicalResp.LeaseID = resp.Secret.LeaseID @@ -140,6 +146,58 @@ func respondLogical(w http.ResponseWriter, r *http.Request, path string, resp *l respondOk(w, httpResp) } +// respondRaw is used when the response is using HTTPContentType and HTTPRawBody +// to change the default response handling. This is only used for specific things like +// returning the CRL information on the PKI backends. +func respondRaw(w http.ResponseWriter, r *http.Request, path string, resp *logical.Response) { + // Ensure this is never a secret or auth response + if resp.Secret != nil || resp.Auth != nil { + respondError(w, http.StatusInternalServerError, nil) + return + } + + // Get the status code + statusRaw, ok := resp.Data[logical.HTTPStatusCode] + if !ok { + respondError(w, http.StatusInternalServerError, nil) + return + } + status, ok := statusRaw.(int) + if !ok { + respondError(w, http.StatusInternalServerError, nil) + return + } + + // Get the header + contentTypeRaw, ok := resp.Data[logical.HTTPContentType] + if !ok { + respondError(w, http.StatusInternalServerError, nil) + return + } + contentType, ok := contentTypeRaw.(string) + if !ok { + respondError(w, http.StatusInternalServerError, nil) + return + } + + // Get the body + bodyRaw, ok := resp.Data[logical.HTTPRawBody] + if !ok { + respondError(w, http.StatusInternalServerError, nil) + return + } + body, ok := bodyRaw.([]byte) + if !ok { + respondError(w, http.StatusInternalServerError, nil) + return + } + + // Write the response + w.Header().Set("Content-Type", contentType) + w.WriteHeader(status) + w.Write(body) +} + type LogicalResponse struct { LeaseID string `json:"lease_id"` Renewable bool `json:"renewable"`