Skip to content

Commit 4b6b093

Browse files
committed
gbk
1 parent e3e54b7 commit 4b6b093

File tree

5 files changed

+123
-66
lines changed

5 files changed

+123
-66
lines changed

base/buffer.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package base
22

33
import (
44
"bytes"
5+
"unicode/utf8"
56
"unsafe"
67
)
78

@@ -121,6 +122,12 @@ func (b Buffer) ToString(args ...string) string {
121122
return ""
122123
}
123124

125+
func (b Buffer) ToRune(args ...int) rune {
126+
n := b.Slice(args...)
127+
v, _ := utf8.DecodeRune(n)
128+
return v
129+
}
130+
124131
func (b Buffer) ToNumber(args ...int) (v int64) {
125132
n := b.Slice(args...)
126133
for i := len(n) - 1; i >= 0; i-- {
@@ -136,7 +143,7 @@ func (b Buffer) ToLine(args ...int) string {
136143
}
137144
pos := bytes.IndexByte(n, byte('\n'))
138145
if pos > 0 {
139-
return __rawString(n[:pos])
146+
n = n[:pos]
140147
}
141-
return ""
148+
return __rawString(n)
142149
}

base/encode.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@ package base
22

33
import (
44
"encoding/base64"
5+
"golang.org/x/text/encoding/simplifiedchinese"
6+
"golang.org/x/text/transform"
57
"unsafe"
68
)
79

810
var EncodeTable = map[string]func(Buffer) string{
9-
"raw": __rawString,
10-
"HEX": __hexUpperString,
11-
"hex": __hexLowerString,
12-
"base64": __base64String,
13-
"gbk": __gbkString,
11+
"raw": __rawString,
12+
"utf8": __rawString,
13+
"HEX": __hexUpperString,
14+
"hex": __hexLowerString,
15+
"base64": __base64String,
16+
"gbk": __gbkString,
17+
"gb18030": __gb18030String,
1418
}
1519

1620
func __hexUpperString(src Buffer) string {
1721
return __hexString(src, "0123456789ABCDEF")
1822
}
23+
1924
func __hexLowerString(src Buffer) string {
2025
return __hexString(src, "0123456789abcdef")
2126
}
27+
2228
func __hexString(src Buffer, tab string) string {
2329
dst := make([]byte, len(src)*2)
2430
for i, v := range src {
@@ -36,6 +42,12 @@ func __rawString(src Buffer) string {
3642
return *(*string)(unsafe.Pointer(&src))
3743
}
3844

45+
func __gb18030String(src Buffer) string {
46+
dst, _, _ := transform.Bytes(simplifiedchinese.GB18030.NewDecoder(), src)
47+
return *(*string)(unsafe.Pointer(&dst))
48+
}
49+
3950
func __gbkString(src Buffer) string {
40-
return ""
51+
dst, _, _ := transform.Bytes(simplifiedchinese.GBK.NewDecoder(), src)
52+
return *(*string)(unsafe.Pointer(&dst))
4153
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ module github.com/vlorc/lua-vm
33
require (
44
github.com/yuin/gopher-lua v0.0.0-20180827083657-b942cacc89fe
55
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd
6+
golang.org/x/text v0.3.0
67
layeh.com/gopher-luar v1.0.4
78
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ github.com/yuin/gopher-lua v0.0.0-20180827083657-b942cacc89fe h1:5Zfs+TirasJUUDU
22
github.com/yuin/gopher-lua v0.0.0-20180827083657-b942cacc89fe/go.mod h1:aEV29XrmTYFr3CiRxZeGHpkvbwq+prZduBqMaascyCU=
33
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
44
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
5+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
6+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
57
layeh.com/gopher-luar v1.0.4 h1:BFgt94J/CXh4HkDcE2b7A7pBaVeQKEVfHEBRKL/K/Tc=
68
layeh.com/gopher-luar v1.0.4/go.mod h1:N3rev/ttQd8yVluXaYsa0M/eknzRYWe+pxZ35ZFmaaI=

net/http/http.go

Lines changed: 93 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package http
22

33
import (
44
"bytes"
5+
"compress/gzip"
56
"context"
67
"crypto/tls"
78
"encoding/json"
89
"github.com/vlorc/lua-vm/base"
910
vmnet "github.com/vlorc/lua-vm/net"
11+
"golang.org/x/text/encoding/simplifiedchinese"
12+
"golang.org/x/text/transform"
1013
"io"
1114
"io/ioutil"
1215
"net"
@@ -52,54 +55,53 @@ func __client(driver vmnet.NetDriver, config *tls.Config) *http.Client {
5255
}
5356
}
5457

55-
func (f *HTTPFactory) __do(method, rawurl string, body io.Reader) (*http.Response, error) {
56-
req, err := http.NewRequest(method, rawurl, body)
57-
if err != nil {
58-
return nil, err
59-
}
60-
return f.client.Do(req)
61-
}
62-
63-
func (f *HTTPFactory) Delete(rawurl string) (*http.Response, error) {
64-
return f.__do("DELETE", rawurl, nil)
65-
}
66-
67-
func (f *HTTPFactory) Put(rawurl string) (*http.Response, error) {
68-
69-
return f.__do("PUT", rawurl, nil)
58+
type Body struct {
59+
io.Reader
60+
closer []io.Closer
7061
}
7162

72-
func (f *HTTPFactory) Get(rawurl string) (*http.Response, error) {
73-
return f.client.Get(rawurl)
63+
func (b *Body) Close() error {
64+
for i := len(b.closer) - 1; i >= 0; i-- {
65+
b.closer[i].Close()
66+
}
67+
return nil
7468
}
75-
76-
func (f *HTTPFactory) Post(rawurl, contentType string, body io.Reader) (*http.Response, error) {
77-
return f.client.Post(rawurl, contentType, body)
69+
func (b *Body) Append(c io.Closer) {
70+
b.closer = append(b.closer, c)
7871
}
79-
80-
func (f *HTTPFactory) PostJson(rawurl string, values interface{}, args ...string) (*http.Response, error) {
81-
contentType := "application/json"
82-
if len(args) > 0 {
83-
contentType = args[0]
72+
func __response(resp *http.Response, err error) (*http.Response, error) {
73+
if nil != err {
74+
return nil, err
8475
}
85-
r, w := io.Pipe()
86-
go func() {
87-
json.NewEncoder(w).Encode(values)
88-
w.Close()
89-
}()
90-
return f.client.Post(rawurl, contentType, r)
91-
}
92-
93-
func (f *HTTPFactory) PostForm(rawurl string, values url.Values, args ...string) (*http.Response, error) {
94-
contentType := "application/x-www-form-urlencoded"
95-
if len(args) > 0 {
96-
contentType = args[0]
76+
reader := &Body{
77+
Reader: resp.Body,
78+
closer: []io.Closer{resp.Body},
79+
}
80+
if resp.Header.Get("Content-Encoding") == "gzip" {
81+
ungzip, err := gzip.NewReader(reader.Reader)
82+
if nil != err {
83+
return resp, err
84+
}
85+
defer ungzip.Close()
86+
reader.Reader = ungzip
87+
reader.Append(ungzip)
88+
}
89+
if strings.Contains(resp.Header.Get("Content-Type"), "charset=GBK") {
90+
reader.Reader = transform.NewReader(reader.Reader, simplifiedchinese.GBK.NewDecoder())
9791
}
98-
return f.client.Post(rawurl, contentType, strings.NewReader(values.Encode()))
92+
resp.Body = reader
93+
return resp, nil
9994
}
10095

101-
func (f *HTTPFactory) Head(rawurl string) (*http.Response, error) {
102-
return f.__do("HEAD", rawurl, nil)
96+
func (f *HTTPFactory) __do(method, rawurl string, body io.Reader, header ...string) (*http.Response, error) {
97+
req, err := http.NewRequest(method, rawurl, body)
98+
if err != nil {
99+
return nil, err
100+
}
101+
for i, l := 0, len(header); i < l; i += 2 {
102+
req.Header.Set(header[i*2+0], header[i*2+1])
103+
}
104+
return __response(f.client.Do(req))
103105
}
104106

105107
func (f *HTTPFactory) Do(r *Request) (*http.Response, error) {
@@ -127,8 +129,57 @@ func (f *HTTPFactory) Do(r *Request) (*http.Response, error) {
127129
req.AddCookie(cookie)
128130
}
129131
}
132+
return __response(f.client.Do(req))
133+
}
134+
135+
func (f *HTTPFactory) Delete(rawurl string) (*http.Response, error) {
136+
return f.__do("DELETE", rawurl, nil)
137+
}
138+
func (f *HTTPFactory) Put(rawurl string) (*http.Response, error) {
139+
return f.__do("PUT", rawurl, nil)
140+
}
141+
func (f *HTTPFactory) Get(rawurl string) (*http.Response, error) {
142+
return f.__do("GET", rawurl, nil)
143+
}
144+
func (f *HTTPFactory) Post(rawurl, contentType string, body io.Reader) (*http.Response, error) {
145+
return f.__do("POST", rawurl, body, "Content-Type", contentType)
146+
}
147+
func (f *HTTPFactory) Head(rawurl string) (*http.Response, error) {
148+
return f.__do("HEAD", rawurl, nil)
149+
}
150+
func (f *HTTPFactory) PostJson(rawurl string, values interface{}, args ...string) (*http.Response, error) {
151+
contentType := "application/json"
152+
if len(args) > 0 {
153+
contentType = args[0]
154+
}
155+
r, w := io.Pipe()
156+
go func() {
157+
json.NewEncoder(w).Encode(values)
158+
w.Close()
159+
}()
160+
return f.Post(rawurl, contentType, r)
161+
}
162+
func (f *HTTPFactory) PostForm(rawurl string, values url.Values, args ...string) (*http.Response, error) {
163+
contentType := "application/x-www-form-urlencoded"
164+
if len(args) > 0 {
165+
contentType = args[0]
166+
}
167+
return f.Post(rawurl, contentType, strings.NewReader(values.Encode()))
168+
}
169+
func (f *HTTPFactory) PostString(rawurl string, values string, args ...string) (*http.Response, error) {
170+
contentType := "text/plain"
171+
if len(args) > 0 {
172+
contentType = args[0]
173+
}
174+
return f.Post(rawurl, contentType, strings.NewReader(values))
175+
}
130176

131-
return f.client.Do(req)
177+
func (f *HTTPFactory) PostBuffer(rawurl string, values base.Buffer, args ...string) (*http.Response, error) {
178+
contentType := "application/octet-stream"
179+
if len(args) > 0 {
180+
contentType = args[0]
181+
}
182+
return f.Post(rawurl, contentType, bytes.NewReader(values))
132183
}
133184

134185
func (f *HTTPFactory) GetString(rawurl string) (string, error) {
@@ -140,7 +191,7 @@ func (f *HTTPFactory) GetString(rawurl string) (string, error) {
140191
}
141192

142193
func (f *HTTPFactory) GetBuffer(rawurl string) (base.Buffer, error) {
143-
resp, err := f.client.Get(rawurl)
194+
resp, err := f.Get(rawurl)
144195
if nil != err {
145196
return nil, err
146197
}
@@ -152,19 +203,3 @@ func (f *HTTPFactory) GetBuffer(rawurl string) (base.Buffer, error) {
152203
}
153204
return base.Buffer(buf), nil
154205
}
155-
156-
func (f *HTTPFactory) PostString(rawurl string, values string, args ...string) (*http.Response, error) {
157-
contentType := "text/plain"
158-
if len(args) > 0 {
159-
contentType = args[0]
160-
}
161-
return f.client.Post(rawurl, contentType, strings.NewReader(values))
162-
}
163-
164-
func (f *HTTPFactory) PostBuffer(rawurl string, values base.Buffer, args ...string) (*http.Response, error) {
165-
contentType := "application/octet-stream"
166-
if len(args) > 0 {
167-
contentType = args[0]
168-
}
169-
return f.client.Post(rawurl, contentType, bytes.NewReader(values))
170-
}

0 commit comments

Comments
 (0)