gofmt-ify hash, http

(gofmt will be able to re-align map entries as in http nicely,
eventually)

R=rsc
http://go/go-review/1018055
This commit is contained in:
Robert Griesemer 2009-11-04 22:45:59 -08:00
parent b16e6ab148
commit ca2a69ea06
5 changed files with 157 additions and 160 deletions

View File

@ -21,7 +21,7 @@ const (
) )
// The size of an Adler-32 checksum in bytes. // The size of an Adler-32 checksum in bytes.
const Size = 4; const Size = 4
// digest represents the partial evaluation of a checksum. // digest represents the partial evaluation of a checksum.
type digest struct { type digest struct {
@ -51,7 +51,7 @@ func update(a, b uint32, p []byte) (aa, bb uint32) {
a += uint32(p[i]); a += uint32(p[i]);
b += a; b += a;
// invariant: a <= b // invariant: a <= b
if b > (0xffffffff - 255) / 2 { if b > (0xffffffff - 255)/2 {
a %= mod; a %= mod;
b %= mod; b %= mod;
// invariant: a < mod && b < mod // invariant: a < mod && b < mod

View File

@ -16,14 +16,14 @@ import (
) )
// TODO this should be in a mime package somewhere // TODO this should be in a mime package somewhere
var contentByExt = map[string] string { var contentByExt = map[string]string{
".css": "text/css", ".css": "text/css",
".gif": "image/gif", ".gif": "image/gif",
".html": "text/html; charset=utf-8", ".html": "text/html; charset=utf-8",
".jpg": "image/jpeg", ".jpg": "image/jpeg",
".js": "application/x-javascript", ".js": "application/x-javascript",
".pdf": "application/pdf", ".pdf": "application/pdf",
".png": "image/png", ".png": "image/png",
} }
// Heuristic: b is text if it is valid UTF-8 and doesn't // Heuristic: b is text if it is valid UTF-8 and doesn't
@ -57,12 +57,12 @@ func dirList(c *Conn, f *os.File) {
for { for {
dirs, err := f.Readdir(100); dirs, err := f.Readdir(100);
if err != nil || len(dirs) == 0 { if err != nil || len(dirs) == 0 {
break break;
} }
for _, d := range dirs { for _, d := range dirs {
name := d.Name; name := d.Name;
if d.IsDirectory() { if d.IsDirectory() {
name += "/" name += "/";
} }
// TODO htmlescape // TODO htmlescape
fmt.Fprintf(c, "<a href=\"%s\">%s</a>\n", name, name); fmt.Fprintf(c, "<a href=\"%s\">%s</a>\n", name, name);
@ -76,9 +76,9 @@ func serveFileInternal(c *Conn, r *Request, name string, redirect bool) {
const indexPage = "/index.html"; const indexPage = "/index.html";
// redirect to strip off any index.html // redirect to strip off any index.html
n := len(name) - len(indexPage); n := len(name)-len(indexPage);
if n >= 0 && name[n:len(name)] == indexPage { if n >= 0 && name[n:len(name)] == indexPage {
Redirect(c, name[0:n+1], StatusMovedPermanently); Redirect(c, name[0 : n+1], StatusMovedPermanently);
return; return;
} }
@ -103,12 +103,12 @@ func serveFileInternal(c *Conn, r *Request, name string, redirect bool) {
url := r.Url.Path; url := r.Url.Path;
if d.IsDirectory() { if d.IsDirectory() {
if url[len(url)-1] != '/' { if url[len(url)-1] != '/' {
Redirect(c, url + "/", StatusMovedPermanently); Redirect(c, url+"/", StatusMovedPermanently);
return; return;
} }
} else { } else {
if url[len(url)-1] == '/' { if url[len(url)-1] == '/' {
Redirect(c, url[0:len(url)-1], StatusMovedPermanently); Redirect(c, url[0 : len(url)-1], StatusMovedPermanently);
return; return;
} }
} }
@ -160,8 +160,8 @@ func ServeFile(c *Conn, r *Request, name string) {
} }
type fileHandler struct { type fileHandler struct {
root string; root string;
prefix string; prefix string;
} }
// FileServer returns a handler that serves HTTP requests // FileServer returns a handler that serves HTTP requests
@ -181,4 +181,3 @@ func (f *fileHandler) ServeHTTP(c *Conn, r *Request) {
path = path[len(f.prefix):len(path)]; path = path[len(f.prefix):len(path)];
serveFileInternal(c, r, f.root + "/" + path, true); serveFileInternal(c, r, f.root + "/" + path, true);
} }

View File

@ -103,8 +103,8 @@ func TestPostContentTypeParsing(t *testing.T) {
func TestRedirect(t *testing.T) { func TestRedirect(t *testing.T) {
const ( const (
start = "http://codesearch.google.com/"; start = "http://codesearch.google.com/";
end = "http://www.google.com/codesearch"; end = "http://www.google.com/codesearch";
) )
r, url, err := Get(start); r, url, err := Get(start);
if err != nil { if err != nil {
@ -112,6 +112,6 @@ func TestRedirect(t *testing.T) {
} }
r.Body.Close(); r.Body.Close();
if r.StatusCode != 200 || url != end { if r.StatusCode != 200 || url != end {
t.Fatalf("Get(%s) got status %d at %s, want 200 at %s", start, r.StatusCode, url, end) t.Fatalf("Get(%s) got status %d at %s, want 200 at %s", start, r.StatusCode, url, end);
} }
} }

View File

@ -6,96 +6,95 @@ package http
// HTTP status codes, defined in RFC 2616. // HTTP status codes, defined in RFC 2616.
const ( const (
StatusContinue = 100; StatusContinue = 100;
StatusSwitchingProtocols = 101; StatusSwitchingProtocols = 101;
StatusOK = 200; StatusOK = 200;
StatusCreated = 201; StatusCreated = 201;
StatusAccepted = 202; StatusAccepted = 202;
StatusNonAuthoritativeInfo = 203; StatusNonAuthoritativeInfo = 203;
StatusNoContent = 204; StatusNoContent = 204;
StatusResetContent = 205; StatusResetContent = 205;
StatusPartialContent = 206; StatusPartialContent = 206;
StatusMultipleChoices = 300; StatusMultipleChoices = 300;
StatusMovedPermanently = 301; StatusMovedPermanently = 301;
StatusFound = 302; StatusFound = 302;
StatusSeeOther = 303; StatusSeeOther = 303;
StatusNotModified = 304; StatusNotModified = 304;
StatusUseProxy = 305; StatusUseProxy = 305;
StatusTemporaryRedirect = 307; StatusTemporaryRedirect = 307;
StatusBadRequest = 400; StatusBadRequest = 400;
StatusUnauthorized = 401; StatusUnauthorized = 401;
StatusPaymentRequired = 402; StatusPaymentRequired = 402;
StatusForbidden = 403; StatusForbidden = 403;
StatusNotFound = 404; StatusNotFound = 404;
StatusMethodNotAllowed = 405; StatusMethodNotAllowed = 405;
StatusNotAcceptable = 406; StatusNotAcceptable = 406;
StatusProxyAuthRequired = 407; StatusProxyAuthRequired = 407;
StatusRequestTimeout = 408; StatusRequestTimeout = 408;
StatusConflict = 409; StatusConflict = 409;
StatusGone = 410; StatusGone = 410;
StatusLengthRequired = 411; StatusLengthRequired = 411;
StatusPreconditionFailed = 412; StatusPreconditionFailed = 412;
StatusRequestEntityTooLarge = 413; StatusRequestEntityTooLarge = 413;
StatusRequestURITooLong = 414; StatusRequestURITooLong = 414;
StatusUnsupportedMediaType = 415; StatusUnsupportedMediaType = 415;
StatusRequestedRangeNotSatisfiable = 416; StatusRequestedRangeNotSatisfiable = 416;
StatusExpectationFailed = 417; StatusExpectationFailed = 417;
StatusInternalServerError = 500; StatusInternalServerError = 500;
StatusNotImplemented = 501; StatusNotImplemented = 501;
StatusBadGateway = 502; StatusBadGateway = 502;
StatusServiceUnavailable = 503; StatusServiceUnavailable = 503;
StatusGatewayTimeout = 504; StatusGatewayTimeout = 504;
StatusHTTPVersionNotSupported = 505; StatusHTTPVersionNotSupported = 505;
) )
var statusText = map[int]string { var statusText = map[int]string{
StatusContinue: "Continue", StatusContinue: "Continue",
StatusSwitchingProtocols: "Switching Protocols", StatusSwitchingProtocols: "Switching Protocols",
StatusOK: "OK", StatusOK: "OK",
StatusCreated: "Created", StatusCreated: "Created",
StatusAccepted: "Accepted", StatusAccepted: "Accepted",
StatusNonAuthoritativeInfo: "Non-Authoritative Information", StatusNonAuthoritativeInfo: "Non-Authoritative Information",
StatusNoContent: "No Content", StatusNoContent: "No Content",
StatusResetContent: "Reset Content", StatusResetContent: "Reset Content",
StatusPartialContent: "Partial Content", StatusPartialContent: "Partial Content",
StatusMultipleChoices: "Multiple Choices", StatusMultipleChoices: "Multiple Choices",
StatusMovedPermanently: "Moved Permanently", StatusMovedPermanently: "Moved Permanently",
StatusFound: "Found", StatusFound: "Found",
StatusSeeOther: "See Other", StatusSeeOther: "See Other",
StatusNotModified: "Not Modified", StatusNotModified: "Not Modified",
StatusUseProxy: "Use Proxy", StatusUseProxy: "Use Proxy",
StatusTemporaryRedirect: "Temporary Redirect", StatusTemporaryRedirect: "Temporary Redirect",
StatusBadRequest: "Bad Request", StatusBadRequest: "Bad Request",
StatusUnauthorized: "Unauthorized", StatusUnauthorized: "Unauthorized",
StatusPaymentRequired: "Payment Required", StatusPaymentRequired: "Payment Required",
StatusForbidden: "Forbidden", StatusForbidden: "Forbidden",
StatusNotFound: "Not Found", StatusNotFound: "Not Found",
StatusMethodNotAllowed: "Method Not Allowed", StatusMethodNotAllowed: "Method Not Allowed",
StatusNotAcceptable: "Not Acceptable", StatusNotAcceptable: "Not Acceptable",
StatusProxyAuthRequired: "Proxy Authentication Required", StatusProxyAuthRequired: "Proxy Authentication Required",
StatusRequestTimeout: "Request Timeout", StatusRequestTimeout: "Request Timeout",
StatusConflict: "Conflict", StatusConflict: "Conflict",
StatusGone: "Gone", StatusGone: "Gone",
StatusLengthRequired: "Length Required", StatusLengthRequired: "Length Required",
StatusPreconditionFailed: "Precondition Failed", StatusPreconditionFailed: "Precondition Failed",
StatusRequestEntityTooLarge: "Request Entity Too Large", StatusRequestEntityTooLarge: "Request Entity Too Large",
StatusRequestURITooLong: "Request URI Too Long", StatusRequestURITooLong: "Request URI Too Long",
StatusUnsupportedMediaType: "Unsupported Media Type", StatusUnsupportedMediaType: "Unsupported Media Type",
StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable", StatusRequestedRangeNotSatisfiable: "Requested Range Not Satisfiable",
StatusExpectationFailed: "Expectation Failed", StatusExpectationFailed: "Expectation Failed",
StatusInternalServerError: "Internal Server Error", StatusInternalServerError: "Internal Server Error",
StatusNotImplemented: "Not Implemented", StatusNotImplemented: "Not Implemented",
StatusBadGateway: "Bad Gateway", StatusBadGateway: "Bad Gateway",
StatusServiceUnavailable: "Service Unavailable", StatusServiceUnavailable: "Service Unavailable",
StatusGatewayTimeout: "Gateway Timeout", StatusGatewayTimeout: "Gateway Timeout",
StatusHTTPVersionNotSupported: "HTTP Version Not Supported", StatusHTTPVersionNotSupported: "HTTP Version Not Supported",
} }

View File

@ -17,12 +17,12 @@ import (
// test ParseURL // test ParseURL
type URLTest struct { type URLTest struct {
in string; in string;
out *URL; out *URL;
roundtrip string; // expected result of reserializing the URL; empty means same as "in". roundtrip string; // expected result of reserializing the URL; empty means same as "in".
} }
var urltests = []URLTest { var urltests = []URLTest{
// no path // no path
URLTest{ URLTest{
"http://www.google.com", "http://www.google.com",
@ -30,9 +30,9 @@ var urltests = []URLTest {
"http://www.google.com", "http://www.google.com",
"http", "//www.google.com", "http", "//www.google.com",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"", "", "" "", "", "",
}, },
"" "",
}, },
// path // path
URLTest{ URLTest{
@ -41,9 +41,9 @@ var urltests = []URLTest {
"http://www.google.com/", "http://www.google.com/",
"http", "//www.google.com/", "http", "//www.google.com/",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/", "", "" "/", "", "",
}, },
"" "",
}, },
// path with hex escaping... note that space roundtrips to + // path with hex escaping... note that space roundtrips to +
URLTest{ URLTest{
@ -52,9 +52,9 @@ var urltests = []URLTest {
"http://www.google.com/file%20one%26two", "http://www.google.com/file%20one%26two",
"http", "//www.google.com/file%20one%26two", "http", "//www.google.com/file%20one%26two",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/file one&two", "", "" "/file one&two", "", "",
}, },
"http://www.google.com/file+one%26two" "http://www.google.com/file+one%26two",
}, },
// user // user
URLTest{ URLTest{
@ -63,9 +63,9 @@ var urltests = []URLTest {
"ftp://webmaster@www.google.com/", "ftp://webmaster@www.google.com/",
"ftp", "//webmaster@www.google.com/", "ftp", "//webmaster@www.google.com/",
"webmaster@www.google.com", "webmaster", "www.google.com", "webmaster@www.google.com", "webmaster", "www.google.com",
"/", "", "" "/", "", "",
}, },
"" "",
}, },
// escape sequence in username // escape sequence in username
URLTest{ URLTest{
@ -74,9 +74,9 @@ var urltests = []URLTest {
"ftp://john%20doe@www.google.com/", "ftp://john%20doe@www.google.com/",
"ftp", "//john%20doe@www.google.com/", "ftp", "//john%20doe@www.google.com/",
"john doe@www.google.com", "john doe", "www.google.com", "john doe@www.google.com", "john doe", "www.google.com",
"/", "", "" "/", "", "",
}, },
"ftp://john+doe@www.google.com/" "ftp://john+doe@www.google.com/",
}, },
// query // query
URLTest{ URLTest{
@ -85,9 +85,9 @@ var urltests = []URLTest {
"http://www.google.com/?q=go+language", "http://www.google.com/?q=go+language",
"http", "//www.google.com/?q=go+language", "http", "//www.google.com/?q=go+language",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/", "q=go+language", "" "/", "q=go+language", "",
}, },
"" "",
}, },
// query with hex escaping: NOT parsed // query with hex escaping: NOT parsed
URLTest{ URLTest{
@ -96,9 +96,9 @@ var urltests = []URLTest {
"http://www.google.com/?q=go%20language", "http://www.google.com/?q=go%20language",
"http", "//www.google.com/?q=go%20language", "http", "//www.google.com/?q=go%20language",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/", "q=go%20language", "" "/", "q=go%20language", "",
}, },
"" "",
}, },
// path without /, so no query parsing // path without /, so no query parsing
URLTest{ URLTest{
@ -107,9 +107,9 @@ var urltests = []URLTest {
"http:www.google.com/?q=go+language", "http:www.google.com/?q=go+language",
"http", "www.google.com/?q=go+language", "http", "www.google.com/?q=go+language",
"", "", "", "", "", "",
"www.google.com/?q=go language", "", "" "www.google.com/?q=go language", "", "",
}, },
"http:www.google.com/%3fq%3dgo+language" "http:www.google.com/%3fq%3dgo+language",
}, },
// non-authority // non-authority
URLTest{ URLTest{
@ -118,9 +118,9 @@ var urltests = []URLTest {
"mailto:/webmaster@golang.org", "mailto:/webmaster@golang.org",
"mailto", "/webmaster@golang.org", "mailto", "/webmaster@golang.org",
"", "", "", "", "", "",
"/webmaster@golang.org", "", "" "/webmaster@golang.org", "", "",
}, },
"" "",
}, },
// non-authority // non-authority
URLTest{ URLTest{
@ -129,9 +129,9 @@ var urltests = []URLTest {
"mailto:webmaster@golang.org", "mailto:webmaster@golang.org",
"mailto", "webmaster@golang.org", "mailto", "webmaster@golang.org",
"", "", "", "", "", "",
"webmaster@golang.org", "", "" "webmaster@golang.org", "", "",
}, },
"" "",
}, },
// unescaped :// in query should not create a scheme // unescaped :// in query should not create a scheme
URLTest{ URLTest{
@ -140,35 +140,35 @@ var urltests = []URLTest {
"/foo?query=http://bad", "/foo?query=http://bad",
"", "/foo?query=http://bad", "", "/foo?query=http://bad",
"", "", "", "", "", "",
"/foo", "query=http://bad", "" "/foo", "query=http://bad", "",
}, },
"" "",
}, },
} }
var urlnofragtests = []URLTest { var urlnofragtests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/?q=go+language#foo", "http://www.google.com/?q=go+language#foo",
&URL{ &URL{
"http://www.google.com/?q=go+language#foo", "http://www.google.com/?q=go+language#foo",
"http", "//www.google.com/?q=go+language#foo", "http", "//www.google.com/?q=go+language#foo",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/", "q=go+language#foo", "" "/", "q=go+language#foo", "",
}, },
"" "",
}, },
} }
var urlfragtests = []URLTest { var urlfragtests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/?q=go+language#foo", "http://www.google.com/?q=go+language#foo",
&URL{ &URL{
"http://www.google.com/?q=go+language", "http://www.google.com/?q=go+language",
"http", "//www.google.com/?q=go+language", "http", "//www.google.com/?q=go+language",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/", "q=go+language", "foo" "/", "q=go+language", "foo",
}, },
"" "",
}, },
URLTest{ URLTest{
"http://www.google.com/?q=go+language#foo%26bar", "http://www.google.com/?q=go+language#foo%26bar",
@ -176,9 +176,9 @@ var urlfragtests = []URLTest {
"http://www.google.com/?q=go+language", "http://www.google.com/?q=go+language",
"http", "//www.google.com/?q=go+language", "http", "//www.google.com/?q=go+language",
"www.google.com", "", "www.google.com", "www.google.com", "", "www.google.com",
"/", "q=go+language", "foo&bar" "/", "q=go+language", "foo&bar",
}, },
"" "",
}, },
} }
@ -241,64 +241,64 @@ func TestURLString(t *testing.T) {
} }
type URLEscapeTest struct { type URLEscapeTest struct {
in string; in string;
out string; out string;
err os.Error; err os.Error;
} }
var unescapeTests = []URLEscapeTest { var unescapeTests = []URLEscapeTest{
URLEscapeTest{ URLEscapeTest{
"", "",
"", "",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"abc", "abc",
"abc", "abc",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"1%41", "1%41",
"1A", "1A",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"1%41%42%43", "1%41%42%43",
"1ABC", "1ABC",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"%4a", "%4a",
"J", "J",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"%6F", "%6F",
"o", "o",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"%", // not enough characters after % "%", // not enough characters after %
"", "",
URLEscapeError("%"), URLEscapeError("%"),
}, },
URLEscapeTest{ URLEscapeTest{
"%a", // not enough characters after % "%a", // not enough characters after %
"", "",
URLEscapeError("%a"), URLEscapeError("%a"),
}, },
URLEscapeTest{ URLEscapeTest{
"%1", // not enough characters after % "%1", // not enough characters after %
"", "",
URLEscapeError("%1"), URLEscapeError("%1"),
}, },
URLEscapeTest{ URLEscapeTest{
"123%45%6", // not enough characters after % "123%45%6", // not enough characters after %
"", "",
URLEscapeError("%6"), URLEscapeError("%6"),
}, },
URLEscapeTest{ URLEscapeTest{
"%zzzzz", // invalid hex digits "%zzzzz", // invalid hex digits
"", "",
URLEscapeError("%zz"), URLEscapeError("%zz"),
}, },
@ -313,31 +313,31 @@ func TestURLUnescape(t *testing.T) {
} }
} }
var escapeTests = []URLEscapeTest { var escapeTests = []URLEscapeTest{
URLEscapeTest{ URLEscapeTest{
"", "",
"", "",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"abc", "abc",
"abc", "abc",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"one two", "one two",
"one+two", "one+two",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
"10%", "10%",
"10%25", "10%25",
nil nil,
}, },
URLEscapeTest{ URLEscapeTest{
" ?&=#+%!", " ?&=#+%!",
"+%3f%26%3d%23%2b%25!", "+%3f%26%3d%23%2b%25!",
nil nil,
}, },
} }
@ -355,4 +355,3 @@ func TestURLEscape(t *testing.T) {
} }
} }
} }