2018-04-24 21:10:01 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
2018-04-25 16:09:26 +00:00
|
|
|
"git.xdrm.io/gws/http/upgrade/response"
|
2018-04-24 21:10:01 +00:00
|
|
|
"git.xdrm.io/gws/internal/http/reader"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2018-04-25 13:54:15 +00:00
|
|
|
// Parse builds an upgrade HTTP request
|
|
|
|
// from a reader (typically bufio.NewRead of the socket)
|
|
|
|
func Build(r io.Reader) (request *T, err error) {
|
2018-04-24 21:10:01 +00:00
|
|
|
|
2018-04-25 16:09:26 +00:00
|
|
|
req := new(T)
|
|
|
|
req.code = 500
|
|
|
|
|
|
|
|
|
2018-04-25 14:25:41 +00:00
|
|
|
/* (1) Parse request
|
|
|
|
---------------------------------------------------------*/
|
2018-04-24 21:10:01 +00:00
|
|
|
/* (1) Get chunk reader */
|
2018-04-25 13:54:15 +00:00
|
|
|
cr := reader.NewReader(r)
|
2018-04-24 21:10:01 +00:00
|
|
|
if err != nil {
|
2018-04-25 16:09:26 +00:00
|
|
|
return req, fmt.Errorf("Error while creating chunk reader: %s", err)
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 16:09:26 +00:00
|
|
|
/* (2) Parse header line by line */
|
2018-04-24 21:10:01 +00:00
|
|
|
for {
|
|
|
|
|
|
|
|
line, err := cr.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-25 16:09:26 +00:00
|
|
|
return req, fmt.Errorf("Cannot read from reader: %s", err)
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
err = req.parseHeader(line)
|
2018-04-24 21:10:01 +00:00
|
|
|
|
2018-04-25 13:41:39 +00:00
|
|
|
if err != nil {
|
2018-04-25 16:09:26 +00:00
|
|
|
return req, fmt.Errorf("Parsing error: %s\n", err);
|
2018-04-24 21:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-25 16:09:26 +00:00
|
|
|
/* (3) Check completion */
|
2018-04-25 15:22:27 +00:00
|
|
|
err = req.isComplete()
|
|
|
|
if err != nil {
|
2018-04-25 16:09:26 +00:00
|
|
|
req.code = response.BAD_REQUEST
|
|
|
|
return req, err
|
2018-04-25 15:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-25 16:11:13 +00:00
|
|
|
req.code = response.SWITCHING_PROTOCOLS
|
2018-04-25 14:25:41 +00:00
|
|
|
return req, nil
|
2018-04-24 21:10:01 +00:00
|
|
|
|
2018-04-25 14:25:41 +00:00
|
|
|
}
|
2018-04-24 21:10:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 14:25:41 +00:00
|
|
|
// String returns a textual representation of the upgrade request
|
|
|
|
func (r T) String() string{
|
2018-04-24 21:10:01 +00:00
|
|
|
|
2018-04-25 15:22:27 +00:00
|
|
|
l1 := fmt.Sprintf("Upgrade Request\n - host: %s\n - port: %d\n - origin: %s\n", r.host, r.port, r.origin)
|
|
|
|
l2 := fmt.Sprintf(" - origin policy: %t\n - connection header: %t\n - upgrade header: %t\n - valid ws version: %t\n", r.validPolicy, r.hasConnection, r.hasUpgrade, r.hasVersion)
|
|
|
|
l3 := fmt.Sprintf(" - key: %s\n - protocols: %s\n", r.key, r.protocols)
|
2018-04-25 16:09:26 +00:00
|
|
|
l4 := fmt.Sprintf(" - current status: %d %s\n", r.code, r.code.Explicit())
|
2018-04-25 15:22:27 +00:00
|
|
|
|
2018-04-25 14:25:41 +00:00
|
|
|
|
2018-04-25 16:09:26 +00:00
|
|
|
return fmt.Sprintf("%s%s%s%s", l1, l2, l3, l4)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// StatusCode returns the status current
|
|
|
|
func (r T) StatusCode() response.StatusCode {
|
|
|
|
return r.code
|
|
|
|
}
|