예제 #1
0
        public static bool NotebookList(int notebookid, string notebookpass, ref List <int> notelist)
        {
            var input = new NotebookListInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
            };
            var json = JsonConvert.SerializeObject(input);


            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.NotebookList, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result    = JsonConvert.DeserializeObject <NotebookListOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                notelist = result.NoteList;
            }
            return(issuccess);
        }
예제 #2
0
        public static bool  UserNotebook(string username, string userpass, ref List <int> notebooklist)
        {
            var input = new UserNotebookInput()
            {
                UserName = username,
                UserPass = userpass,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.UserNotebook, json);
            }
            catch (System.AggregateException)
            {
                return(false);
            }

            var result    = JsonConvert.DeserializeObject <UserNotebookOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                notebooklist = result.NotebookList;
            }
            return(issuccess);
        }
예제 #3
0
        public static bool UserUpdate(string username, string userpass, string newname, string newpass)
        {
            var input = new UserUpdateInput()
            {
                UserName = username,
                UserPass = userpass,
                NewName  = newname,
                NewPass  = newpass,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.UserUpdate, json);
            }
            catch (System.AggregateException)
            {
                return(false);
            }

            var result = JsonConvert.DeserializeObject <UserUpdateOutput>(output);

            return(ResultStatus.IsSuccess(result.ResultStatus));
        }
예제 #4
0
        public static bool NotebookGet(int notebookid, ref Tuple <string, int> info)
        {
            var input = new NotebookGetInput()
            {
                NotebookId = notebookid,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.NotebookGet, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result    = JsonConvert.DeserializeObject <NotebookGetOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                info = Tuple.Create(result.NotebookName, result.NoteSum);
            }
            return(issuccess);
        }
예제 #5
0
        public static bool NoteCreate(int notebookid, string notebookpass, string notename, int type, string notedate, ref int noteid)
        {
            var input = new NoteCreateInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
                NoteName     = notename,
                NoteType     = type,
                NoteData     = notedate,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.NoteCreate, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result    = JsonConvert.DeserializeObject <NoteCreateOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                noteid = result.NoteId;
            }
            return(issuccess);
        }
예제 #6
0
        public async Task ReadHeaderCount()
        {
            for (int i = 2; i < 300; i++)
            {
                for (int j = 30; j < 1000; j++)
                {
                    byte[] bytes = CreateCountRequest(i, j);

                    byte[] protocol = new byte[8];
                    Array.Copy(bytes, 0, protocol, 0, protocol.Length);

                    HttpReader reader = new HttpReader(new HttpOptions());
                    reader.HandshakeResult = new ProtocolHandshakeResult
                    {
                        ReadAfter      = true,
                        PreviouslyRead = protocol
                    };

                    MemoryStream ms = new MemoryStream();
                    ms.Write(bytes, 8, bytes.Length - 8);

                    ms.Position = 0;
                    HttpMessage message = await reader.Read(ms);

                    Assert.Equal(i, message.Request.Headers.Count - 1); // - 1 for "Content-Length" header
                    Assert.Equal(j, message.Request.ContentStream.Length);
                    Assert.Equal(message.Request.ContentLength, message.Request.ContentStream.Length);

                    string str = Encoding.UTF8.GetString(message.Request.ContentStream.ToArray());
                    Assert.StartsWith("Form", str);
                    Assert.EndsWith("b", str);
                }
            }
        }
예제 #7
0
        private void OnLinkTag(HTMLParser instance, string tag)
        {
            HashMap attrMap = instance.ParseAttributes(tag);

            if ((string)attrMap ["rel"] == "alternate" &&
                ((string)attrMap ["type"] == "application/rss+xml" || (string)attrMap ["type"] == "application/atom+xml"))
            {
                string href = (string)attrMap ["href"];
                if (href != null)
                {
                    string url;
                    try
                    {
                        url = new Uri(_baseUri, href).ToString();
                    }
                    catch (UriFormatException)
                    {
                        return;
                    }
                    if (!HttpReader.IsSupportedProtocol(url))
                    {
                        return;
                    }
                    _candidateURLs.Push(10, url);
                    _candidateURLSet.Add(url);
                    _candidateHintTexts [url] = attrMap ["title"];
                }
            }
        }
