-
Notifications
You must be signed in to change notification settings - Fork 115
/
import.go
70 lines (56 loc) · 1.16 KB
/
import.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
package gout
import (
"bufio"
"bytes"
"github.com/guonaihong/gout/core"
"github.com/guonaihong/gout/dataflow"
"io"
"io/ioutil"
"net/http"
"strings"
)
type Import struct{}
func NewImport() *Import {
return &Import{}
}
const rawTextSpace = "\r\n "
func (i *Import) RawText(text interface{}) *Text {
var read io.Reader
r := &Text{}
out := dataflow.New()
// TODO 函数
r.SetGout(out)
switch body := text.(type) {
case string:
body = strings.TrimLeft(body, rawTextSpace)
read = strings.NewReader(body)
case []byte:
body = bytes.TrimLeft(body, rawTextSpace)
read = bytes.NewReader(body)
default:
r.Err = core.ErrUnknownType
return r
}
req, err := http.ReadRequest(bufio.NewReader(read))
if err != nil {
r.Err = err
return r
}
// TODO 探索下能否支持https
req.URL.Scheme = "http"
req.URL.Host = req.Host
req.RequestURI = ""
if req.GetBody == nil {
all, err := ioutil.ReadAll(req.Body)
if err != nil {
r.Err = err
return r
}
req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(all)), nil
}
req.Body = ioutil.NopCloser(bytes.NewReader(all))
}
r.SetRequest(req)
return r
}