private void curlSetupCookies(CurlHandleType curlHandle) { // cookies, got a bunch of info from this place on how to do cookies: http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php //string cookieFilePath = "/tmp/cookie.txt"; string cookieFilePath = ""; curl.SetOpt(curlHandle, CURLoption.COOKIEJAR, cookieFilePath); curl.SetOpt(curlHandle, CURLoption.COOKIEFILE, cookieFilePath); curl.SetOpt(curlHandle, CURLoption.COOKIESESSION, 1); }
private void curlSetupAuthentication(CurlHandleType curlHandle) { if (this.options.useKerberosAuthentication) { // kerberos auth curl.SetOpt(curlHandle, CURLoption.HTTPAUTH, CURLAUTH.GSSNEGOTIATE); curl.SetOpt(curlHandle, CURLoption.USERPWD, ":"); } else if (!string.IsNullOrWhiteSpace(this.options.user)) { // basic auth curl.SetOpt(curlHandle, CURLoption.HTTPAUTH, CURLAUTH.BASIC); curl.SetOpt(curlHandle, CURLoption.USERPWD, this.options.getUserPasswordCurlOptValue()); } }
private nac.CurlThin.SafeHandles.SafeSlistHandle curlSetHeader(CurlHandleType curlHandle, Dictionary <string, string> headers) { // followed documentation here: https://curl.se/libcurl/c/CURLOPT_HTTPHEADER.html if (headers?.Any() == true) { nac.CurlThin.SafeHandles.SafeSlistHandle list = SafeSlistHandle.Null; foreach (var pair in headers) { list = nac.CurlThin.CurlNative.Slist.Append(list, $"{pair.Key}: {pair.Value}"); } curl.SetOpt(curlHandle, CURLoption.HTTPHEADER, list.DangerousGetHandle()); return(list); // list will need to be freed } return(null); }
private void curlSetupSSLVerification(CurlHandleType curlHandle) { // for now don't do any ssl verification, later we could make this an option or something curl.SetOpt(curlHandle, CURLoption.SSL_VERIFYHOST, 0); curl.SetOpt(curlHandle, CURLoption.SSL_VERIFYPEER, 0); }
private model.CurlExecResult execCurl(CurlHandleType curlHandle, string url, Dictionary <string, string> headers = null) { var result = new model.CurlExecResult(); if (!this.isAbsoluteUrl(url)) { url = this.options.appendToBaseAddress(url); } // we know the final URL here result.RequestUrl = url; /* * people may need to modify headers on every call + like setting an oauth Authorization header */ if (this.options.onNewHttpRequest != null) { /* * make sure headers is not null + If they specified a onNewHttpRequest callback they probably want to modify headers, so it needs to be non null + Normally it's fine for headers to be null, so just set it if they wanted to get a onNewHttpRequest callback */ if (headers == null) { headers = new Dictionary <string, string>(); } this.options.onNewHttpRequest.Invoke(headers: headers); } // all the request headers should be set by this point result.RequestHeaders = headers; var headerListHandle = this.curlSetHeader(curlHandle, headers); curl.SetOpt(curlHandle, CURLoption.URL, url); result.ResponseStream = new System.IO.MemoryStream(); curl.SetOpt(curlHandle, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) => { var length = (int)size * (int)nmemb; var buffer = new byte[length]; System.Runtime.InteropServices.Marshal.Copy(data, buffer, 0, length); result.ResponseStream.Write(buffer, 0, length); return((UIntPtr)length); }); if (options.Timeout.HasValue) { // timeout is in 'seconds'; see: https://curl.se/libcurl/c/CURLOPT_TIMEOUT.html curl.SetOpt(curlHandle, CURLoption.TIMEOUT_MS, (int)options.Timeout.Value.TotalMilliseconds); } result.CurlResultCode = curl.Perform(curlHandle); // get the http response code // documentation on getInfo is available here: https://curl.se/libcurl/c/curl_easy_getinfo.html if (curl.GetInfo(curlHandle, CURLINFO.RESPONSE_CODE, out int httpResponseCode) == CURLcode.OK) { result.ResponseCode = (System.Net.HttpStatusCode)httpResponseCode; } // free up some stuff if (headerListHandle != null) { headerListHandle.Dispose(); } // give back result return(result); }