mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 21:03:41 +00:00
resolve
This commit is contained in:
1 parent
dace77c07c
commit
00468e4e4a
6 files changed
+124
-27
No files matched your search
+51
-18
@@ -4,38 +4,39 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Resolve 解析下载请求
|
||||
// Resolve return the file response to be downloaded
|
||||
func Resolve(request *Request) (*Response, error) {
|
||||
// Build request
|
||||
httpRequest, err := http.NewRequest(strings.ToUpper(request.Method), request.URL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
/* for k, v := range request.Header {
|
||||
for k, v := range request.Header {
|
||||
httpRequest.Header.Add(k, v)
|
||||
} */
|
||||
httpClient := &http.Client{}
|
||||
}
|
||||
// Use "Range" header to resolve
|
||||
httpRequest.Header.Add("Range", "bytes=0-0")
|
||||
// Cookie handle
|
||||
jar, _ := cookiejar.New(nil)
|
||||
httpClient := &http.Client{Jar: jar}
|
||||
response, err := httpClient.Do(httpRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if response.StatusCode != 200 && response.StatusCode != 206 {
|
||||
fmt.Println(response.Header)
|
||||
body, _ := ioutil.ReadAll(response.Body)
|
||||
fmt.Println(string(body))
|
||||
return nil, fmt.Errorf("Response status error:%d", response.StatusCode)
|
||||
}
|
||||
|
||||
ret := &Response{}
|
||||
|
||||
//解析文件名
|
||||
// Get file name by "Content-Disposition"
|
||||
contentDisposition := response.Header.Get("Content-Disposition")
|
||||
if contentDisposition != "" {
|
||||
_, params, _ := mime.ParseMediaType(contentDisposition)
|
||||
@@ -44,19 +45,51 @@ func Resolve(request *Request) (*Response, error) {
|
||||
ret.Name = filename
|
||||
}
|
||||
}
|
||||
// Get file name by URL
|
||||
if ret.Name == "" {
|
||||
|
||||
parse, err := url.Parse(request.URL)
|
||||
if err == nil {
|
||||
// e.g. /files/test.txt => test.txt
|
||||
ret.Name = subLastSlash(parse.Path)
|
||||
}
|
||||
}
|
||||
//解析文件大小
|
||||
contentLength := response.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
ret.size, _ = strconv.ParseInt(contentLength, 10, 64)
|
||||
// Unknow file name
|
||||
if ret.Name == "" {
|
||||
ret.Name = "unknow"
|
||||
}
|
||||
// Is support range
|
||||
ret.Range = response.StatusCode == 206
|
||||
// Get file size
|
||||
if ret.Range {
|
||||
contentRange := response.Header.Get("Content-Range")
|
||||
if contentRange != "" {
|
||||
// e.g. bytes 0-1000/1001 => 1001
|
||||
total := subLastSlash(contentRange)
|
||||
if total != "" && total != "*" {
|
||||
parse, err := strconv.ParseInt(total, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret.Size = parse
|
||||
}
|
||||
}
|
||||
} else {
|
||||
contentLength := response.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
ret.Size, _ = strconv.ParseInt(contentLength, 10, 64)
|
||||
}
|
||||
}
|
||||
//判断是否支持分段下载
|
||||
ret.Partial = ret.size > 0 && response.StatusCode == 206
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func subLastSlash(str string) string {
|
||||
index := strings.LastIndex(str, "/")
|
||||
if index != -1 {
|
||||
return str[index+1:]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Down 下载
|
||||
func Down(request *Request) {
|
||||
httpRequest, err := http.NewRequest(request.Method, request.URL, bytes.NewReader(request.content))
|
||||
|
||||
+18
-3
@@ -1,7 +1,10 @@
|
||||
package down
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -42,6 +45,7 @@ func TestDown(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolve(t *testing.T) {
|
||||
// os.Setenv("HTTP_PROXY", "http://127.0.0.1:8888")
|
||||
type args struct {
|
||||
request *Request
|
||||
}
|
||||
@@ -56,19 +60,19 @@ func TestResolve(t *testing.T) {
|
||||
args{
|
||||
&Request{
|
||||
"get",
|
||||
"https://github.com/proxyee-down-org/proxyee-down/releases/download/3.4/proxyee-down-main.jar",
|
||||
"http://github.com/proxyee-down-org/proxyee-down/releases/download/3.4/proxyee-down-main.jar",
|
||||
map[string]string{
|
||||
"Host": "github.com",
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
|
||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
|
||||
"Referer": "https://github.com/proxyee-down-org/proxyee-down/releases",
|
||||
"Referer": "http://github.com/proxyee-down-org/proxyee-down/releases",
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
|
||||
},
|
||||
nil,
|
||||
},
|
||||
},
|
||||
&Response{"proxyee-down-main.jar", 30159703, false},
|
||||
&Response{"proxyee-down-main.jar", 30159703, true},
|
||||
false,
|
||||
},
|
||||
}
|
||||
@@ -85,3 +89,14 @@ func TestResolve(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemp(t *testing.T) {
|
||||
parse, _ := url.Parse("http://www.baidu.com/asda/text.txt?a=11&b=333")
|
||||
arr := strings.Split(parse.Path, "/")
|
||||
for i := range arr {
|
||||
fmt.Println(arr[i])
|
||||
}
|
||||
// var begin, end, total int64
|
||||
//fmt.Sscanf("bytes 0-1000/1001", "%s %d-%d/%d", _, &begin, &end, &total)
|
||||
//fmt.Printf("%d-%d/%d", begin, end, total)
|
||||
}
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package down
|
||||
|
||||
type Response struct {
|
||||
Name string
|
||||
size int64
|
||||
Partial bool
|
||||
Name string
|
||||
Size int64
|
||||
Range bool
|
||||
}
|
||||
Reference in new issue
Block a user