Skip to content

Commit

Permalink
Improve error handling when initializing validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 21, 2023
1 parent 3983f67 commit 71c64e1
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions requests/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package requests
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"

"howett.net/plist"
Expand All @@ -23,48 +25,55 @@ type CertResponse struct {
Cert []byte `plist:"cert"`
}

func makeRequest(ctx context.Context, url string, body, output any) error {
func makeRequest(ctx context.Context, url string, body, output any) ([]byte, error) {
method := http.MethodGet
var bodyReader io.Reader
if body != nil {
method = http.MethodPost
var buf bytes.Buffer
err := plist.NewEncoder(&buf).Encode(body)
if err != nil {
return fmt.Errorf("failed to encode body: %w", err)
return nil, fmt.Errorf("failed to encode body: %w", err)
}
bodyReader = &buf
}
req, err := http.NewRequestWithContext(ctx, method, url, bodyReader)
if err != nil {
return fmt.Errorf("failed to prepare request: %w", err)
return nil, fmt.Errorf("failed to prepare request: %w", err)
}
req.Header.Set("User-Agent", versions.Current.UserAgent())
if bodyReader != nil {
req.Header.Set("Content-Type", "application/x-apple-plist")
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
respData, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read body: %w", err)
return nil, fmt.Errorf("failed to read body: %w", err)
} else if resp.StatusCode != http.StatusOK {
return respData, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}
_, err = plist.Unmarshal(respData, output)
if err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
return nil
return respData, nil
}

func FetchCert(ctx context.Context) ([]byte, error) {
var parsedResp CertResponse
err := makeRequest(ctx, validationCertURL, nil, &parsedResp)
return parsedResp.Cert, err
respData, err := makeRequest(ctx, validationCertURL, nil, &parsedResp)
if err != nil {
return nil, err
}
_, err = plist.Unmarshal(respData, &parsedResp)
if err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
} else if len(parsedResp.Cert) == 0 {
return nil, fmt.Errorf("didn't get cert in response")
}
return parsedResp.Cert, nil
}

type ReqInitializeValidation struct {
Expand All @@ -75,8 +84,30 @@ type RespInitializeValidation struct {
SessionInfo []byte `plist:"session-info"`
}

func InitializeValidation(ctx context.Context, request []byte) ([]byte, error) {
func InitializeValidation(ctx context.Context, request []byte) (sessionInfo []byte, err error) {
var parsedResp RespInitializeValidation
err := makeRequest(ctx, initializeValidationURL, &ReqInitializeValidation{request}, &parsedResp)
return parsedResp.SessionInfo, err
var respData []byte
defer func() {
if err != nil && respData != nil {
var rawData map[string]any
_, err = plist.Unmarshal(respData, &rawData)
if err == nil {
log.Printf("Plist response data of errored request: %+v", rawData)
} else {
log.Printf("Raw response data of errored request: %s", base64.StdEncoding.EncodeToString(respData))
}
}
}()
respData, err = makeRequest(ctx, initializeValidationURL, &ReqInitializeValidation{request}, &parsedResp)
if err != nil {
return nil, err
}
_, err = plist.Unmarshal(respData, &parsedResp)
if err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
} else if len(parsedResp.SessionInfo) == 0 {
return nil, fmt.Errorf("didn't get session info in initialize validation response")
}
sessionInfo = parsedResp.SessionInfo
return
}

0 comments on commit 71c64e1

Please sign in to comment.