Пример #1
0
 public WebPubSubRequestValueProvider(WebPubSubRequest request, Type type)
 {
     _request = request;
     Type     = type;
 }
        protected async override Task <IValueProvider> BuildAsync(WebPubSubRequestAttribute attrResolved, IReadOnlyDictionary <string, object> bindingData)
        {
            if (bindingData == null)
            {
                throw new ArgumentNullException(nameof(bindingData));
            }
            bindingData.TryGetValue(HttpRequestName, out var httpRequest);
            var request = httpRequest as HttpRequest;

            var httpContext = request?.HttpContext;

            if (httpContext == null)
            {
                return(new WebPubSubRequestValueProvider(new WebPubSubRequest(null, new InvalidRequest(HttpStatusCode.BadRequest), HttpStatusCode.BadRequest), _userType));
            }

            // Build abuse response
            if (Utilities.RespondToServiceAbuseCheck(httpContext.Request, _options.AllowedHosts, out var abuseResponse))
            {
                var abuseRequest = new WebPubSubRequest(null, new ValidationRequest(abuseResponse.StatusCode == HttpStatusCode.OK), abuseResponse);
                return(new WebPubSubRequestValueProvider(abuseRequest, _userType));
            }

            // Build service request context
            if (!TryParseRequest(request, out var connectionContext))
            {
                // Not valid WebPubSubRequest
                return(new WebPubSubRequestValueProvider(new WebPubSubRequest(connectionContext, new InvalidRequest(HttpStatusCode.BadRequest, Constants.ErrorMessages.NotValidWebPubSubRequest), HttpStatusCode.BadRequest), _userType));
            }

            // Signature check
            // TODO: make the check more accurate for current function instead from global settings.
            if (!Utilities.ValidateSignature(connectionContext.ConnectionId, connectionContext.Signature, _options.AccessKeys))
            {
                return(new WebPubSubRequestValueProvider(new WebPubSubRequest(connectionContext, new InvalidRequest(HttpStatusCode.Unauthorized, Constants.ErrorMessages.SignatureValidationFailed), HttpStatusCode.Unauthorized), _userType));
            }

            WebPubSubRequest wpsRequest;
            var requestType = Utilities.GetRequestType(connectionContext.EventType, connectionContext.EventName);

            switch (requestType)
            {
            case RequestType.Connect:
            {
                var content = await ReadString(request.Body).ConfigureAwait(false);

                var eventRequest = JsonConvert.DeserializeObject <ConnectEventRequest>(content);
                wpsRequest = new WebPubSubRequest(connectionContext, eventRequest);
            }
            break;

            case RequestType.Connected:
            {
                wpsRequest = new WebPubSubRequest(connectionContext, new ConnectedEventRequest());
            }
            break;

            case RequestType.Disconnected:
            {
                var content = await ReadString(request.Body).ConfigureAwait(false);

                var eventRequest = JsonConvert.DeserializeObject <DisconnectedEventRequest>(content);
                wpsRequest = new WebPubSubRequest(connectionContext, eventRequest);
            }
            break;

            case RequestType.User:
            {
                var contentType = MediaTypeHeaderValue.Parse(request.ContentType);
                if (!Utilities.ValidateMediaType(contentType.MediaType, out var dataType))
                {
                    var invalidRequest = new InvalidRequest(HttpStatusCode.BadRequest, $"{Constants.ErrorMessages.NotSupportedDataType}{request.ContentType}");
                    return(new WebPubSubRequestValueProvider(new WebPubSubRequest(connectionContext, invalidRequest, HttpStatusCode.BadRequest), _userType));
                }
                var payload      = ReadBytes(request.Body);
                var eventRequest = new MessageEventRequest(BinaryData.FromBytes(payload), dataType);
                wpsRequest = new WebPubSubRequest(connectionContext, eventRequest);
            }
            break;

            default:
                wpsRequest = new WebPubSubRequest(connectionContext, new InvalidRequest(HttpStatusCode.NotFound, "Unknown request"));
                break;
            }

            return(new WebPubSubRequestValueProvider(wpsRequest, _userType));
        }