This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
114 lines (101 loc) · 1.9 KB
/
http.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package swtch
import (
"io"
"net"
"github.com/soypat/ether-swtch/bytealg"
"github.com/soypat/ether-swtch/lax"
)
// HTTP unmarshal http requests and marshal body data
type HTTP struct {
// Request
Method HTTPMethod
// buffer HTTPBuffer
buff [32]byte
URL []byte
// Response
Body []byte
}
type HTTPMethod uint8
const (
httpUNDEFINED HTTPMethod = iota
HTTPGET
HTTPHEAD
HTTPPOST
HTTPPUT
HTTPDELETE
HTTPTRACE
HTTPOPTIONS
HTTPCONNECT
HTTPPATCH
)
var spaceByte = []byte{' '}
func (h *HTTP) Decode(r io.Reader) (n int, err error) {
n, err = r.Read(h.buff[:])
_log(lax.Strcat("http decode: ", string(h.buff[:n])))
if err != nil && !lax.IsEOF(err) {
return n, err
}
if n <= 6 {
return n, ErrShortRead
}
idx1 := bytealg.IdxRabinKarpBytes(h.buff[:], spaceByte)
if idx1 < 3 {
return n, ErrHTTPField
}
idx2 := bytealg.IdxRabinKarpBytes(h.buff[idx1+1:], spaceByte) + idx1 + 1
if idx2 < 5 {
return n, ErrHTTPField
}
h.URL = h.buff[idx1+1 : idx2]
_log("got url", h.URL)
switch bytealg.String(h.buff[:3]) {
case "GET":
h.Method = HTTPGET
case "POS":
h.Method = HTTPPOST
case "DEL":
h.Method = HTTPDELETE
case "PUT":
h.Method = HTTPPUT
}
return n, err
}
func (h *HTTP) Encode(w io.Writer) (n int, err error) {
_log("http:send", h.buff[:n])
if len(h.Body) == 0 {
return 0, nil
}
return w.Write(h.Body)
}
func (h *HTTP) FrameLength() uint16 {
return uint16(len(h.Body))
}
func (h *HTTP) Reset() error {
h.Body = nil
h.Method = httpUNDEFINED
return nil
}
func (h *HTTP) SetResponse(MAC net.HardwareAddr) error {
return nil
}
func (h *HTTP) String() string {
if h.URL == nil {
return "undefined http request"
}
return lax.Strcat(h.Method.String(), " @ ", bytealg.String(h.URL))
}
func (h HTTPMethod) String() (s string) {
switch h {
case HTTPGET:
s = "GET"
case HTTPDELETE:
s = "DELETE"
case HTTPPOST:
s = "POST"
case HTTPPUT:
s = "PUT"
default:
s = "?"
}
return s
}