protected override internal Task ExecuteRequest(HttpClient httpClient, HttpRequestMessage request) { object fullBody = null; Exception error = null; HttpResponseMessage response = null; if (_tokenSource.IsCancellationRequested) { RespondWithResult(fullBody, new Exception(string.Format("{0}: Request {1} has been aborted", this, request)), response); var tcs = new TaskCompletionSource <bool>(); tcs.SetCanceled(); return(tcs.Task); } Log.D(Tag + ".ExecuteRequest", "Sending request: {0}", request); var requestTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_tokenSource.Token); var retVal = httpClient.SendAsync(request, requestTokenSource.Token); retVal.ConfigureAwait(false).GetAwaiter().OnCompleted(() => { requestTokenSource.Dispose(); try { response = retVal.Result; } catch (Exception e) { var err = (e is AggregateException) ? e.InnerException : e; Log.E(Tag, "Unhandled Exception", err); error = err; RespondWithResult(fullBody, err, response); return; } try { if (response == null) { Log.E(Tag, "Didn't get response for {0}", request); error = new HttpRequestException(); RespondWithResult(fullBody, error, response); } else if (!response.IsSuccessStatusCode) { HttpStatusCode status = response.StatusCode; Log.E(Tag, "Got error status: {0} for {1}. Reason: {2}", status.GetStatusCode(), request, response.ReasonPhrase); error = new HttpResponseException(status); RespondWithResult(fullBody, error, response); } else { Log.D(Tag, "Processing response: {0}", response); var entity = response.Content; var contentTypeHeader = entity.Headers.ContentType; Stream inputStream = null; if (contentTypeHeader != null && contentTypeHeader.ToString().Contains("multipart/")) { Log.V(Tag, "contentTypeHeader = {0}", contentTypeHeader.ToString()); try { _topReader = new MultipartReader(contentTypeHeader.ToString(), this); inputStream = entity.ReadAsStreamAsync().Result; const int bufLen = 1024; var buffer = new byte[bufLen]; var numBytesRead = 0; while ((numBytesRead = inputStream.Read(buffer, 0, bufLen)) > 0) { if (numBytesRead != bufLen) { var bufferToAppend = new Couchbase.Lite.Util.ArraySegment <byte>(buffer, 0, numBytesRead).ToArray(); _topReader.AppendData(bufferToAppend); } else { _topReader.AppendData(buffer); } } _topReader.Finished(); RespondWithResult(fullBody, error, response); } finally { try { inputStream.Close(); } catch (IOException) { } } } else { Log.V(Tag, "contentTypeHeader is not multipart = {0}", contentTypeHeader.ToString()); if (entity != null) { try { inputStream = entity.ReadAsStreamAsync().Result; fullBody = Manager.GetObjectMapper().ReadValue <object>(inputStream); RespondWithResult(fullBody, error, response); } finally { try { inputStream.Close(); } catch (IOException) { } } } } } } catch (AggregateException e) { var err = e.InnerException; Log.E(Tag, "Unhandled Exception", err); error = err; RespondWithResult(fullBody, err, response); } catch (IOException e) { Log.E(Tag, "IO Exception", e); error = e; RespondWithResult(fullBody, e, response); } catch (Exception e) { Log.E(Tag, "ExecuteRequest Exception: ", e); error = e; RespondWithResult(fullBody, e, response); } finally { response.Dispose(); } }); return(retVal); }
private Task ExecuteRequest(CouchbaseLiteHttpClient httpClient, HttpRequestMessage request) { object fullBody = null; Exception error = null; HttpResponseMessage response = null; if (_tokenSource.IsCancellationRequested) { RespondWithResult(fullBody, new Exception(string.Format("{0}: Request {1} has been aborted", this, request)), response); var tcs = new TaskCompletionSource <bool>(); tcs.SetCanceled(); return(tcs.Task); } request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip")); request.Headers.Add("X-Accept-Part-Encoding", "gzip"); Log.To.Sync.V(Tag, "Sending request: {0}", request); var requestTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_tokenSource.Token); httpClient.Authenticator = Authenticator; return(httpClient.SendAsync(request, requestTokenSource.Token).ContinueWith(t => { requestTokenSource.Dispose(); try { response = t.Result; } catch (Exception e) { var err = Misc.Flatten(e).First(); Log.To.Sync.E(Tag, "Unhandled exception while getting bulk documents", err); error = err; RespondWithResult(fullBody, err, response); return; } try { if (response == null) { Log.To.Sync.I(Tag, "Didn't get response for {0}", request); error = new HttpRequestException(); RespondWithResult(fullBody, error, response); } else if (!response.IsSuccessStatusCode) { HttpStatusCode status = response.StatusCode; Log.To.Sync.I(Tag, "Got error status: {0} for {1}. Reason: {2}", status.GetStatusCode(), request, response.ReasonPhrase); error = new HttpResponseException(status); RespondWithResult(fullBody, error, response); } else { Log.To.Sync.D(Tag, "Processing response: {0}", response); var entity = response.Content; var contentTypeHeader = entity.Headers.ContentType; Stream inputStream = null; if (contentTypeHeader != null && contentTypeHeader.ToString().Contains("multipart/")) { Log.To.Sync.D(Tag, "contentTypeHeader = {0}", contentTypeHeader.ToString()); try { _topReader = new MultipartReader(contentTypeHeader.ToString(), this); inputStream = entity.ReadAsStreamAsync().Result; const int bufLen = 1024; var buffer = new byte[bufLen]; var numBytesRead = 0; while ((numBytesRead = inputStream.Read(buffer, 0, bufLen)) > 0) { if (numBytesRead != bufLen) { var bufferToAppend = new Couchbase.Lite.Util.ArraySegment <byte>(buffer, 0, numBytesRead).ToArray(); _topReader.AppendData(bufferToAppend); } else { _topReader.AppendData(buffer); } } RespondWithResult(fullBody, error, response); } finally { try { inputStream.Close(); } catch (IOException) { } } } else { Log.To.Sync.D(Tag, "contentTypeHeader is not multipart = {0}", contentTypeHeader.ToString()); if (entity != null) { try { inputStream = entity.ReadAsStreamAsync().Result; fullBody = Manager.GetObjectMapper().ReadValue <object>(inputStream); RespondWithResult(fullBody, error, response); } finally { try { inputStream.Close(); } catch (IOException) { } } } } } } catch (Exception e) { var err = (e is AggregateException) ? e.InnerException : e; Log.To.Sync.E(Tag, "Exception while processing bulk download response", err); error = err; RespondWithResult(fullBody, err, response); } })); }
protected override internal void ExecuteRequest(HttpClient httpClient, HttpRequestMessage request) { object fullBody = null; Exception error = null; HttpResponseMessage response = null; try { if (_tokenSource.IsCancellationRequested) { RespondWithResult(fullBody, new Exception(string.Format("{0}: Request {1} has been aborted", this, request)), response); return; } } catch (AggregateException e) { var err = e.InnerException; Log.E(Tag, "Unhandled Exception", err); error = err; RespondWithResult(fullBody, err, response); return; } catch (IOException e) { Log.E(Tag, "IO Exception", e); error = e; RespondWithResult(fullBody, e, response); return; } catch (Exception e) { Log.E(Tag, "ExecuteRequest Exception: ", e); error = e; RespondWithResult(fullBody, e, response); return; } try { Log.D(Tag + ".ExecuteRequest", "Sending request: {0}", request); var requestTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_tokenSource.Token); var responseTask = httpClient.SendAsync(request, requestTokenSource.Token); if (!responseTask.Wait((Int32)ManagerOptions.Default.RequestTimeout.TotalMilliseconds, requestTokenSource.Token)) { Log.E(Tag, "Response task timed out: {0}, {1}, {2}", responseTask, TaskScheduler.Current, Description()); throw new HttpResponseException(HttpStatusCode.RequestTimeout); } requestTokenSource.Dispose(); response = responseTask.Result; } catch (AggregateException e) { var err = e.InnerException; Log.E(Tag, "Unhandled Exception: {0}, {1}", TaskScheduler.Current, Description()); Log.E(Tag, "Unhandled Exception at Line 129 or 130", err); error = err; RespondWithResult(fullBody, err, response); return; } catch (IOException e) { Log.E(Tag, "IO Exception", e); error = e; RespondWithResult(fullBody, e, response); return; } catch (Exception e) { Log.E(Tag, "ExecuteRequest Exception: ", e); error = e; RespondWithResult(fullBody, e, response); return; } try { if (response == null) { Log.E(Tag, "Didn't get response for {0}", request); error = new HttpRequestException(); RespondWithResult(fullBody, error, response); } else if (!response.IsSuccessStatusCode) { HttpStatusCode status = response.StatusCode; Log.E(Tag, "Got error status: {0} for {1}. Reason: {2}", status.GetStatusCode(), request, response.ReasonPhrase); error = new HttpResponseException(status); RespondWithResult(fullBody, error, response); } else { Log.D(Tag, "Processing response: {0}", response); var entity = response.Content; var contentTypeHeader = entity.Headers.ContentType; Stream inputStream = null; if (contentTypeHeader != null && contentTypeHeader.ToString().Contains("multipart/")) { Log.V(Tag, "contentTypeHeader = {0}", contentTypeHeader.ToString()); try { _topReader = new MultipartReader(contentTypeHeader.ToString(), this); inputStream = entity.ReadAsStreamAsync().Result; const int bufLen = 1024; var buffer = new byte[bufLen]; var numBytesRead = 0; while ((numBytesRead = inputStream.Read(buffer, 0, bufLen)) > 0) { if (numBytesRead != bufLen) { var bufferToAppend = new Couchbase.Lite.Util.ArraySegment <byte>(buffer, 0, numBytesRead).ToArray(); _topReader.AppendData(bufferToAppend); } else { _topReader.AppendData(buffer); } } _topReader.Finished(); RespondWithResult(fullBody, error, response); } finally { try { inputStream.Close(); } catch (IOException) { } } } else { Log.V(Tag, "contentTypeHeader is not multipart = {0}", contentTypeHeader.ToString()); if (entity != null) { try { inputStream = entity.ReadAsStreamAsync().Result; fullBody = Manager.GetObjectMapper().ReadValue <object>(inputStream); RespondWithResult(fullBody, error, response); } finally { try { inputStream.Close(); } catch (IOException) { } } } } } } catch (AggregateException e) { var err = e.InnerException; Log.E(Tag, "Unhandled Exception", err); error = err; RespondWithResult(fullBody, err, response); } catch (IOException e) { Log.E(Tag, "IO Exception", e); error = e; RespondWithResult(fullBody, e, response); } catch (Exception e) { Log.E(Tag, "ExecuteRequest Exception: ", e); error = e; RespondWithResult(fullBody, e, response); } }