예제 #1
0
        public IContentResponse Get(string method, string rawUrl, NameValueCollection headers, Stream inputStream)
        {
            long length;

            using (new MutexLock(2000, _lockName))
            {
                using (Stream output = new FileStream(_filename, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                    length = output.Length;
            }
            using (Stream rawinput = new FileStream(_filename, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
            {
                long  position = 0;
                Match m        = Regex.Match(headers["If-None-Match"] ?? String.Empty, "^\"position:(?<position>\\d+)\"$");
                if (m.Success)
                {
                    position = Math.Min(Math.Max(0, long.Parse(m.Groups["position"].Value)), length);
                }

                using (Stream input = new ClampedStream(rawinput, position, Math.Max(0, length - position)))
                {
                    PingbackRecord rec = PingbackRecord.ParseFrom(input);
                    return(new DynamicResponse("application/vnd.google.protobuf", rec.ToByteArray())
                    {
                        ETag = String.Format("position:{0}", length),
                        LastModified = rec.RecordsCount > 0
                                                      ? rec.RecordsList[rec.RecordsCount - 1].When
                                                      : DateTime.Now,
                    });
                }
            }
        }
예제 #2
0
        public void Playback(Uri sourceSite, string stateFile)
        {
            Uri pingbackApi;

            if (!TryGetPingbackFromHeader(out pingbackApi))
            {
                throw new ArgumentException("The site does not have an X-Pingback header.");
            }

            if (!Directory.Exists(Path.GetDirectoryName(stateFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(stateFile));
            }

            using (Stream stream = new FileStream(stateFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read))
            {
                stream.Seek(0, SeekOrigin.End);
                HttpRequestUtil http = new HttpRequestUtil(sourceSite);
                http.RequestHeaders["If-None-Match"] = String.Format("\"position:{0}\"", stream.Position);

                if (http.Get(sourceSite.PathAndQuery) == HttpStatusCode.NotModified)
                {
                    return;
                }

                if (http.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException(String.Format("Unexpected http result: {0}/{1}", (int)http.StatusCode, http.StatusCode));
                }

                PingbackRecord response = PingbackRecord.ParseFrom(http.Content);
                foreach (PingbackInfo ping in response.RecordsList)
                {
                    try
                    {
                        int    errorCode;
                        string serverMessage;
                        Uri    source = new Uri(ping.SourceUri, UriKind.Absolute);
                        Uri    target = new Uri(_targetLink, ping.TargetUri);
                        if (!SendPingback(source, target, pingbackApi, LogError, out errorCode, out serverMessage))
                        {
                            LogError(String.Format("Failed to register pingback from {0}, {1}", ping.SourceUri, serverMessage));
                        }
                    }
                    catch (Exception e)
                    {
                        LogError(String.Format("Failed to register pingback from {0}, {1}", ping.SourceUri, e.Message));
                    }
                }

                response.WriteTo(stream);
            }
        }