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 (
)
const
(
MaxLineLength
=
1024
;
// assumed < bufio.DefaultBufSize
MaxValueLength
=
1024
;
MaxHeaderLines
=
1024
;
_
MaxLineLength
=
1024
;
// assumed < bufio.DefaultBufSize
_
MaxValueLength
=
1024
;
_
MaxHeaderLines
=
1024
;
)
export
var
(
...
...
@@ -46,14 +46,14 @@ export type Request struct {
}
// 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 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
{
return
nil
,
err
}
if
len
(
p
)
>=
MaxLineLength
{
if
len
(
p
)
>=
_
MaxLineLength
{
return
nil
,
LineTooLong
}
...
...
@@ -67,9 +67,9 @@ func ReadLineBytes(b *bufio.BufRead) (p []byte, err *os.Error) {
return
p
[
0
:
i
],
nil
}
//
ReadLineByte
, but convert the bytes into a string.
func
R
eadLine
(
b
*
bufio
.
BufRead
)
(
s
string
,
err
*
os
.
Error
)
{
p
,
e
:=
R
eadLineBytes
(
b
);
//
readLineBytes
, but convert the bytes into a string.
func
r
eadLine
(
b
*
bufio
.
BufRead
)
(
s
string
,
err
*
os
.
Error
)
{
p
,
e
:=
r
eadLineBytes
(
b
);
if
e
!=
nil
{
return
""
,
e
}
...
...
@@ -80,8 +80,8 @@ func ReadLine(b *bufio.BufRead) (s string, err *os.Error) {
// A key/value has the form Key: Value\r\n
// and the Value can continue on multiple lines if each continuation line
// starts with a space.
func
R
eadKeyValue
(
b
*
bufio
.
BufRead
)
(
key
,
value
string
,
err
*
os
.
Error
)
{
line
,
e
:=
R
eadLineBytes
(
b
);
func
r
eadKeyValue
(
b
*
bufio
.
BufRead
)
(
key
,
value
string
,
err
*
os
.
Error
)
{
line
,
e
:=
r
eadLineBytes
(
b
);
if
e
!=
nil
{
return
""
,
""
,
e
}
...
...
@@ -127,12 +127,12 @@ func ReadKeyValue(b *bufio.BufRead) (key, value string, err *os.Error) {
b
.
UnreadByte
();
// 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
}
value
+=
" "
+
string
(
line
);
if
len
(
value
)
>=
MaxValueLength
{
if
len
(
value
)
>=
_
MaxValueLength
{
return
""
,
""
,
ValueTooLong
}
}
...
...
@@ -163,7 +163,7 @@ func atoi(s string, i int) (n, i1 int, ok bool) {
}
// 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/"
{
return
0
,
0
,
false
}
...
...
@@ -185,7 +185,7 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
// First line: GET /index.html HTTP/1.0
var
s
string
;
if
s
,
err
=
R
eadLine
(
b
);
err
!=
nil
{
if
s
,
err
=
r
eadLine
(
b
);
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -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
];
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
}
...
...
@@ -208,13 +208,13 @@ export func ReadRequest(b *bufio.BufRead) (req *Request, err *os.Error) {
req
.
header
=
make
(
map
[
string
]
string
);
for
{
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
}
if
key
==
""
{
break
}
if
nheader
++
;
nheader
>=
MaxHeaderLines
{
if
nheader
++
;
nheader
>=
_
MaxHeaderLines
{
return
nil
,
HeaderTooLong
}
...
...
src/lib/http/server.go
View file @
5d41f55a
...
...
@@ -17,7 +17,7 @@ import (
)
// 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
);
if
err
!=
nil
{
return
...
...
@@ -48,7 +48,7 @@ export func Serve(l net.Listener, f *(*Conn, *Request)) *os.Error {
if
e
!=
nil
{
return
e
}
go
S
erveConnection
(
rw
,
raddr
,
f
)
go
s
erveConnection
(
rw
,
raddr
,
f
)
}
panic
(
"not reached"
)
}
...
...
src/lib/http/url.go
View file @
5d41f55a
...
...
@@ -16,7 +16,7 @@ export var (
BadURL
=
os
.
NewError
(
"bad url syntax"
)
)
func
IsH
ex
(
c
byte
)
bool
{
func
ish
ex
(
c
byte
)
bool
{
switch
{
case
'0'
<=
c
&&
c
<=
'9'
:
return
true
;
...
...
@@ -28,7 +28,7 @@ func IsHex(c byte) bool {
return
false
}
func
UnH
ex
(
c
byte
)
byte
{
func
unh
ex
(
c
byte
)
byte
{
switch
{
case
'0'
<=
c
&&
c
<=
'9'
:
return
c
-
'0'
;
...
...
@@ -47,7 +47,7 @@ export func URLUnescape(s string) (string, *os.Error) {
for
i
:=
0
;
i
<
len
(
s
);
{
if
s
[
i
]
==
'%'
{
n
++
;
if
!
IsHex
(
s
[
i
+
1
])
||
!
IsH
ex
(
s
[
i
+
2
])
{
if
!
ishex
(
s
[
i
+
1
])
||
!
ish
ex
(
s
[
i
+
2
])
{
return
""
,
BadURL
;
}
i
+=
3
...
...
@@ -64,7 +64,7 @@ export func URLUnescape(s string) (string, *os.Error) {
j
:=
0
;
for
i
:=
0
;
i
<
len
(
s
);
{
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
++
;
i
+=
3
;
}
else
{
...
...
@@ -91,7 +91,7 @@ export type URL struct {
// Maybe rawurl is of the form scheme:path.
// (Scheme must be [a-zA-Z][a-zA-Z0-9+-.]*)
// 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
++
{
c
:=
rawurl
[
i
];
switch
{
...
...
@@ -114,7 +114,7 @@ func GetScheme(rawurl string) (scheme, path string, err *os.Error) {
// Maybe s is of the form t c u.
// If so, return t, c u (or t, u if cutc == true).
// 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
++
{
if
s
[
i
]
==
c
{
if
cutc
{
...
...
@@ -134,9 +134,9 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
url
=
new
(
URL
);
url
.
raw
=
rawurl
;
//
S
plit off possible leading "http:", "mailto:", etc.
//
s
plit off possible leading "http:", "mailto:", etc.
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
}
url
.
rawpath
=
path
;
...
...
@@ -144,7 +144,7 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
// RFC 2396: a relative URI (no scheme) has a ?query,
// but absolute URIs only have query if path begins with /
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
{
return
nil
,
err
}
...
...
@@ -152,14 +152,14 @@ export func ParseURL(rawurl string) (url *URL, err *os.Error) {
// Maybe path is //authority/path
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
{
url
.
host
=
url
.
authority
;
}
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.
...
...
@@ -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.
export
func
ParseURLReference
(
rawurlref
string
)
(
url
*
URL
,
err
*
os
.
Error
)
{
// Cut off #frag.
rawurl
,
frag
:=
S
plit
(
rawurlref
,
'#'
,
true
);
rawurl
,
frag
:=
s
plit
(
rawurlref
,
'#'
,
true
);
if
url
,
err
=
ParseURL
(
rawurl
);
err
!=
nil
{
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