moved http.* into /internal | created upgrader interface in /upgrader
This commit is contained in:
parent
6c8afe8720
commit
a85e16e96a
|
@ -1,11 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.xdrm.io/gws/upgrader"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
"git.xdrm.io/gws/http/upgrade/request"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -27,8 +27,6 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("+ new client\n")
|
|
||||||
|
|
||||||
go manageClient(sock)
|
go manageClient(sock)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -43,26 +41,21 @@ func manageClient(sock net.Conn) {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
/* (1) Parse request */
|
fmt.Printf("+ new client\n")
|
||||||
req, errRq := request.Parse(sock)
|
|
||||||
if errRq != nil { fmt.Printf("Request parsing error: %s\n", errRq) }
|
|
||||||
fmt.Printf("%v\n", req)
|
|
||||||
|
|
||||||
/* (2) Build response */
|
fmt.Printf(" + upgrade\n")
|
||||||
res := req.BuildResponse()
|
err := upgrader.Upgrade(sock)
|
||||||
|
|
||||||
/* (3) Write into socket */
|
|
||||||
written, err := res.Send(sock)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("WRITE ERROR: %s\n", err);
|
fmt.Printf(" + error: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Printf(" + 2-way exchange\n")
|
||||||
fmt.Printf("Written %d bytes\n", written)
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
|
fmt.Printf("+ elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.xdrm.io/gws/http/upgrade/response"
|
"git.xdrm.io/gws/internal/http/upgrade/response"
|
||||||
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
|
@ -1,7 +1,7 @@
|
||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.xdrm.io/gws/http/upgrade/response"
|
"git.xdrm.io/gws/internal/http/upgrade/response"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
"git.xdrm.io/gws/internal/http/upgrade/request/parser/header"
|
||||||
)
|
)
|
|
@ -1,7 +1,7 @@
|
||||||
package request
|
package request
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.xdrm.io/gws/http/upgrade/response"
|
"git.xdrm.io/gws/internal/http/upgrade/response"
|
||||||
"git.xdrm.io/gws/internal/http/reader"
|
"git.xdrm.io/gws/internal/http/reader"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
|
@ -1,7 +1,7 @@
|
||||||
package request
|
package request
|
||||||
|
|
||||||
import "git.xdrm.io/gws/internal/http/upgrade/request/parser/reqline"
|
import "git.xdrm.io/gws/internal/http/upgrade/request/parser/reqline"
|
||||||
import "git.xdrm.io/gws/http/upgrade/response"
|
import "git.xdrm.io/gws/internal/http/upgrade/response"
|
||||||
|
|
||||||
// T represents an HTTP Upgrade request
|
// T represents an HTTP Upgrade request
|
||||||
type T struct {
|
type T struct {
|
|
@ -54,7 +54,6 @@ func (r T) Send(w io.Writer) (int, error) {
|
||||||
raw := []byte(fmt.Sprintf("%s%s\r\n", responseLine, headers))
|
raw := []byte(fmt.Sprintf("%s%s\r\n", responseLine, headers))
|
||||||
|
|
||||||
/* (4) Write */
|
/* (4) Write */
|
||||||
fmt.Printf("Upgrade Response\n----------------\n%s----------------\n", raw)
|
|
||||||
written, err := w.Write(raw)
|
written, err := w.Write(raw)
|
||||||
|
|
||||||
return written, err
|
return written, err
|
|
@ -0,0 +1,26 @@
|
||||||
|
package upgrader
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.xdrm.io/gws/internal/http/upgrade/request"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func Upgrade(s net.Conn) error {
|
||||||
|
|
||||||
|
/* (1) Parse request */
|
||||||
|
req, _ := request.Parse(s)
|
||||||
|
|
||||||
|
/* (2) Build response */
|
||||||
|
res := req.BuildResponse()
|
||||||
|
|
||||||
|
/* (3) Write into socket */
|
||||||
|
_, err := res.Send(s)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Socket write: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package upgrader
|
Loading…
Reference in New Issue