Пример #1
0
        public static void CastFailure(Protocol.Error code, WebSocket socket)
        {
            var writer = new Library.Writer(1 << 4);

            writer.WriteFailure((int)code);
            writer.Flush(socket);
            socket.Close();
        }
        private AdsErrorCode MapError(Protocol.Error error)
        {
            switch (error)
            {
            case Protocol.Error.NoError:
                return(AdsErrorCode.NoError);

            case Protocol.Error.ApiError:
                return(AdsErrorCode.ClientError);

            default:
                return(AdsErrorCode.InternalError);
            }
        }
Пример #3
0
        protected async Task <AcmeResponse> Request(
            string url,
            RequestParams @params = null
            )
        {
            @params = @params ?? new RequestParams();

            string content = null;

            var request = new HttpRequestMessage(@params.Method, url);

            if (@params.Payload != null)
            {
                var jws = new JsonWebSignature
                {
                    Payload = "",
                };

                jws.SetProtected(new JsonWebSignatureProtected
                {
                    Algorithm = AlgorithmsEnum.RS256,
                    Nonce     = Nonce,
                    Url       = url,
                    KeyID     = @params.IncludePublicKey ? null : Location,
                    Key       = @params.IncludePublicKey ? new JsonWebKey(Key) : null
                });

                if (!(@params.Payload is string))
                {
                    jws.SetPayload(@params.Payload);
                }

                jws.Sign(Key);

                content = JsonConvert.SerializeObject(jws, Formatting.Indented);
            }

            if (!string.IsNullOrEmpty(content))
            {
                request.Content = new StringContent(content);
                request.Content.Headers.ContentType = MediaTypeHeader.JsonContentTypeHeaderValue;
            }

            // Copy headers to HTTP request from ACME request
            foreach (var key in @params.Headers.AllKeys)
            {
                request.Headers.Add(key, @params.Headers.Get(key));
            }

            Logger.Debug("Request {method} {path} {token}", @params.Method, url, @params.Headers.ToString(), content);

            var response = _http.SendAsync(request).Result;

            // Get Replay-Nonce from response
            IEnumerable <string> nonceList;

            response.Headers.TryGetValues(HeaderCollection.ReplayNonceHeader, out nonceList);
            Nonce = nonceList?.FirstOrDefault();

            try
            {
                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException)
            {
                string         message   = null;
                string         errorJson = null;
                Protocol.Error error     = null;
                try
                {
                    errorJson = await response.Content.ReadAsStringAsync();

                    try
                    {
                        error = JsonConvert.DeserializeObject <Protocol.Error>(errorJson);

                        message = $"{error.Type}: {error.Detail}";
                        Logger.Debug("ACME Error {@error}", error);
                    }
                    catch
                    {
                        Logger.Error("Cannot parse ACME Error from Client response");
                        Logger.Debug(errorJson);
                    }
                }
                catch (System.Exception e)
                {
                    Logger.Error("Cannot parse content from Client response");
                    Logger.Debug(e);
                }

                AcmeException ex;
                if (string.IsNullOrEmpty(message))
                {
                    message = $"Unexpected response status code [{response.StatusCode}] for [{@params.Method}] request to [{url}]";
                    ex      = new AcmeException(Protocol.ErrorType.ServerInternal, message, response.StatusCode);
                }
                else
                {
                    ex = new AcmeException(error.Type, error.Detail, response.StatusCode);
                }

                Logger.Error(ex);

                throw ex;
            }

            // Copy headers to ACME Response
            var headers = new HeaderCollection();

            foreach (var header in response.Headers)
            {
                headers.Add(header.Key, string.Join(", ", header.Value));
            }

            var acmeResponse = new AcmeResponse
            {
                StatusCode = (int)response.StatusCode,
                Headers    = headers,
                Content    = response.Content == null
                    ? new MediaTypeContent("", new byte[0])
                    : new MediaTypeContent(
                    response.Content.Headers.ContentType?.MediaType ?? "",
                    await response.Content.ReadAsByteArrayAsync()),
            };

            Logger.Debug("Response {@response}", acmeResponse);

            return(acmeResponse);
        }