Esempio n. 1
0
			public HttpClientCacheKey(TimeSpan timeout, OperationCredentials credentials)
			{
				Timeout = timeout;
				Credentials = credentials != null ? credentials.Credentials : null;
				ApiKey = credentials != null ? credentials.ApiKey : null;
				AuthenticationDisabled = credentials == null;
			}
        internal static async Task<RavenJObject> GetMetadataForAsyncImpl(IHoldProfilingInformation self, HttpJsonRequestFactory requestFactory, FilesConvention conventions,
            NameValueCollection operationsHeaders, string filename, string baseUrl, OperationCredentials credentials)
        {
            using (var request = requestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(self, baseUrl + "/files?name=" + Uri.EscapeDataString(filename), "HEAD", credentials, conventions)).AddOperationHeaders(operationsHeaders))
            {
                try
                {
                    await request.ExecuteRequestAsync().ConfigureAwait(false);

                    var response = request.Response;

                    var metadata = response.HeadersToObject();
                    metadata[Constants.MetadataEtagField] = metadata[Constants.MetadataEtagField].Value<string>().Trim('\"');
                    return metadata;
                }
                catch (Exception e)
                {
                    try
                    {
                        throw e.SimplifyException();
                    }
                    catch (FileNotFoundException)
                    {
                        return null;
                    }
                }
            }
        }
Esempio n. 3
0
		internal HttpJsonRequest(
			CreateHttpJsonRequestParams requestParams,
			HttpJsonRequestFactory factory)
		{
			_credentials = requestParams.Credentials;
			Url = requestParams.Url;
			this.factory = factory;
			owner = requestParams.Owner;
			conventions = requestParams.Convention;
			Method = requestParams.Method;
			webRequest = (HttpWebRequest)WebRequest.Create(requestParams.Url);
			webRequest.UseDefaultCredentials = true;
			webRequest.Credentials = requestParams.Credentials.Credentials;
			webRequest.Method = requestParams.Method;
			if (factory.DisableRequestCompression == false && requestParams.DisableRequestCompression == false)
			{
				if (requestParams.Method == "POST" || requestParams.Method == "PUT" ||
					requestParams.Method == "PATCH" || requestParams.Method == "EVAL")
					webRequest.Headers["Content-Encoding"] = "gzip";

				webRequest.Headers["Accept-Encoding"] = "gzip";
			}
			webRequest.ContentType = "application/json; charset=utf-8";
			webRequest.Headers.Add("Raven-Client-Version", ClientVersion);
			WriteMetadata(requestParams.Metadata);
			requestParams.UpdateHeaders(webRequest);
		}
        internal static async Task<Stream> DownloadAsyncImpl(IHoldProfilingInformation self, HttpJsonRequestFactory requestFactory, FilesConvention conventions, 
            NameValueCollection operationsHeaders, string path, string filename, Reference<RavenJObject> metadataRef, long? @from, long? to, string baseUrl, OperationCredentials credentials)
        {
            var request = requestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(self, baseUrl + path + Uri.EscapeDataString(filename), "GET", credentials, conventions)).AddOperationHeaders(operationsHeaders);

            if (@from != null)
            {
                if (to != null)
                    request.AddRange(@from.Value, to.Value);
                else
                    request.AddRange(@from.Value);
            }

            try
            {
                var response = await request.ExecuteRawResponseAsync().ConfigureAwait(false);
                if (response.StatusCode == HttpStatusCode.NotFound)
                    throw new FileNotFoundException("The file requested does not exists on the file system.", baseUrl + path + filename);

                await response.AssertNotFailingResponse().ConfigureAwait(false);

                if (metadataRef != null)
                    metadataRef.Value = response.HeadersToObject();

                return new DisposableStream(await response.GetResponseStreamWithHttpDecompression().ConfigureAwait(false), request.Dispose);
            }
            catch (Exception e)
            {
                throw e.SimplifyException();
            }
        }
		public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, RavenJObject metadata, OperationCredentials credentials, DocumentConvention convention)
		{
			Owner = self;
			Url = url;
			Method = method;
			Metadata = metadata;
			Credentials = credentials;
			Convention = convention;
			operationsHeadersCollection = new NameValueCollection();
		}