예제 #8
0
        public static bool NoteGet(int notebookid, string notebookpass, int noteid, ref Tuple <int, string, string> note)
        {
            var input = new NoteGetInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
                NoteId       = noteid,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.NoteGet, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result    = JsonConvert.DeserializeObject <NoteGetOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                note = Tuple.Create(result.Result.type, result.Result.name, result.Result.data);
            }
            return(issuccess);
        }
예제 #9
0
        public static bool NoteMove(int fromid, string frompass, int toid, string topass, int noteid)
        {
            var input = new NoteMoveInput()
            {
                FromId   = fromid,
                FromPass = frompass,
                ToId     = toid,
                ToPass   = topass,
                NoteId   = noteid,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.NoteMove, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result = JsonConvert.DeserializeObject <NoteMoveOutput>(output);

            return(ResultStatus.IsSuccess(result.ResultStatus));
        }
예제 #10
0
        public static bool UserCreate(string username, string pass, ref int userid)
        {
            var input = new UserCreateInput()
            {
                UserName = username,
                UserPass = pass,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.UserCreate, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result    = JsonConvert.DeserializeObject <UserCreateOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                userid = result.UserId;
            }
            return(issuccess);
        }
예제 #11
0
        public static bool NotebookUpdate(int notebookid, string notebookpass, string newname, string newpass)
        {
            var input = new NotebookUpdateInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
                NewName      = newname,
                NewPass      = newpass,
            };
            var json = JsonConvert.SerializeObject(input);

            string output = null;

            try
            {
                output = HttpReader.HttpPost(ApiDef.NotebookUpdate, json);
            }
            catch (AggregateException)
            {
                return(false);
            }

            var result = JsonConvert.DeserializeObject <NotebookUpdateOutput>(output);

            return(ResultStatus.IsSuccess(result.ResultStatus));
        }
예제 #12
0
        private void OnDownloadClick()
        {
            _feedAddressPane.SetExistingFeedLink(null);
            if (File.Exists(_feedAddressPane.FeedUrl))
            {
                _feedAddressPane.FeedUrl = "file://" + _feedAddressPane.FeedUrl;
            }
            else
            if (_feedAddressPane.FeedUrl.IndexOf("://") < 0)
            {
                _feedAddressPane.FeedUrl = "http://" + _feedAddressPane.FeedUrl;
            }
            else
            {
                string url = _feedAddressPane.FeedUrl.ToLower();
                if (!HttpReader.IsSupportedProtocol(url))
                {
                    _feedAddressPane.ErrorMessage = "Unknown URL schema. Only http:, https: and file: are supported.";
                    return;
                }
            }

            try
            {
                new Uri(_feedAddressPane.FeedUrl);
            }
            catch (Exception ex)
            {
                _feedAddressPane.ErrorMessage = ex.Message;
                return;
            }

            IResource existingFeed = RSSPlugin.GetExistingFeed(_feedAddressPane.FeedUrl);

            if (existingFeed != null)
            {
                _feedAddressPane.ErrorMessage = "You are already subscribed to that feed.";
                _feedAddressPane.SetExistingFeedLink(existingFeed);
                return;
            }

            _progressLabel.Text = "Downloading...";
            _nextButton.Enabled = false;
            _feedAddressPane.ControlsEnabled = false;

            if (_newFeedProxy != null)
            {
                _newFeedProxy.DeleteAsync();
                _newFeedProxy = null;
            }

            _newFeedProxy = CreateFeedProxy(_feedAddressPane.FeedUrl, null);

            _rssUnitOfWork = new RSSUnitOfWork(_newFeedProxy.Resource, false, true);
            _rssUnitOfWork.DownloadProgress += OnDownloadProgress;
            _rssUnitOfWork.ParseDone        += OnParseDone;
            Core.NetworkAP.QueueJob(JobPriority.Immediate, _rssUnitOfWork);
        }
