Ejemplo n.º 1
0
func main() {
	curl.GlobalInit(curl.GLOBAL_ALL)

	// init the curl session
	easy := curl.EasyInit()
	defer easy.Cleanup()

	// set URL to get
	easy.Setopt(curl.OPT_URL, "http://cn.bing.com/")

	// no progress meter
	easy.Setopt(curl.OPT_NOPROGRESS, true)

	easy.Setopt(curl.OPT_WRITEFUNCTION, write_data)

	// write file
	fp, _ := os.OpenFile(bodyfilename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
	defer fp.Close()
	easy.Setopt(curl.OPT_WRITEDATA, fp)

	// easy.Setopt(curl.OPT_WRITEHEADER, 0)

	if err := easy.Perform(); err != nil {
		println("ERROR: ", err.Error())
	}

}
Ejemplo n.º 2
0
func main() {
	// init the curl session

	var username *string = flag.String("username", "test", "renren.com email")
	var password *string = flag.String("password", "test", "renren.com password")

	flag.Parse()

	easy := curl.EasyInit()
	defer easy.Cleanup()

	easy.Setopt(curl.OPT_URL, "http://m.renren.com/home.do")

	easy.Setopt(curl.OPT_PORT, 80)
	easy.Setopt(curl.OPT_VERBOSE, true)

	easy.Setopt(curl.OPT_COOKIEJAR, "./cookie.jar")

	// disable HTTP/1.1 Expect: 100-continue
	easy.Setopt(curl.OPT_HTTPHEADER, []string{"Expect:"})

	postdata := "email=" + *username + "&password="******"&login="******"登录")
	easy.Setopt(curl.OPT_POSTFIELDS, postdata)

	if err := easy.Perform(); err != nil {
		println("ERROR: ", err.Error())
	}
}
Ejemplo n.º 3
0
func main() {
	curl.GlobalInit(curl.GLOBAL_DEFAULT)
	defer curl.GlobalCleanup()
	easy := curl.EasyInit()
	defer easy.Cleanup()

	easy.Setopt(curl.OPT_URL, "ftp://ftp.gnu.org/README")

	// define our callback use lambda function
	easy.Setopt(curl.OPT_WRITEFUNCTION, func(ptr []byte, userdata interface{}) bool {
		file := userdata.(*os.File)
		if _, err := file.Write(ptr); err != nil {
			return false
		}
		return true
	})

	fp, _ := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
	defer fp.Close() // defer close

	easy.Setopt(curl.OPT_WRITEDATA, fp)

	easy.Setopt(curl.OPT_VERBOSE, true)

	if err := easy.Perform(); err != nil {
		println("ERROR", err.Error())
	}
}
Ejemplo n.º 4
0
func main() {
	curl.GlobalInit(curl.GLOBAL_ALL)

	// init the curl session
	easy := curl.EasyInit()
	defer easy.Cleanup()

	easy.Setopt(curl.OPT_URL, "http://curl.haxx.se/download/curl-7.22.0.tar.gz")

	easy.Setopt(curl.OPT_WRITEFUNCTION, write_data)

	easy.Setopt(curl.OPT_NOPROGRESS, false)

	started := int64(0)
	easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool {
		// canceled when 50% finished
		if dlnow/dltotal > 0.5 {
			println("")
			// abort downloading
			return false
		}
		if started == 0 {
			started = time.Now().Unix()
		}
		fmt.Printf("Downloaded: %3.2f%%, Speed: %.1fKiB/s \r", dlnow/dltotal*100, dlnow/1000/float64((time.Now().Unix()-started)))
		return true
	})

	if err := easy.Perform(); err != nil {
		fmt.Printf("ERROR: %v\n", err)
	}
}
Ejemplo n.º 5
0
func main() {
	// init the curl session
	easy := curl.EasyInit()
	defer easy.Cleanup()

	easy.Setopt(curl.OPT_URL, "http://www.google.com")

	easy.Setopt(curl.OPT_POST, true)
	easy.Setopt(curl.OPT_VERBOSE, true)

	easy.Setopt(curl.OPT_READFUNCTION,
		func(ptr []byte, userdata interface{}) int {
			// WARNING: never use append()
			if !sent {
				sent = true
				ret := copy(ptr, POST_DATA)
				return ret
			}
			return 0 // sent ok
		})

	// disable HTTP/1.1 Expect 100
	easy.Setopt(curl.OPT_HTTPHEADER, []string{"Expect:"})
	// must set
	easy.Setopt(curl.OPT_POSTFIELDSIZE, len(POST_DATA))

	if err := easy.Perform(); err != nil {
		println("ERROR: ", err.Error())
	}

	time.Sleep(10000) // wait gorotine
}
Ejemplo n.º 6
0
func main() {
	// init the curl session

	easy := curl.EasyInit()
	defer easy.Cleanup()

	easy.Setopt(curl.OPT_URL, "http://www.renren.com")

	easy.Setopt(curl.OPT_PORT, 80)
	easy.Setopt(curl.OPT_VERBOSE, true)
	easy.Setopt(curl.OPT_CONNECT_ONLY, true)

	easy.Setopt(curl.OPT_WRITEFUNCTION, nil)

	if err := easy.Perform(); err != nil {
		println("ERROR: ", err.Error())
	}

	easy.Send([]byte("HEAD / HTTP/1.0\r\nHost: www.renren.com\r\n\r\n"))

	buf := make([]byte, 1000)
	time.Sleep(1000000000) // wait gorotine
	num, err := easy.Recv(buf)
	if err != nil {
		println("ERROR:", err.Error())
	}
	println("recv num = ", num)
	// NOTE: must use buf[:num]
	println(string(buf[:num]))

	fmt.Printf("got:\n%#v\n", string(buf[:num]))
}
Ejemplo n.º 7
0
func main() {
	curl.GlobalInit(curl.GLOBAL_ALL)

	// init the curl session
	easy := curl.EasyInit()
	defer easy.Cleanup()

	easy.Setopt(curl.OPT_URL, "http://cn.bing.com/")

	easy.Setopt(curl.OPT_WRITEFUNCTION, write_data)

	// make a chan
	ch := make(chan string, 100)
	go func(ch chan string) {
		for {
			data := <-ch
			println("Got data size=", len(data))
		}
	}(ch)

	easy.Setopt(curl.OPT_WRITEDATA, ch)

	if err := easy.Perform(); err != nil {
		println("ERROR: ", err.Error())
	}

	time.Sleep(10000) // wait gorotine
}
Ejemplo n.º 8
0
func main() {
	easy := curl.EasyInit()
	defer easy.Cleanup()
	if easy != nil {
		easy.Setopt(curl.OPT_URL, "http://www.google.com/")
		easy.Perform()
	}
}
Ejemplo n.º 9
0
func main() {
	easy := curl.EasyInit()
	defer easy.Cleanup()
	if easy != nil {
		easy.Setopt(curl.OPT_URL, "https://mail.google.com/")
		// skip_peer_verification
		easy.Setopt(curl.OPT_SSL_VERIFYPEER, false) // 0 is ok

		easy.Perform()
	}
}
Ejemplo n.º 10
0
func main() {
	// init the curl session

	easy := curl.EasyInit()
	defer easy.Cleanup()

	posturl := getUploadUrl()

	easy.Setopt(curl.OPT_URL, posturl)

	easy.Setopt(curl.OPT_PORT, 80)
	easy.Setopt(curl.OPT_VERBOSE, true)

	// save cookie and load cookie
	easy.Setopt(curl.OPT_COOKIEFILE, "./cookie.jar")
	easy.Setopt(curl.OPT_COOKIEJAR, "./cookie.jar")

	// disable HTTP/1.1 Expect: 100-continue
	easy.Setopt(curl.OPT_HTTPHEADER, []string{"Expect:"})

	form := curl.NewForm()
	form.Add("albumid", "452618633") // your album id
	form.AddFile("theFile", "./test.jpg")
	form.Add("description", "我就尝试下这段代码靠谱不。。截图下看看")
	form.Add("post", "上传照片")

	easy.Setopt(curl.OPT_HTTPPOST, form)

	// print upload progress
	easy.Setopt(curl.OPT_NOPROGRESS, false)
	easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, _ interface{}) bool {
		fmt.Printf("Download %3.2f%%, Uploading %3.2f%%\r", dlnow/dltotal*100, ulnow/ultotal*100)
		return true
	})

	if err := easy.Perform(); err != nil {
		println("ERROR: ", err.Error())
	}

	time.Sleep(1000000000) // wait gorotine

}
Ejemplo n.º 11
0
func getUploadUrl() string {
	page := ""
	easy := curl.EasyInit()
	defer easy.Cleanup()

	u := "http://3g.renren.com/album/wuploadphoto.do"
	easy.Setopt(curl.OPT_URL, u)
	easy.Setopt(curl.OPT_COOKIEFILE, "./cookie.jar")
	easy.Setopt(curl.OPT_COOKIEJAR, "./cookie.jar")
	easy.Setopt(curl.OPT_VERBOSE, true)
	easy.Setopt(curl.OPT_WRITEFUNCTION, func(ptr []byte, _ interface{}) bool {
		page += string(ptr)
		return true
	})
	easy.Perform()
	// extract url from
	// <form enctype="multipart/form-data" action="http://3g.renren.com/album/wuploadphoto.do?type=3&amp;sid=zv3tiXTZr6Cu1rj5dhgX_X"
	pattern, _ := regexp.Compile(`action="(.*?)"`)

	if matches := pattern.FindStringSubmatch(page); len(matches) == 2 {
		return matches[1]
	}
	return ""
}
Ejemplo n.º 12
0
func main() {
	println("DEBUG chdir=>", os.Chdir("/sadf"))
	ret := curl.EasyInit()
	defer ret.Cleanup()
	print("init =>", ret, " ", reflect.TypeOf(ret).String(), endl)
	ret = ret.Duphandle()
	defer ret.Cleanup()

	print("dup =>", ret, " ", reflect.TypeOf(ret).String(), endl)
	print("global init =>", curl.GlobalInit(curl.GLOBAL_ALL), endl)
	print("version =>", curl.Version(), endl)
	// debug
	//print("set verbose =>", ret.Setopt(curl.OPT_VERBOSE, true), endl)

	//print("set header =>", ret.Setopt(curl.OPT_HEADER, true), endl)

	// auto calculate port
	// print("set port =>", ret.EasySetopt(curl.OPT_PORT, 6060), endl)
	fmt.Printf("XXXX debug setopt %#v \n", ret.Setopt(30000, 19).Error())

	print("set timeout =>", ret.Setopt(curl.OPT_TIMEOUT, 20), endl)

	//print("set post size =>", ret.Setopt(curl.OPT_POSTFIELDSIZE, 10), endl)
	if ret.Setopt(curl.OPT_URL, "http://www.baidu.com:8000/") != nil {
		println("set url ok!")
	}
	//print("set url =>", ret.Setopt(curl.OPT_URL, "http://commondatastorage.googleapis.com/chromium-browser-continuous/Linux_x64/104547/chrome-linux.zip"), endl)

	print("set user_agent =>", ret.Setopt(curl.OPT_USERAGENT, "go-curl v0.0.1") == nil, endl)
	// add to DNS cache
	print("set resolve =>", ret.Setopt(curl.OPT_RESOLVE, []string{"www.baidu.com:8000:127.0.0.1"}) == nil, endl)
	// ret.EasyReset()  clean seted

	// currently not finished!
	//
	fooTest := func(buf []byte, userdata interface{}) bool {
		// buf := ptr.([]byte)
		println("size=>", len(buf))
		println("DEBUG(in callback)", buf, userdata)
		println("data = >", string(buf))
		return true
	}

	ret.Setopt(curl.OPT_WRITEFUNCTION, fooTest) // curl.CallbackWriteFunction(fooTest))
	println("set opt!")
	// for test only

	code := ret.Perform()
	//	dump.Dump(code)
	fmt.Printf("code -> %v\n", code)

	println("================================")
	print("pause =>", ret.Pause(curl.PAUSE_ALL), endl)

	print("escape =>", ret.Escape("http://baidu.com/"), endl)
	print("unescape =>", ret.Unescape("http://baidu.com/-%00-%5c"), endl)

	print("unescape lenght =>", len(ret.Unescape("http://baidu.com/-%00-%5c")), endl)
	// print("version info data =>", curl.VersionInfo(1), endl)
	ver := curl.VersionInfo(curl.VERSION_NOW)
	fmt.Printf("VersionInfo: Age: %d, Version:%s, Host:%s, Features:%d, SslVer: %s, LibzV: %s, ssh: %s\n",
		ver.Age, ver.Version, ver.Host, ver.Features, ver.SslVersion, ver.LibzVersion, ver.LibsshVersion)

	print("Protocols:")
	for _, p := range ver.Protocols {
		print(p, ", ")
	}
	print(endl)
	println(curl.Getdate("20111002 15:05:58 +0800").String())
	ret.Getinfo(curl.INFO_EFFECTIVE_URL)
	ret.Getinfo(curl.INFO_RESPONSE_CODE)

	ret.Getinfo(curl.INFO_FILETIME)
	ret.Getinfo(curl.INFO_SSL_ENGINES)

	ret.Getinfo(curl.INFO_TOTAL_TIME)

	println("================================")

	// ret.Getinfo(curl.INFO_SSL_ENGINES)

	/*	mret := curl.MultiInit()
		mret.AddHandle(ret)			// works
		defer mret.Cleanup()
		if ok, handles := mret.Perform(); ok == curl.OK {
			fmt.Printf("ok=%s, handles=%d\n", ok, handles)
		} else {
			fmt.Printf("error calling multi\n")
		}
	*/
	println("================================")
	//println(curl.GlobalInit(curl.GLOBAL_SSL))
}
Ejemplo n.º 13
0
func DoHttpCheck(task *Task, opts *Options) (result *Result, eval error) {
	easy := curl.EasyInit()
	defer easy.Cleanup()

	result = new(Result)

	//curl options
	easy.Setopt(curl.OPT_URL, task.address)
	easy.Setopt(curl.OPT_NOSIGNAL, 1)
	easy.Setopt(curl.OPT_USERAGENT, "Uptimo.com Website Performance Monitoring web-agent version-0.2 http://uptimo.com")
	easy.Setopt(curl.OPT_FORBID_REUSE, 1)

	//timeouts
	easy.Setopt(curl.OPT_TIMEOUT, 30)
	easy.Setopt(curl.OPT_CONNECTTIMEOUT, 30)

	if opts.follow {
		easy.Setopt(curl.OPT_FOLLOWLOCATION, 1)
		easy.Setopt(curl.OPT_MAXREDIRS, int(opts.redirects))
	}

	if opts.port > 0 {
		easy.Setopt(curl.OPT_PORT, int(opts.port))
	}

	if !opts.withBody {
		easy.Setopt(curl.OPT_NOBODY, 1)
	}

	err := easy.Perform()

	if err != nil {
		switch err {
		case errors.New(fmt.Sprintf("%d", curl.E_COULDNT_RESOLVE_HOST)):
			result.msg = "DNSE" //DNS Error
		case errors.New(fmt.Sprintf("%d", curl.E_COULDNT_CONNECT)):
			result.msg = "CNC" //Could not connect
		case errors.New(fmt.Sprintf("%d", curl.E_HTTP_RETURNED_ERROR)):
			result.msg = "DNF" // http error
			msg, _ := easy.Getinfo(curl.INFO_RESPONSE_CODE)
			result.httpMsg = msg.(int32)
		case errors.New(fmt.Sprintf("%d", curl.E_OPERATION_TIMEDOUT)):
			result.msg = "TO" // timeout
		case errors.New(fmt.Sprintf("%d", curl.E_TOO_MANY_REDIRECTS)):
			result.msg = "TMR" //  too many redirects
		case errors.New(fmt.Sprintf("%d", curl.E_SEND_ERROR)):
			result.msg = "IESEND" // internal error
		case errors.New(fmt.Sprintf("%d", curl.E_RECV_ERROR)):
			result.msg = "IERECV" // internal error
		case errors.New(fmt.Sprintf("%d", curl.E_BAD_CONTENT_ENCODING)):
			result.msg = "BCE" // bad content encoding
		case errors.New(fmt.Sprintf("%d", curl.E_SSL_CONNECT_ERROR)):
			result.msg = "SSL"
		default:
			result.msg = fmt.Sprintf("UKE,%s", err)
		}
	} else {
		msg, err := easy.Getinfo(curl.INFO_RESPONSE_CODE)
		result.httpMsg = int32(msg.(int))
		if err != nil {
			eval = errors.New("CGE") //curl general error
			return
		}

		red, err := easy.Getinfo(curl.INFO_REDIRECT_COUNT)
		result.numRedirects = uint16(red.(int))
		if err != nil {
			eval = errors.New("CGE") //curl general error
			return
		}

		if result.httpMsg == 200 || result.httpMsg == 302 {
			result.success = true
			result.msg = "OK"
		} else {
			result.success = false
			result.msg = fmt.Sprint("%d", result.httpMsg)
		}
	}

	result.timeline = *NewTimeline(easy)

	return

}