Fixed curl multithreaded access.

pull/6/head
Taloth Saldono 9 years ago
parent 7bc4249ede
commit 786e0b825a

@ -40,56 +40,59 @@ namespace NzbDrone.Common.Http
public HttpResponse GetResponse(HttpRequest httpRequest, HttpWebRequest webRequest) public HttpResponse GetResponse(HttpRequest httpRequest, HttpWebRequest webRequest)
{ {
Stream responseStream = new MemoryStream(); lock (CurlGlobalHandle.Instance)
Stream headerStream = new MemoryStream();
var curlEasy = new CurlEasy();
curlEasy.AutoReferer = false;
curlEasy.WriteFunction = (b, s, n, o) =>
{
responseStream.Write(b, 0, s * n);
return s * n;
};
curlEasy.HeaderFunction = (b, s, n, o) =>
{
headerStream.Write(b, 0, s * n);
return s * n;
};
curlEasy.UserAgent = webRequest.UserAgent;
curlEasy.FollowLocation = webRequest.AllowAutoRedirect;
curlEasy.HttpGet = webRequest.Method == "GET";
curlEasy.Post = webRequest.Method == "POST";
curlEasy.Put = webRequest.Method == "PUT";
curlEasy.Url = webRequest.RequestUri.ToString();
if (webRequest.CookieContainer != null)
{ {
curlEasy.Cookie = webRequest.CookieContainer.GetCookieHeader(webRequest.RequestUri); Stream responseStream = new MemoryStream();
} Stream headerStream = new MemoryStream();
if (!httpRequest.Body.IsNullOrWhiteSpace()) var curlEasy = new CurlEasy();
{ curlEasy.AutoReferer = false;
// TODO: This might not go well with encoding. curlEasy.WriteFunction = (b, s, n, o) =>
curlEasy.PostFields = httpRequest.Body; {
curlEasy.PostFieldSize = httpRequest.Body.Length; responseStream.Write(b, 0, s * n);
} return s * n;
};
curlEasy.HeaderFunction = (b, s, n, o) =>
{
headerStream.Write(b, 0, s * n);
return s * n;
};
curlEasy.UserAgent = webRequest.UserAgent;
curlEasy.FollowLocation = webRequest.AllowAutoRedirect;
curlEasy.HttpGet = webRequest.Method == "GET";
curlEasy.Post = webRequest.Method == "POST";
curlEasy.Put = webRequest.Method == "PUT";
curlEasy.Url = webRequest.RequestUri.ToString();
if (webRequest.CookieContainer != null)
{
curlEasy.Cookie = webRequest.CookieContainer.GetCookieHeader(webRequest.RequestUri);
}
if (!httpRequest.Body.IsNullOrWhiteSpace())
{
// TODO: This might not go well with encoding.
curlEasy.PostFields = httpRequest.Body;
curlEasy.PostFieldSize = httpRequest.Body.Length;
}
curlEasy.HttpHeader = SerializeHeaders(webRequest); curlEasy.HttpHeader = SerializeHeaders(webRequest);
var result = curlEasy.Perform(); var result = curlEasy.Perform();
if (result != CurlCode.Ok) if (result != CurlCode.Ok)
{ {
throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url)); throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url));
} }
var webHeaderCollection = ProcessHeaderStream(webRequest, headerStream); var webHeaderCollection = ProcessHeaderStream(webRequest, headerStream);
var responseData = ProcessResponseStream(webRequest, responseStream, webHeaderCollection); var responseData = ProcessResponseStream(webRequest, responseStream, webHeaderCollection);
var httpHeader = new HttpHeader(webHeaderCollection); var httpHeader = new HttpHeader(webHeaderCollection);
return new HttpResponse(httpRequest, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode); return new HttpResponse(httpRequest, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode);
}
} }
private CurlSlist SerializeHeaders(HttpWebRequest webRequest) private CurlSlist SerializeHeaders(HttpWebRequest webRequest)
@ -206,13 +209,16 @@ namespace NzbDrone.Common.Http
public bool Initialize() public bool Initialize()
{ {
if (_initialized) lock (CurlGlobalHandle.Instance)
return _available; {
if (_initialized)
return _available;
_initialized = true; _initialized = true;
_available = Curl.GlobalInit(CurlInitFlag.All) == CurlCode.Ok; _available = Curl.GlobalInit(CurlInitFlag.All) == CurlCode.Ok;
return _available; return _available;
}
} }
protected override bool ReleaseHandle() protected override bool ReleaseHandle()

Loading…
Cancel
Save