Skip to content

Commit

Permalink
Improve URL handling (#183)
Browse files Browse the repository at this point in the history
fix: fixes the path for latency.txt and improves general URL handling.
  • Loading branch information
eric committed Apr 24, 2024
1 parent 156e462 commit f728f84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 9 additions & 3 deletions speedtest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"math"
"net/http"
"net/url"
"strconv"
"path"
"strings"
"time"

Expand Down Expand Up @@ -139,7 +139,12 @@ func (s *Server) uploadTestContext(ctx context.Context, uploadRequest uploadFunc

func downloadRequest(ctx context.Context, s *Server, w int) error {
size := dlSizes[w]
xdlURL := strings.Split(s.URL, "/upload.php")[0] + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
u, err := url.Parse(s.URL)
if err != nil {
return err
}
u.Path = path.Dir(u.Path)
xdlURL := u.JoinPath(fmt.Sprintf("random%dx%d.jpg", size, size)).String()
dbg.Printf("XdlURL: %s\n", xdlURL)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, xdlURL, nil)
if err != nil {
Expand Down Expand Up @@ -267,7 +272,8 @@ func (s *Server) HTTPPing(
if err != nil || len(u.Host) == 0 {
return nil, err
}
pingDst := fmt.Sprintf("%s/latency.txt", s.URL)
u.Path = path.Dir(u.Path)
pingDst := u.JoinPath("latency.txt").String()
dbg.Printf("Echo: %s\n", pingDst)
failTimes := 0
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pingDst, nil)
Expand Down
7 changes: 4 additions & 3 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"net/http"
"net/url"
"path"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -71,13 +72,13 @@ func CustomServer(host string) (*Server, error) {
// CustomServer given a URL string, return a new Server object, with as much
// filled in as we can
func (s *Speedtest) CustomServer(host string) (*Server, error) {
if !strings.HasSuffix(host, "/upload.php") {
return nil, errors.New("please use the full URL of the server, ending in '/upload.php'")
}
u, err := url.Parse(host)
if err != nil {
return nil, err
}
if path.Base(u.Path) != "upload.php" {
return nil, errors.New("please use the full URL of the server, ending in '/upload.php'")
}
return &Server{
ID: "Custom",
Lat: "?",
Expand Down

0 comments on commit f728f84

Please sign in to comment.