Add NewRequestFromURL function
This commit is contained in:
parent
c44f011b15
commit
8ab4064841
|
@ -7,6 +7,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -76,21 +77,23 @@ func sendRequest(req *gmi.Request) error {
|
||||||
case gmi.StatusClassInput:
|
case gmi.StatusClassInput:
|
||||||
fmt.Printf("%s: ", resp.Meta)
|
fmt.Printf("%s: ", resp.Meta)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
req.URL.RawQuery = scanner.Text()
|
req.URL.RawQuery = url.QueryEscape(scanner.Text())
|
||||||
return sendRequest(req)
|
return sendRequest(req)
|
||||||
case gmi.StatusClassSuccess:
|
case gmi.StatusClassSuccess:
|
||||||
fmt.Print(string(resp.Body))
|
fmt.Print(string(resp.Body))
|
||||||
return nil
|
return nil
|
||||||
case gmi.StatusClassRedirect:
|
case gmi.StatusClassRedirect:
|
||||||
fmt.Println("Redirecting to", resp.Meta)
|
fmt.Println("Redirecting to", resp.Meta)
|
||||||
// Make the request to the same host
|
target, err := url.Parse(resp.Meta)
|
||||||
red, err := gmi.NewRequestTo(resp.Meta, req.Host)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Handle relative redirects
|
// TODO: Prompt the user if the redirect is to another domain.
|
||||||
red.URL = req.URL.ResolveReference(red.URL)
|
redirect, err := gmi.NewRequestFromURL(req.URL.ResolveReference(target))
|
||||||
return sendRequest(red)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sendRequest(redirect)
|
||||||
case gmi.StatusClassTemporaryFailure:
|
case gmi.StatusClassTemporaryFailure:
|
||||||
return fmt.Errorf("Temporary failure: %s", resp.Meta)
|
return fmt.Errorf("Temporary failure: %s", resp.Meta)
|
||||||
case gmi.StatusClassPermanentFailure:
|
case gmi.StatusClassPermanentFailure:
|
||||||
|
|
13
request.go
13
request.go
|
@ -42,22 +42,27 @@ func hostname(host string) string {
|
||||||
return hostname
|
return hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRequest returns a new request. The host is inferred from the provided URL.
|
// NewRequest returns a new request. The host is inferred from the URL.
|
||||||
func NewRequest(rawurl string) (*Request, error) {
|
func NewRequest(rawurl string) (*Request, error) {
|
||||||
u, err := url.Parse(rawurl)
|
u, err := url.Parse(rawurl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
return NewRequestFromURL(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRequestFromURL returns a new request for the given URL.
|
||||||
|
// The host is inferred from the URL.
|
||||||
|
func NewRequestFromURL(url *url.URL) (*Request, error) {
|
||||||
// If there is no port, use the default port of 1965
|
// If there is no port, use the default port of 1965
|
||||||
host := u.Host
|
host := url.Host
|
||||||
if u.Port() == "" {
|
if url.Port() == "" {
|
||||||
host += ":1965"
|
host += ":1965"
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Request{
|
return &Request{
|
||||||
Host: host,
|
Host: host,
|
||||||
URL: u,
|
URL: url,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user