diff --git a/README.md b/README.md index a299335..492c6e3 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,18 @@ go-curl [![Build Status](https://secure.travis-ci.org/andelf/go-curl.png?branch=master)](http://travis-ci.org/andelf/go-curl) -my golang libcurl(curl) binding. +go-curl is a GoLang interface to [libcurl](https://curl.haxx.se/libcurl/), +the multiprotocol file transfer library. Similar to the HTTP +support in [net/http] (https://pkg.go.dev/net/http), go-curl can be used to +fetch objects from a Go program. While go-curl can provide simple fetches, +it also exposes most of the functionality of libcurl, including: -See more examples in ./examples/ directory~! + * Speed - libcurl is very fast. + * Multiple protocol (not just HTTP). + * SSL, authentication and proxy support. + * Support for libcurl's callbacks. + +This said, libcurl API can be less easy to learn than net/http. LICENSE ------- @@ -22,11 +31,15 @@ Current Development Status * partly implement share & multi interface * new callback function prototype -How to Install --------------- +Requirements +------------ + * Any version of Go + * libcurl 7.x or higher (including development headers and static/dynamic libs) + * Python 3 (used only by configure scripts) -Make Sure You Have libcurl (and its develop headers, static/dynamic libs) installed! +How to Install +-------------- $ go get -u github.com/andelf/go-curl @@ -55,9 +68,9 @@ func main() { easy := curl.EasyInit() defer easy.Cleanup() - easy.Setopt(curl.OPT_URL, "http://www.baidu.com/") + easy.Setopt(curl.OPT_URL, "https://www.baidu.com/") - // make a callback function + // OPTIONAL - make a callback function fooTest := func (buf []byte, userdata interface{}) bool { println("DEBUG: size=>", len(buf)) println("DEBUG: content=>", string(buf)) @@ -71,3 +84,5 @@ func main() { } } ``` + +See also the [examples](./examples/) directory! diff --git a/examples/https.go b/examples/https.go index 0a4e031..a305c30 100644 --- a/examples/https.go +++ b/examples/https.go @@ -8,10 +8,7 @@ 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.Setopt(curl.OPT_URL, "https://baidu.com/") easy.Perform() } } diff --git a/examples/https_callback.go b/examples/https_callback.go new file mode 100644 index 0000000..5de5255 --- /dev/null +++ b/examples/https_callback.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + curl "github.com/andelf/go-curl" +) + +func main() { + easy := curl.EasyInit() + defer easy.Cleanup() + + easy.Setopt(curl.OPT_URL, "https://www.baidu.com/") + + // OPTIONAL - make a callback function + fooTest := func (buf []byte, userdata interface{}) bool { + println("DEBUG: size=>", len(buf)) + println("DEBUG: content=>", string(buf)) + return true + } + + easy.Setopt(curl.OPT_WRITEFUNCTION, fooTest) + + if err := easy.Perform(); err != nil { + fmt.Printf("ERROR: %v\n", err) + } +} diff --git a/examples/https_skip_peer_verify.go b/examples/https_skip_peer_verify.go new file mode 100644 index 0000000..d0aafdc --- /dev/null +++ b/examples/https_skip_peer_verify.go @@ -0,0 +1,20 @@ +package main + +import ( + curl "github.com/andelf/go-curl" +) + +func main() { + easy := curl.EasyInit() + defer easy.Cleanup() + if easy != nil { + easy.Setopt(curl.OPT_URL, "https://mail.google.com/") + + // OPT_SSL_VERIFYPEER determines whether curl verifies the authenticity of the peer's certificate. + // Do not disable OPT_SSL_VERIFYPEER unless you absolutely sure of the security implications. + // https://curl.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html + easy.Setopt(curl.OPT_SSL_VERIFYPEER, false) // 0 also means false + + easy.Perform() + } +} diff --git a/examples/misc.go b/examples/misc.go index d66d2b7..dd054c3 100644 --- a/examples/misc.go +++ b/examples/misc.go @@ -2,9 +2,10 @@ package main import ( "fmt" - curl "github.com/andelf/go-curl" "os" "reflect" + + curl "github.com/andelf/go-curl" ) const endl = "\n" @@ -37,7 +38,7 @@ func main() { } //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) + print("set user_agent =>", ret.Setopt(curl.OPT_USERAGENT, "github.com/andelf/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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..004a461 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/andelf/go-curl + +go 1.21.5 diff --git a/misc/codegen.py b/misc/codegen.py index 050f826..7ee8310 100644 --- a/misc/codegen.py +++ b/misc/codegen.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python # -*- coding: utf-8 -*- import os diff --git a/misc/compatgen.py b/misc/compatgen.py index 2646d4d..cb41359 100644 --- a/misc/compatgen.py +++ b/misc/compatgen.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- import os @@ -118,5 +118,5 @@ def version_symbol(ver): result.extend(result_tail) -with open("./compat.h", 'w') as fp: +with open("./compat.h", 'w', encoding='utf-8') as fp: fp.write('\n'.join(result))