aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-25 02:11:52 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-25 02:11:52 +0100
commit0ac7df7972d1c16ea4dd9eaf5aadaa91956da6f3 (patch)
tree391df58fffba4cf61a20b4a7d5feb88598e2b8d4 /client.go
parent66ee37696f8a77152dfe2fd75f1eab173c71f213 (diff)
Content-Type switch
Diffstat (limited to 'client.go')
-rw-r--r--client.go35
1 files changed, 14 insertions, 21 deletions
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
}