Openstatus www.openstatus.dev

๐Ÿ› binary files (#1003)

authored by

Thibault Le Ouay and committed by
GitHub
d006fde3 9a940932

+43 -2
+43 -2
apps/checker/ping.go
··· 4 4 "bytes" 5 5 "context" 6 6 "crypto/tls" 7 + "encoding/base64" 7 8 "encoding/json" 8 9 "errors" 9 10 "fmt" ··· 12 13 "net/http/httptrace" 13 14 "net/url" 14 15 "os" 16 + "strings" 15 17 "time" 16 18 17 19 "github.com/openstatushq/openstatus/apps/checker/request" ··· 65 67 func Ping(ctx context.Context, client *http.Client, inputData request.CheckerRequest) (PingData, error) { 66 68 logger := log.Ctx(ctx).With().Str("monitor", inputData.URL).Logger() 67 69 region := os.Getenv("FLY_REGION") 68 - req, err := http.NewRequestWithContext(ctx, inputData.Method, inputData.URL, bytes.NewReader([]byte(inputData.Body))) 70 + 71 + b := []byte(inputData.Body) 72 + for _, header := range inputData.Headers { 73 + if header.Key == "Content-Type" && header.Value == "application/octet-stream" { 74 + 75 + // split the body by comma and convert it to bytes 76 + data := strings.Split(inputData.Body, ",") 77 + if len(data) == 2 { 78 + 79 + decoded, err := base64.StdEncoding.DecodeString(data[1]) 80 + if err != nil { 81 + return PingData{}, fmt.Errorf("error while decoding base64: %w", err) 82 + } 83 + 84 + b = decoded 85 + 86 + } 87 + } 88 + } 89 + 90 + req, err := http.NewRequestWithContext(ctx, inputData.Method, inputData.URL, bytes.NewReader(b)) 69 91 if err != nil { 70 92 logger.Error().Err(err).Msg("error while creating req") 71 93 return PingData{}, fmt.Errorf("unable to create req: %w", err) ··· 183 205 func SinglePing(ctx context.Context, client *http.Client, inputData request.PingRequest) (Response, error) { 184 206 logger := log.Ctx(ctx).With().Str("monitor", inputData.URL).Logger() 185 207 186 - req, err := http.NewRequestWithContext(ctx, inputData.Method, inputData.URL, bytes.NewReader([]byte(inputData.Body))) 208 + b := []byte(inputData.Body) 209 + if inputData.Headers["Content-Type"] == "application/octet-stream" { 210 + 211 + // split the body by comma and convert it to bytes 212 + data := strings.Split(inputData.Body, ",") 213 + if len(data) == 2 { 214 + 215 + decoded, err := base64.StdEncoding.DecodeString(data[1]) 216 + if err != nil { 217 + return Response{}, fmt.Errorf("error while decoding base64: %w", err) 218 + } 219 + 220 + b = decoded 221 + 222 + } 223 + } 224 + 225 + req, err := http.NewRequestWithContext(ctx, inputData.Method, inputData.URL, bytes.NewReader(b)) 187 226 if err != nil { 188 227 logger.Error().Err(err).Msg("error while creating req") 189 228 return Response{}, fmt.Errorf("unable to create req: %w", err) ··· 201 240 // by default we set the content type to application/json if it's a POST request 202 241 req.Header.Set("Content-Type", "application/json") 203 242 } 243 + // if the content type is octet-stream, we need to set the body as bytes 244 + 204 245 } 205 246 206 247 timing := Timing{}