예제 #13
0
 protected void Init(string url, ReadyDelegate readyCallback, bool initReader)
 {
     Guard.NullArgument(url, "url");
     _url           = url;
     _readyCallback = readyCallback;
     if (initReader)
     {
         _reader = new HttpReader(_url);
     }
 }
예제 #14
0
 public HttpReaderTest(string url, bool toFile)
 {
     if (toFile)
     {
         FileStream file = File.Create("c:\\aaa");
         _reader = new HttpReaderToFile(url, file);
     }
     else
     {
         _reader = new HttpReader(url);
     }
 }
예제 #15
0
        public static bool NotebookDelete(int notebookid, string notebookpass)
        {
            var input = new NotebookDeleteInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
            };
            var json   = JsonConvert.SerializeObject(input);
            var output = HttpReader.HttpPost(ApiDef.NotebookDelete, json);

            var result = JsonConvert.DeserializeObject <NotebookDeleteOutput>(output);

            return(ResultStatus.IsSuccess(result.ResultStatus));
        }
예제 #16
0
        public RSSUnitOfWork(IResource feed, bool parseItems, bool acceptHtmlIfXmlError)
        {
            Trace.WriteLineIf(Settings.Trace, "Starting update of feed " + feed.DisplayName);
            _feed                 = feed;
            _parseItems           = parseItems;
            _acceptHtmlIfXmlError = acceptHtmlIfXmlError;
            _statusWriter         = Core.UIManager.GetStatusWriter(this, StatusPane.Network);

            string feedUrl = feed.GetStringProp(Props.URL);

            if (!HttpReader.IsSupportedProtocol(feedUrl))
            {
                throw new ArgumentException("Unsupported feed protocol: " + feedUrl);
            }
            if (parseItems)
            {
                FavIconManager favIconManager = (FavIconManager)Core.GetComponentImplementation(typeof(FavIconManager));
                favIconManager.DownloadFavIcon(feedUrl);
            }
            _httpReader = new HttpReader(feedUrl);

            string etag = feed.GetPropText(Props.ETag);

            if (etag.Length > 0)
            {
                _httpReader.IfNoneMatch = etag;
            }

            string httpUserName = feed.GetStringProp(Props.HttpUserName);
            string httpPassword = feed.GetStringProp(Props.HttpPassword);

            if (httpUserName != null && httpPassword != null)
            {
                _httpReader.Credentials = new NetworkCredential(httpUserName, httpPassword);
            }

            _httpReader.AcceptInstanceManipulation = "feed";

            if (_feed.HasProp(Props.DisableCompression) || Settings.DisableCompression)
            {
                _httpReader.AcceptEncoding = null;
            }

            _httpReader.CookieProvider = CookiesManager.GetUserCookieProvider(typeof(RSSUnitOfWork));

            _status = RSSWorkStatus.NotStarted;

            Timeout    = Settings.TimeoutInSec * 1000;
            OnTimeout += RSSUnitOfWork_OnTimeout;
        }
예제 #17
0
        public static bool UserUpdate(string username, string userpass, string newname, string newpass)
        {
            var input = new UserUpdateInput()
            {
                UserName = username,
                UserPass = userpass,
                NewName  = newname,
                NewPass  = newpass,
            };
            var json   = JsonConvert.SerializeObject(input);
            var output = HttpReader.HttpPost(ApiDef.UserUpdate, json);

            var result = JsonConvert.DeserializeObject <UserUpdateOutput>(output);

            return(ResultStatus.IsSuccess(result.ResultStatus));
        }
