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
0cb585f9
Commit
0cb585f9
authored
Jun 07, 2009
by
David Symonds
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Basic HTTP POST support.
R=rsc APPROVED=rsc DELTA=45 (37 added, 1 deleted, 7 changed) OCL=29964 CL=29990
parent
0d2c63a0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
6 deletions
+43
-6
request.go
src/lib/http/request.go
+27
-4
triv.go
src/lib/http/triv.go
+16
-2
No files found.
src/lib/http/request.go
View file @
0cb585f9
...
...
@@ -13,10 +13,12 @@ package http
import
(
"bufio"
;
"fmt"
;
"http"
;
"io"
;
"os"
;
"strings"
"strconv"
;
"strings"
;
)
const
(
...
...
@@ -33,6 +35,8 @@ var (
LineTooLong
=
&
ProtocolError
{
"http header line too long"
};
ValueTooLong
=
&
ProtocolError
{
"http header value too long"
};
HeaderTooLong
=
&
ProtocolError
{
"http header too long"
};
BadContentLength
=
&
ProtocolError
{
"invalid content length"
};
ShortEntityBody
=
&
ProtocolError
{
"entity body too short"
};
BadHeader
=
&
ProtocolError
{
"malformed http header"
};
BadRequest
=
&
ProtocolError
{
"invalid http request"
};
BadHTTPVersion
=
&
ProtocolError
{
"unsupported http version"
};
...
...
@@ -40,9 +44,9 @@ var (
// A Request represents a parsed HTTP request header.
type
Request
struct
{
Method
string
;
// GET, P
UT,
etc.
Method
string
;
// GET, P
OST, PUT,
etc.
RawUrl
string
;
// The raw URL given in the request.
Url
*
URL
;
//
URL after GET, PUT etc
.
Url
*
URL
;
//
Parsed URL
.
Proto
string
;
// "HTTP/1.0"
ProtoMajor
int
;
// 1
ProtoMinor
int
;
// 0
...
...
@@ -68,6 +72,9 @@ type Request struct {
// following a hyphen uppercase and the rest lowercase.
Header
map
[
string
]
string
;
// The message body.
Body
io
.
Reader
;
// Whether to close the connection after replying to this request.
Close
bool
;
...
...
@@ -386,5 +393,21 @@ func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) {
// Via
// Warning
return
req
,
nil
;
// A message body exists when either Content-Length or Transfer-Encoding
// headers are present. TODO: Handle Transfer-Encoding.
if
v
,
present
:=
req
.
Header
[
"Content-Length"
];
present
{
length
,
err
:=
strconv
.
Btoui64
(
v
,
10
);
if
err
!=
nil
{
return
nil
,
BadContentLength
}
// TODO: limit the Content-Length. This is an easy DoS vector.
raw
:=
make
([]
byte
,
length
);
n
,
err
:=
b
.
Read
(
raw
);
if
err
!=
nil
||
uint64
(
n
)
<
length
{
return
nil
,
ShortEntityBody
}
req
.
Body
=
io
.
NewByteReader
(
raw
);
}
return
req
,
nil
}
src/lib/http/triv.go
View file @
0cb585f9
...
...
@@ -14,6 +14,7 @@ import (
"log"
;
"net"
;
"os"
;
"strconv"
;
)
...
...
@@ -24,7 +25,7 @@ func HelloServer(c *http.Conn, req *http.Request) {
io
.
WriteString
(
c
,
"hello, world!
\n
"
);
}
//
simple counter server
//
Simple counter server. POSTing to it will set the value.
type
Counter
struct
{
n
int
;
}
...
...
@@ -36,8 +37,21 @@ func (ctr *Counter) String() string {
}
func
(
ctr
*
Counter
)
ServeHTTP
(
c
*
http
.
Conn
,
req
*
http
.
Request
)
{
switch
req
.
Method
{
case
"GET"
:
ctr
.
n
++
;
case
"POST"
:
buf
:=
new
(
io
.
ByteBuffer
);
io
.
Copy
(
req
.
Body
,
buf
);
body
:=
string
(
buf
.
Data
());
if
n
,
err
:=
strconv
.
Atoi
(
body
);
err
!=
nil
{
fmt
.
Fprintf
(
c
,
"bad POST: %v
\n
body: [%v]
\n
"
,
err
,
body
);
}
else
{
ctr
.
n
=
n
;
fmt
.
Fprint
(
c
,
"counter reset
\n
"
);
}
}
fmt
.
Fprintf
(
c
,
"counter = %d
\n
"
,
ctr
.
n
);
ctr
.
n
++
;
}
// simple file server
...
...
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