Esempio n. 1
0
 private void VerboseTraceIf(bool condition, string text = null, [CallerMemberName] string memberName = null, EasyRequest easy = null)
 {
     if (condition)
     {
         CurlHandler.VerboseTrace(text, memberName, easy, agent: this);
     }
 }
Esempio n. 2
0
            private static ulong CurlSendCallback(IntPtr buffer, ulong size, ulong nitems, IntPtr context)
            {
                CurlHandler.VerboseTrace("size: " + size + ", nitems: " + nitems);
                int length = checked((int)(size * nitems));
                Debug.Assert(length <= RequestBufferSize, "length " + length + " should not be larger than RequestBufferSize " + RequestBufferSize);
                if (length == 0)
                {
                    return 0;
                }

                EasyRequest easy;
                if (TryGetEasyRequestFromContext(context, out easy))
                {
                    Debug.Assert(easy._requestContentStream != null, "We should only be in the send callback if we have a request content stream");
                    Debug.Assert(easy._associatedMultiAgent != null, "The request should be associated with a multi agent.");

                    try
                    {
                        // Transfer data from the request's content stream to libcurl
                        return TransferDataFromRequestStream(buffer, length, easy);
                    }
                    catch (Exception ex)
                    {
                        easy.FailRequest(ex); // cleanup will be handled by main processing loop
                    }
                }

                // Something went wrong.
                return Interop.Http.CURL_READFUNC_ABORT;
            }
Esempio n. 3
0
            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;
            }
            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);
                                }
                                else if (string.Equals(headerName, HttpKnownHeaderNames.SetCookie, StringComparison.OrdinalIgnoreCase))
                                {
                                    easy._handler.AddResponseCookies(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);
            }
            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 void VerboseTrace(string text = null, [CallerMemberName] string memberName = null)
 {
     CurlHandler.VerboseTrace(text, memberName, easy: this, agent: null);
 }
Esempio n. 7
0
 private void VerboseTrace(string text = null, [CallerMemberName] string memberName = null)
 {
     CurlHandler.VerboseTrace(text, memberName, _easy);
 }
Esempio n. 8
0
 private void VerboseTrace(string text = null, [CallerMemberName] string memberName = null, EasyRequest easy = null)
 {
     CurlHandler.VerboseTrace(text, memberName, easy, agent: this);
 }