2022-02-10 22:06:57 +00:00
|
|
|
//go:build !solution
|
2020-02-13 16:00:29 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2024-06-01 23:47:30 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-06-03 17:25:40 +00:00
|
|
|
type HTTPGetResult struct {
|
2024-06-01 23:47:30 +00:00
|
|
|
Success bool
|
|
|
|
Body string
|
|
|
|
Elapsed time.Duration
|
|
|
|
Size int
|
2024-06-03 17:25:40 +00:00
|
|
|
URL string
|
2024-06-01 23:47:30 +00:00
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2024-06-03 17:25:40 +00:00
|
|
|
func (result HTTPGetResult) String() string {
|
2024-06-01 23:47:30 +00:00
|
|
|
if result.Success {
|
2024-06-03 17:25:40 +00:00
|
|
|
return fmt.Sprintf("%v\t%v\t%v", result.Elapsed, result.Size, result.URL)
|
2024-06-01 23:47:30 +00:00
|
|
|
} else {
|
|
|
|
return result.Error.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 23:15:25 +00:00
|
|
|
func GetHTTPBody(url string, ch chan string) {
|
2024-06-01 23:47:30 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
2024-06-05 23:15:25 +00:00
|
|
|
ch <- fmt.Sprint(err)
|
2024-06-01 23:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2024-06-05 23:15:25 +00:00
|
|
|
n, err := io.Copy(io.Discard, resp.Body)
|
2024-06-01 23:47:30 +00:00
|
|
|
if err != nil {
|
2024-06-05 23:15:25 +00:00
|
|
|
ch <- fmt.Sprint(err)
|
2024-06-01 23:47:30 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-05 23:15:25 +00:00
|
|
|
ch <- fmt.Sprintf("%.2fs\t%v\t%v", time.Since(startTime).Seconds(), n, url)
|
2024-06-01 23:47:30 +00:00
|
|
|
}
|
2020-02-13 16:00:29 +00:00
|
|
|
|
2024-06-01 23:47:30 +00:00
|
|
|
func main() {
|
|
|
|
startTime := time.Now()
|
2024-06-05 23:15:25 +00:00
|
|
|
urls, ch := os.Args[1:], make(chan string)
|
2024-06-01 23:47:30 +00:00
|
|
|
for _, url := range urls {
|
2024-06-03 17:25:40 +00:00
|
|
|
go GetHTTPBody(url, ch)
|
2024-06-01 23:47:30 +00:00
|
|
|
}
|
2024-06-05 23:15:25 +00:00
|
|
|
for range urls {
|
2024-06-01 23:47:30 +00:00
|
|
|
fmt.Println(<-ch)
|
|
|
|
}
|
2024-06-05 23:15:25 +00:00
|
|
|
fmt.Printf("%.2fs elapsed\n", time.Since(startTime).Seconds())
|
2020-02-13 16:00:29 +00:00
|
|
|
}
|