Ejemplo n.º 1
0
        private CacheRec CheckCacheHeaders(string FullPath, DateTime LastModified, HttpRequest Request)
        {
            string            CacheKey = FullPath.ToLower();
            HttpRequestHeader Header   = Request.Header;
            CacheRec          Rec;
            DateTimeOffset?   Limit;

            lock (this.cacheInfo)
            {
                if (this.cacheInfo.TryGetValue(CacheKey, out Rec))
                {
                    if (Rec.LastModified != LastModified)
                    {
                        this.cacheInfo.Remove(CacheKey);
                        Rec = null;
                    }
                }
            }

            if (Rec == null)
            {
                Rec = new CacheRec()
                {
                    LastModified = LastModified,
                    IsDynamic    = false
                };

                using (FileStream fs = File.OpenRead(FullPath))
                {
                    Rec.ETag = Hashes.ComputeSHA1HashString(fs);
                }

                lock (this.cacheInfo)
                {
                    this.cacheInfo[CacheKey] = Rec;
                }
            }

            if (!Rec.IsDynamic)
            {
                if (Header.IfNoneMatch != null)
                {
                    if (Header.IfNoneMatch.Value == Rec.ETag)
                    {
                        throw new NotModifiedException();
                    }
                }
                else if (Header.IfModifiedSince != null)
                {
                    if ((Limit = Header.IfModifiedSince.Timestamp).HasValue &&
                        LessOrEqual(LastModified, Limit.Value.ToUniversalTime()))
                    {
                        throw new NotModifiedException();
                    }
                }
            }

            return(Rec);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Contains information about an avatar.
 /// </summary>
 /// <param name="BareJid">Bare JID related to the avatar.</param>
 /// <param name="ContentType">Content-Type of the avatar image.</param>
 /// <param name="Binary">Binary encoding of the image.</param>
 /// <param name="Width">Width of avatar, in pixels.</param>
 /// <param name="Height">Height of avatar, in pixels.</param>
 public Avatar(string BareJid, string ContentType, byte[] Binary, int Width, int Height)
 {
     this.bareJid     = BareJid;
     this.contentType = ContentType;
     this.binary      = Binary;
     this.hash        = Hashes.ComputeSHA1HashString(Binary);
     this.width       = Width;
     this.height      = Height;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets sort key for MQTT broker
        /// </summary>
        /// <param name="Host">Host address</param>
        /// <param name="Port">Port number</param>
        /// <param name="Tls">If TLS is used</param>
        /// <param name="UserName">User name</param>
        /// <param name="Password">Password</param>
        /// <returns></returns>
        public static string GetKey(string Host, int Port, bool Tls, string UserName, string Password)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine(Host);
            sb.AppendLine(Port.ToString());
            sb.AppendLine(Tls.ToString());
            sb.AppendLine(UserName);
            sb.AppendLine(Password);

            return(Hashes.ComputeSHA1HashString(Encoding.UTF8.GetBytes(sb.ToString())));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Computes an ETag value for a resource.
        /// </summary>
        /// <param name="fs">Stream containing data of resource.</param>
        /// <returns>ETag value.</returns>
        public string ComputeETag(Stream fs)
        {
            string ETag = Hashes.ComputeSHA1HashString(fs);

            fs.Position = 0;

            string Salt = this.ETagSalt;

            if (!string.IsNullOrEmpty(Salt))
            {
                ETag = Hashes.ComputeSHA1HashString(Encoding.UTF8.GetBytes(ETag + Salt));
            }

            return(ETag);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Executes the GET method on the resource.
        /// </summary>
        /// <param name="Request">HTTP Request</param>
        /// <param name="Response">HTTP Response</param>
        /// <exception cref="HttpException">If an error occurred when processing the method.</exception>
        public void GET(HttpRequest Request, HttpResponse Response)
        {
            using (Stream f = this.assembly.GetManifestResourceStream(this.embeddedResourceName))
            {
                if (f == null)
                {
                    throw new NotFoundException();
                }

                if (this.etag == null)
                {
                    this.etag  = Hashes.ComputeSHA1HashString(f);
                    f.Position = 0;
                }

                if (Request.Header.IfNoneMatch != null && Request.Header.IfNoneMatch.Value == this.etag)
                {
                    throw new NotModifiedException();
                }

                Response.SetHeader("ETag", "\"" + this.etag + "\"");

                long   l      = f.Length;
                long   Pos    = 0;
                int    Size   = (int)Math.Min(BufferSize, l);
                byte[] Buffer = new byte[Size];
                int    i;

                Response.ContentType   = this.contentType;
                Response.ContentLength = l;

                if (!Response.OnlyHeader)
                {
                    while (Pos < l)
                    {
                        i = f.Read(Buffer, 0, Size);
                        if (i <= 0)
                        {
                            throw new Exception("Unexpected end of stream.");
                        }

                        Response.Write(Buffer, 0, i);
                        Pos += i;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void GenerateKeysLocked()
        {
            int n = XmppClient.GetRandomValue(8, 24);

            byte[] Bin = XmppClient.GetRandomBytes(20);
            string Key = Hashes.ComputeSHA1HashString(Bin);

            this.keys.Clear();
            this.keys.AddFirst(Key);

            while (--n > 0)
            {
                Bin = Encoding.ASCII.GetBytes(Key);
                Key = Hashes.ComputeSHA1HashString(Bin);
                this.keys.AddFirst(Key);
            }
        }