From 0ac7df7972d1c16ea4dd9eaf5aadaa91956da6f3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 25 Dec 2015 02:11:52 +0100 Subject: Content-Type switch --- client.go | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index 0dacdd8..867c500 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ import ( "crypto/rsa" "encoding/json" "errors" - "io/ioutil" "log" "net/http" "net/url" @@ -35,7 +34,6 @@ func (c Client) Nonce() (string, error) { func (c Client) replyNonce(r *http.Response) { if rn := r.Header.Get("Replay-Nonce"); rn != "" { - log.Println(ansi.Color("NONCE", "blue"), rn) c.nonce <- rn } } @@ -69,6 +67,8 @@ func NewClient(directory string) (*Client, error) { // Request issuance POST new-cert 201 -> cert // Check for new cert GET cert 200 +var errContentType = errors.New("unknown content type") + // request is used for // new-reg, new-authz, challenge, new-cert func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, error) { @@ -87,36 +87,28 @@ func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, erro if err != nil { return nil, err } - // TODO: add content-type switch - // application/problem+json - // application/json - // application/pkix-cert - ct := resp.Header.Get("Content-Type") - log.Println(ansi.Color("CT", "green"), ct) - defer resp.Body.Close() defer c.replyNonce(resp) log.Println(ansi.Color("STATUS", "yellow"), resp.Status) - if resp.StatusCode >= http.StatusBadRequest { + switch resp.Header.Get("Content-Type") { + case "application/problem+json": + defer resp.Body.Close() var p Problem - err = json.NewDecoder(resp.Body).Decode(&p) - if err != nil { + if err = json.NewDecoder(resp.Body).Decode(&p); err != nil { return resp, err } if err, ok := urnErrors[p.Type]; ok { p.Err = err } return resp, p + case "application/json": + defer resp.Body.Close() + return resp, json.NewDecoder(resp.Body).Decode(v) + case "application/pkix-cert": + return resp, nil + default: + return resp, errContentType } - - // DEBUG - body, err = ioutil.ReadAll(resp.Body) - if err != nil { - return resp, err - } - log.Println(ansi.Color("RESP", "red"), string(body)) - return resp, json.Unmarshal(body, v) - //return json.NewDecoder(resp.Body).Decode(v) } type Links map[string]string @@ -324,6 +316,7 @@ func (c *Client) CSR(s Signer, altnames []string, key *rsa.PrivateKey) error { if err != nil { return err } + return nil } -- cgit v1.2.3