private static HgResumeApiResponse HandleResponse(HttpWebResponse res) { var apiResponse = new HgResumeApiResponse(); apiResponse.ResumableResponse = new HgResumeApiResponseHeaders(res.Headers); apiResponse.HttpStatus = res.StatusCode; var responseStream = res.GetResponseStream(); if (responseStream != null && !String.IsNullOrEmpty(res.Headers["Content-Length"])) { apiResponse.Content = ReadStream(responseStream, Convert.ToInt32(res.Headers["Content-Length"])); } else { apiResponse.Content = new byte[0]; } return(apiResponse); }
private PullResponse PullOneChunk(HgResumeApiParameters request) { var pullResponse = new PullResponse(PullStatus.Fail); try { HgResumeApiResponse response = _apiServer.Execute("pullBundleChunk", request, TimeoutInSeconds); if (response == null) { _progress.WriteVerbose("API REQ: {0} Timeout", _apiServer.Url); pullResponse.Status = PullStatus.Timeout; return(pullResponse); } /* API returns the following HTTP codes: * 200 OK (SUCCESS) * 304 Not Modified (NOCHANGE) * 400 Bad Request (FAIL, UNKNOWNID) */ _progress.WriteVerbose("API REQ: {0} RSP: {1} in {2}ms", _apiServer.Url, response.HttpStatus, response.ResponseTimeInMilliseconds); if (response.ResumableResponse.HasNote) { _progress.WriteMessage(String.Format("Server replied: {0}", response.ResumableResponse.Note)); } if (response.HttpStatus == HttpStatusCode.ServiceUnavailable && response.Content.Length > 0) { var msg = String.Format("Server temporarily unavailable: {0}", Encoding.UTF8.GetString(response.Content)); _progress.WriteError(msg); pullResponse.Status = PullStatus.NotAvailable; return(pullResponse); } if (response.HttpStatus == HttpStatusCode.NotModified) { pullResponse.Status = PullStatus.NoChange; return(pullResponse); } if (response.HttpStatus == HttpStatusCode.Accepted) { pullResponse.Status = PullStatus.InProgress; return(pullResponse); } // chunk pulled OK if (response.HttpStatus == HttpStatusCode.OK) { pullResponse.BundleSize = response.ResumableResponse.BundleSize; pullResponse.Status = PullStatus.OK; pullResponse.ChunkSize = CalculateChunkSize(request.ChunkSize, response.ResponseTimeInMilliseconds); pullResponse.Chunk = response.Content; return(pullResponse); } if (response.HttpStatus == HttpStatusCode.BadRequest && response.ResumableResponse.Status == "UNKNOWNID") { // this is not implemented currently (feb 2012 cjh) _progress.WriteError("The server {0} does not have the project '{1}'", _targetLabel, request.RepoId); return(pullResponse); } if (response.HttpStatus == HttpStatusCode.BadRequest && response.ResumableResponse.Status == "RESET") { pullResponse.Status = PullStatus.Reset; return(pullResponse); } if (response.HttpStatus == HttpStatusCode.BadRequest) { if (response.ResumableResponse.HasError) { if (response.ResumableResponse.Error == "invalid baseHash") { pullResponse.Status = PullStatus.InvalidHash; } else { _progress.WriteWarning("Server Error: {0}", response.ResumableResponse.Error); } } return(pullResponse); } if (response.HttpStatus == HttpStatusCode.Unauthorized) { _progress.WriteWarning("There is an authorization problem accessing this project. Check the project ID as well as your username and password. Alternatively, you may not be authorized to access this project."); pullResponse.Status = PullStatus.Unauthorized; } _progress.WriteWarning("Invalid Server Response '{0}'", response.HttpStatus); return(pullResponse); } catch (WebException e) { _progress.WriteWarning(String.Format("Pull data chunk failed: {0}", e.Message)); return(pullResponse); } }
private PushResponse PushOneChunk(HgResumeApiParameters request, byte[] dataToPush) { var pushResponse = new PushResponse(PushStatus.Fail); try { HgResumeApiResponse response = _apiServer.Execute("pushBundleChunk", request, dataToPush, TimeoutInSeconds); if (response == null) { _progress.WriteVerbose("API REQ: {0} Timeout"); pushResponse.Status = PushStatus.Timeout; return(pushResponse); } /* API returns the following HTTP codes: * 200 OK (SUCCESS) * 202 Accepted (RECEIVED) * 412 Precondition Failed (RESEND) * 400 Bad Request (FAIL, UNKNOWNID, and RESET) */ _progress.WriteVerbose("API REQ: {0} RSP: {1} in {2}ms", _apiServer.Url, response.HttpStatus, response.ResponseTimeInMilliseconds); if (response.HttpStatus == HttpStatusCode.ServiceUnavailable && response.Content.Length > 0) { var msg = String.Format("Server temporarily unavailable: {0}", Encoding.UTF8.GetString(response.Content)); _progress.WriteError(msg); pushResponse.Status = PushStatus.NotAvailable; return(pushResponse); } if (response.ResumableResponse.HasNote) { _progress.WriteWarning(String.Format("Server replied: {0}", response.ResumableResponse.Note)); } // the chunk was received successfully if (response.HttpStatus == HttpStatusCode.Accepted) { pushResponse.StartOfWindow = response.ResumableResponse.StartOfWindow; pushResponse.Status = PushStatus.Received; pushResponse.ChunkSize = CalculateChunkSize(request.ChunkSize, response.ResponseTimeInMilliseconds); return(pushResponse); } // the final chunk was received successfully if (response.HttpStatus == HttpStatusCode.OK) { pushResponse.Status = PushStatus.Complete; return(pushResponse); } if (response.HttpStatus == HttpStatusCode.BadRequest) { if (response.ResumableResponse.Status == "UNKNOWNID") { _progress.WriteError("The server {0} does not have the project '{1}'", _targetLabel, _apiServer.ProjectId); return(pushResponse); } if (response.ResumableResponse.Status == "RESET") { _progress.WriteError("Push failed: All chunks were pushed to the server, but the unbundle operation failed. Try again later."); pushResponse.Status = PushStatus.Reset; return(pushResponse); } if (response.ResumableResponse.HasError) { if (response.ResumableResponse.Error == "invalid baseHash") { pushResponse.Status = PushStatus.InvalidHash; } else { _progress.WriteError("Server Error: {0}", response.ResumableResponse.Error); } return(pushResponse); } } _progress.WriteWarning("Invalid Server Response '{0}'", response.HttpStatus); return(pushResponse); } catch (WebException e) { _progress.WriteWarning(String.Format("Push data chunk failed: {0}", e.Message)); return(pushResponse); } }