Exemplo n.º 1
0
        public void opImplicit_EntityTag_MD5Hash()
        {
            EntityTag expected = _emptyEtag;
            EntityTag actual   = MD5Hash.Compute(string.Empty);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 2
0
        public void ComputeTest()
        {
            var h1 = MD5Hash.Parse("0B1DBE29DBED4AFC1196CC430FD370C1");
            var h2 = MD5Hash.Compute("http://en.wikipedia.org/robots.txt");

            Assert.IsTrue(h1 == h2, h2.ToString());
        }
Exemplo n.º 3
0
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string loginString      = textBoxLogin.Text.Trim();
            string passwordString   = textBoxPassword.Text.Trim();
            string nameString       = textBoxName.Text.Trim();
            string lastNameString   = textBoxLastName.Text.Trim();
            string patronimicString = textBoxPatronimic.Text.Trim();
            string specialityString = textBoxSpeciality.Text.Trim();
            string groupString      = textBoxGroup.Text.Trim();
            int    accessLvl        = checkBoxAccessLvl.Checked ? 0 : 1;

            if (labelTypeForm.Text.Equals("SignUp"))
            {
                int status = Controller.Insert.InsertUser(loginString, MD5Hash.Compute(passwordString), nameString, lastNameString, patronimicString,
                                                          specialityString, groupString, accessLvl);
                if (status == 0)
                {
                    MessageBox.Show(@"Пользователь успешно добавлен", @"Sucsesful",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else if (status == -1)
                {
                    MessageBox.Show(@"Пользователь с таким именем пользователя уже существует", @"Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (status == 1)
                {
                    MessageBox.Show(@"Не удалось добавить нового пользователя", @"Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
            else if (labelTypeForm.Text.Contains("EditUserInfo"))
            {
                string id     = labelTypeForm.Text.Substring(15);
                int    status = Controller.Update.UpdateUser(id, loginString, MD5Hash.Compute(passwordString), nameString, lastNameString, patronimicString,
                                                             specialityString, groupString, accessLvl);
                if (status == 0)
                {
                    MessageBox.Show(@"Информация о пользователе успешно обновлена", @"Sucsesful",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                else if (status == -1)
                {
                    MessageBox.Show(@"Не удалось сохранить новую информацию о пользователе", @"Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Exemplo n.º 4
0
        public async Task VisitedSpeedTest()
        {
            var l = new List <Uri>();

            l.Add(new Uri("http://some.example.com"));
            l.Add(new Uri("http://www.pandora.tv/theme/main/8/74"));
            l.Add(new Uri("http://drupal.org/"));
            l.Add(new Uri("https://www.archaeological.org/about/eupdate"));
            l.Add(new Uri("https://blahblabhlahblahb"));

            using (var db = new Database("Server=127.0.0.1;Port=5433;User Id=postgres;Password=postgres;Database=fetcho;Enlist=false"))
            {
                var hashes = l.Select(x => MD5Hash.Compute(x)).ToList();

                DateTime start = DateTime.UtcNow;
                for (int i = 0; i < 100000; i++)
                {
                    Assert.IsTrue((await db.NeedsVisiting(hashes)).Any());
                }
                Assert.IsTrue(false, (DateTime.UtcNow - start).ToString()); // set n5 (any) set n5 (in): 23.88 33.32 set n1: 17.13 single: 21.73 22.59
            }
        }
Exemplo n.º 5
0
        public async Task <HttpResponseMessage> PostWebResourceDataCache([FromBody] Stream data)
        {
            MD5Hash hash = null;

            try
            {
                using (var ms = new MemoryStream())
                {
                    data.CopyTo(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    hash = MD5Hash.Compute(ms);
                    ms.Seek(0, SeekOrigin.Begin);

                    using (var db = new Database())
                        await db.AddWebResourceDataCache(hash, ms.GetBuffer());
                }

                return(CreateCreatedResponse((object)null));
            }
            catch (Exception ex)
            {
                return(CreateExceptionResponse(ex));
            }
        }
Exemplo n.º 6
0
        public WorkspaceResult Build(Stream stream, string requestString, string responseHeaders, out string evalText)
        {
            WorkspaceResult result = new WorkspaceResult();

            result.SourceServerId = FetchoConfiguration.Current.CurrentServerNode.ServerId;

            if (!stream.CanSeek)
            {
                throw new FetchoException("WorkspaceResultBuilder needs a seekable stream");
            }

            ProcessHeaders(result, requestString, responseHeaders);
            result.DataHash = MD5Hash.Compute(stream).ToString();
            result.PageSize = stream.Length;
            stream.Seek(0, SeekOrigin.Begin);

            ContentType contentType = GetContentType(result);
            int         titleness   = 4;

            if (contentType != null)
            {
                if (contentType.SubType.Contains("html"))
                {
                    using (var reader = new HtmlReader(stream))
                    {
                        while (!reader.EOF)
                        {
                            var node = reader.NextNode();
                            if (node.Type == HtmlTokenType.Text)
                            {
                                evaluationText.Append(node.Value);
                                evaluationText.Append(' ');
                            }

                            if (node.Value == "script")
                            {
                                ReadToEndTag(reader, "script");
                            }
                            else if (node.Value == "style")
                            {
                                ReadToEndTag(reader, "style");
                            }
                            else if (node.Value == "title" && titleness > 1 && !result.PropertyCache.ContainsKey("title"))
                            {
                                string title = ReadTitle(reader, "title");
                                result.PropertyCache.Add("title", title);
                                titleness = 1;
                            }
                            else if (node.Value == "h1" && titleness > 2 && !result.PropertyCache.ContainsKey("title"))
                            {
                                string title = ReadTitle(reader, "h1");
                                result.PropertyCache.Add("title", title);
                                titleness = 2;
                            }
                            else if (node.Value == "h2" && titleness > 3 && !result.PropertyCache.ContainsKey("title"))
                            {
                                string title = ReadTitle(reader, "h2");
                                result.PropertyCache.Add("title", title);
                                titleness = 3;
                            }
                            else if (node.Value == "meta")
                            {
                                ProcessMetaTag(reader, result);
                            }
                        }
                    }
                }
                else if (contentType.IsTextType || ContentType.IsJavascriptContentType(contentType))
                {
                    // leave the stream open so other tasks can reset it and use it
                    using (var reader = new StreamReader(stream, Encoding.Default, true, 1024, true))
                    {
                        evaluationText.Append(reader.ReadToEnd());
                    }
                }
            }

            result.UriHash     = MD5Hash.Compute(result.RequestProperties.SafeGet("uri") ?? "").ToString();
            result.RefererUri  = result.RequestProperties.SafeGet("referer");
            result.Uri         = result.RequestProperties.SafeGet("uri") ?? "";
            result.Title       = result.PropertyCache.SafeGet("title")?.ToString();
            result.Description = result.PropertyCache.SafeGet("description")?.ToString();
            result.Created     = DateTime.UtcNow;
            result.Updated     = DateTime.UtcNow;

            evalText = evaluationText.ToString();

            return(result);
        }
        IRecord <T> IRepository <T> .Insert(IRecord <T> record)
        {
            if (null == record)
            {
                throw new ArgumentNullException("record");
            }

            if (null == record.Cacheability)
            {
                throw new RepositoryException();
            }

            if (null == record.Expiration)
            {
                throw new RepositoryException();
            }

            if (null == record.Status)
            {
                throw new RepositoryException();
            }

            if (null == record.Urn)
            {
                throw new RepositoryException();
            }

            if (record.Key.HasValue)
            {
                throw new RepositoryException();
            }

            if (Repository.Exists(record.Urn))
            {
                throw new RepositoryException();
            }

            var date = DateTime.UtcNow.ToXmlString();
            var key  = AlphaDecimal.Random();
            var node = Xml.CreateElement("object");

            ////record.Urn = new AbsoluteUri(record.Urn.ToString().Replace("{token}", token));
            var attribute = Xml.CreateAttribute("cacheability");

            attribute.Value = record.Cacheability;
            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("created");
            attribute.Value = date;
            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("etag");
            attribute.Value = (EntityTag)MD5Hash.Compute(record.ToEntity());
            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("expiration");
            attribute.Value = record.Expiration;
            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("key");
            attribute.Value = key;
            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("modified");
            attribute.Value = date;
            node.Attributes.Append(attribute);

            attribute = Xml.CreateAttribute("status");
            if (record.Status != null)
            {
                attribute.Value = XmlConvert.ToString(record.Status.Value);
            }

            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("urn");
            attribute.Value = record.Urn;
            node.Attributes.Append(attribute);

            attribute       = Xml.CreateAttribute("type");
            attribute.Value = "{0}, {1}".FormatWith(record.GetType().FullName, record.GetType().Assembly.GetName().Name);
            node.Attributes.Append(attribute);

            node.InnerXml = record.Value.XmlSerialize().CreateNavigator().OuterXml;

            if (Xml.DocumentElement != null)
            {
                Xml.DocumentElement.AppendChild(node);
            }

            return(Repository.Select(key));
        }
        IRecord <T> IRepository <T> .Update(IRecord <T> record)
        {
            if (null == record)
            {
                throw new ArgumentNullException("record");
            }

            if (null == record.Cacheability)
            {
                throw new RepositoryException();
            }

            if (null == record.Expiration)
            {
                throw new RepositoryException();
            }

            if (null == record.Status)
            {
                throw new RepositoryException();
            }

            if (null == record.Urn)
            {
                throw new RepositoryException();
            }

            if (!record.Key.HasValue)
            {
                throw new RepositoryException();
            }

            var keySelection = Repository.Select(record.Key.Value);

            if (null == keySelection)
            {
                throw new RepositoryException();
            }

            var urnSelection = Repository.Select(record.Urn);

            if (null != urnSelection &&
                keySelection.Key != urnSelection.Key)
            {
                throw new RepositoryException();
            }

            var node = Select(record.Key.Value);

            if (null == node)
            {
                return(null);
            }

            if (node.Attributes != null)
            {
                node.Attributes["cacheability"].Value = record.Cacheability;
                node.Attributes["etag"].Value         = (EntityTag)MD5Hash.Compute(record.ToEntity());
                node.Attributes["expiration"].Value   = record.Expiration;
                node.Attributes["modified"].Value     = DateTime.UtcNow.ToXmlString();
                if (record.Status != null)
                {
                    node.Attributes["status"].Value = XmlConvert.ToString(record.Status.Value);
                }

                node.Attributes["type"].Value = "{0}, {1}".FormatWith(record.GetType().FullName, record.GetType().Assembly.GetName().Name);
                node.Attributes["urn"].Value  = record.Urn;
            }

            node.InnerXml = record.Value.XmlSerialize().CreateNavigator().OuterXml;

            return(record.Key != null
                       ? Repository.Select(record.Key.Value)
                       : null);
        }