ws/http/upgrade/request/public.go

56 lines
1.0 KiB
Go
Raw Normal View History

package request
import (
"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 Build(r io.Reader) (request *T, err error) {
2018-04-25 14:25:41 +00:00
/* (1) Parse request
---------------------------------------------------------*/
/* (1) Get chunk reader */
cr := reader.NewReader(r)
if err != nil {
return nil, fmt.Errorf("Error while creating chunk reader: %s", err)
}
/* (2) Init request */
req := new(T)
/* (3) Parse header line by line */
for {
line, err := cr.Read()
if err == io.EOF {
fmt.Printf("END OF READING\n");
break
}
if err != nil {
return nil, fmt.Errorf("Cannot read from reader: %s", err)
}
err = req.parseHeader(line)
if err != nil {
return nil, fmt.Errorf("Parsing error: %s\n", err);
}
}
2018-04-25 14:25:41 +00:00
return req, nil
2018-04-25 14:25:41 +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-25 14:25:41 +00:00
return fmt.Sprintf("Upgrade Request\n - host: %s\n - port: %d\n", r.host, r.port)
}