private static int DebugFunction(IntPtr ptr, CURLINFODEBUG type, IntPtr data, int size, IntPtr userdata) { if (type == CURLINFODEBUG.HEADER_OUT) { StreamReader sr = null; var thiz = ((GCHandle)userdata).Target as CurlEasy; #if ALLOW_UNSAFE unsafe { var ums = new UnmanagedMemoryStream((byte *)data, (int)size); sr = new StreamReader(ums); } #else var bytes = thiz.AcquireBuffer(size); Marshal.Copy(data, bytes, 0, (int)size); sr = new StreamReader(new MemoryStream(bytes, 0, (int)size)); #endif // Handle first line var firstLine = sr.ReadLine(); while (true) { var line = sr.ReadLine(); if (!string.IsNullOrEmpty(line)) { var index = line.IndexOf(':'); if (index >= 0) { if (thiz.outHeader == null) { thiz.outHeader = new HeaderDict(); } var key = line.Substring(0, index).Trim(); var value = line.Substring(index + 1).Trim(); thiz.outHeader[key] = value; } else { CurlLog.LogWarning($"Invalid header: {line}"); } } else { break; } } } return(0); }
private bool ProcessResponse(CURLE result) { var done = false; try { thisHandle.Free(); if (result == CURLE.OK) { responseHeaderStream.Position = 0; var sr = new StreamReader(responseHeaderStream); // Handle first line { var line = sr.ReadLine(); var index = line.IndexOf(' '); httpVersion = line.Substring(0, index); var nextIndex = line.IndexOf(' ', index + 1); if (int.TryParse(line.Substring(index + 1, nextIndex - index), out var _status)) { status = _status; } message = line.Substring(nextIndex + 1); } inHeader = new HeaderDict(); while (true) { var line = sr.ReadLine(); if (!string.IsNullOrEmpty(line)) { var index = line.IndexOf(':'); var key = line.Substring(0, index).Trim(); var value = line.Substring(index + 1).Trim(); inHeader[key] = value; } else { break; } } var ms = responseBodyStream as MemoryStream; if (ms != null) { inData = ms.ToArray(); } if (status / 100 == 3) { if (followRedirect && GetInfo(CURLINFO.REDIRECT_URL, out string location) == CURLE.OK) { uri = new Uri(location); } else { done = true; } } else { done = true; } } else { CurlLog.LogWarning($"Failed to request: {uri}, reason: {result}"); } CloseStreams(); } catch (Exception e) { CurlLog.LogError("Unexpected exception: " + e); } return(done); }