Exemplo n.º 1
0
        // BASIC AUTH
        // This method is accessed via a custom descriptor so it must not be an auto block
        public static async Task HttpRequestBasicAuth(BotData data, BasicAuthHttpRequestOptions options)
        {
            var clientOptions = GetClientOptions(data, options);

            using var client = HttpFactory.GetRLHttpClient(data.UseProxy ? data.Proxy : null, clientOptions);

            foreach (var cookie in options.CustomCookies)
            {
                data.COOKIES[cookie.Key] = cookie.Value;
            }

            using var request = new HttpRequest
                  {
                      Method  = new System.Net.Http.HttpMethod(options.Method.ToString()),
                      Uri     = new Uri(options.Url),
                      Version = Version.Parse(options.HttpVersion),
                      Headers = options.CustomHeaders,
                      Cookies = data.COOKIES,
                      AbsoluteUriInFirstLine = options.AbsoluteUriInFirstLine
                  };

            // Add the basic auth header
            request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(
                                  Encoding.UTF8.GetBytes($"{options.Username}:{options.Password}")));

            data.Logger.LogHeader();

            try
            {
                Activity.Current     = null;
                using var timeoutCts = new CancellationTokenSource(options.TimeoutMilliseconds);
                using var linkedCts  = CancellationTokenSource.CreateLinkedTokenSource(data.CancellationToken, timeoutCts.Token);
                using var response   = await client.SendAsync(request, linkedCts.Token);

                LogHttpRequestData(data, client);
                await LogHttpResponseData(data, response, request, options);
            }
            catch
            {
                LogHttpRequestData(data, request);
                throw;
            }
            finally
            {
                request.Dispose();
                client.Dispose();
            }
        }
Exemplo n.º 2
0
        // RAW REQUESTS
        // This method is accessed via a custom descriptor so it must not be an auto block
        public static async Task HttpRequestRaw(BotData data, RawHttpRequestOptions options)
        {
            var clientOptions = GetClientOptions(data, options);

            using var client = HttpFactory.GetRLHttpClient(data.UseProxy ? data.Proxy : null, clientOptions);

            foreach (var cookie in options.CustomCookies)
            {
                data.COOKIES[cookie.Key] = cookie.Value;
            }

            using var request = new HttpRequest
                  {
                      Method  = new System.Net.Http.HttpMethod(options.Method.ToString()),
                      Uri     = new Uri(options.Url),
                      Version = Version.Parse(options.HttpVersion),
                      Headers = options.CustomHeaders,
                      Cookies = data.COOKIES,
                      AbsoluteUriInFirstLine = options.AbsoluteUriInFirstLine,
                      Content = new ByteArrayContent(options.Content)
                  };

            request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(options.ContentType);

            data.Logger.LogHeader();

            try
            {
                Activity.Current     = null;
                using var timeoutCts = new CancellationTokenSource(options.TimeoutMilliseconds);
                using var linkedCts  = CancellationTokenSource.CreateLinkedTokenSource(data.CancellationToken, timeoutCts.Token);
                using var response   = await client.SendAsync(request, linkedCts.Token);

                LogHttpRequestData(data, client);
                await LogHttpResponseData(data, response, request, options);
            }
            catch
            {
                LogHttpRequestData(data, request);
                throw;
            }
            finally
            {
                request.Dispose();
                client.Dispose();
            }
        }
Exemplo n.º 3
0
        // MULTIPART
        // This method is accessed via a custom descriptor so it must not be an auto block
        public static async Task HttpRequestMultipart(BotData data, MultipartHttpRequestOptions options)
        {
            var clientOptions = GetClientOptions(data, options);

            using var client = HttpFactory.GetRLHttpClient(data.UseProxy ? data.Proxy : null, clientOptions);

            foreach (var cookie in options.CustomCookies)
            {
                data.COOKIES[cookie.Key] = cookie.Value;
            }

            if (string.IsNullOrWhiteSpace(options.Boundary))
            {
                options.Boundary = GenerateMultipartBoundary();
            }

            // Rewrite the value of the Content-Type header otherwise it will add double quotes around it like
            // Content-Type: multipart/form-data; boundary="------WebKitFormBoundaryewozmkbxwbblilpm"
            var multipartContent = new MultipartFormDataContent(options.Boundary);

            multipartContent.Headers.ContentType.Parameters.First(o => o.Name == "boundary").Value = options.Boundary;

            FileStream fileStream = null;

            foreach (var c in options.Contents)
            {
                switch (c)
                {
                case StringHttpContent x:
                    multipartContent.Add(new StringContent(x.Data, Encoding.UTF8, x.ContentType), x.Name);
                    break;

                case RawHttpContent x:
                    var byteContent = new ByteArrayContent(x.Data);
                    byteContent.Headers.ContentType = new MediaTypeHeaderValue(x.ContentType);
                    multipartContent.Add(byteContent, x.Name);
                    break;

                case FileHttpContent x:
                    lock (FileLocker.GetHandle(x.FileName))
                    {
                        if (data.Providers.Security.RestrictBlocksToCWD)
                        {
                            FileUtils.ThrowIfNotInCWD(x.FileName);
                        }

                        fileStream = new FileStream(x.FileName, FileMode.Open);
                        var fileContent = CreateFileContent(fileStream, x.Name, Path.GetFileName(x.FileName), x.ContentType);
                        multipartContent.Add(fileContent, x.Name);
                    }
                    break;
                }
            }

            using var request = new HttpRequest
                  {
                      Method  = new System.Net.Http.HttpMethod(options.Method.ToString()),
                      Uri     = new Uri(options.Url),
                      Version = Version.Parse(options.HttpVersion),
                      Headers = options.CustomHeaders,
                      Cookies = data.COOKIES,
                      AbsoluteUriInFirstLine = options.AbsoluteUriInFirstLine,
                      Content = multipartContent
                  };

            data.Logger.LogHeader();

            try
            {
                Activity.Current     = null;
                using var timeoutCts = new CancellationTokenSource(options.TimeoutMilliseconds);
                using var linkedCts  = CancellationTokenSource.CreateLinkedTokenSource(data.CancellationToken, timeoutCts.Token);
                using var response   = await client.SendAsync(request, linkedCts.Token);

                LogHttpRequestData(data, client);
                await LogHttpResponseData(data, response, request, options);
            }
            catch
            {
                LogHttpRequestData(data, request, options.Boundary, options.Contents);
                throw;
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }

                request.Dispose();
                client.Dispose();
            }
        }