Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
5d41f55a
Commit
5d41f55a
authored
Jan 16, 2009
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
casify http
R=r DELTA=33 (0 added, 0 deleted, 33 changed) OCL=22947 CL=22949
parent
c840657f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
33 deletions
+33
-33
request.go
src/lib/http/request.go
+18
-18
server.go
src/lib/http/server.go
+2
-2
url.go
src/lib/http/url.go
+13
-13
No files found.
src/lib/http/request.go
View file @
5d41f55a
...
@@ -14,9 +14,9 @@ import (
...
@@ -14,9 +14,9 @@ import (
)
)
const
(
const
(
MaxLineLength
=
1024
;
// assumed < bufio.DefaultBufSize
_
MaxLineLength
=
1024
;
// assumed < bufio.DefaultBufSize
MaxValueLength
=
1024
;
_
MaxValueLength
=
1024
;
MaxHeaderLines
=
1024
;
_
MaxHeaderLines
=
1024
;
)
)
export
var
(
export
var
(
...
@@ -46,14 +46,14 @@ export type Request struct {
...
@@ -46,14 +46,14 @@ export type Request struct {
}
}
// Read a line of bytes (up to \n) from b.
// Read a line of bytes (up to \n) from b.
// Give up if the line exceeds MaxLineLength.
// Give up if the line exceeds
_
MaxLineLength.
// The returned bytes are a pointer into storage in
// The returned bytes are a pointer into storage in
// the bufio, so they are only valid until the next bufio read.
// the bufio, so they are only valid until the next bufio read.
func
R
eadLineBytes
(
b
*
bufio
.
BufRead
)
(
p
[]
byte
,
err
*
os
.
Error
)
{
func
r
eadLineBytes
(
b
*
bufio
.
BufRead
)
(
p
[]
byte
,
err
*
os
.
Error
)
{
if
p
,
err
=
b
.
ReadLineSlice
(
'\n'
);
err
!=
nil
{
if
p
,
err
=
b
.
ReadLineSlice
(
'\n'
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
if
len
(
p
)
>=
MaxLineLength
{
if
len
(
p
)
>=
_
MaxLineLength
{
return
nil
,
LineTooLong
return
nil
,
LineTooLong
}
}
...
@@ -67,9 +67,9 @@ func ReadLineBytes(b *bufio.BufRead) (p []byte, err *os.Error) {
...
@@ -67,9 +67,9 @@ func ReadLineBytes(b *bufio.BufRead) (p []byte, err *os.Error) {
return
p
[
0
:
i
],
nil
return
p
[
0
:
i
],
nil
}
}
//
ReadLineByte
, but convert the bytes into a string.
//
readLineBytes
, but convert the bytes into a string.
func
R
eadLine
(
b
*
bufio
.
BufRead
)
(
s
string
,
err
*
os
.
Error
)
{
func
r
eadLine
(
b
*
bufio
.
BufRead
)
(
s
string
,
err
*
os
.
Error
)
{
p
,
e
:=
R
eadLineBytes
(
b
);
p
,
e
:=
r
eadLineBytes
(
b
);
if
e
!=
nil
{
if
e
!=
nil
{
return
""
,
e
return
""
,
e
}
}
...
@@ -80,8 +80,8 @@ func ReadLine(b *bufio.BufRead) (s string, err *os.Error) {
...
@@ -80,8 +80,8 @@ func ReadLine(b *bufio.BufRead) (s string, err *os.Error) {
// A key/value has the form Key: Value\r\n
// A key/value has the form Key: Value\r\n
// and the Value can continue on multiple lines if each continuation line
// and the Value can continue on multiple lines if each continuation line
// starts with a space.
// starts with a space.
func
R
eadKeyValue
(
b
*
bufio
.
BufRead
)
(
key
,
value
string
,
err
*
os
.
Error
)
{
func
r
eadKeyValue
(
b
*
bufio
.
BufRead
)
(
key
,
value
string
,
err
*
os
.
Error
)
{
line
,
e
:=
R
eadLineBytes
(
b
);
line
,
e
:=
r
eadLineBytes
(
b
);
if
e
!=
nil
{
if
e
!=
nil
{
return
""
,
""
,
e
return
""
,
""
,
e
}
}
...
@@ -127,12 +127,12 @@ func ReadKeyValue(b *bufio.BufRead) (key, value string, err *os.Error) {
...
@@ -127,12 +127,12 @@ func ReadKeyValue(b *bufio.BufRead) (key, value string, err *os.Error) {
b
.
UnreadByte
();
b
.
UnreadByte
();
// Read the rest of the line and add to value.
// Read the rest of the line and add to value.
if
line
,
e
=
R
eadLineBytes
(
b
);
e
!=
nil
{
if
line
,
e
=
r
eadLineBytes
(
b
);
e
!=
nil
{
return
""
,
""
,
e
return
""
,
""
,
e
}
}
value
+=
" "
+
string
(
line
);
value
+=
" "
+
string
(
line
);
if
len
(
value
)
>=
MaxValueLength
{
if
len
(
value
)
>=
_
MaxValueLength
{
return
""
,
""
,
ValueTooLong
return
""
,
""
,
ValueTooLong
}
}
}
}
...
@@ -163,7 +163,7 @@ func atoi(s string, i int) (n, i1 int, ok bool) {
...
@@ -163,7 +163,7 @@ func atoi(s string, i int) (n, i1 int, ok bool) {
}
}
// Parse HTTP version: "HTTP/1.2" -> (1, 2, true).
// Parse HTTP version: "HTTP/1.2" -> (1, 2, true).
func
P
arseHTTPVersion
(
vers
string
)
(
int
,
int
,
bool
)
{
func
p
arseHTTPVersion
(
vers
string
)
(
int
,
int
,
bool
)
{
if
vers
[
0
:
5
]
!=
"HTTP/"
{
if
vers
[
0
:
5
]
!=
"HTTP/"
{
return
0
,
0
,
false
return
0
,
0
,
false
}
}
...
@@ -185,7 +185,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
...
@@ -185,7 +185,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
// First line: GET /index.html HTTP/1.0
// First line: GET /index.html HTTP/1.0
var
s
string
;
var
s
string
;
if
s
,
err
=
R
eadLine
(
b
);
err
!=
nil
{
if
s
,
err
=
r
eadLine
(
b
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -195,7 +195,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
...
@@ -195,7 +195,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
}
}
req
.
method
,
req
.
rawurl
,
req
.
proto
=
f
[
0
],
f
[
1
],
f
[
2
];
req
.
method
,
req
.
rawurl
,
req
.
proto
=
f
[
0
],
f
[
1
],
f
[
2
];
var
ok
bool
;
var
ok
bool
;
if
req
.
pmajor
,
req
.
pminor
,
ok
=
P
arseHTTPVersion
(
req
.
proto
);
!
ok
{
if
req
.
pmajor
,
req
.
pminor
,
ok
=
p
arseHTTPVersion
(
req
.
proto
);
!
ok
{
return
nil
,
BadHTTPVersion
return
nil
,
BadHTTPVersion
}
}
...
@@ -208,13 +208,13 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
...
@@ -208,13 +208,13 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
req
.
header
=
make
(
map
[
string
]
string
);
req
.
header
=
make
(
map
[
string
]
string
);
for
{
for
{
var
key
,
value
string
;
var
key
,
value
string
;
if
key
,
value
,
err
=
R
eadKeyValue
(
b
);
err
!=
nil
{
if
key
,
value
,
err
=
r
eadKeyValue
(
b
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
if
key
==
""
{
if
key
==
""
{
break
break
}
}
if
nheader
++
;
nheader
>=
MaxHeaderLines
{
if
nheader
++
;
nheader
>=
_
MaxHeaderLines
{
return
nil
,
HeaderTooLong
return
nil
,
HeaderTooLong
}
}
...
...
src/lib/http/server.go
View file @
5d41f55a
...
@@ -17,7 +17,7 @@ import (
...
@@ -17,7 +17,7 @@ import (
)
)
// Serve a new connection.
// Serve a new connection.
func
S
erveConnection
(
fd
net
.
Conn
,
raddr
string
,
f
*
(
*
Conn
,
*
Request
))
{
func
s
erveConnection
(
fd
net
.
Conn
,
raddr
string
,
f
*
(
*
Conn
,
*
Request
))
{
c
,
err
:=
NewConn
(
fd
);
c
,
err
:=
NewConn
(
fd
);
if
err
!=
nil
{
if
err
!=
nil
{
return
return
...
@@ -48,7 +48,7 @@ export func Serve(l net.Listener, f *(*Conn, *Request)) *os.Error {
...
@@ -48,7 +48,7 @@ export func Serve(l net.Listener, f *(*Conn, *Request)) *os.Error {
if
e
!=
nil
{
if
e
!=
nil
{
return
e
return
e
}
}
go
S
erveConnection
(
rw
,
raddr
,
f
)
go
s
erveConnection
(
rw
,
raddr
,
f
)
}
}
panic
(
"not reached"
)
panic
(
"not reached"
)
}
}
...
...
src/lib/http/url.go
View file @
5d41f55a
...
@@ -16,7 +16,7 @@ export var (
...
@@ -16,7 +16,7 @@ export var (
BadURL
=
os
.
NewError
(
"bad url syntax"
)
BadURL
=
os
.
NewError
(
"bad url syntax"
)
)
)
func
IsH
ex
(
c
byte
)
bool
{
func
ish
ex
(
c
byte
)
bool
{
switch
{
switch
{
case
'0'
<=
c
&&
c
<=
'9'
:
case
'0'
<=
c
&&
c
<=
'9'
:
return
true
;
return
true
;
...
@@ -28,7 +28,7 @@ func IsHex(c byte) bool {
...
@@ -28,7 +28,7 @@ func IsHex(c byte) bool {
return
false
return
false
}
}
func
UnH
ex
(
c
byte
)
byte
{
func
unh
ex
(
c
byte
)
byte
{
switch
{
switch
{
case
'0'
<=
c
&&
c
<=
'9'
:
case
'0'
<=
c
&&
c
<=
'9'
:
return
c
-
'0'
;
return
c
-
'0'
;
...
@@ -47,7 +47,7 @@ export func URLUnescape(s string) (string, *os.Error) {
...
@@ -47,7 +47,7 @@ export func URLUnescape(s string) (string, *os.Error) {
for
i
:=
0
;
i
<
len
(
s
);
{
for
i
:=
0
;
i
<
len
(
s
);
{
if
s
[
i
]
==
'%'
{
if
s
[
i
]
==
'%'
{
n
++
;
n
++
;
if
!
IsHex
(
s
[
i
+
1
])
||
!
IsH
ex
(
s
[
i
+
2
])
{
if
!
ishex
(
s
[
i
+
1
])
||
!
ish
ex
(
s
[
i
+
2
])
{
return
""
,
BadURL
;
return
""
,
BadURL
;
}
}
i
+=
3
i
+=
3
...
@@ -64,7 +64,7 @@ export func URLUnescape(s string) (string, *os.Error) {
...
@@ -64,7 +64,7 @@ export func URLUnescape(s string) (string, *os.Error) {
j
:=
0
;
j
:=
0
;
for
i
:=
0
;
i
<
len
(
s
);
{
for
i
:=
0
;
i
<
len
(
s
);
{
if
s
[
i
]
==
'%'
{
if
s
[
i
]
==
'%'
{
t
[
j
]
=
UnHex
(
s
[
i
+
1
])
<<
4
|
UnH
ex
(
s
[
i
+
2
]);
t
[
j
]
=
unhex
(
s
[
i
+
1
])
<<
4
|
unh
ex
(
s
[
i
+
2
]);
j
++
;
j
++
;
i
+=
3
;
i
+=
3
;
}
else
{
}
else
{
...
@@ -91,7 +91,7 @@ export type URL struct {
...
@@ -91,7 +91,7 @@ export type URL struct {
// Maybe rawurl is of the form scheme:path.
// Maybe rawurl is of the form scheme:path.
// (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
// (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
// If so, return scheme, path; else return "", rawurl.
// If so, return scheme, path; else return "", rawurl.
func
GetS
cheme
(
rawurl
string
)
(
scheme
,
path
string
,
err
*
os
.
Error
)
{
func
gets
cheme
(
rawurl
string
)
(
scheme
,
path
string
,
err
*
os
.
Error
)
{
for
i
:=
0
;
i
<
len
(
rawurl
);
i
++
{
for
i
:=
0
;
i
<
len
(
rawurl
);
i
++
{
c
:=
rawurl
[
i
];
c
:=
rawurl
[
i
];
switch
{
switch
{
...
@@ -114,7 +114,7 @@ func GetScheme(rawurl string) (scheme, path string, err *os.Error) {
...
@@ -114,7 +114,7 @@ func GetScheme(rawurl string) (scheme, path string, err *os.Error) {
// Maybe s is of the form t c u.
// Maybe s is of the form t c u.
// If so, return t, c u (or t, u if cutc == true).
// If so, return t, c u (or t, u if cutc == true).
// If not, return s, "".
// If not, return s, "".
func
S
plit
(
s
string
,
c
byte
,
cutc
bool
)
(
string
,
string
)
{
func
s
plit
(
s
string
,
c
byte
,
cutc
bool
)
(
string
,
string
)
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
for
i
:=
0
;
i
<
len
(
s
);
i
++
{
if
s
[
i
]
==
c
{
if
s
[
i
]
==
c
{
if
cutc
{
if
cutc
{
...
@@ -134,9 +134,9 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
...
@@ -134,9 +134,9 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
url
=
new
(
URL
);
url
=
new
(
URL
);
url
.
raw
=
rawurl
;
url
.
raw
=
rawurl
;
//
S
plit off possible leading "http:", "mailto:", etc.
//
s
plit off possible leading "http:", "mailto:", etc.
var
path
string
;
var
path
string
;
if
url
.
scheme
,
path
,
err
=
GetS
cheme
(
rawurl
);
err
!=
nil
{
if
url
.
scheme
,
path
,
err
=
gets
cheme
(
rawurl
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
url
.
rawpath
=
path
;
url
.
rawpath
=
path
;
...
@@ -144,7 +144,7 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
...
@@ -144,7 +144,7 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
// RFC 2396: a relative URI (no scheme) has a ?query,
// RFC 2396: a relative URI (no scheme) has a ?query,
// but absolute URIs only have query if path begins with /
// but absolute URIs only have query if path begins with /
if
url
.
scheme
==
""
||
len
(
path
)
>
0
&&
path
[
0
]
==
'/'
{
if
url
.
scheme
==
""
||
len
(
path
)
>
0
&&
path
[
0
]
==
'/'
{
path
,
url
.
query
=
S
plit
(
path
,
'?'
,
true
);
path
,
url
.
query
=
s
plit
(
path
,
'?'
,
true
);
if
url
.
query
,
err
=
URLUnescape
(
url
.
query
);
err
!=
nil
{
if
url
.
query
,
err
=
URLUnescape
(
url
.
query
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -152,14 +152,14 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
...
@@ -152,14 +152,14 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
// Maybe path is //authority/path
// Maybe path is //authority/path
if
len
(
path
)
>
2
&&
path
[
0
:
2
]
==
"//"
{
if
len
(
path
)
>
2
&&
path
[
0
:
2
]
==
"//"
{
url
.
authority
,
path
=
S
plit
(
path
[
2
:
len
(
path
)],
'/'
,
false
);
url
.
authority
,
path
=
s
plit
(
path
[
2
:
len
(
path
)],
'/'
,
false
);
}
}
// If there's no @,
S
plit's default is wrong. Check explicitly.
// If there's no @,
s
plit's default is wrong. Check explicitly.
if
strings
.
index
(
url
.
authority
,
"@"
)
<
0
{
if
strings
.
index
(
url
.
authority
,
"@"
)
<
0
{
url
.
host
=
url
.
authority
;
url
.
host
=
url
.
authority
;
}
else
{
}
else
{
url
.
userinfo
,
url
.
host
=
S
plit
(
url
.
authority
,
'@'
,
true
);
url
.
userinfo
,
url
.
host
=
s
plit
(
url
.
authority
,
'@'
,
true
);
}
}
// What's left is the path.
// What's left is the path.
...
@@ -174,7 +174,7 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
...
@@ -174,7 +174,7 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
// A URL reference is a URL with #frag potentially added. Parse it.
// A URL reference is a URL with #frag potentially added. Parse it.
export
func
ParseURLReference
(
rawurlref
string
)
(
url
*
URL
,
err
*
os
.
Error
)
{
export
func
ParseURLReference
(
rawurlref
string
)
(
url
*
URL
,
err
*
os
.
Error
)
{
// Cut off #frag.
// Cut off #frag.
rawurl
,
frag
:=
S
plit
(
rawurlref
,
'#'
,
true
);
rawurl
,
frag
:=
s
plit
(
rawurlref
,
'#'
,
true
);
if
url
,
err
=
ParseURL
(
rawurl
);
err
!=
nil
{
if
url
,
err
=
ParseURL
(
rawurl
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment