Esempio n. 1
0
            public EasyRequest(CurlHandler handler, MultiAgent agent, HttpRequestMessage requestMessage, CancellationToken cancellationToken) :
                base(TaskCreationOptions.RunContinuationsAsynchronously)
            {
                Debug.Assert(handler != null, $"Expected non-null {nameof(handler)}");
                Debug.Assert(agent != null, $"Expected non-null {nameof(agent)}");
                Debug.Assert(requestMessage != null, $"Expected non-null {nameof(requestMessage)}");

                _handler = handler;
                _associatedMultiAgent = agent;
                _requestMessage       = requestMessage;

                _cancellationToken = cancellationToken;
                _responseMessage   = new CurlResponseMessage(this);
            }
Esempio n. 2
0
            public EasyRequest(CurlHandler handler, HttpRequestMessage requestMessage, CancellationToken cancellationToken) :
                base(TaskCreationOptions.RunContinuationsAsynchronously)
            {
                _handler           = handler;
                _requestMessage    = requestMessage;
                _cancellationToken = cancellationToken;

                if (requestMessage.Content != null)
                {
                    _requestContentStream = new HttpContentAsyncStream(requestMessage.Content);
                }

                _responseMessage = new CurlResponseMessage(this);
            }
Esempio n. 3
0
 private HttpClientHandler(bool useSocketsHttpHandler) // used by parameterless ctor and as hook for testing
 {
     if (useSocketsHttpHandler)
     {
         _socketsHttpHandler      = new SocketsHttpHandler();
         _diagnosticsHandler      = new DiagnosticsHandler(_socketsHttpHandler);
         ClientCertificateOptions = ClientCertificateOption.Manual;
     }
     else
     {
         _curlHandler        = new CurlHandler();
         _diagnosticsHandler = new DiagnosticsHandler(_curlHandler);
     }
 }
            private static ulong CurlReceiveHeadersCallback(IntPtr buffer, ulong size, ulong nitems, IntPtr context)
            {
                CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems);
                size *= nitems;
                if (size == 0)
                {
                    return(0);
                }

                EasyRequest easy;

                if (TryGetEasyRequestFromContext(context, out easy))
                {
                    try
                    {
                        // The callback is invoked once per header; multi-line headers get merged into a single line.
                        string responseHeader        = Marshal.PtrToStringAnsi(buffer).Trim();
                        HttpResponseMessage response = easy._responseMessage;

                        if (!TryParseStatusLine(response, responseHeader, easy))
                        {
                            int    index      = 0;
                            string headerName = CurlResponseParseUtils.ReadHeaderName(responseHeader, out index);
                            if (headerName != null)
                            {
                                string headerValue = responseHeader.Substring(index).Trim();
                                if (!response.Headers.TryAddWithoutValidation(headerName, headerValue))
                                {
                                    response.Content.Headers.TryAddWithoutValidation(headerName, headerValue);
                                }
                                else if (easy._isRedirect && string.Equals(headerName, HttpKnownHeaderNames.Location, StringComparison.OrdinalIgnoreCase))
                                {
                                    HandleRedirectLocationHeader(easy, headerValue);
                                }
                            }
                        }

                        return(size);
                    }
                    catch (Exception ex)
                    {
                        easy.FailRequest(ex); // cleanup will be handled by main processing loop
                    }
                }

                // Returing a value other than size fails the callback and forces
                // request completion with an error
                return(size - 1);
            }
