master
兔子 4 years ago
parent 6eadbc3c6b
commit b02c1f0a81

@ -1,158 +0,0 @@
package starnet
import (
"errors"
"fmt"
"io"
"time"
)
type CircleByteBuffer struct {
io.Reader
io.Writer
io.Closer
datas []byte
start int
end int
size int
isClose bool
isEnd bool
}
func NewCircleByteBuffer(len int) *CircleByteBuffer {
var e = new(CircleByteBuffer)
e.datas = make([]byte, len)
e.start = 0
e.end = 0
e.size = len
e.isClose = false
e.isEnd = false
return e
}
func (e *CircleByteBuffer) getLen() int {
if e.start == e.end {
return 0
} else if e.start < e.end {
return e.end - e.start
} else {
return e.start - e.end
}
}
func (e *CircleByteBuffer) getFree() int {
return e.size - e.getLen()
}
func (e *CircleByteBuffer) putByte(b byte) error {
if e.isClose {
return io.EOF
}
e.datas[e.end] = b
var pos = e.end + 1
for pos == e.start {
if e.isClose {
return io.EOF
}
time.Sleep(time.Microsecond)
}
if pos == e.size {
e.end = 0
} else {
e.end = pos
}
return nil
}
func (e *CircleByteBuffer) getByte() (byte, error) {
if e.isClose {
return 0, io.EOF
}
if e.isEnd && e.getLen() <= 0 {
return 0, io.EOF
}
if e.getLen() <= 0 {
return 0, errors.New("no datas")
}
var ret = e.datas[e.start]
e.start++
if e.start == e.size {
e.start = 0
}
return ret, nil
}
func (e *CircleByteBuffer) geti(i int) byte {
if i >= e.getLen() {
panic("out buffer")
}
var pos = e.start + i
if pos >= e.size {
pos -= e.size
}
return e.datas[pos]
}
/*func (e*CircleByteBuffer)puts(bts []byte){
for i:=0;i<len(bts);i++{
e.put(bts[i])
}
}
func (e*CircleByteBuffer)gets(bts []byte)int{
if bts==nil {return 0}
var ret=0
for i:=0;i<len(bts);i++{
if e.getLen()<=0{break}
bts[i]=e.get()
ret++
}
return ret
}*/
func (e *CircleByteBuffer) Close() error {
e.isClose = true
return nil
}
func (e *CircleByteBuffer) Read(bts []byte) (int, error) {
if e.isClose {
return 0, io.EOF
}
if bts == nil {
return 0, errors.New("bts is nil")
}
var ret = 0
for i := 0; i < len(bts); i++ {
b, err := e.getByte()
if err != nil {
if err == io.EOF {
return ret, err
}
return ret, nil
}
bts[i] = b
ret++
}
if e.isClose {
return ret, io.EOF
}
return ret, nil
}
func (e *CircleByteBuffer) Write(bts []byte) (int, error) {
if e.isClose {
return 0, io.EOF
}
if bts == nil {
e.isEnd = true
return 0, io.EOF
}
var ret = 0
for i := 0; i < len(bts); i++ {
err := e.putByte(bts[i])
if err != nil {
fmt.Println("Write bts err:", err)
return ret, err
}
ret++
}
if e.isClose {
return ret, io.EOF
}
return ret, nil
}

