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 {
|
2018-04-26 21:26:39 +00:00
|
|
|
Request *request.T
|
|
|
|
Response *response.T
|
2018-04-26 21:01:25 +00:00
|
|
|
}
|
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 */
|
2018-04-26 21:26:39 +00:00
|
|
|
inst.Request, _ = request.Parse(s)
|
2018-04-25 19:51:53 +00:00
|
|
|
|
2018-04-26 21:01:25 +00:00
|
|
|
/* (3) Build response */
|
2018-04-26 21:26:39 +00:00
|
|
|
inst.Response = inst.Request.BuildResponse()
|
2018-04-26 21:01:25 +00:00
|
|
|
|
|
|
|
/* (4) Write into socket */
|
2018-04-26 21:26:39 +00:00
|
|
|
_, err := inst.Response.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
|
|
|
|
|
|
|
}
|