示例#1
0
        private static HgResumeApiResponse HandleResponse(HttpWebResponse res)
        {
            var apiResponse = new HgResumeApiResponse();

            apiResponse.ResumableResponse = new HgResumeApiResponseHeaders(res.Headers);
            apiResponse.HttpStatus        = res.StatusCode;
            apiResponse.Content           = WebResponseHelper.ReadResponseContent(res);
            return(apiResponse);
        }
示例#2
0
        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);
        }
示例#3
0
        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(Resources.ksHgTransptUnauthorized);
                    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);
            }
        }
示例#4
0
        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.");
                        _progress.WriteVerbose(string.Format("Additional error info: {0}", response.ResumableResponse.Error));
                        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);
            }
        }
示例#5
0
 public void AddResponse(HgResumeApiResponse response)
 {
     throw new NotImplementedException();
 }
示例#6
0
 public static HgResumeApiResponse Failed(string message)
 {
     var parameters = new Dictionary<string, string>
                          {
                              {"X-HgR-Status", "FAIL"},
                              {"X-HgR-Version", "1"}
                          };
     if (!String.IsNullOrEmpty(message))
     {
         parameters.Add("X-HgR-Error", message);
     }
     var response = new HgResumeApiResponse
     {
         HttpStatus = HttpStatusCode.BadRequest,
         ResumableResponse = new HgResumeApiResponseHeaders(GetWebHeaderCollection(parameters)),
         ResponseTimeInMilliseconds = 200
     };
     return response;
 }
示例#7
0
 public HgResumeApiResponse Execute(string method, HgResumeApiParameters request, byte[] contentToSend, int secondsBeforeTimeout)
 {
     ValidateParameters(method, request, contentToSend, secondsBeforeTimeout);
     HgResumeApiResponse response;
     if (_responseQueue.Count > 0)
     {
         response = _responseQueue.Dequeue();
         if (response.HttpStatus == HttpStatusCode.RequestTimeout)
         {
             return null;
         }
     } else
     {
         response = new HgResumeApiResponse {HttpStatus = HttpStatusCode.InternalServerError};
     }
     response.ResponseTimeInMilliseconds = 200;
     return response;
 }
示例#8
0
 public void AddResponse(HgResumeApiResponse response)
 {
     _responseQueue.Enqueue(response);
 }
        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;
        }