A representation of an HTTP json request to the RavenDB server
		/// <summary>
		/// Creates the HTTP json request.
		/// </summary>
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			if (disposed)
				throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);

			var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
			{
				ShouldCacheRequest =
					createHttpJsonRequestParams.AvoidCachingRequest == false && 
					createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
			};

			if (request.ShouldCacheRequest && createHttpJsonRequestParams.Method == "GET" && !DisableHttpCaching)
			{
				var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.AddHeader);
				request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
				request.SkipServerCheck = cachedRequestDetails.SkipServerCheck;
			}

			if (RequestTimeout != null)
				request.Timeout = RequestTimeout.Value;

			ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {Client = request.httpClient, Credentials = createHttpJsonRequestParams.Credentials});
			return request;
		}
 internal static void AddEtagHeader(HttpJsonRequest request, Etag etag)
 {
     if (etag != null)
     {
         request.AddHeader("If-None-Match", "\"" + etag + "\"");
     }
 }
Example #3
0
        public static Guid GetEtagHeader(this HttpJsonRequest request)
        {
#if SILVERLIGHT
            return(EtagHeaderToGuid(request.ResponseHeaders["ETag"].FirstOrDefault()));
#else
            return(EtagHeaderToGuid(request.ResponseHeaders["ETag"]));
#endif
        }
 internal void UpdateCacheTime(HttpJsonRequest httpJsonRequest)
 {
     if (httpJsonRequest.CachedRequestDetails == null)
     {
         throw new InvalidOperationException("Cannot update cached response from a request that has no cached information");
     }
     httpJsonRequest.CachedRequestDetails.Time = SystemTime.UtcNow;
 }
Example #5
0
        /// <summary>
        /// Creates the HTTP json request.
        /// </summary>
        /// <param name="self">The self.</param>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="credentials">The credentials.</param>
        /// <param name="convention">The document conventions governing this request</param>
        /// <returns></returns>
        public HttpJsonRequest CreateHttpJsonRequest(object self, string url, string method, RavenJObject metadata,
                                                     ICredentials credentials, DocumentConvention convention)
        {
            var request = new HttpJsonRequest(url, method, metadata, credentials, this);

            ConfigureCaching(url, method, convention, request);
            ConfigureRequest(self, new WebRequestEventArgs {
                Request = request.WebRequest
            });
            return(request);
        }
Example #6
0
 internal string GetCachedResponse(HttpJsonRequest httpJsonRequest)
 {
     if (httpJsonRequest.CachedRequestDetails == null)
     {
         throw new InvalidOperationException("Cannot get cached response from a request that has no cached infomration");
     }
     httpJsonRequest.ResponseStatusCode = HttpStatusCode.NotModified;
     httpJsonRequest.ResponseHeaders    = new NameValueCollection(httpJsonRequest.CachedRequestDetails.Headers);
     Interlocked.Increment(ref NumOfCachedRequests);
     return(httpJsonRequest.CachedRequestDetails.Data);
 }
Example #7
0
 internal RavenJToken GetCachedResponse(HttpJsonRequest httpJsonRequest)
 {
     if (httpJsonRequest.CachedRequestDetails == null)
     {
         throw new InvalidOperationException("Cannot get cached response from a request that has no cached information");
     }
     httpJsonRequest.ResponseStatusCode = HttpStatusCode.NotModified;
     httpJsonRequest.ResponseHeaders    = new NameValueCollection(httpJsonRequest.CachedRequestDetails.Headers);
     IncrementCachedRequests();
     return(httpJsonRequest.CachedRequestDetails.Data.CloneToken());
 }