Esempio n. 6
0
        public OperationMetadata(string url, string username = null, string password = null, string domain = null, string apiKey = null)
        {
            Url = url;

            ICredentials credentials = null;
            if (!string.IsNullOrEmpty(username))
                credentials = new NetworkCredential(username, password ?? string.Empty, domain ?? string.Empty);

            Credentials = new OperationCredentials(apiKey, credentials);
        }
		private const int DefaultNumberOfCachedRequests = 2048; //put this in asyncserverclient

		public SynchronizationServerClient(string serverUrl, string fileSystem, string apiKey, ICredentials credentials, FilesConvention convention = null, 
			OperationCredentials operationCredentials = null, HttpJsonRequestFactory requestFactory = null, NameValueCollection operationsHeaders = null)
		{
			serverUrl = serverUrl.TrimEnd('/');
			baseUrl = serverUrl + "/fs/" + Uri.EscapeDataString(fileSystem);
			credentials = credentials ?? CredentialCache.DefaultNetworkCredentials;
			this.filesConvention = convention ?? new FilesConvention();
			this.operationCredentials = operationCredentials ?? new OperationCredentials(apiKey, credentials);
			this.requestFactory = requestFactory ?? new HttpJsonRequestFactory(DefaultNumberOfCachedRequests);
			this.OperationsHeaders = operationsHeaders ?? new NameValueCollection();

			SecurityExtensions.InitializeSecurity(Conventions, RequestFactory, serverUrl, credentials);
		}
Esempio n. 8
0
		public HttpClient GetClient(TimeSpan timeout, OperationCredentials credentials, Func<HttpMessageHandler> handlerFactory)
		{
			var key = new HttpClientCacheKey(timeout, credentials);
			var queue = cache.GetOrAdd(key, i => new ConcurrentQueue<HttpClient>());

			HttpClient client;
			if (queue.TryDequeue(out client))
			{
				client.CancelPendingRequests();
				client.DefaultRequestHeaders.Clear();
				return client;
			}

			return new HttpClient(handlerFactory())
				   {
					   Timeout = timeout
				   };
		}
Esempio n. 9
0
		public HttpClient GetClient(TimeSpan timeout, OperationCredentials credentials, Func<HttpMessageHandler> handlerFactory)
		{
			var key = new HttpClientCacheKey(timeout, credentials);
			var queue = cache.GetOrAdd(key, i => new ConcurrentQueue<Tuple<long, HttpClient>>());

			Tuple<long, HttpClient> client;
			while (queue.TryDequeue(out client))
			{
				if (Stopwatch.GetTimestamp() - client.Item1 > _maxIdleTime)
				{
					client.Item2.Dispose();
					continue;
				}
				client.Item2.CancelPendingRequests();
				client.Item2.DefaultRequestHeaders.Clear();
				return client.Item2;
			}

			return new HttpClient(handlerFactory())
			{
				Timeout = timeout
			};
		}
Esempio n. 10
0
 public OperationMetadata(OperationMetadata operationMetadata)
 {
     Url = operationMetadata.Url;
     Credentials = new OperationCredentials(operationMetadata.Credentials.ApiKey, operationMetadata.Credentials.Credentials);
 }
Esempio n. 11
0
 public OperationMetadata(string url, OperationCredentials credentials)
 {
     Url = url;
     Credentials = credentials != null ? new OperationCredentials(credentials.ApiKey, credentials.Credentials) : new OperationCredentials(null,null);
 }
Esempio n. 12
0
 public OperationMetadata(string url, ICredentials credentials, string apiKey = null)
 {
     Url = url;
     Credentials = new OperationCredentials(apiKey, credentials);
 }
Esempio n. 13
0
		public static HttpJsonRequest ToJsonRequest(this string url, AsyncServerClient requestor, OperationCredentials credentials, DocumentConvention convention, IDictionary<string, string> operationsHeaders, string method)
		{
			var httpJsonRequest = requestor.jsonRequestFactory.CreateHttpJsonRequest(
				new CreateHttpJsonRequestParams(requestor, url, method, credentials, convention)
					.AddOperationHeaders(operationsHeaders));
			
			return httpJsonRequest;
		}
Esempio n. 14
0
		public static HttpJsonRequest ToJsonRequest(this string url, AsyncServerClient requestor, OperationCredentials credentials, Document.DocumentConvention convention)
		{
			return requestor.jsonRequestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(requestor, url, "GET", credentials, convention));
		}
Esempio n. 15
0
			    public CounterBatch(RavenCountersClient countersClient, CounterConvention convention)
			    {
				    credentials = countersClient.PrimaryCredentials;
				    jsonRequestFactory = countersClient.JsonRequestFactory;
					counterStorageUrl = countersClient.CounterStorageUrl;
				    this.convention = convention;
			    }
