Add some better error messages to http and jws
This commit is contained in:
parent
0c0d57a545
commit
66d8acbf89
2 changed files with 12 additions and 12 deletions
10
acme/http.go
10
acme/http.go
|
@ -31,14 +31,14 @@ const (
|
||||||
func httpHead(url string) (resp *http.Response, err error) {
|
func httpHead(url string) (resp *http.Response, err error) {
|
||||||
req, err := http.NewRequest("HEAD", url, nil)
|
req, err := http.NewRequest("HEAD", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to head %q: %v", url, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("User-Agent", userAgent())
|
req.Header.Set("User-Agent", userAgent())
|
||||||
|
|
||||||
resp, err = HTTPClient.Do(req)
|
resp, err = HTTPClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resp, err
|
return resp, fmt.Errorf("failed to do head %q: %v", url, err)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
return resp, err
|
return resp, err
|
||||||
|
@ -49,7 +49,7 @@ func httpHead(url string) (resp *http.Response, err error) {
|
||||||
func httpPost(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
|
func httpPost(url string, bodyType string, body io.Reader) (resp *http.Response, err error) {
|
||||||
req, err := http.NewRequest("POST", url, body)
|
req, err := http.NewRequest("POST", url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to post %q: %v", url, err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", bodyType)
|
req.Header.Set("Content-Type", bodyType)
|
||||||
req.Header.Set("User-Agent", userAgent())
|
req.Header.Set("User-Agent", userAgent())
|
||||||
|
@ -62,7 +62,7 @@ func httpPost(url string, bodyType string, body io.Reader) (resp *http.Response,
|
||||||
func httpGet(url string) (resp *http.Response, err error) {
|
func httpGet(url string) (resp *http.Response, err error) {
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to get %q: %v", url, err)
|
||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", userAgent())
|
req.Header.Set("User-Agent", userAgent())
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ func httpGet(url string) (resp *http.Response, err error) {
|
||||||
func getJSON(uri string, respBody interface{}) (http.Header, error) {
|
func getJSON(uri string, respBody interface{}) (http.Header, error) {
|
||||||
resp, err := httpGet(uri)
|
resp, err := httpGet(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get %q: %v", uri, err)
|
return nil, fmt.Errorf("failed to get json %q: %v", uri, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|
14
acme/jws.go
14
acme/jws.go
|
@ -37,7 +37,7 @@ func keyAsJWK(key interface{}) *jose.JsonWebKey {
|
||||||
func (j *jws) post(url string, content []byte) (*http.Response, error) {
|
func (j *jws) post(url string, content []byte) (*http.Response, error) {
|
||||||
signedContent, err := j.signContent(content)
|
signedContent, err := j.signContent(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("Failed to sign content -> %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := httpPost(url, "application/jose+json", bytes.NewBuffer([]byte(signedContent.FullSerialize())))
|
resp, err := httpPost(url, "application/jose+json", bytes.NewBuffer([]byte(signedContent.FullSerialize())))
|
||||||
|
@ -54,14 +54,14 @@ func (j *jws) post(url string, content []byte) (*http.Response, error) {
|
||||||
// In case of a nonce error - retry once
|
// In case of a nonce error - retry once
|
||||||
resp, err = httpPost(url, "application/jose+json", bytes.NewBuffer([]byte(signedContent.FullSerialize())))
|
resp, err = httpPost(url, "application/jose+json", bytes.NewBuffer([]byte(signedContent.FullSerialize())))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("Failed to HTTP POST to %s -> %s", url, err.Error())
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, err
|
return nil, fmt.Errorf("Failed to HTTP POST to %s -> %s", url, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, err
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *jws) signContent(content []byte) (*jose.JsonWebSignature, error) {
|
func (j *jws) signContent(content []byte) (*jose.JsonWebSignature, error) {
|
||||||
|
@ -80,13 +80,13 @@ func (j *jws) signContent(content []byte) (*jose.JsonWebSignature, error) {
|
||||||
|
|
||||||
signer, err := jose.NewSigner(alg, j.privKey)
|
signer, err := jose.NewSigner(alg, j.privKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("Failed to create jose signer -> %s", err.Error())
|
||||||
}
|
}
|
||||||
signer.SetNonceSource(j)
|
signer.SetNonceSource(j)
|
||||||
|
|
||||||
signed, err := signer.Sign(content)
|
signed, err := signer.Sign(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("Failed to sign content -> %s", err.Error())
|
||||||
}
|
}
|
||||||
return signed, nil
|
return signed, nil
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ func (n *nonceManager) Push(nonce string) {
|
||||||
func getNonce(url string) (string, error) {
|
func getNonce(url string) (string, error) {
|
||||||
resp, err := httpHead(url)
|
resp, err := httpHead(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", fmt.Errorf("Failed to get nonce from HTTP HEAD -> %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return getNonceFromResponse(resp)
|
return getNonceFromResponse(resp)
|
||||||
|
|
Loading…
Reference in a new issue