예제 #18
0
        private void QueueFeedUpdate(IResource feed, int attempt, JobPriority jobPriority)
        {
            if (feed.Type != "RSSFeed")
            {
                throw new ArgumentException("Invalid resource type for QueueFeedUpdate: " + feed.Type);
            }

            if (!HttpReader.IsSupportedProtocol(feed.GetPropText(Props.URL)))
            {
                return;
            }

            //  Do not update feeds which were manually set into
            //  hybernating state.
            if (feed.HasProp(Props.IsPaused))
            {
                return;
            }

            lock (this)
            {
                if (_updatingCount >= _maxCount)
                {
                    _pendingFeeds.Push((int)jobPriority, new PendingFeed(feed, attempt));
                }
                else
                {
                    RSSUnitOfWork uow = new RSSUnitOfWork(feed, true, false);
                    uow.Attempts   = attempt;
                    uow.ParseDone += new EventHandler(OnRSSParseDone);
                    Core.NetworkAP.QueueJob(jobPriority, uow);
                    _updatingCount++;
                }
            }
            if (feed.HasProp(Props.AutoUpdateComments))
            {
                foreach (IResource commentFeed in feed.GetLinksTo(null, Props.FeedComment2Feed))
                {
                    if (NeedUpdate(commentFeed))
                    {
                        QueueFeedUpdate(commentFeed);
                    }
                }
            }
        }
예제 #19
0
        public static bool NoteUpdate(int notebookid, string notebookpass, int noteid, string newname, int newtype, string newdate)
        {
            var input = new NoteUpdateInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
                NoteId       = noteid,
                NewName      = newname,
                NewType      = newtype,
                NewData      = newdate,
            };
            var json   = JsonConvert.SerializeObject(input);
            var output = HttpReader.HttpPost(ApiDef.NoteUpdate, json);

            var result = JsonConvert.DeserializeObject <NoteUpdateOutput>(output);

            return(ResultStatus.IsSuccess(result.ResultStatus));
        }
예제 #20
0
파일: FavoriteUOW.cs 프로젝트: mo5h/omeo
        public FavoriteJob(IResource webLink, string URL)
        {
            _webLink      = webLink;
            _statusWriter = Core.UIManager.GetStatusWriter(this, StatusPane.Network);
            _reader       = new HttpReader(URL);
            Timeout       = Core.SettingStore.ReadInt(
                "Favorites", "DownloadTimeout", 60000);  // 1 minute in milliseconds
            OnTimeout += new MethodInvoker(_reader_OnTimeout);
            string ifNoneMatch = _webLink.GetPropText(FavoritesPlugin._propETag);

            if (ifNoneMatch.Length > 0)
            {
                _reader.IfNoneMatch = ifNoneMatch;
            }
            _reader.IfModifiedSince = LastModified();
            _reader.CookieProvider  = CookiesManager.GetUserCookieProvider(typeof(FavoriteJob));
            _lastState = _reader.CurrentState;
        }
예제 #21
0
        public static bool NotebookGet(int notebookid, string notebookpass, ref Tuple <string, int> info)
        {
            var input = new NotebookGetInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
            };
            var json   = JsonConvert.SerializeObject(input);
            var output = HttpReader.HttpPost(ApiDef.NotebookGet, json);

            var result    = JsonConvert.DeserializeObject <NotebookGetOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                info = Tuple.Create(result.NotebookName, result.NoteSum);
            }
            return(issuccess);
        }
예제 #22
0
        public static bool NoteGet(int notebookid, string notebookpass, int noteid, ref Note note)
        {
            var input = new NoteGetInput()
            {
                NotebookId   = notebookid,
                NotebookPass = notebookpass,
                NoteId       = noteid,
            };
            var json   = JsonConvert.SerializeObject(input);
            var output = HttpReader.HttpPost(ApiDef.NoteGet, json);

            var result    = JsonConvert.DeserializeObject <NoteGetOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                note = result.Result;
            }
            return(issuccess);
        }
예제 #23
0
파일: ProxyConfig.cs 프로젝트: mo5h/omeo
        public override void OK()
        {
            ISettingStore ini = Core.SettingStore;

            if (_defaultProxyButton.Checked)
            {
                ini.WriteString("HttpProxy", "Address", string.Empty);
            }
            else
            {
                string address = _address.Text;
                ini.WriteString("HttpProxy", "Address", address);
                ini.WriteInt("HttpProxy", "Port", (int)_port.Value);
                ini.WriteString("HttpProxy", "User", _user.Text);
                ini.WriteString("HttpProxy", "Password", _password.Text);
                ini.WriteBool("HttpProxy", "BypassLocal", _bypassLocal.Checked);
                ini.WriteString("HttpProxy", "BypassList", _bypassList.Text);
            }
            HttpReader.LoadHttpConfig();
        }
