Exemplo n.º 1
0
func (easy *Easy) SetOpt(opt EasyOption, param interface{}) error {
	switch {
	case opt >= C.CURLOPTTYPE_OFF_T:
		return easy.getError(C.my_esetoptl(easy.cptr, C.CURLoption(opt), C.long(param.(int))))
	case opt >= C.CURLOPTTYPE_FUNCTIONPOINT:
		return errors.New("function options not supported by SetOpt()")
	case opt >= C.CURLOPTTYPE_OBJECTPOINT:
		if isStringOption(opt) {
			c_param := C.CString(param.(string))
			defer C.free(unsafe.Pointer(c_param))
			return easy.getError(C.my_esetoptc(easy.cptr, C.CURLoption(opt), c_param))
		} else {
			return fmt.Errorf("option %d not supported", opt)
		}
	case opt >= C.CURLOPTTYPE_LONG:
		var val C.long
		switch t := param.(type) {
		case bool:
			if t {
				val = C.long(1)
			}
		case int:
			val = C.long(t)
		case int32:
			val = C.long(t)
		case int64:
			val = C.long(t)
		default:
			panic("CURLOPTTYPE_LONG param must be of golang type bool, int, int32, or int64")
		}
		return easy.getError(C.my_esetoptl(easy.cptr, C.CURLoption(opt), val))
	}
	return nil
}
Exemplo n.º 2
0
func EasyInit() (easy *Easy, err *CurlError) {
	easy = new(Easy)
	if easy.cptr = C.curl_easy_init(); easy.cptr == nil {
		return nil, NewError(E_FAILED_INIT)
	}
	easy.cerrbuf = (*C.char)(C.malloc(C.CURL_ERROR_SIZE))
	err = NewError(C.my_esetoptc(easy.cptr, C.CURLOPT_ERRORBUFFER, easy.cerrbuf))
	if err != nil {
		C.curl_easy_cleanup(easy.cptr)
		return nil, err
	}
	runtime.SetFinalizer(easy, func(e *Easy) { e.Cleanup() })
	return
}