Skip to content

Commit

Permalink
net/url: API
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsc committed Feb 17, 2012
1 parent d8e715c commit b27bd42
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/cmd/fix/go1pkgrename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package main

func init() {
addTestCases(go1renameTests, go1pkgrename)
addTestCases(go1pkgrenameTests, go1pkgrename)
}

var go1renameTests = []testCase{
var go1pkgrenameTests = []testCase{
{
Name: "go1rename.0",
In: `package main
Expand Down
33 changes: 28 additions & 5 deletions src/cmd/fix/cryptotype.go → src/cmd/fix/go1rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

package main

var cryptotypeFix = fix{
"cryptotype",
func init() {
register(go1renameFix)
}

var go1renameFix = fix{
"go1rename",
"2012-02-12",
renameFix(cryptotypeReplace),
`Rewrite uses of concrete cipher types to refer to the generic cipher.Block.
renameFix(go1renameReplace),
`Rewrite package-level names that have been renamed in Go 1.
http:https://codereview.appspot.com/5625045/
http:https://codereview.appspot.com/5672072/
`,
}

var cryptotypeReplace = []rename{
var go1renameReplace = []rename{
{
OldImport: "crypto/aes",
NewImport: "crypto/cipher",
Expand All @@ -33,4 +38,22 @@ var cryptotypeReplace = []rename{
Old: "*des.TripleDESCipher",
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",
},
}
18 changes: 15 additions & 3 deletions src/cmd/fix/cryptotype_test.go → src/cmd/fix/go1rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,50 @@
package main

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
import (
"crypto/aes"
"crypto/des"
"net/url"
"os"
)
var (
_ *aes.Cipher
_ *des.Cipher
_ *des.TripleDESCipher
_ = aes.New()
_ = url.Parse
_ = url.ParseWithReference
_ = url.ParseRequest
_ = os.Exec
)
`,
Out: `package main
import (
"crypto/aes"
"crypto/cipher"
"net/url"
"syscall"
)
var (
_ cipher.Block
_ cipher.Block
_ cipher.Block
_ = aes.New()
_ = url.Parse
_ = url.Parse
_ = url.ParseRequestURI
_ = syscall.Exec
)
`,
},
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/net/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (r *Request) Cookies() []*Cookie {
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
// ErrNoCookie if not found.
Expand Down Expand Up @@ -486,7 +486,7 @@ func ReadRequest(b *bufio.Reader) (req *Request, err error) {
rawurl = "http:https://" + rawurl
}

if req.URL, err = url.ParseRequest(rawurl); err != nil {
if req.URL, err = url.ParseRequestURI(rawurl); err != nil {
return nil, err
}

Expand Down
37 changes: 15 additions & 22 deletions src/pkg/net/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,28 @@ func split(s string, c byte, cutc bool) (string, string) {
}

// 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.
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
// rawurl was received from an HTTP request, so the rawurl is interpreted
// ParseRequestURI parses rawurl into a URL structure. It assumes that
// rawurl was received in an HTTP request, so the rawurl is interpreted
// only as an absolute URI or an absolute path.
// The string rawurl is assumed not to have a #fragment suffix.
// (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)
}

Expand Down Expand Up @@ -415,22 +424,6 @@ func parseAuthority(authority string) (user *Userinfo, host string, err error) {
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.
func (u *URL) String() string {
// TODO: Rewrite to use bytes.Buffer
Expand Down
35 changes: 5 additions & 30 deletions src/pkg/net/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,6 @@ var urltests = []URLTest{
},
"http:https://user:[email protected]",
},
}

var urlnofragtests = []URLTest{
{
"http:https://www.google.com/?q=go+language#foo",
&URL{
Scheme: "http",
Host: "www.google.com",
Path: "/",
RawQuery: "q=go+language#foo",
},
"",
},
}

var urlfragtests = []URLTest{
{
"http:https://www.google.com/?q=go+language#foo",
&URL{
Expand Down Expand Up @@ -257,12 +241,6 @@ func DoTest(t *testing.T, parse func(string) (*URL, error), name string, tests [

func TestParse(t *testing.T) {
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 = "//[email protected]/just/a/path"
Expand All @@ -281,16 +259,16 @@ var parseRequestUrlTests = []struct {
{"../dir/", false},
}

func TestParseRequest(t *testing.T) {
func TestParseRequestURI(t *testing.T) {
for _, test := range parseRequestUrlTests {
_, err := ParseRequest(test.url)
_, err := ParseRequestURI(test.url)
valid := err == nil
if valid != test.expectedValid {
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 {
t.Fatalf("Unexpected error %v", err)
}
Expand Down Expand Up @@ -319,9 +297,6 @@ func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, t

func TestURLString(t *testing.T) {
DoTestString(t, Parse, "Parse", urltests)
DoTestString(t, Parse, "Parse", urlnofragtests)
DoTestString(t, ParseWithFragment, "ParseWithFragment", urltests)
DoTestString(t, ParseWithFragment, "ParseWithFragment", urlfragtests)
}

type EscapeTest struct {
Expand Down Expand Up @@ -538,7 +513,7 @@ var resolveReferenceTests = []struct {

func TestResolveReference(t *testing.T) {
mustParse := func(url string) *URL {
u, err := ParseWithFragment(url)
u, err := Parse(url)
if err != nil {
t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
}
Expand Down Expand Up @@ -589,7 +564,7 @@ func TestResolveReference(t *testing.T) {

func TestResolveReferenceOpaque(t *testing.T) {
mustParse := func(url string) *URL {
u, err := ParseWithFragment(url)
u, err := Parse(url)
if err != nil {
t.Fatalf("Expected URL to parse: %q, got error: %v", url, err)
}
Expand Down

0 comments on commit b27bd42

Please sign in to comment.