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

41 lines
803 B
Go
Raw Normal View History

package reqline
import (
"fmt"
"bytes"
)
// parseRequestLine parses the first HTTP request line
func (r *T) Parse(b []byte) error {
/* (1) Split by ' ' */
parts := bytes.Split(b, []byte(" "))
/* (2) Fail when missing parts */
if len(parts) != 3 {
return fmt.Errorf("Malformed Request-Line must have 3 space-separated elements, got %d", len(parts))
}
/* (3) Extract HTTP method */
err := r.extractHttpMethod(parts[0])
if err != nil {
return err
}
/* (4) Extract URI */
err = r.extractURI(parts[1])
if err != nil {
return err
}
/* (5) Extract version */
err = r.extractHttpVersion(parts[2])
if err != nil {
return err
}
fmt.Printf("REQUEST-LINE\n------------\n - Method: '%d'\n - URI: '%s'\n - Version: %f\n", r.method, r.uri, float32(r.version)/10);
return nil
}