コード例 #1
0
ファイル: CurlHttpClient.cs プロジェクト: slpaige/Sonarr
        public HttpResponse GetResponse(HttpRequest httpRequest, HttpWebRequest webRequest)
        {
            lock (CurlGlobalHandle.Instance)
            {
                Stream responseStream = new MemoryStream();
                Stream headerStream   = new MemoryStream();

                using (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);
                    }

                    if (!httpRequest.Body.IsNullOrWhiteSpace())
                    {
                        // TODO: This might not go well with encoding.
                        curlEasy.PostFieldSize = httpRequest.Body.Length;
                        curlEasy.SetOpt(CurlOption.CopyPostFields, httpRequest.Body);
                    }

                    // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state
                    using (var httpRequestHeaders = SerializeHeaders(webRequest))
                    {
                        curlEasy.HttpHeader = httpRequestHeaders;

                        var result = curlEasy.Perform();

                        if (result != CurlCode.Ok)
                        {
                            throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url));
                        }
                    }

                    var webHeaderCollection = ProcessHeaderStream(webRequest, headerStream);
                    var responseData        = ProcessResponseStream(webRequest, responseStream, webHeaderCollection);

                    var httpHeader = new HttpHeader(webHeaderCollection);

                    return(new HttpResponse(httpRequest, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode));
                }
            }
        }
コード例 #2
0
ファイル: HttpRequest.cs プロジェクト: PearsonFlyer/Librarr
        public void SetContent(string data)
        {
            var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType);

            ContentData = encoding.GetBytes(data);
        }
コード例 #3
0
ファイル: HttpClient.cs プロジェクト: jtolar/NzbDrone
        protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader headers)
        {
            foreach (var header in headers)
            {
                switch (header.Key)
                {
                case "Accept":
                    webRequest.Accept = header.Value.ToString();
                    break;

                case "Connection":
                    webRequest.Connection = header.Value.ToString();
                    break;

                case "Content-Length":
                    webRequest.ContentLength = Convert.ToInt64(header.Value);
                    break;

                case "Content-Type":
                    webRequest.ContentType = header.Value.ToString();
                    break;

                case "Date":
                    webRequest.Date = (DateTime)header.Value;
                    break;

                case "Expect":
                    webRequest.Expect = header.Value.ToString();
                    break;

                case "Host":
                    webRequest.Host = header.Value.ToString();
                    break;

                case "If-Modified-Since":
                    webRequest.IfModifiedSince = (DateTime)header.Value;
                    break;

                case "Range":
                    throw new NotImplementedException();
                    break;

                case "Referer":
                    webRequest.Referer = header.Value.ToString();
                    break;

                case "Transfer-Encoding":
                    webRequest.TransferEncoding = header.Value.ToString();
                    break;

                case "User-Agent":
                    throw new NotSupportedException("User-Agent other than NzbDrone not allowed.");

                case "Proxy-Connection":
                    throw new NotImplementedException();
                    break;

                default:
                    webRequest.Headers.Add(header.Key, header.Value.ToString());
                    break;
                }
            }
        }