예제 #24
0
        public static bool NotebookCreate(string username, string userpass, string notebookname, string notebookpass, ref int id)
        {
            var input = new NotebookCreateInput()
            {
                UserName     = username,
                UserPass     = userpass,
                NotebookName = notebookname,
                NotebookPass = notebookpass,
            };
            var json   = JsonConvert.SerializeObject(input);
            var output = HttpReader.HttpPost(ApiDef.NotebookCreate, json);

            var result    = JsonConvert.DeserializeObject <NotebookCreateOutput>(output);
            var issuccess = ResultStatus.IsSuccess(result.ResultStatus);

            if (issuccess)
            {
                id = result.NotebookId;
            }
            return(issuccess);
        }
예제 #25
0
        public ITextReader Create(Uri uri)
        {
            string prevSymb = uri.ToString().Substring(0, 1);

            if (prevSymb == "f")
            {
                FileReader reader = new FileReader
                {
                    uri = uri
                };
                return(reader);
            }
            if (prevSymb == "h")
            {
                HttpReader reader = new HttpReader
                {
                    uri = uri
                };
                return(reader);
            }
            return(null);
        }
예제 #26
0
 public int GetDownloadedSize()
 {
     return(HttpReader.GetDownloadedSize());
 }
예제 #27
0
        /// <summary>
        /// Private method that handles an incoming request.
        /// It sets up a RequestHandlerContext instance with the data from
        /// the incoming HTTP request, finds a suitable request handler to
        /// produce the response, and then sends the response as an HTTP
        /// response back to the client.
        /// Preconditions
        ///     "connection is open"
        ///     serviceRoot != null
        ///     serviceRoot.Length > 8
        ///     "serviceRoot starts with 'http://' and ends with '/'"
        ///     requestRouting != null
        /// </summary>
        /// <param name="connection">Open TCP/IP connection</param>
        /// <param name="serviceRoot">The absolute URI that is a prefix of
        /// all request URIs that this web service supports. It must start
        /// with "http://" and must end with "/".</param>
        /// <param name="relayDomain">Host name or Internet address of the
        /// relay to be used, or null if no relay is used</param>
        /// <param name="requestRouting">Collection of
        ///   { request pattern, request handler}
        /// pairs</param>
        /// <param name="connectionClose">Return parameter that indicates
        /// that the connection should be closed after this call. This may
        /// be because the incoming request has a "Connection: close"
        /// header, because the request handler has set the
        /// ConnectionClose property, or because some error occurred.
        /// </param>
        internal static void ConsumeRequest(Stream connection,
            string serviceRoot, string relayDomain,
            RequestRouting requestRouting,
            ref bool connectionClose)
        {
            Contract.Requires(connection != null);
            Contract.Requires(serviceRoot != null);
            Contract.Requires(serviceRoot.Length > 8);
            Contract.Requires(serviceRoot.Substring(0, 7) == "http://");
            Contract.Requires(serviceRoot[serviceRoot.Length - 1] != '/');
            Contract.Requires(requestRouting != null);

            // initialization --------------------------------------------
            HttpReader reader = new HttpReader();
            HttpWriter writer = new HttpWriter();
            var context = new RequestHandlerContext(serviceRoot,
                relayDomain);
            // Both client and server may request closing the connection.
            // Initially, we assume that neither one wants to close the
            // connection.
            context.ConnectionClose = false;

            // receive request -------------------------------------------
            reader.Attach(connection);

            // request line
            string httpMethod;
            string requestUri;
            string httpVersion;

            reader.ReadStringToBlank(out httpMethod);
            reader.ReadStringToBlank(out requestUri);
            reader.ReadFieldValue(out httpVersion);
            if (reader.Status != HttpStatus.BeforeContent)  // error
            {
                reader.Detach();
                connectionClose = true;
                return;
            }

            context.RequestMethod = httpMethod;
            context.RequestUri = requestUri;
            // ignore version

            // headers
            string fieldName;
            string fieldValue;
            int requestContentLength = -1;
            reader.ReadFieldName(out fieldName);
            while (reader.Status == HttpStatus.BeforeContent)
            {
                reader.ReadFieldValue(out fieldValue);
                if (fieldValue != null)
                {
                    Contract.Assert(reader.Status ==
                                    HttpStatus.BeforeContent);
                    if (fieldName == "Connection")
                    {
                        context.ConnectionClose =
                            ((fieldValue == "close") ||
                             (fieldValue == "Close"));
                    }
                    else if (fieldName == "Content-Type")
                    {
                        context.RequestContentType = fieldValue;
                    }
                    else if (fieldName == "Content-Length")
                    {
                        if (Utilities.TryParseUInt32(fieldValue,
                            out requestContentLength))
                        {
                            // content length is now known
                        }
                        else
                        {
                            reader.Status = HttpStatus.SyntaxError;
                            reader.Detach();
                            connectionClose = true;
                            return;
                        }
                    }
                }
                else
                {
                    // it's ok to skip header whose value is too long
                }
                Contract.Assert(reader.Status == HttpStatus.BeforeContent);
                reader.ReadFieldName(out fieldName);
            }
            if (reader.Status != HttpStatus.InContent)
            {
                reader.Detach();
                connectionClose = true;
                return;
            }

            // content
            if (requestContentLength > 0)
            {
                // receive content
                var requestContent = new byte[requestContentLength];
                int toRead = requestContentLength;
                var read = 0;
                while ((toRead > 0) && (read >= 0))
                {
                    // already read: requestContentLength - toRead
                    read = reader.ReadContent(requestContent,
                        requestContentLength - toRead, toRead);
                    if (read < 0) { break; }    // timeout or shutdown
                    toRead = toRead - read;
                }
                try
                {
                    char[] chars = Encoding.UTF8.GetChars(requestContent);
                    context.RequestContent = new string(chars);
                }
                catch (Exception)
                {
                    context.RequestContentType = "text/plain";
                    context.RequestContent = "request content is not in UTF8 format";
                    Debug.Print(context.RequestContent);
                    // TODO signal wrong content format through HTTP status code 400 (Bad Request)?
                }
            }

            reader.Detach();
            if (reader.Status != HttpStatus.InContent)
            {
                connectionClose = true;
                return;
            }

            // delegate request processing to a request handler ----------
            var match = false;
            foreach (Element e in requestRouting)
            {
                if (context.RequestMatch(e))
                {
                    bool connClose = context.ConnectionClose;
                    context.ResponseStatusCode = -1;                                    // undefined
                    Contract.Requires(context.ResponseContentType != null);
                    Contract.Requires(context.ResponseContentType == "text/plain");     // default
                    try
                    {
                        e.Handler(context);
                    }
                    catch (Exception h)
                    {
                        Debug.Print("exception in request handler: " + h);    // TODO how to better handle handler exceptions?
                        throw;                                  // rethrow, to avoid masking of errors
                    }
                    Contract.Ensures(context.ResponseStatusCode >= 100);
                    Contract.Ensures(context.ResponseStatusCode < 600);
                    Contract.Ensures(context.ResponseContentType != null);
                    Contract.Ensures(context.ResponseContentType.Length > 0);
                    // make sure that handler has not reset connectionClose flag:
                    Contract.Ensures((!connClose) || (context.ConnectionClose));
                    match = true;
                    break;
                }
            }
            if (!match)
            {
                context.ResponseStatusCode = 404;    // Not Found
                context.ResponseContentType = "text/plain";
                context.ResponseContent = null;
            }

            uint availableMemory = Debug.GC(true);
            Debug.Print(context.RequestMethod + " " +
                        context.RequestUri + " -> " +
                        context.ResponseStatusCode + " [" + availableMemory + "]");

            // send response ---------------------------------------------
            writer.Attach(connection);

            // status line
            writer.WriteString("HTTP/1.1 ");
            writer.WriteString(context.ResponseStatusCode.ToString());
            writer.WriteLine(" ");  // omit optional reason phrase

            // headers
            if (connectionClose)   // TODO (context.ConnectionClose)
            {
                writer.WriteLine("Connection: close");
            }
            byte[] responseBuffer = null;
            var responseLength = 0;
            if (context.ResponseContent != null)
            {
                responseBuffer =
                    Encoding.UTF8.GetBytes(context.ResponseContent);
                responseLength = responseBuffer.Length;
                writer.WriteString("Content-Type: ");
                writer.WriteLine(context.ResponseContentType);
            }
            else
            {
                responseLength = 0;
            }
            writer.WriteString("Content-Length: ");
            writer.WriteLine(responseLength.ToString());
            if (context.ResponseMaxAge > 0)
            {
                writer.WriteLine("Cache-Control: max-age=" + context.ResponseMaxAge);
            }
            else if (context.ResponseMaxAge == 0)
            {
                writer.WriteLine("Cache-Control: no-cache");
            }

            // content
            writer.WriteBeginOfContent();
            if (context.ResponseContent != null)    // send content
            {
                writer.WriteContent(responseBuffer, 0, responseLength);
            }

            writer.Detach();
            connectionClose = context.ConnectionClose;
        }
