93 lines
1.4 KiB
Go
93 lines
1.4 KiB
Go
package request
|
|
|
|
import (
|
|
"git.xdrm.io/gws/internal/http/upgrade/response"
|
|
"git.xdrm.io/gws/internal/http/reader"
|
|
"fmt"
|
|
"io"
|
|
)
|
|
|
|
// Parse builds an upgrade HTTP request
|
|
// from a reader (typically bufio.NewRead of the socket)
|
|
func Parse(r io.Reader) (request *T, err error) {
|
|
|
|
req := new(T)
|
|
req.code = 500
|
|
|
|
|
|
/* (1) Parse request
|
|
---------------------------------------------------------*/
|
|
/* (1) Get chunk reader */
|
|
cr := reader.NewReader(r)
|
|
if err != nil {
|
|
return req, fmt.Errorf("Error while creating chunk reader: %s", err)
|
|
}
|
|
|
|
/* (2) Parse header line by line */
|
|
for {
|
|
|
|
line, err := cr.Read()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
|
|
if err != nil {
|
|
return req, err
|
|
}
|
|
|
|
err = req.parseHeader(line)
|
|
|
|
if err != nil {
|
|
return req, err
|
|
}
|
|
|
|
}
|
|
|
|
/* (3) Check completion */
|
|
err = req.isComplete()
|
|
if err != nil {
|
|
req.code = response.BAD_REQUEST
|
|
return req, err
|
|
}
|
|
|
|
|
|
req.code = response.SWITCHING_PROTOCOLS
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusCode returns the status current
|
|
func (r T) StatusCode() response.StatusCode {
|
|
return r.code
|
|
}
|
|
|
|
|
|
|
|
// BuildResponse builds a response.T from the request
|
|
func (r *T) BuildResponse() *response.T{
|
|
|
|
inst := new(response.T)
|
|
|
|
/* (1) Copy code */
|
|
inst.SetStatusCode(r.code)
|
|
|
|
/* (2) Set Protocol */
|
|
if len(r.protocols) > 0 {
|
|
inst.SetProtocol(r.protocols[0])
|
|
}
|
|
|
|
/* (4) Process key */
|
|
inst.ProcessKey(r.key)
|
|
|
|
return inst
|
|
}
|
|
|
|
|
|
|
|
// GetURI returns the actual URI
|
|
func (r T) GetURI() string{
|
|
return r.request.GetURI()
|
|
}
|