From a791b0b5d41ef166a7045eb61b5d6ac57f31e96f Mon Sep 17 00:00:00 2001 From: sashayakovtseva Date: Mon, 4 Apr 2022 19:27:40 +0200 Subject: [PATCH 1/6] Add .idea to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index a4a1e00..34967ae 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.o src/_cgo_* src/_obj/ + +.idea/ From 4c39c22556ceac8d237e1e702cf284d02cfa1766 Mon Sep 17 00:00:00 2001 From: sashayakovtseva Date: Mon, 4 Apr 2022 19:29:59 +0200 Subject: [PATCH 2/6] Format code Signed-off-by: sashayakovtseva --- easy.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/easy.go b/easy.go index fe28330..498e2e8 100644 --- a/easy.go +++ b/easy.go @@ -66,8 +66,8 @@ import ( "fmt" "mime" "path" - "unsafe" "sync" + "unsafe" ) type CurlInfo C.CURLINFO @@ -137,7 +137,7 @@ func (c *contextMap) Delete(k uintptr) { delete(c.items, k) } -var context_map = &contextMap { +var context_map = &contextMap{ items: make(map[uintptr]*CURL), } @@ -292,8 +292,8 @@ func (curl *CURL) Setopt(opt int, param interface{}) error { default: // It panics if v's Kind is not Chan, Func, Map, Ptr, Slice, or UnsafePointer. // val := reflect.ValueOf(param) - //fmt.Printf("DEBUG(Setopt): param=%x\n", val.Pointer()) - //println("DEBUG can addr =", val.Pointer(), "opt=", opt) + // fmt.Printf("DEBUG(Setopt): param=%x\n", val.Pointer()) + // println("DEBUG can addr =", val.Pointer(), "opt=", opt) // pass a pointer to GoInterface return newCurlError(C.curl_easy_setopt_pointer(p, C.CURLoption(opt), unsafe.Pointer(¶m))) From 03388ab3501a06584bc3aa07172106db59e1c4d3 Mon Sep 17 00:00:00 2001 From: sashayakovtseva Date: Mon, 4 Apr 2022 19:30:31 +0200 Subject: [PATCH 3/6] Prevent pointer to pointer for CURLINFO_SLIST Signed-off-by: sashayakovtseva --- easy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easy.go b/easy.go index 498e2e8..6dbd4de 100644 --- a/easy.go +++ b/easy.go @@ -405,7 +405,7 @@ func (curl *CURL) Getinfo(info CurlInfo) (ret interface{}, err error) { debugf("Getinfo %s", ret) return ret, err case C.CURLINFO_SLIST: - a_ptr_slist := new(C.struct_curl_slist) + var a_ptr_slist *C.struct_curl_slist err := newCurlError(C.curl_easy_getinfo_slist(p, cInfo, &a_ptr_slist)) ret := []string{} for a_ptr_slist != nil { From 00042ca854cee4308a00251cfc709caa5c22b086 Mon Sep 17 00:00:00 2001 From: sashayakovtseva Date: Mon, 4 Apr 2022 19:37:10 +0200 Subject: [PATCH 4/6] Support certinfo Signed-off-by: sashayakovtseva --- easy.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/easy.go b/easy.go index 6dbd4de..5d18d08 100644 --- a/easy.go +++ b/easy.go @@ -34,6 +34,9 @@ static CURLcode curl_easy_getinfo_double(CURL *curl, CURLINFO info, double *p) { static CURLcode curl_easy_getinfo_slist(CURL *curl, CURLINFO info, struct curl_slist **p) { return curl_easy_getinfo(curl, info, p); } +static CURLcode curl_easy_getinfo_certinfo(CURL *curl, CURLINFO info, struct curl_certinfo **p) { + return curl_easy_getinfo(curl, info, p); +} static CURLFORMcode curl_formadd_name_content_length( struct curl_httppost **httppost, struct curl_httppost **last_post, char *name, char *content, int length) { @@ -384,6 +387,36 @@ func (curl *CURL) Unescape(url string) string { func (curl *CURL) Getinfo(info CurlInfo) (ret interface{}, err error) { p := curl.handle cInfo := C.CURLINFO(info) + if cInfo == C.CURLINFO_CERTINFO { + var certInfo *C.struct_curl_certinfo + err := newCurlError(C.curl_easy_getinfo_certinfo(p, cInfo, &certInfo)) + if err != nil { + return nil, err + } + if certInfo == nil { + return nil, fmt.Errorf("nil certinfo") + } + + certsCount := C.int(certInfo.num_of_certs) + certs := make([]string, certsCount) + + nextCert := (**C.struct_curl_slist)(certInfo.certinfo) + for i := range certs { + certPtr := unsafe.Pointer(uintptr(unsafe.Pointer(nextCert)) + unsafe.Sizeof(nextCert)*uintptr(i)) + certSlist := *(**C.struct_curl_slist)(certPtr) + + var certData string + for certSlist != nil { + certData += C.GoString(certSlist.data) + certSlist = certSlist.next + } + + certs[i] = certData + } + + return certs, nil + } + switch cInfo & C.CURLINFO_TYPEMASK { case C.CURLINFO_STRING: a_string := C.CString("") From 1679c8c2e2c9478cd95a2c4897afb70568b7ffac Mon Sep 17 00:00:00 2001 From: sashayakovtseva Date: Mon, 4 Apr 2022 19:37:17 +0200 Subject: [PATCH 5/6] Add certinfo example Signed-off-by: sashayakovtseva --- examples/certinfo.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/certinfo.go diff --git a/examples/certinfo.go b/examples/certinfo.go new file mode 100644 index 0000000..8362b3c --- /dev/null +++ b/examples/certinfo.go @@ -0,0 +1,60 @@ +package main + +import ( + "fmt" + "log" + + "github.com/andelf/go-curl" +) + +func main() { + const domain = "google.com" + + if err := curl.GlobalInit(curl.GLOBAL_ALL); err != nil { + log.Println("failed to init libcurl") + return + } + defer curl.GlobalCleanup() + + easy := curl.EasyInit() + defer easy.Cleanup() + + if easy == nil { + log.Println("failed to init easy curl") + return + } + + url := fmt.Sprintf("https://%s", domain) + + easy.Setopt(curl.OPT_URL, url) + easy.Setopt(curl.OPT_SSL_VERIFYPEER, true) + easy.Setopt(curl.OPT_SSL_VERIFYHOST, true) + easy.Setopt(curl.OPT_TIMEOUT, 5) + easy.Setopt(curl.OPT_CERTINFO, true) + easy.Setopt(curl.OPT_NOPROGRESS, true) + easy.Setopt(curl.OPT_NOBODY, true) + + if err := easy.Perform(); err != nil { + log.Printf("failed to preform request: %v", err) + return + } + + res, err := easy.Getinfo(curl.INFO_CERTINFO) + if err != nil { + log.Printf("failed to get cert info: %v", err) + return + } + + info, ok := res.([]string) + if !ok { + log.Printf("unknown cert info result: %T", res) + return + } + + fmt.Printf("Fetched %d certificates!\n\n", len(info)) + for i, certInfo := range info { + fmt.Printf("Certificate info %d:\n", i) + fmt.Printf("%+v\n", certInfo) + fmt.Println() + } +} From f444c31c6bb6eecaf3bff2ead2cc8f2641749642 Mon Sep 17 00:00:00 2001 From: sashayakovtseva Date: Mon, 4 Apr 2022 19:40:35 +0200 Subject: [PATCH 6/6] Add missing newline Signed-off-by: sashayakovtseva --- easy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easy.go b/easy.go index 5d18d08..66c5e08 100644 --- a/easy.go +++ b/easy.go @@ -407,7 +407,7 @@ func (curl *CURL) Getinfo(info CurlInfo) (ret interface{}, err error) { var certData string for certSlist != nil { - certData += C.GoString(certSlist.data) + certData += C.GoString(certSlist.data) + "\n" certSlist = certSlist.next }