예제 #28
0
 public int GetLength()
 {
     return(HttpReader.GetLength());
 }
예제 #29
0
        private void OnATag(HTMLParser instance, string tag)
        {
            if (instance.InScript)
            {
                return;
            }

            HashMap attrMap = instance.ParseAttributes(tag);
            string  href    = (string)attrMap ["href"];

            if (href == null)
            {
                return;
            }

            if (href.StartsWith("feed:"))
            {
                href = "http:" + href.Substring(5);
            }

            Uri hrefUri;

            try
            {
                hrefUri = new Uri(_baseUri, href);
            }
            catch (Exception)
            {
                // sometimes generic exceptions are thrown from Uri constructor (see OM-9323)
                return;
            }

            string hrefUriString;

            try
            {
/*
 *              OM-12523.
 *              System.UriFormatException: Invalid URI: The hostname could not be parsed.
 *              at System.Uri.CreateHostStringHelper(String str, UInt16 idx, UInt16 end, Flags& flags, String& scopeId)
 *              at System.Uri.CreateHostString()
 *              at System.Uri.EnsureHostString(Boolean allowDnsOptimization)
 *              at System.Uri.GetComponentsHelper(UriComponents uriComponents, UriFormat uriFormat)
 *              at System.Uri.ToString()
 */
                hrefUriString = hrefUri.ToString();
            }
            catch (System.UriFormatException)
            {
                return;
            }

            if (!HttpReader.IsSupportedProtocol(hrefUriString))
            {
                return;
            }

            bool sameServer = (String.Compare(_baseUri.Host, hrefUri.Host, true) == 0);

            int    pos      = href.LastIndexOf(".");
            string ext      = (pos < 0) ? "" : href.Substring(pos).ToLower();
            int    priority = 0;

            if (ext == ".rss" || ext == ".rdf" || ext == ".xml")
            {
                priority = sameServer ? 9 : 7;
            }
            else
            {
                href = href.ToLower();
                if (href.IndexOf("rss") >= 0 || href.IndexOf("rdf") >= 0 || href.IndexOf("xml") >= 0)
                {
                    priority = sameServer ? 8 : 6;
                }
            }
            if (priority != 0)
            {
                if (!_candidateURLSet.Contains(hrefUriString))
                {
                    _lastCandidateURL = hrefUriString;
                    _candidateURLSet.Add(_lastCandidateURL);
                    _candidateURLs.Push(priority, _lastCandidateURL);
                }
            }
        }