-
Notifications
You must be signed in to change notification settings - Fork 5
/
http.go
52 lines (45 loc) · 1.13 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
package tcpip
import (
"fmt"
"reflect"
)
type HttpRequest struct {
Request []byte
Header HttpHeader
Body []byte
}
type HttpHeader struct {
Host []byte
UserAgent []byte
Accept []byte
Connection []byte
}
func NewHttpGetRequest(url, host string) HttpRequest {
header := HttpHeader{
Host: []byte(fmt.Sprintf("Host: %s", host)),
UserAgent: []byte(`User-Agent: curl/7.68.0`),
Accept: []byte(`Accept: */*`),
Connection: []byte(`Connection: close`),
}
return HttpRequest{
Request: []byte(fmt.Sprintf("GET %s HTTP/1.1", url)),
Header: header,
}
}
// https://www.infraexpert.com/study/tcpip16.html
// HTTPリクエストをbyteにして返す
func (*HttpRequest) ReqtoByteArr(request HttpRequest) []byte {
var packet []byte
var CRLF = []byte{0x0d, 0x0a}
packet = append(packet, request.Request...)
packet = append(packet, CRLF...)
rv := reflect.ValueOf(request.Header)
for i := 0; i < rv.NumField(); i++ {
b := rv.Field(i).Interface().([]byte)
packet = append(packet, b...)
packet = append(packet, CRLF...)
}
// 空白行を入れて戻す
packet = append(packet, CRLF...)
return packet
}