Example #8
0
 internal void CacheResponse(WebResponse response, string text, HttpJsonRequest httpJsonRequest)
 {
     if (httpJsonRequest.Method == "GET" && httpJsonRequest.ShouldCacheRequest &&
         string.IsNullOrEmpty(response.Headers["ETag"]) == false)
     {
         cache.Set(httpJsonRequest.Url, new CachedRequest
         {
             Data    = text,
             Headers = response.Headers
         }, new CacheItemPolicy());                 // cache as much as possible, for as long as possible, using the default cache limits
     }
 }
		public async Task<SynchronizationReport> PushChangesAsync(CancellationToken token)
		{
			token.Register(() => { });//request.Abort() TODO: check this

			token.ThrowIfCancellationRequested();

			if (sourceStream.CanRead == false)
				throw new Exception("Stream does not support reading");

            var commands = (IAsyncFilesCommandsImpl)this.destination.Commands;

            var baseUrl = commands.UrlFor();
            var credentials = commands.PrimaryCredentials;
            var conventions = commands.Conventions;

            request = commands.RequestFactory.CreateHttpJsonRequest(
                                    new CreateHttpJsonRequestParams(this, baseUrl + "/synchronization/MultipartProceed",
                                                                    "POST", credentials, conventions));

            // REVIEW: (Oren) There is a mismatch of expectations in the AddHeaders. ETag must always have to be surrounded by quotes. 
            //         If AddHeader/s ever put an etag it should check for that.
            //         I was hesitant to do the change though, because I do not understand the complete scope of such a change.
            request.AddHeaders(sourceMetadata);           
			request.AddHeader("Content-Type", "multipart/form-data; boundary=" + syncingBoundary);

			request.AddHeader(SyncingMultipartConstants.FileName, fileName);
			request.AddHeader(SyncingMultipartConstants.SourceServerInfo, serverInfo.AsJson());

			try
			{
				await request.WriteAsync(PrepareMultipartContent(token));

				var response = await request.ReadResponseJsonAsync();
				return new JsonSerializer().Deserialize<SynchronizationReport>(new RavenJTokenReader(response));
			}
			catch (Exception exception)
			{
				if (token.IsCancellationRequested)
				{
					throw new OperationCanceledException(token);
				}

				var webException = exception as ErrorResponseException;

				if (webException != null)
				{
					webException.SimplifyException();
				}

				throw;
			}
		}
		public async Task<SynchronizationReport> PushChangesAsync(CancellationToken token)
		{
			token.Register(() => { });//request.Abort() TODO: check this

			token.ThrowIfCancellationRequested();

			if (sourceStream.CanRead == false)
			{
				throw new Exception("Stream does not support reading");
			}

			request =
				destination.JsonRequestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(this,
					destination.FileSystemUrl + "/synchronization/MultipartProceed",
					"POST", destination.Credentials, destination.Convention));

			//request.SendChunked = true;
			//request.AllowWriteStreamBuffering = false;
			//request.KeepAlive = true;

			request.AddHeaders(sourceMetadata);

			request.AddHeader("Content-Type", "multipart/form-data; boundary=" + syncingBoundary);

			request.AddHeader(SyncingMultipartConstants.FileName, fileName);
			request.AddHeader(SyncingMultipartConstants.SourceServerInfo, serverInfo.AsJson());

			try
			{
				await request.WriteAsync(PrepareMultipartContent(token));

				var response = await request.ReadResponseJsonAsync();
				return new JsonSerializer().Deserialize<SynchronizationReport>(new RavenJTokenReader(response));
			}
			catch (Exception exception)
			{
				if (token.IsCancellationRequested)
				{
					throw new OperationCanceledException(token);
				}

				var webException = exception as ErrorResponseException;

				if (webException != null)
				{
					webException.BetterWebExceptionError();
				}

				throw;
			}
		}
