aboutsummaryrefslogtreecommitdiff
path: root/app/request.go
blob: a8591540e8318a5eee3a072c88ce17a93492f41c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
)

type Request struct {
	Method  string
	Target  string
	Version string
}

func NewRequest(reader io.Reader) (*Request, error) {
	scanner := bufio.NewScanner(reader)

	ok := scanner.Scan()
	if !ok {
		err := scanner.Err()
		if err == nil {
			err = io.EOF
		}

		return nil, err
	}

	line := scanner.Bytes()
	elements := bytes.Split(line, []byte(" "))

	if len(elements) != 3 {
		return nil, fmt.Errorf("invalid request, excepted 3 elements, got %d", len(elements))
	}

	return &Request{Method: string(elements[0]), Target: string(elements[1]), Version: string(elements[2])}, nil
}