2021-05-14 15:19:02 +00:00
|
|
|
package upgrade
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-09-29 12:39:12 +00:00
|
|
|
"fmt"
|
2018-05-04 06:41:40 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// Line represents the HTTP Request line
|
2018-05-04 06:41:40 +00:00
|
|
|
// defined in rfc-2616 : https://tools.ietf.org/html/rfc2616#section-5.1
|
2021-05-14 15:19:02 +00:00
|
|
|
type Line struct {
|
|
|
|
method Method
|
2018-05-04 06:41:40 +00:00
|
|
|
uri string
|
|
|
|
version byte
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// Parse parses the first HTTP request line
|
|
|
|
func (r *Line) Parse(b []byte) error {
|
2018-05-04 06:41:40 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Split by ' '
|
2018-05-04 06:41:40 +00:00
|
|
|
parts := bytes.Split(b, []byte(" "))
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Fail when missing parts
|
2018-05-04 06:41:40 +00:00
|
|
|
if len(parts) != 3 {
|
|
|
|
return fmt.Errorf("expected 3 space-separated elements, got %d elements", len(parts))
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Extract HTTP method
|
2018-05-04 06:41:40 +00:00
|
|
|
err := r.extractHttpMethod(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. Extract URI
|
2018-05-04 06:41:40 +00:00
|
|
|
err = r.extractURI(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 5. Extract version
|
2018-05-04 06:41:40 +00:00
|
|
|
err = r.extractHttpVersion(parts[2])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetURI returns the actual URI
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r Line) GetURI() string {
|
2018-05-04 06:41:40 +00:00
|
|
|
return r.uri
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractHttpMethod extracts the HTTP method from a []byte
|
|
|
|
// and checks for errors
|
|
|
|
// allowed format: OPTIONS|GET|HEAD|POST|PUT|DELETE
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Line) extractHttpMethod(b []byte) error {
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
switch string(b) {
|
2018-09-29 12:39:12 +00:00
|
|
|
// case "OPTIONS": r.method = OPTIONS
|
|
|
|
case "GET":
|
2021-05-14 15:19:02 +00:00
|
|
|
r.method = Get
|
2018-09-29 12:39:12 +00:00
|
|
|
// case "HEAD": r.method = HEAD
|
|
|
|
// case "POST": r.method = POST
|
|
|
|
// case "PUT": r.method = PUT
|
|
|
|
// case "DELETE": r.method = DELETE
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid HTTP method '%s', expected 'GET'", b)
|
2018-05-04 06:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractURI extracts the URI from a []byte and checks for errors
|
|
|
|
// allowed format: /([^/]/)*/?
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Line) extractURI(b []byte) error {
|
2018-05-04 06:41:40 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Check format
|
2018-05-04 06:41:40 +00:00
|
|
|
checker := regexp.MustCompile("^(?:/[^/]+)*/?$")
|
|
|
|
if !checker.Match(b) {
|
|
|
|
return fmt.Errorf("invalid URI, expected an absolute path, got '%s'", b)
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Store
|
2018-05-04 06:41:40 +00:00
|
|
|
r.uri = string(b)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractHttpVersion extracts the version and checks for errors
|
|
|
|
// allowed format: [1-9] or [1.9].[0-9]
|
2021-05-14 15:19:02 +00:00
|
|
|
func (r *Line) extractHttpVersion(b []byte) error {
|
2018-05-04 06:41:40 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 1. Extract version parts
|
2018-09-29 12:39:12 +00:00
|
|
|
extractor := regexp.MustCompile(`^HTTP/([1-9])(?:\.([0-9]))?$`)
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
if !extractor.Match(b) {
|
|
|
|
return fmt.Errorf("HTTP version, expected INT or INT.INT, got '%s'", b)
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 2. Extract version number
|
2018-05-04 06:41:40 +00:00
|
|
|
matches := extractor.FindSubmatch(b)
|
2018-09-29 12:39:12 +00:00
|
|
|
var version byte = matches[1][0] - '0'
|
2018-05-04 06:41:40 +00:00
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 3. Extract subversion (if exists)
|
2018-05-04 06:41:40 +00:00
|
|
|
var subVersion byte = 0
|
|
|
|
if len(matches[2]) > 0 {
|
|
|
|
subVersion = matches[2][0] - '0'
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:19:02 +00:00
|
|
|
// 4. Store version (x 10 to fit uint8)
|
2018-09-29 12:39:12 +00:00
|
|
|
r.version = version*10 + subVersion
|
2018-05-04 06:41:40 +00:00
|
|
|
|
|
|
|
return nil
|
2018-09-29 12:39:12 +00:00
|
|
|
}
|