File tree 2 files changed +80
-0
lines changed
2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ format:
23
23
${GOFMT} -w scgi.go
24
24
${GOFMT} -w servefile.go
25
25
${GOFMT} -w status.go
26
+ ${GOFMT} -w streaming.go
26
27
${GOFMT} -w web.go
27
28
${GOFMT} -w web_test.go
28
29
${GOFMT} -w examples/hello.go
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "http"
5
+ "net"
6
+ "os"
7
+ "time"
8
+ )
9
+
10
+ type Server struct {
11
+ addr string
12
+ }
13
+
14
+ func (s * Server ) handleRequest (req * http.Request ) (* http.Response , os.Error ) {
15
+ resp := http.Response {
16
+ StatusCode : 200 ,
17
+ RequestMethod : req .Method ,
18
+ ProtoMajor : 1 ,
19
+ ProtoMinor : 1 ,
20
+ Close : false ,
21
+ }
22
+
23
+ return & resp , nil
24
+ }
25
+
26
+ func (s * Server ) handleConn (conn net.Conn ) {
27
+ for {
28
+ sc := http .NewServerConn (conn , nil )
29
+ req , err := sc .Read ()
30
+ if err != nil {
31
+ println ("read error" , err .String ())
32
+ break
33
+ }
34
+ resp , err := s .handleRequest (req )
35
+ if err != nil {
36
+ println ("handle error" , err .String ())
37
+ break
38
+ }
39
+
40
+ err = sc .Write (resp )
41
+ if err != nil {
42
+ println ("write error" , err .String ())
43
+ break
44
+ }
45
+
46
+ for {
47
+ conn .Write ([]byte ("hello\n " ))
48
+ time .Sleep (5e9 )
49
+ }
50
+ }
51
+ }
52
+
53
+ func (s * Server ) Serve (l net.Listener ) os.Error {
54
+ for {
55
+ conn , e := l .Accept ()
56
+ if e != nil {
57
+ return e
58
+ }
59
+
60
+ go s .handleConn (conn )
61
+ }
62
+ panic ("not reached" )
63
+ }
64
+
65
+ func (s * Server ) Run (addr string ) os.Error {
66
+ l , e := net .Listen ("tcp" , addr )
67
+ if e != nil {
68
+ return e
69
+ }
70
+ e = s .Serve (l )
71
+ l .Close ()
72
+ return e
73
+ }
74
+
75
+ func main () {
76
+ var server Server
77
+ server .Run ("127.0.0.1:9999" )
78
+ <- make (chan int )
79
+ }
You can’t perform that action at this time.
0 commit comments