Code maintenance: better JSON decoding for HTTP
This commit is contained in:
@@ -469,19 +469,18 @@ func fetchPIAToken(ctx context.Context, openFile os.OpenFileFunc,
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
b, err := ioutil.ReadAll(response.Body)
|
|
||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
|
b, _ := ioutil.ReadAll(response.Body)
|
||||||
shortenMessage := string(b)
|
shortenMessage := string(b)
|
||||||
shortenMessage = strings.ReplaceAll(shortenMessage, "\n", "")
|
shortenMessage = strings.ReplaceAll(shortenMessage, "\n", "")
|
||||||
shortenMessage = strings.ReplaceAll(shortenMessage, " ", " ")
|
shortenMessage = strings.ReplaceAll(shortenMessage, " ", " ")
|
||||||
return "", fmt.Errorf("%s: response received: %q", response.Status, shortenMessage)
|
return "", fmt.Errorf("%s: response received: %q", response.Status, shortenMessage)
|
||||||
} else if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
decoder := json.NewDecoder(response.Body)
|
||||||
var result struct {
|
var result struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(b, &result); err != nil {
|
if err := decoder.Decode(&result); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
} else if len(result.Token) == 0 {
|
} else if len(result.Token) == 0 {
|
||||||
return "", fmt.Errorf("token is empty")
|
return "", fmt.Errorf("token is empty")
|
||||||
@@ -534,16 +533,13 @@ func fetchPIAPortForwardData(ctx context.Context, client *http.Client, gateway n
|
|||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
return 0, "", expiration, fmt.Errorf("cannot obtain signature: %s", response.Status)
|
return 0, "", expiration, fmt.Errorf("cannot obtain signature: %s", response.Status)
|
||||||
}
|
}
|
||||||
b, err := ioutil.ReadAll(response.Body)
|
decoder := json.NewDecoder(response.Body)
|
||||||
if err != nil {
|
|
||||||
return 0, "", expiration, fmt.Errorf("cannot obtain signature: %w", err)
|
|
||||||
}
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Payload string `json:"payload"`
|
Payload string `json:"payload"`
|
||||||
Signature string `json:"signature"`
|
Signature string `json:"signature"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(b, &data); err != nil {
|
if err := decoder.Decode(&data); err != nil {
|
||||||
return 0, "", expiration, fmt.Errorf("cannot decode received data: %w", err)
|
return 0, "", expiration, fmt.Errorf("cannot decode received data: %w", err)
|
||||||
} else if data.Status != "OK" {
|
} else if data.Status != "OK" {
|
||||||
return 0, "", expiration, fmt.Errorf("response received from PIA has status %s", data.Status)
|
return 0, "", expiration, fmt.Errorf("response received from PIA has status %s", data.Status)
|
||||||
@@ -580,15 +576,13 @@ func bindPIAPort(ctx context.Context, client *http.Client, gateway net.IP, data
|
|||||||
if response.StatusCode != http.StatusOK {
|
if response.StatusCode != http.StatusOK {
|
||||||
return fmt.Errorf("cannot bind port: %s", response.Status)
|
return fmt.Errorf("cannot bind port: %s", response.Status)
|
||||||
}
|
}
|
||||||
b, err := ioutil.ReadAll(response.Body)
|
|
||||||
if err != nil {
|
decoder := json.NewDecoder(response.Body)
|
||||||
return fmt.Errorf("cannot bind port: %w", err)
|
|
||||||
}
|
|
||||||
var responseData struct {
|
var responseData struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(b, &responseData); err != nil {
|
if err := decoder.Decode(&responseData); err != nil {
|
||||||
return fmt.Errorf("cannot bind port: %w", err)
|
return fmt.Errorf("cannot bind port: %w", err)
|
||||||
} else if responseData.Status != "OK" {
|
} else if responseData.Status != "OK" {
|
||||||
return fmt.Errorf("response received from PIA: %s (%s)", responseData.Status, responseData.Message)
|
return fmt.Errorf("response received from PIA: %s (%s)", responseData.Status, responseData.Message)
|
||||||
|
|||||||
@@ -50,13 +50,9 @@ func (h *handlerV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *handlerV1) getVersion(w http.ResponseWriter) {
|
func (h *handlerV1) getVersion(w http.ResponseWriter) {
|
||||||
data, err := json.Marshal(h.buildInfo)
|
encoder := json.NewEncoder(w)
|
||||||
if err != nil {
|
if err := encoder.Encode(h.buildInfo); err != nil {
|
||||||
h.logger.Warn(err)
|
h.logger.Warn(err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
|
||||||
}
|
|
||||||
if _, err := w.Write(data); err != nil {
|
|
||||||
h.logger.Warn(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package version
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -35,11 +34,8 @@ func getGithubReleases(ctx context.Context, client *http.Client) (releases []git
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
b, err := ioutil.ReadAll(response.Body)
|
decoder := json.NewDecoder(response.Body)
|
||||||
if err != nil {
|
if err := decoder.Decode(&releases); err != nil {
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(b, &releases); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return releases, nil
|
return releases, nil
|
||||||
@@ -56,11 +52,8 @@ func getGithubCommits(ctx context.Context, client *http.Client) (commits []githu
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
b, err := ioutil.ReadAll(response.Body)
|
decoder := json.NewDecoder(response.Body)
|
||||||
if err != nil {
|
if err := decoder.Decode(&commits); err != nil {
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(b, &commits); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return commits, nil
|
return commits, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user