@ -10,9 +10,16 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"time" "time"
) )
const (
HEADER_FORM_URLENCODE = `application/x-www-form-urlencoded`
HEADER_FORM_DATA = `multipart/form-data`
HEADER_JSON = `application/json`
)
type RequestFile struct { type RequestFile struct {
UploadFile string UploadFile string
UploadForm map[string]string UploadForm map[string]string
@ -34,7 +41,7 @@ type Request struct {
RequestFile RequestFile
RespHttpCode int RespHttpCode int
PostBuffer *bytes.Buffer PostBuffer *bytes.Buffer
CircleBuffer *CircleByteBuffer CircleBuffer *StarBuffer
Proxy string Proxy string
Process func(float64) Process func(float64)
} }
@ -49,6 +56,10 @@ func NewRequests(url string, postdata []byte, method string) Request {
WriteRecvData: true, WriteRecvData: true,
} }
req.ReqHeader = make(http.Header) req.ReqHeader = make(http.Header)
if strings.ToUpper(method) == "POST" {
req.ReqHeader.Set("Content-Type", HEADER_FORM_URLENCODE)
}
req.ReqHeader.Set("User-Agent", "B612 / 1.0.0")
return req return req
} }
@ -87,7 +98,7 @@ func Curl(curl Request) (resps Request, err error) {
fpstat, _ := fpsrc.Stat() fpstat, _ := fpsrc.Stat()
filebig := float64(fpstat.Size()) filebig := float64(fpstat.Size())
sum, n := 0, 0 sum, n := 0, 0
fpdst := NewCircleByteBuffer(1048576) fpdst := NewStarBuffer(1048576)
if curl.UploadForm != nil { if curl.UploadForm != nil {
for k, v := range curl.UploadForm { for k, v := range curl.UploadForm {
header := fmt.Sprintf("Content-Disposition: form-data; name=\"%s\";\r\nContent-Type: x-www-form-urlencoded \r\n\r\n", k) header := fmt.Sprintf("Content-Disposition: form-data; name=\"%s\";\r\nContent-Type: x-www-form-urlencoded \r\n\r\n", k)
@ -191,7 +202,7 @@ func netcurl(curl Request) (*http.Response, error) {
} }
if curl.PostBuffer != nil && curl.PostBuffer.Len() > 0 { if curl.PostBuffer != nil && curl.PostBuffer.Len() > 0 {
req, err = http.NewRequest(curl.Method, curl.Url, curl.PostBuffer) req, err = http.NewRequest(curl.Method, curl.Url, curl.PostBuffer)
} else if curl.CircleBuffer != nil && curl.CircleBuffer.getLen() > 0 { } else if curl.CircleBuffer != nil && curl.CircleBuffer.Len() > 0 {
req, err = http.NewRequest(curl.Method, curl.Url, curl.CircleBuffer) req, err = http.NewRequest(curl.Method, curl.Url, curl.CircleBuffer)
} else { } else {
req, err = http.NewRequest(curl.Method, curl.Url, nil) req, err = http.NewRequest(curl.Method, curl.Url, nil)
@ -219,7 +230,10 @@ func netcurl(curl Request) (*http.Response, error) {
}, },
} }
if curl.Proxy != "" { if curl.Proxy != "" {
purl, _ := url.Parse(curl.Proxy) purl, err := url.Parse(curl.Proxy)
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(purl) transport.Proxy = http.ProxyURL(purl)
} }
client := &http.Client{ client := &http.Client{
@ -228,3 +242,24 @@ func netcurl(curl Request) (*http.Response, error) {
resp, err := client.Do(req) resp, err := client.Do(req)
return resp, err return resp, err
} }
func UrlEncodeRaw(str string) string {
strs := strings.Replace(url.QueryEscape(str), "+", "%20", -1)
return strs
}
func UrlEncode(str string) string {
return url.QueryEscape(str)
}
func UrlDecode(str string) (string, error) {
return url.QueryUnescape(str)
}
func Build_Query(queryData map[string]string) string {
query := url.Values{}
for k, v := range queryData {
query.Add(k, v)
}
return query.Encode()
}

@ -0,0 +1,128 @@
package starnet
import (
"errors"
"fmt"
"io"
"sync"
"time"
)
type StarBuffer struct {
io.Reader
io.Writer
io.Closer
datas []byte
pStart int
pEnd int
cap int
isClose bool
isEnd bool
rmu sync.Mutex
wmu sync.Mutex
}
func NewStarBuffer(cap int) *StarBuffer {
rtnBuffer := new(StarBuffer)
rtnBuffer.cap = cap
rtnBuffer.datas = make([]byte, cap)
return rtnBuffer
}
func (star *StarBuffer) Free() int {
return star.cap - star.Len()
}
func (star *StarBuffer) Cap() int {
return star.cap
}
func (star *StarBuffer) Len() int {
length := star.pEnd - star.pStart
if length < 0 {
return star.cap + length - 1
}
return length
}
func (star *StarBuffer) getByte() (byte, error) {
if star.isClose || (star.isEnd && star.Len() == 0) {
return 0, io.EOF
}
if star.Len() == 0 {
return 0, errors.New("no byte available now")
}
data := star.datas[star.pStart]
star.pStart++
if star.pStart == star.cap {
star.pStart = 0
}
return data, nil
}
func (star *StarBuffer) putByte(data byte) error {
if star.isClose || star.isEnd {
return io.EOF
}
kariEnd := star.pEnd + 1
if kariEnd == star.cap {
kariEnd = 0
}
if kariEnd == star.pStart {
for {
time.Sleep(time.Microsecond)
if kariEnd != star.pStart {
break
}
}
}
star.datas[star.pEnd] = data
star.pEnd = kariEnd
return nil
}
func (star *StarBuffer) Close() error {
star.isClose = true
return nil
}
func (star *StarBuffer) Read(buf []byte) (int, error) {
if star.isClose {
return 0, io.EOF
}
if buf == nil {
return 0, errors.New("buffer is nil")
}
star.rmu.Lock()
defer star.rmu.Unlock()
var sum int = 0
for i := 0; i < len(buf); i++ {
data, err := star.getByte()
if err != nil {
if err == io.EOF {
return sum, err
}
return sum, nil
}
buf[i] = data
sum++
}
return sum, nil
}
func (star *StarBuffer) Write(bts []byte) (int, error) {
if bts == nil || star.isClose {
star.isEnd = true
return 0, io.EOF
}
star.wmu.Lock()
defer star.wmu.Unlock()
var sum = 0
for i := 0; i < len(bts); i++ {
err := star.putByte(bts[i])
if err != nil {
fmt.Println("Write bts err:", err)
return sum, err
}
sum++
}
return sum, nil
}
Loading…
Cancel
Save