Example #11
0
        internal RavenJToken GetCachedResponse(HttpJsonRequest httpJsonRequest, NameValueCollection additionalHeaders = null)
        {
            if (httpJsonRequest.CachedRequestDetails == null)
            {
                throw new InvalidOperationException("Cannot get cached response from a request that has no cached information");
            }
            httpJsonRequest.ResponseStatusCode = HttpStatusCode.NotModified;
            httpJsonRequest.ResponseHeaders    = new NameValueCollection(httpJsonRequest.CachedRequestDetails.Headers);

            if (additionalHeaders != null && additionalHeaders[Constants.RavenForcePrimaryServerCheck] != null)
            {
                httpJsonRequest.ResponseHeaders.Add(Constants.RavenForcePrimaryServerCheck, additionalHeaders[Constants.RavenForcePrimaryServerCheck]);
            }

            IncrementCachedRequests();
            return(httpJsonRequest.CachedRequestDetails.Data.CloneToken());
        }
Example #12
0
		public bool CanFullyCache(HttpJsonRequestFactory jsonRequestFactory, HttpJsonRequest httpJsonRequest, string postedData)
		{
			if (allRequestsCanBeServedFromAggressiveCache) // can be fully served from aggresive cache
			{
				jsonRequestFactory.InvokeLogRequest(holdProfilingInformation, new RequestResultArgs
				{
					DurationMilliseconds = httpJsonRequest.CalculateDuration(),
					Method = httpJsonRequest.webRequest.Method,
					HttpResult = 0,
					Status = RequestStatus.AggresivelyCached,
					Result = "",
					Url = httpJsonRequest.webRequest.RequestUri.PathAndQuery,
					PostedData = postedData
				});
				return true;
			}
			return false;
		}
 public bool CanFullyCache(HttpJsonRequestFactory jsonRequestFactory, HttpJsonRequest httpJsonRequest, string postedData)
 {
     if (allRequestsCanBeServedFromAggressiveCache)             // can be fully served from aggressive cache
     {
         jsonRequestFactory.InvokeLogRequest(holdProfilingInformation, new RequestResultArgs
         {
             DurationMilliseconds = httpJsonRequest.CalculateDuration(),
             Method     = httpJsonRequest.webRequest.Method,
             HttpResult = 0,
             Status     = RequestStatus.AggresivelyCached,
             Result     = "",
             Url        = httpJsonRequest.webRequest.RequestUri.PathAndQuery,
             PostedData = postedData
         });
         return(true);
     }
     return(false);
 }
		public RemoteBulkInsertOperation(BulkInsertOptions options, ServerClient client)
		{
			this.client = client;
			items = new BlockingCollection<RavenJObject>(options.BatchSize*8);
			string requestUrl = "/bulkInsert?";
			if (options.CheckForUpdates)
				requestUrl += "checkForUpdates=true";
			if (options.CheckReferencesInIndexes)
				requestUrl += "&checkReferencesInIndexes=true";

			// this will force the HTTP layer to authenticate, meaning that our next request won't have to
			HttpJsonRequest req = client.CreateRequest("POST", requestUrl + "&no-op=for-auth-only",
			                                           disableRequestCompression: true);
			req.ExecuteRequest();


			httpJsonRequest = client.CreateRequest("POST", requestUrl, disableRequestCompression: true);
			// the request may take a long time to process, so we need to set a large timeout value
			httpJsonRequest.Timeout = TimeSpan.FromHours(6); 
			nextTask = httpJsonRequest.GetRawRequestStream()
			                          .ContinueWith(task =>
			                          {
				                          Stream requestStream = task.Result;
				                          while (true)
				                          {
					                          var batch = new List<RavenJObject>();
					                          RavenJObject item;
					                          while (items.TryTake(out item, 200))
					                          {
						                          if (item == null) // marker
						                          {
							                          FlushBatch(requestStream, batch);
							                          return;
						                          }
						                          batch.Add(item);
						                          if (batch.Count >= options.BatchSize)
							                          break;
					                          }
					                          FlushBatch(requestStream, batch);
				                          }
			                          });
		}
		/// <summary>
		/// Creates the HTTP json request.
		/// </summary>
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			if (disposed)
				throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);
			var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
			{
				ShouldCacheRequest =
					createHttpJsonRequestParams.AvoidCachingRequest == false && 
					createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
			};

			if (request.ShouldCacheRequest && createHttpJsonRequestParams.Method == "GET" && !DisableHttpCaching)
			{
				var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.webRequest.Headers.Set);
				request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
				request.SkipServerCheck = cachedRequestDetails.SkipServerCheck;
			}
			ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {Request = request.webRequest});
			return request;
		}
