diff --git a/dist/ttfb-go b/dist/ttfb-go index 79abe67..02871b0 100755 Binary files a/dist/ttfb-go and b/dist/ttfb-go differ diff --git a/dist/ttfb-go_darwin_amd64 b/dist/ttfb-go_darwin_amd64 index e79dc61..40010fc 100755 Binary files a/dist/ttfb-go_darwin_amd64 and b/dist/ttfb-go_darwin_amd64 differ diff --git a/dist/ttfb-go_darwin_arm64 b/dist/ttfb-go_darwin_arm64 index ac4ba57..be2f1f5 100755 Binary files a/dist/ttfb-go_darwin_arm64 and b/dist/ttfb-go_darwin_arm64 differ diff --git a/dist/ttfb-go_linux_arm b/dist/ttfb-go_linux_arm index cac03c5..01416ca 100755 Binary files a/dist/ttfb-go_linux_arm and b/dist/ttfb-go_linux_arm differ diff --git a/dist/ttfb-go_linux_arm64 b/dist/ttfb-go_linux_arm64 index b262fc3..2038048 100755 Binary files a/dist/ttfb-go_linux_arm64 and b/dist/ttfb-go_linux_arm64 differ diff --git a/dist/ttfb-go_linux_arm64_static b/dist/ttfb-go_linux_arm64_static index 1c6c92f..4c39f3c 100755 Binary files a/dist/ttfb-go_linux_arm64_static and b/dist/ttfb-go_linux_arm64_static differ diff --git a/dist/ttfb-go_linux_arm_static b/dist/ttfb-go_linux_arm_static index 365530c..bc642d1 100755 Binary files a/dist/ttfb-go_linux_arm_static and b/dist/ttfb-go_linux_arm_static differ diff --git a/dist/ttfb-go_static b/dist/ttfb-go_static index 21a51af..c2eac76 100755 Binary files a/dist/ttfb-go_static and b/dist/ttfb-go_static differ diff --git a/main.go b/main.go index 31e1294..e902ee6 100644 --- a/main.go +++ b/main.go @@ -30,42 +30,28 @@ func MeasureTTFB(url string, cookie string) (int64, int, error) { // If a cookie is provided, set it in the request header. if cookie != "" { + // Clean up and format the cookie properly + cookie = strings.TrimSpace(cookie) + if !strings.Contains(cookie, "=") { + return 0, 0, fmt.Errorf("invalid cookie format") + } req.Header.Set("Cookie", cookie) } - // Create a channel to capture the time to first byte - ttfbChan := make(chan int64) - errorChan := make(chan error) - statusCodeChan := make(chan int) - - // Record the start time + // Record the start time. start := time.Now() - go func() { - // Perform the request - resp, err := client.Do(req) - if err != nil { - errorChan <- err - return - } - defer resp.Body.Close() - - // Measure the time taken to receive the first byte - ttfb := time.Since(start).Milliseconds() - ttfbChan <- ttfb - statusCodeChan <- resp.StatusCode - - // Read the body to ensure we handle the response properly - _, _ = ioutil.ReadAll(resp.Body) - }() - - select { - case ttfb := <-ttfbChan: - statusCode := <-statusCodeChan - return ttfb, statusCode, nil - case err := <-errorChan: + // Perform the request. + resp, err := client.Do(req) + if err != nil { return 0, 0, err } + defer resp.Body.Close() + + // Measure the time taken to receive the first byte. + ttfb := time.Since(start).Milliseconds() + + return ttfb, resp.StatusCode, nil } func main() {