ws/internal/http/upgrade/request/public.go

94 lines
1.6 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, fmt.Errorf("Cannot read from reader: %s", err)
}
err = req.parseHeader(line)
if err != nil {
return req, fmt.Errorf("Parsing error: %s\n", err);
}
}
/* (3) Check completion */
err = req.isComplete()
if err != nil {
fmt.Printf("not complete: %s\b", err)
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()
}