Example #16
0
        /// <summary>
        /// Creates the HTTP json request.
        /// </summary>
        public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);
            }
            var request = new HttpJsonRequest(createHttpJsonRequestParams, this)
            {
                ShouldCacheRequest = createHttpJsonRequestParams.Convention.ShouldCacheRequest(createHttpJsonRequestParams.Url)
            };

            if (request.ShouldCacheRequest && createHttpJsonRequestParams.Method == "GET" && !DisableHttpCaching)
            {
                var cachedRequestDetails = ConfigureCaching(createHttpJsonRequestParams.Url, request.webRequest.Headers.Set);
                request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
                request.SkipServerCheck      = cachedRequestDetails.SkipServerCheck;
            }
            ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs {
                Request = request.webRequest
            });
            return(request);
        }
        /// <summary>
        /// Creates the HTTP json request.
        /// </summary>
        public HttpJsonRequest CreateHttpJsonRequest(IHoldProfilingInformation self, string url, string method, RavenJObject metadata,
                                                     ICredentials credentials, DocumentConvention convention)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(typeof(HttpJsonRequestFactory).FullName);
            }
            var request = new HttpJsonRequest(url, method, metadata, credentials, this, self, convention)
            {
                ShouldCacheRequest = convention.ShouldCacheRequest(url)
            };

            if (request.ShouldCacheRequest && method == "GET" && !DisableHttpCaching)
            {
                var cachedRequestDetails = ConfigureCaching(url, request.webRequest.Headers.Set);
                request.CachedRequestDetails = cachedRequestDetails.CachedRequest;
                request.SkipServerCheck      = cachedRequestDetails.SkipServerCheck;
            }
            ConfigureRequest(self, new WebRequestEventArgs {
                Request = request.webRequest
            });
            return(request);
        }
		public RemoteBulkInsertOperation(BulkInsertOptions options, ServerClient client)
		{
			this.options = options;
			this.client = client;
			items = new BlockingCollection<RavenJObject>(options.BatchSize*8);
			string requestUrl = "/bulkInsert?";
			if (options.CheckForUpdates)
				requestUrl += "checkForUpdates=true";
			if (options.CheckReferencesInIndexes)
				requestUrl += "&checkReferencesInIndexes=true";

			var expect100Continue = client.Expect100Continue();

			// this will force the HTTP layer to authenticate, meaning that our next request won't have to
			HttpJsonRequest req = client.CreateRequest("POST", requestUrl + "&op=generate-single-use-auth-token",
														disableRequestCompression: true);
			var token = req.ReadResponseJson();


			httpJsonRequest = client.CreateRequest("POST", requestUrl, disableRequestCompression: true);
			// the request may take a long time to process, so we need to set a large timeout value
			httpJsonRequest.PrepareForLongRequest();
			httpJsonRequest.AddOperationHeader("Single-Use-Auth-Token", token.Value<string>("Token"));
			nextTask = httpJsonRequest.GetRawRequestStream()
			                          .ContinueWith(task =>
			                          {
				                          try
				                          {
					                          expect100Continue.Dispose();
				                          }
				                          catch (Exception)
				                          {
				                          }
										  WriteQueueToServer(task);
			                          });
		}
        internal RavenJToken GetCachedResponse(HttpJsonRequest httpJsonRequest, HttpResponseHeaders additionalHeaders)
        {
            if (httpJsonRequest.CachedRequestDetails == null)
                throw new InvalidOperationException("Cannot get cached response from a request that has no cached information");
            httpJsonRequest.ResponseStatusCode = HttpStatusCode.NotModified;
            httpJsonRequest.ResponseHeaders = new NameValueCollection(httpJsonRequest.CachedRequestDetails.Headers);

            if (additionalHeaders != null)
            {
                string forcePrimaryServerCHeck = additionalHeaders.GetFirstValue(Constants.RavenForcePrimaryServerCheck);
                if (forcePrimaryServerCHeck != null)
                    httpJsonRequest.ResponseHeaders.Add(Constants.RavenForcePrimaryServerCheck, forcePrimaryServerCHeck);
            }

            IncrementCachedRequests();
            return httpJsonRequest.CachedRequestDetails.Data.CloneToken();
        }
