Exemplo n.º 1
0
        public async Task <PingbackServiceResponse> ProcessReceivedPingback(HttpContext context)
        {
            var response = await _pingbackReceiver.ValidatePingRequest(context);

            if (response == PingbackServiceResponse.ValidPingRequest)
            {
                Logger.LogInformation($"Pingback Attempt from {context.Connection.RemoteIpAddress} is valid");

                var pingRequest = await _pingbackReceiver.GetPingRequest();

                var postResponse = _postService.GetPostIdTitle(pingRequest.TargetUrl);
                if (postResponse.IsSuccess)
                {
                    var post = postResponse.Item;

                    _pingbackReceiver.OnPingSuccess += async(sender, args) => await SavePingbackRecord(
                        args.Domain,
                        args.PingRequest.SourceUrl,
                        args.PingRequest.SourceDocumentInfo.Title,
                        post.Id,
                        post.Title,
                        context.Connection.RemoteIpAddress.ToString());

                    return(_pingbackReceiver.ProcessReceivedPingback(
                               pingRequest,
                               () => true,
                               () => HasAlreadyBeenPinged(post.Id, pingRequest.SourceUrl, pingRequest.TargetUrl)));
                }

                Logger.LogError(postResponse.Exception, postResponse.Message);
                return(PingbackServiceResponse.GenericError);
            }
            return(response);
        }
Exemplo n.º 2
0
        public async Task <PingbackServiceResponse> ProcessReceivedPingbackAsync(HttpContext context)
        {
            var response = await _pingbackReceiver.ValidatePingRequest(context);

            if (response == PingbackValidationResult.ValidPingRequest)
            {
                Logger.LogInformation($"Pingback attempt from '{context.Connection.RemoteIpAddress}' is valid");

                var pingRequest = await _pingbackReceiver.GetPingRequest();

                var postResponse = TryGetPostIdTitle(pingRequest.TargetUrl, out var idTitleTuple);
                if (postResponse)
                {
                    Logger.LogInformation($"Post '{idTitleTuple.Id}:{idTitleTuple.Title}' is found for ping.");

                    _pingbackReceiver.OnPingSuccess += async(sender, args) => await SavePingbackRecordAsync(
                        new PingbackRequest
                    {
                        Domain          = args.Domain,
                        SourceUrl       = args.PingRequest.SourceUrl,
                        SourceTitle     = args.PingRequest.SourceDocumentInfo.Title,
                        TargetPostId    = idTitleTuple.Id,
                        TargetPostTitle = idTitleTuple.Title,
                        SourceIp        = context.Connection.RemoteIpAddress.ToString()
                    });

                    return(_pingbackReceiver.ProcessReceivedPingback(
                               pingRequest,
                               () => true,
                               () => HasAlreadyBeenPinged(idTitleTuple.Id, pingRequest.SourceUrl, pingRequest.TargetUrl)));
                }

                Logger.LogError($"Can not get post id and title for url '{pingRequest.TargetUrl}'");
                return(PingbackServiceResponse.GenericError);
            }

            return(PingbackServiceResponse.InvalidPingRequest);
        }