Esempio n. 16
0
		public void ReleaseClient(HttpClient client, OperationCredentials credentials)
		{
			var key = new HttpClientCacheKey(client.Timeout, credentials);
			var queue = cache.GetOrAdd(key, i => new ConcurrentQueue<Tuple<long, HttpClient>>());
			queue.Enqueue(Tuple.Create(Stopwatch.GetTimestamp(), client));
		}
		public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, OperationCredentials credentials, DocumentConvention convention)
			: this(self, url, method, new RavenJObject(), credentials, convention)
		{
			
		}
Esempio n. 18
0
		public void ReleaseClient(HttpClient client, OperationCredentials credentials)
		{
			var key = new HttpClientCacheKey(client.Timeout, credentials);
			var queue = cache.GetOrAdd(key, i => new ConcurrentQueue<HttpClient>());
			queue.Enqueue(client);
		}
Esempio n. 19
0
        private void InitializeSecurity()
        {
            if (convention.HandleUnauthorizedResponseAsync != null)
                return; // already setup by the user

            if (string.IsNullOrEmpty(ApiKey) == false)
            {
                Credentials = null;
            }

            credentialsThatShouldBeUsedOnlyInOperationsWithoutReplication = new OperationCredentials(ApiKey, Credentials);

            var basicAuthenticator = new BasicAuthenticator(JsonRequestFactory.EnableBasicAuthenticationOverUnsecuredHttpEvenThoughPasswordsWouldBeSentOverTheWireInClearTextToBeStolenByHackers);
            var securedAuthenticator = new SecuredAuthenticator();

            JsonRequestFactory.ConfigureRequest += basicAuthenticator.ConfigureRequest;
            JsonRequestFactory.ConfigureRequest += securedAuthenticator.ConfigureRequest;

            convention.HandleForbiddenResponseAsync = (forbiddenResponse, credentials) =>
            {
                if (credentials.ApiKey == null)
                {
                    AssertForbiddenCredentialSupportWindowsAuth(forbiddenResponse);
                    return null;
                }

                return null;
            };

            convention.HandleUnauthorizedResponseAsync = (unauthorizedResponse, credentials) =>
            {
                var oauthSource = unauthorizedResponse.Headers.GetFirstValue("OAuth-Source");

#if DEBUG && FIDDLER
                // Make sure to avoid a cross DNS security issue, when running with Fiddler
				if (string.IsNullOrEmpty(oauthSource) == false)
					oauthSource = oauthSource.Replace("localhost:", "localhost.fiddler:");
#endif

                // Legacy support
                if (string.IsNullOrEmpty(oauthSource) == false &&
                    oauthSource.EndsWith("/OAuth/API-Key", StringComparison.CurrentCultureIgnoreCase) == false)
                {
                    return basicAuthenticator.HandleOAuthResponseAsync(oauthSource, credentials.ApiKey);
                }

                if (credentials.ApiKey == null)
                {
                    AssertUnauthorizedCredentialSupportWindowsAuth(unauthorizedResponse, credentials.Credentials);
                    return null;
                }

                if (string.IsNullOrEmpty(oauthSource))
                    oauthSource = ServerUrl + "/OAuth/API-Key";

                return securedAuthenticator.DoOAuthRequestAsync(ServerUrl, oauthSource, credentials.ApiKey);
            };

        }
 public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, OperationCredentials credentials, Convention convention, TimeSpan? timeout = null)
     : this(self, url, method, new RavenJObject(), credentials, convention, timeout)
 {}
Esempio n. 21
0
			public AdminClient(RavenCountersClient countersClient, CounterConvention convention)
            {
				credentials = countersClient.PrimaryCredentials;
				jsonRequestFactory = countersClient.JsonRequestFactory;
				serverUrl = countersClient.ServerUrl;
				counterStorageName = countersClient.CounterStorageName;
				this.convention = convention;
            }
Esempio n. 22
0
        public HttpClient GetClient(TimeSpan timeout, OperationCredentials credentials, Func<HttpMessageHandler> handlerFactory)
        {
            var key = new HttpClientCacheKey(timeout, credentials);
            var stack = cache.GetOrAdd(key, i => new ConcurrentStack<Tuple<long, HttpClient>>());

            Tuple<long, HttpClient> client;
            while (stack.TryPop(out client))
            {
                if (IsClientTooOld(client.Item1))
                {
                    client.Item2.Dispose();
                    continue;
                }
                client.Item2.CancelPendingRequests();
                client.Item2.DefaultRequestHeaders.Clear();
                return client.Item2;
            }

            return new HttpClient(handlerFactory())
            {
                Timeout = timeout
            };
        }