move option code to option.go file

This commit is contained in:
huangsimin 2018-11-30 17:04:40 +08:00
parent 4710732833
commit 7cf976d94a
2 changed files with 146 additions and 139 deletions

146
option.go
View File

@ -1,5 +1,16 @@
package curl2info
import (
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
"474420502.top/eson/requests"
)
func init() {
optionTrie = newTrie()
oelist := []*optionExecute{
@ -65,3 +76,138 @@ func (oe *optionExecute) BuildFunction(curl *CURL, soption string) *parseFunctio
}
return &parseFunction{ParamCURL: curl, ParamData: data, ExecuteFunction: oe.Execute, Priority: oe.Priority}
}
func judgeOptions(u *CURL, soption string) *parseFunction {
word := trieStrWord(soption)
if ioe := optionTrie.SearchMostPrefix(&word); ioe != nil {
oe := ioe.(*optionExecute)
return oe.BuildFunction(u, soption)
}
log.Println(soption, " no haved this option")
return nil
}
func extractData(re, soption string) string {
datas := regexp.MustCompile(re).FindStringSubmatch(soption)
return strings.Trim(datas[1], "'")
}
func parseITask(u *CURL, value string) {
u.ITask = value
}
func parseTimeout(u *CURL, value string) {
timeout, err := strconv.Atoi(value)
if err != nil {
panic(err)
}
u.Timeout = timeout
}
func parseInsecure(u *CURL, soption string) {
u.Insecure = true
}
func parseUser(u *CURL, soption string) {
auth := strings.Split(soption, ":")
u.Auth = &requests.BasicAuth{User: auth[0], Password: auth[1]}
}
func parseUserAgent(u *CURL, value string) {
u.Header.Add("User-Agent", value)
}
func parseOptI(u *CURL, soption string) {
u.Method = "HEAD"
}
func parseOptX(u *CURL, soption string) {
matches := regexp.MustCompile("-X +(.+)").FindStringSubmatch(soption)
method := strings.Trim(matches[1], "'")
u.Method = method
}
func parseBodyURLEncode(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
u.Body.SetIOBody(data)
}
func parseBodyRaw(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
u.Body.SetIOBody(data)
}
func parseBodyASCII(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
if data[0] != '@' {
u.Body.SetIOBody(data)
} else {
f, err := os.Open(data[1:])
if err != nil {
panic(err)
}
defer f.Close()
bdata, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
u.Body.SetIOBody(bdata)
}
}
// 处理@ 并且替/r/n符号
func parseBodyBinary(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
if data[0] != '@' {
u.Body.SetIOBody(data)
} else {
f, err := os.Open(data[1:])
if err != nil {
panic(err)
}
defer f.Close()
bdata, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
bdata = regexp.MustCompile("\n|\r").ReplaceAll(bdata, []byte(""))
u.Body.SetIOBody(bdata)
}
}
func parseHeader(u *CURL, soption string) {
matches := regexp.MustCompile(`'([^:]+): ([^']+)'`).FindAllStringSubmatch(soption, 1)[0]
key := matches[1]
value := matches[2]
switch key {
case "Cookie":
u.Cookies = ReadRawCookies(value, "")
u.CookieJar.SetCookies(u.ParsedURL, u.Cookies)
case "Content-Type":
u.Body.SetPrefix(value)
default:
u.Header.Add(key, value)
}
}

View File

@ -2,14 +2,10 @@ package curl2info
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"474420502.top/eson/requests"
@ -150,138 +146,3 @@ func ParseRawCURL(scurl string) (cURL *CURL, err error) {
return curl, nil
}
func judgeOptions(u *CURL, soption string) *parseFunction {
word := trieStrWord(soption)
if ioe := optionTrie.SearchMostPrefix(&word); ioe != nil {
oe := ioe.(*optionExecute)
return oe.BuildFunction(u, soption)
}
log.Println(soption, " no haved this option")
return nil
}
func extractData(re, soption string) string {
datas := regexp.MustCompile(re).FindStringSubmatch(soption)
return strings.Trim(datas[1], "'")
}
func parseITask(u *CURL, value string) {
u.ITask = value
}
func parseTimeout(u *CURL, value string) {
timeout, err := strconv.Atoi(value)
if err != nil {
panic(err)
}
u.Timeout = timeout
}
func parseInsecure(u *CURL, soption string) {
u.Insecure = true
}
func parseUser(u *CURL, soption string) {
auth := strings.Split(soption, ":")
u.Auth = &requests.BasicAuth{User: auth[0], Password: auth[1]}
}
func parseUserAgent(u *CURL, value string) {
u.Header.Add("User-Agent", value)
}
func parseOptI(u *CURL, soption string) {
u.Method = "HEAD"
}
func parseOptX(u *CURL, soption string) {
matches := regexp.MustCompile("-X +(.+)").FindStringSubmatch(soption)
method := strings.Trim(matches[1], "'")
u.Method = method
}
func parseBodyURLEncode(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
u.Body.SetIOBody(data)
}
func parseBodyRaw(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
u.Body.SetIOBody(data)
}
func parseBodyASCII(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
if data[0] != '@' {
u.Body.SetIOBody(data)
} else {
f, err := os.Open(data[1:])
if err != nil {
panic(err)
}
defer f.Close()
bdata, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
u.Body.SetIOBody(bdata)
}
}
// 处理@ 并且替/r/n符号
func parseBodyBinary(u *CURL, data string) {
if u.Method != "" {
u.Method = "POST"
}
u.Body.SetPrefix(requests.TypeURLENCODED)
if data[0] != '@' {
u.Body.SetIOBody(data)
} else {
f, err := os.Open(data[1:])
if err != nil {
panic(err)
}
defer f.Close()
bdata, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
bdata = regexp.MustCompile("\n|\r").ReplaceAll(bdata, []byte(""))
u.Body.SetIOBody(bdata)
}
}
func parseHeader(u *CURL, soption string) {
matches := regexp.MustCompile(`'([^:]+): ([^']+)'`).FindAllStringSubmatch(soption, 1)[0]
key := matches[1]
value := matches[2]
switch key {
case "Cookie":
u.Cookies = ReadRawCookies(value, "")
u.CookieJar.SetCookies(u.ParsedURL, u.Cookies)
case "Content-Type":
u.Body.SetPrefix(value)
default:
u.Header.Add(key, value)
}
}