Example #20
0
 public static Guid GetEtagHeader(this HttpJsonRequest request)
 {
     return(EtagHeaderToGuid(request.ResponseHeaders["ETag"]));
 }
		/// <summary>
		/// Creates the HTTP json request.
		/// </summary>
		/// <param name="self">The self.</param>
		/// <param name="url">The URL.</param>
		/// <param name="method">The method.</param>
		/// <param name="metadata">The metadata.</param>
		/// <param name="credentials">The credentials.</param>
		/// <param name="convention">The document conventions governing this request</param>
		/// <returns></returns>
		public HttpJsonRequest CreateHttpJsonRequest(object self, string url, string method, RavenJObject metadata, ICredentials credentials, DocumentConvention convention)
		{
			var request = new HttpJsonRequest(url, method, metadata, convention);
			ConfigureRequest(self, new WebRequestEventArgs { Request = request.webRequest });
			return request;
		}
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			var request = new HttpJsonRequest(createHttpJsonRequestParams, this);
			ConfigureRequest(createHttpJsonRequestParams.Owner, new WebRequestEventArgs { Request = request.webRequest, JsonRequest = request });
			return request;
		}
Example #23
0
 public static Etag GetEtagHeader(this HttpJsonRequest request)
 {
     return(EtagHeaderToEtag(request.ResponseHeaders[Constants.MetadataEtagField]));
 }
		/// <summary>
		/// Creates the HTTP json request.
		/// </summary>
		public HttpJsonRequest CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams)
		{
			var request = new HttpJsonRequest(createHttpJsonRequestParams.Url, createHttpJsonRequestParams.Method, createHttpJsonRequestParams.Metadata, createHttpJsonRequestParams.Convention, this);
			ConfigureRequest(createHttpJsonRequestParams.Self, new WebRequestEventArgs { Request = request.webRequest, JsonRequest = request });
			return request;
		}
        internal RavenJToken GetCachedResponse(HttpJsonRequest httpJsonRequest)
        {
            if (httpJsonRequest.CachedRequestDetails == null)
                throw new InvalidOperationException("Cannot get cached response from a request that has no cached information");
            httpJsonRequest.ResponseStatusCode = HttpStatusCode.NotModified;
            httpJsonRequest.ResponseHeaders = new NameValueCollection(httpJsonRequest.CachedRequestDetails.Headers);

            IncrementCachedRequests();
            return httpJsonRequest.CachedRequestDetails.Data.CloneToken();
        }
 internal void UpdateCacheTime(HttpJsonRequest httpJsonRequest)
 {
     if (httpJsonRequest.CachedRequestDetails == null)
         throw new InvalidOperationException("Cannot update cached response from a request that has no cached information");
     httpJsonRequest.CachedRequestDetails.Time = SystemTime.UtcNow;
 }
Example #27
0
        private void ConfigureCaching(string url, string method, DocumentConvention convention, HttpJsonRequest request)
        {
            request.ShouldCacheRequest = convention.ShouldCacheRequest(url);
            if (!request.ShouldCacheRequest || method != "GET")
            {
                return;
            }

            var cachedRequest = (CachedRequest)cache.Get(url);

            if (cachedRequest == null)
            {
                return;
            }
            request.CachedRequestDetails = cachedRequest;
            request.WebRequest.Headers["If-None-Match"] = cachedRequest.Headers["ETag"];
        }