2018-04-25 19:51:53 +00:00
|
|
|
package upgrader
|
|
|
|
|
|
|
|
import (
|
2018-04-26 21:01:25 +00:00
|
|
|
"git.xdrm.io/gws/internal/http/upgrade/response"
|
2018-04-25 19:51:53 +00:00
|
|
|
"fmt"
|
|
|
|
"git.xdrm.io/gws/internal/http/upgrade/request"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
type T struct {
|
|
|
|
req *request.T
|
|
|
|
res *response.T
|
|
|
|
}
|
2018-04-25 19:51:53 +00:00
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
func Upgrade(s net.Conn) (*T, error) {
|
2018-04-25 19:51:53 +00:00
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
/* (1) Create upgrder */
|
|
|
|
inst := new(T)
|
2018-04-25 19:51:53 +00:00
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
/* (2) Parse request */
|
|
|
|
inst.req, _ = request.Parse(s)
|
2018-04-25 19:51:53 +00:00
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
/* (3) Build response */
|
|
|
|
inst.res = inst.req.BuildResponse()
|
|
|
|
|
|
|
|
/* (4) Write into socket */
|
|
|
|
_, err := inst.res.Send(s)
|
2018-04-25 19:51:53 +00:00
|
|
|
if err != nil {
|
2018-04-26 21:01:25 +00:00
|
|
|
return inst, fmt.Errorf("Socket write error: %s", err)
|
2018-04-25 19:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
return inst, nil
|
2018-04-25 19:51:53 +00:00
|
|
|
|
|
|
|
}
|