mirror of
https://github.com/golang/go
synced 2024-10-14 03:43:28 +00:00
net/url: API
Convert cryptotype to general go1rename fix. Add os.Exec -> syscall.Exec fix along with new URL fixes. Fixes #2946. R=golang-dev, r, dsymonds CC=golang-dev https://golang.org/cl/5672072
This commit is contained in:
parent
d8e715cab4
commit
b27bd42a9a
|
@ -5,10 +5,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
addTestCases(go1renameTests, go1pkgrename)
|
addTestCases(go1pkgrenameTests, go1pkgrename)
|
||||||
}
|
}
|
||||||
|
|
||||||
var go1renameTests = []testCase{
|
var go1pkgrenameTests = []testCase{
|
||||||
{
|
{
|
||||||
Name: "go1rename.0",
|
Name: "go1rename.0",
|
||||||
In: `package main
|
In: `package main
|
||||||
|
|
|
@ -4,17 +4,22 @@
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
var cryptotypeFix = fix{
|
func init() {
|
||||||
"cryptotype",
|
register(go1renameFix)
|
||||||
|
}
|
||||||
|
|
||||||
|
var go1renameFix = fix{
|
||||||
|
"go1rename",
|
||||||
"2012-02-12",
|
"2012-02-12",
|
||||||
renameFix(cryptotypeReplace),
|
renameFix(go1renameReplace),
|
||||||
`Rewrite uses of concrete cipher types to refer to the generic cipher.Block.
|
`Rewrite package-level names that have been renamed in Go 1.
|
||||||
|
|
||||||
http://codereview.appspot.com/5625045/
|
http://codereview.appspot.com/5625045/
|
||||||
|
http://codereview.appspot.com/5672072/
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
var cryptotypeReplace = []rename{
|
var go1renameReplace = []rename{
|
||||||
{
|
{
|
||||||
OldImport: "crypto/aes",
|
OldImport: "crypto/aes",
|
||||||
NewImport: "crypto/cipher",
|
NewImport: "crypto/cipher",
|
||||||
|
@ -33,4 +38,22 @@ var cryptotypeReplace = []rename{
|
||||||
Old: "*des.TripleDESCipher",
|
Old: "*des.TripleDESCipher",
|
||||||
New: "cipher.Block",
|
New: "cipher.Block",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
OldImport: "net/url",
|
||||||
|
NewImport: "",
|
||||||
|
Old: "url.ParseWithReference",
|
||||||
|
New: "url.Parse",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
OldImport: "net/url",
|
||||||
|
NewImport: "",
|
||||||
|
Old: "url.ParseRequest",
|
||||||
|
New: "url.ParseRequestURI",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
OldImport: "os",
|
||||||
|
NewImport: "syscall",
|
||||||
|
Old: "os.Exec",
|
||||||
|
New: "syscall.Exec",
|
||||||
|
},
|
||||||
}
|
}
|
|
@ -5,17 +5,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
addTestCases(cryptotypeTests, cryptotypeFix.f)
|
addTestCases(go1renameTests, go1renameFix.f)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cryptotypeTests = []testCase{
|
var go1renameTests = []testCase{
|
||||||
{
|
{
|
||||||
Name: "cryptotype.0",
|
Name: "go1rename.0",
|
||||||
In: `package main
|
In: `package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/des"
|
"crypto/des"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -23,6 +25,10 @@ var (
|
||||||
_ *des.Cipher
|
_ *des.Cipher
|
||||||
_ *des.TripleDESCipher
|
_ *des.TripleDESCipher
|
||||||
_ = aes.New()
|
_ = aes.New()
|
||||||
|
_ = url.Parse
|
||||||
|
_ = url.ParseWithReference
|
||||||
|
_ = url.ParseRequest
|
||||||
|
_ = os.Exec
|
||||||
)
|
)
|
||||||
`,
|
`,
|
||||||
Out: `package main
|
Out: `package main
|
||||||
|
@ -30,6 +36,8 @@ var (
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
|
"net/url"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -37,6 +45,10 @@ var (
|
||||||
_ cipher.Block
|
_ cipher.Block
|
||||||
_ cipher.Block
|
_ cipher.Block
|
||||||
_ = aes.New()
|
_ = aes.New()
|
||||||
|
_ = url.Parse
|
||||||
|
_ = url.Parse
|
||||||
|
_ = url.ParseRequestURI
|
||||||
|
_ = syscall.Exec
|
||||||
)
|
)
|
||||||
`,
|
`,
|
||||||
},
|
},
|
|
@ -186,7 +186,7 @@ func (r *Request) Cookies() []*Cookie {
|
||||||
return readCookies(r.Header, "")
|
return readCookies(r.Header, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrNoCookie = errors.New("http: named cookied not present")
|
var ErrNoCookie = errors.New("http: named cookie not present")
|
||||||
|
|
||||||
// Cookie returns the named cookie provided in the request or
|
// Cookie returns the named cookie provided in the request or
|
||||||
// ErrNoCookie if not found.
|
// ErrNoCookie if not found.
|
||||||
|
@ -486,7 +486,7 @@ func ReadRequest(b *bufio.Reader) (req *Request, err error) {
|
||||||
rawurl = "http://" + rawurl
|
rawurl = "http://" + rawurl
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.URL, err = url.ParseRequest(rawurl); err != nil {
|
if req.URL, err = url.ParseRequestURI(rawurl); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -321,19 +321,28 @@ func split(s string, c byte, cutc bool) (string, string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses rawurl into a URL structure.
|
// Parse parses rawurl into a URL structure.
|
||||||
// The string rawurl is assumed not to have a #fragment suffix.
|
|
||||||
// (Web browsers strip #fragment before sending the URL to a web server.)
|
|
||||||
// The rawurl may be relative or absolute.
|
// The rawurl may be relative or absolute.
|
||||||
func Parse(rawurl string) (url *URL, err error) {
|
func Parse(rawurl string) (url *URL, err error) {
|
||||||
return parse(rawurl, false)
|
// Cut off #frag
|
||||||
|
u, frag := split(rawurl, '#', true)
|
||||||
|
if url, err = parse(u, false); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if frag == "" {
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
if url.Fragment, err = unescape(frag, encodeFragment); err != nil {
|
||||||
|
return nil, &Error{"parse", rawurl, err}
|
||||||
|
}
|
||||||
|
return url, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseRequest parses rawurl into a URL structure. It assumes that
|
// ParseRequestURI parses rawurl into a URL structure. It assumes that
|
||||||
// rawurl was received from an HTTP request, so the rawurl is interpreted
|
// rawurl was received in an HTTP request, so the rawurl is interpreted
|
||||||
// only as an absolute URI or an absolute path.
|
// only as an absolute URI or an absolute path.
|
||||||
// The string rawurl is assumed not to have a #fragment suffix.
|
// The string rawurl is assumed not to have a #fragment suffix.
|
||||||
// (Web browsers strip #fragment before sending the URL to a web server.)
|
// (Web browsers strip #fragment before sending the URL to a web server.)
|
||||||
func ParseRequest(rawurl string) (url *URL, err error) {
|
func ParseRequestURI(rawurl string) (url *URL, err error) {
|
||||||
return parse(rawurl, true)
|
return parse(rawurl, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -415,22 +424,6 @@ func parseAuthority(authority string) (user *Userinfo, host string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseWithFragment is like Parse but allows a trailing #fragment.
|
|
||||||
func ParseWithFragment(rawurl string) (url *URL, err error) {
|
|
||||||
// Cut off #frag
|
|
||||||
u, frag := split(rawurl, '#', true)
|
|
||||||
if url, err = Parse(u); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if frag == "" {
|
|
||||||
return url, nil
|
|
||||||
}
|
|
||||||
if url.Fragment, err = unescape(frag, encodeFragment); err != nil {
|
|
||||||
return nil, &Error{"parse", rawurl, err}
|
|
||||||
}
|
|
||||||
return url, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// String reassembles the URL into a valid URL string.
|
// String reassembles the URL into a valid URL string.
|
||||||
func (u *URL) String() string {
|
func (u *URL) String() string {
|
||||||
// TODO: Rewrite to use bytes.Buffer
|
// TODO: Rewrite to use bytes.Buffer
|
||||||
|
|
|
@ -188,22 +188,6 @@ var urltests = []URLTest{
|
||||||
},
|
},
|
||||||
"http://user:password@google.com",
|
"http://user:password@google.com",
|
||||||
},
|
},
|
||||||
}
|
|
||||||
|
|
||||||
var urlnofragtests = []URLTest{
|
|
||||||
{
|
|
||||||
"http://www.google.com/?q=go+language#foo",
|
|
||||||
&URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: "www.google.com",
|
|
||||||
Path: "/",
|
|
||||||
RawQuery: "q=go+language#foo",
|
|
||||||
},
|
|
||||||
"",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var urlfragtests = []URLTest{
|
|
||||||
{
|
{
|
||||||
"http://www.google.com/?q=go+language#foo",
|
"http://www.google.com/?q=go+language#foo",
|
||||||
&URL{
|
&URL{
|
||||||
|
@ -257,12 +241,6 @@ func DoTest(t *testing.T, parse func(string) (*URL, error), name string, tests [
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
DoTest(t, Parse, "Parse", urltests)
|
DoTest(t, Parse, "Parse", urltests)
|
||||||
DoTest(t, Parse, "Parse", urlnofragtests)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseWithFragment(t *testing.T) {
|
|
||||||
DoTest(t, ParseWithFragment, "ParseWithFragment", urltests)
|
|
||||||
DoTest(t, ParseWithFragment, "ParseWithFragment", urlfragtests)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
|
const pathThatLooksSchemeRelative = "//not.a.user@not.a.host/just/a/path"
|
||||||
|
@ -281,16 +259,16 @@ var parseRequestUrlTests = []struct {
|
||||||
{"../dir/", false},
|
{"../dir/", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseRequest(t *testing.T) {
|
func TestParseRequestURI(t *testing.T) {
|
||||||
for _, test := range parseRequestUrlTests {
|
for _, test := range parseRequestUrlTests {
|
||||||
_, err := ParseRequest(test.url)
|
_, err := ParseRequestURI(test.url)
|
||||||
valid := err == nil
|
valid := err == nil
|
||||||
if valid != test.expectedValid {
|
if valid != test.expectedValid {
|
||||||
t.Errorf("Expected valid=%v for %q; got %v", test.expectedValid, test.url, valid)
|
t.Errorf("Expected valid=%v for %q; got %v", test.expectedValid, test.url, valid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
url, err := ParseRequest(pathThatLooksSchemeRelative)
|
url, err := ParseRequestURI(pathThatLooksSchemeRelative)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected error %v", err)
|
t.Fatalf("Unexpected error %v", err)
|
||||||
}
|
}
|
||||||
|
@ -319,9 +297,6 @@ func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, t
|
||||||
|
|
||||||
func TestURLString(t *testing.T) {
|
func TestURLString(t *testing.T) {
|
||||||
DoTestString(t, Parse, "Parse", urltests)
|
DoTestString(t, Parse, "Parse", urltests)
|
||||||
DoTestString(t, Parse, "Parse", urlnofragtests)
|
|
||||||
DoTestString(t, ParseWithFragment, "ParseWithFragment", urltests)
|
|
||||||
DoTestString(t, ParseWithFragment, "ParseWithFragment", urlfragtests)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EscapeTest struct {
|
type EscapeTest struct {
|
||||||
|
@ -538,7 +513,7 @@ var resolveReferenceTests = []struct {
|
||||||
|
|
||||||
func TestResolveReference(t *testing.T) {
|
func TestResolveReference(t *testing.T) {
|
||||||
mustParse := func(url string) *URL {
|
mustParse := func(url string) *URL {
|
||||||
u, err := ParseWithFragment(url)
|
u, err := Parse(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
|
t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
|
||||||
}
|
}
|
||||||
|
@ -589,7 +564,7 @@ func TestResolveReference(t *testing.T) {
|
||||||
|
|
||||||
func TestResolveReferenceOpaque(t *testing.T) {
|
func TestResolveReferenceOpaque(t *testing.T) {
|
||||||
mustParse := func(url string) *URL {
|
mustParse := func(url string) *URL {
|
||||||
u, err := ParseWithFragment(url)
|
u, err := Parse(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
|
t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue