Exemple #1
0
 public void FillValues(SemanticsSessionScope semanticsSessionScope, PURLConnection purlConnection, MetaMetadataCompositeField metaMetadata, DocumentClosure documentClosure)
 {
     SemanticsSessionScope = semanticsSessionScope;
     PURLConnection        = purlConnection;
     MetaMetadata          = metaMetadata;
     DocumentClosure       = documentClosure;
 }
Exemple #2
0
        public async Task <Document> PerformDownload()
        {
            // change status
            lock (downloadStatusLock)
            {
                if (!(DownloadStatus == DownloadStatus.QUEUED || DownloadStatus == DownloadStatus.UNPROCESSED))
                {
                    return(Document); // if not queued or unprocessed, it must either hasn't enter the queue or has already been processed.
                }
                DownloadStatus = DownloadStatus.CONNECTING;
            }

            Document.SemanticsSessionScope = SemanticsSessionScope;

            ParsedUri location = Document == null ? null : Document.Location == null ? null : Document.Location.Value;

            Debug.WriteLine("Calling connect from thread: " + Task.CurrentId);
            DateTime time = DateTime.Now;

            Connect();
            Debug.WriteLine("Completed connect: " + DateTime.Now.Subtract(time).TotalMilliseconds);

            if (PURLConnection != null && PURLConnection.Good) // && DocumentParser != null)
            {
                // parsing
                lock (downloadStatusLock)
                {
                    DownloadStatus = DownloadStatus.PARSING;
                }
                // TODO display message from DocumentParser
                MetaMetadata mmd = Document.MetaMetadata as MetaMetadata;
                // TODO before semantic actions

                //This call returns void
                //Parser is responsible for setting result in the documentClosure.TaskCompletionSource
                DocumentParser.Parse();

                await GetMetadata();

                // TODO after semantic actions

                lock (downloadStatusLock)
                {
                    DownloadStatus = DownloadStatus.DOWNLOAD_DONE;
                }
            }
            else
            {
                Debug.WriteLine("ERROR: cannot perform downloading on " + location);
            }
            // Document.DownloadDone = true;
            Document.DownloadAndParseDone();

            if (PURLConnection != null)
            {
                PURLConnection.Recycle();
            }
            PURLConnection = null;

            return(Document);
        }
Exemple #3
0
        public void Connect()
        {
            DocumentClosureConnectionHelper documentClosureConnectionHelper = new DocumentClosureConnectionHelper(SemanticsSessionScope, Document, this);

            Debug.WriteLine("Connect running from thread: " + Task.CurrentId);
            MetaMetadataCompositeField metaMetadata = Document.MetaMetadata;

            // then try to create a connection using the PURL
            string    userAgentString = metaMetadata.UserAgentString;
            ParsedUri originalPURL    = Document.Location.Value;

            PURLConnection = new PURLConnection(originalPURL);
            if (originalPURL.IsFile)
            {
                // TODO handle local files here!
                var file = PURLConnection.File;
                if (SemanticsPlatformSpecifics.Get().FileIsADictionary(file))
                {
                    // TODO FileDirectoryParser
                    // DocumentParser = DocumentParser.GetDocumentParser(FILE_DIRECTORY_PARSER);
                }
                else
                {
                    PURLConnection.FileConnect();
                    // we already have the correct meta-metadata, having used suffix to construct, or having gotten it from a restore.
                }
            }
            else
            {
                PURLConnection.NetworkConnect(documentClosureConnectionHelper, userAgentString); // HERE!
                if (PURLConnection.Good)
                {
                    Document document = this.Document; // may have changed during redirect processing
                    metaMetadata = document.MetaMetadata;

                    // check for a parser that was discovered while processing a re-direct

                    // if we made PURL connection but could not find parser using container
                    if ((PURLConnection != null) && !originalPURL.IsFile)
                    {
                        string cacheValue = PURLConnection.Response.Headers == null
                                                ? null
                                                : PURLConnection.Response.Headers["X-Cache"];
                        bool cacheHit = cacheValue != null && cacheValue.Contains("HIT");
                        if (metaMetadata.IsGenericMetadata)
                        {
                            // see if we can find more specifc meta-metadata using mimeType
                            MetaMetadataRepository repository = SemanticsSessionScope.MetaMetadataRepository;
                            string       mimeType             = PURLConnection.MimeType;
                            MetaMetadata mimeMmd = mimeType == null ? null : repository.GetMMByMime(mimeType);
                            if (mimeMmd != null && !mimeMmd.Equals(metaMetadata))
                            {
                                // new meta-metadata!
                                if (!mimeMmd.MetadataClass.GetTypeInfo().IsAssignableFrom(document.GetType().GetTypeInfo()))
                                //if (!mimeMmd.MetadataClass.IsAssignableFrom(document.GetType()))
                                {
                                    // more specifc so we need new metadata!
                                    document = (Document)(mimeMmd).ConstructMetadata();
                                    // set temporary on stack
                                    ChangeDocument(document);
                                }
                                metaMetadata = mimeMmd;
                            }
                        }
                    }
                }
            }

            if (DocumentParser == null)
            {
                DocumentParser = DocumentParser.GetDocumentParser(metaMetadata.Parser);
            }
            if (DocumentParser != null)
            {
                DocumentParser.FillValues(SemanticsSessionScope, PURLConnection, metaMetadata, this);
            }
            else
            {
                Debug.WriteLine("WARNING: no parser found: " + metaMetadata);
            }
//            else if (!DocumentParser.isRegisteredNoParser(PURLConnection.getPurl()))
//            {
//                warning("No DocumentParser found: " + metaMetadata);
//            }
        }
 public void Connect(IConnectionHelper connectionHelper, string userAgent, int connectionTimeout, int readTimeout, PURLConnection purlConnection)
 {
     if (purlConnection.PURL.IsFile)
     {
         FileAttributes attributes = ((FileInfo)purlConnection.File).Attributes;
         if (attributes.HasFlag(FileAttributes.Directory))
         {
             connectionHelper.HandleFileDirectory(purlConnection.File);
         }
         else
         {
             string suffix = purlConnection.PURL.Suffix;
             if (suffix != null)
             {
                 if (connectionHelper.ParseFilesWithSuffix(suffix))
                 {
                     try
                     {
                         purlConnection.FileConnect();
                     }
                     catch (FileNotFoundException e)
                     {
                         Debug.WriteLine("ERROR: Can't open because FileNotFoundException");
                     }
                 }
             }
         }
     }
     else
     {
         purlConnection.NetworkConnectAndCatch(connectionHelper, userAgent, connectionTimeout, readTimeout);
     }
 }
        public void NetworkConnect(IConnectionHelperJustRemote connectionHelper, string userAgent, PURLConnection purlConnection, int connectionTimeout = ParsedUri.CONNECT_TIMEOUT, int readTimeout = ParsedUri.READ_TIMEOUT)
        {
            Uri url = purlConnection.PURL;

            purlConnection.Request = WebRequest.CreateDefault(url) as HttpWebRequest;
            if (purlConnection.Request != null)
            {
                purlConnection.Request.UserAgent        = userAgent;
                purlConnection.Request.Timeout          = connectionTimeout;
                purlConnection.Request.ReadWriteTimeout = readTimeout;

                try
                {
                    purlConnection.Response = (HttpWebResponse)purlConnection.Request.GetResponse();
                }
                catch (WebException e)
                {
                    Debug.WriteLine("Web Exception ::" + e.Message);
                }
                if (purlConnection.Response != null)
                {
                    // TODO check charset (using mime type) and display error message if charset not supported.

                    Uri responseUrl = purlConnection.Response.ResponseUri;
                    if (responseUrl != url) // follow redirects!
                    {
                        string requestPath  = url.AbsolutePath;
                        string responsePath = responseUrl.AbsolutePath;
                        if (requestPath.IndexOf("http://") < 0 && responsePath.IndexOf("http://") < 0)
                        {
                            if (connectionHelper.ProcessRedirect(responseUrl))
                            {
                                purlConnection.Stream = purlConnection.Response.GetResponseStream();
                            }
                            purlConnection.Good = true;
                        }
                        else
                        {
                            Debug.WriteLine("WEIRD: skipping double stuffed URL: " + responseUrl);
                        }
                    }
                    else
                    {
                        purlConnection.Stream = purlConnection.Response.GetResponseStream();
                        purlConnection.Good   = true;
                    }
                }
                else
                {
                    Debug.WriteLine("ERROR: failure to get response from " + url);
                }
            }
            else
            {
                Debug.WriteLine("ERROR: cannot create a connection to " + url);
            }
        }