2018-04-24 21:10:01 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
2018-04-25 19:51:53 +00:00
|
|
|
"git.xdrm.io/gws/internal/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)
|
2018-04-25 16:58:48 +00:00
|
|
|
func Parse(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-05-01 20:14:35 +00:00
|
|
|
fmt.Printf("not complete: %s\b", err)
|
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 16:09:26 +00:00
|
|
|
// StatusCode returns the status current
|
|
|
|
func (r T) StatusCode() response.StatusCode {
|
|
|
|
return r.code
|
|
|
|
}
|
2018-04-25 16:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2018-04-26 21:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GetURI returns the actual URI
|
|
|
|
func (r T) GetURI() string{
|
|
|
|
return r.request.GetURI()
|
|
|
|
}
|