Esempio n. 5
0
 public HttpClientHandler()
 {
     if (UseManagedHandler)
     {
         _managedHandler = new ManagedHandler()
         {
             SslOptions = new SslClientAuthenticationOptions()
         };
         _diagnosticsHandler = new DiagnosticsHandler(_managedHandler);
     }
     else
     {
         _curlHandler        = new CurlHandler();
         _diagnosticsHandler = new DiagnosticsHandler(_curlHandler);
     }
 }
            private static CurlSeekResult CurlSeekCallback(IntPtr context, long offset, int origin)
            {
                CurlHandler.VerboseTrace("offset: " + offset + ", origin: " + origin);
                EasyRequest easy;

                if (TryGetEasyRequestFromContext(context, out easy))
                {
                    try
                    {
                        // If libcul is requesting we seek back to the beginning and if the request
                        // content stream is in a position to reset itself, reset and let libcurl
                        // know we did the seek; otherwise, let it know we can't seek.
                        if (offset == 0 && origin == (int)SeekOrigin.Begin &&
                            easy._requestContentStream != null && easy._requestContentStream.TryReset())
                        {
                            // Dump any state associated with the old stream's position
                            if (easy._sendTransferState != null)
                            {
                                easy._sendTransferState.SetTaskOffsetCount(null, 0, 0);
                            }

                            // Restart the transfer
                            easy._requestContentStream.Run();

                            return(CurlSeekResult.CURL_SEEKFUNC_OK);
                        }
                        else
                        {
                            return(CurlSeekResult.CURL_SEEKFUNC_CANTSEEK);
                        }
                    }
                    catch (Exception ex)
                    {
                        easy.FailRequest(ex); // cleanup will be handled by main processing loop
                    }
                }

                // Something went wrong
                return(CurlSeekResult.CURL_SEEKFUNC_FAIL);
            }
            private static ulong CurlReceiveBodyCallback(
                IntPtr buffer, ulong size, ulong nitems, IntPtr context)
            {
                CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems);
                size *= nitems;

                EasyRequest easy;

                if (TryGetEasyRequestFromContext(context, out easy))
                {
                    try
                    {
                        if (!(easy.Task.IsCanceled || easy.Task.IsFaulted))
                        {
                            // Complete the task if it hasn't already been.  This will make the
                            // stream available to consumers.  A previous write callback
                            // may have already completed the task to publish the response.
                            easy.EnsureResponseMessagePublished();

                            // Try to transfer the data to a reader.  This will return either the
                            // amount of data transferred (equal to the amount requested
                            // to be transferred), or it will return a pause request.
                            return(easy._responseMessage.ResponseStream.TransferDataToStream(buffer, (long)size));
                        }
                    }
                    catch (Exception ex)
                    {
                        easy.FailRequest(ex); // cleanup will be handled by main processing loop
                    }
                }

                // Returing a value other than size fails the callback and forces
                // request completion with an error.
                CurlHandler.VerboseTrace("Error: returning a bad size to abort the request");
                return((size > 0) ? size - 1 : 1);
            }
Esempio n. 8
0
 private void VerboseTrace(string text = null, [CallerMemberName] string memberName = null)
 {
     CurlHandler.VerboseTrace(text, memberName, _easy);
 }
Esempio n. 9
0
 private void EventSourceTrace(string message, [CallerMemberName] string memberName = null)
 {
     CurlHandler.EventSourceTrace(message, easy: this, memberName: memberName);
 }
Esempio n. 10
0
 private void EventSourceTrace <TArg0>(string formatMessage, TArg0 arg0, [CallerMemberName] string memberName = null)
 {
     CurlHandler.EventSourceTrace(formatMessage, arg0, easy: this, memberName: memberName);
 }
Esempio n. 11
0
 private void EventSourceTrace <TArg0, TArg1, TArg2>(string formatMessage, TArg0 arg0, TArg1 arg1, TArg2 arg2, [CallerMemberName] string memberName = null)
 {
     CurlHandler.EventSourceTrace(formatMessage, arg0, arg1, arg2, agent: null, easy: _easy, memberName: memberName);
 }
Esempio n. 12
0
 public HttpClientHandler()
 {
     _curlHandler = new CurlHandler();
 }
 public HttpClientHandler()
 {
     _curlHandler = new CurlHandler();
 }
Esempio n. 14
0
 private void VerboseTrace(string text = null, [CallerMemberName] string memberName = null, EasyRequest easy = null)
 {
     CurlHandler.VerboseTrace(text, memberName, easy, agent: this);
 }
Esempio n. 15
0
 public HttpClientHandler()
 {
     _curlHandler         = new CurlHandler();
     _diagnosticsPipeline = new DiagnosticsHandler(_curlHandler);
 }
Esempio n. 16
0
        public HttpClientHandler()
        {
            _curlHandler = new CurlHandler();

            // TODO: Set same defaults as Windows handler
            AllowAutoRedirect = true;
        }
 private void VerboseTrace(string text = null, [CallerMemberName] string memberName = null)
 {
     CurlHandler.VerboseTrace(text, memberName, easy: this, agent: null);
 }
Esempio n. 18
0
 public RequestCompletionSource(CurlHandler handler)
 {
     this._handler = handler;
 }