2018-04-24 21:10:01 +00:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-05-14 14:47:02 +00:00
|
|
|
|
|
|
|
"git.xdrm.io/go/ws/internal/http/reader"
|
|
|
|
"git.xdrm.io/go/ws/internal/http/upgrade/response"
|
2018-04-24 21:10:01 +00:00
|
|
|
)
|
|
|
|
|
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-05-04 06:41:40 +00:00
|
|
|
return req, 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-05-04 06:41:40 +00:00
|
|
|
return req, 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 {
|
2021-05-14 14:47:02 +00:00
|
|
|
req.code = response.BadRequest
|
2018-04-25 16:09:26 +00:00
|
|
|
return req, err
|
2018-04-25 15:22:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:47:02 +00:00
|
|
|
req.code = response.SwitchingProtocols
|
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
|
2018-09-29 12:36:47 +00:00
|
|
|
func (r *T) BuildResponse() *response.T {
|
2018-04-25 16:58:48 +00:00
|
|
|
|
|
|
|
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
|
2018-09-29 12:36:47 +00:00
|
|
|
func (r T) GetURI() string {
|
2018-04-26 21:26:20 +00:00
|
|
|
return r.request.GetURI()
|
|
|
|
}
|