Exemplo n.º 1
0
        private void Redirect(Net.NetworkStream streamEnclosure)
        {
            StringBuilder sB = new StringBuilder();

            sB.AppendLine("HTTP/1.1 302 Object Moved");

            sB.AppendFormat("Date: {0}", DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'", new CultureInfo("en-US")));
            sB.AppendLine();

            sB.AppendFormat("Location: {0}", this._RedirectedURL);
            sB.AppendLine();

            sB.AppendLine("Connection: close");

            this.Header.Cookie.AddOrUpdate(SessionCookieRequested?.Invoke(false));

            // put cookies because it may contain sessionid
            foreach (string key in this.Header.Cookie.Keys)
            {
                sB.AppendFormat("Set-Cookie: {0}", this.Header.Cookie[key].ToString());
                sB.AppendLine();
            }
            sB.AppendLine();

            byte[] buffer = Encoding.ASCII.GetBytes(sB.ToString());
            streamEnclosure.Write(buffer, 0, buffer.Length);
        }
Exemplo n.º 2
0
        public HttpRequestHeader(Net.NetworkStream streamEnclosure) :
            base(StringComparer.OrdinalIgnoreCase)
        {
            this._StreamEnclosure = streamEnclosure;

            this.Cookie = new HttpCookie();
        }
Exemplo n.º 3
0
        public HttpRequestBody(string contextID, Basics.Context.IHttpRequestHeader header, Net.NetworkStream streamEnclosure)
        {
            this._ContextID = contextID;

            this._Header          = header;
            this._StreamEnclosure = streamEnclosure;

            this.Form = new HttpRequestForm();
            this.File = new HttpRequestFile();

            this.Parse();
        }
Exemplo n.º 4
0
        public void Flush(Net.NetworkStream streamEnclosure)
        {
            if (this.IsRedirected)
            {
                this.Redirect(streamEnclosure);

                return;
            }

            this.PushHeaders(streamEnclosure);

            this._ResponseOutput.Seek(0, SeekOrigin.Begin);
            this._ResponseOutput.CopyTo(streamEnclosure);
        }
Exemplo n.º 5
0
        public void Write(byte[] buffer, int offset, int count)
        {
            if (this._HeaderFlushed)
            {
                Net.NetworkStream streamEnclosure = null;
                StreamEnclosureRequested?.Invoke(out streamEnclosure);

                if (streamEnclosure == null)
                {
                    throw new Exception("Inaccessible stream enclosure to do realtime streaming on http response socket!");
                }

                streamEnclosure.Write(buffer, offset, count);

                return;
            }

            this._ResponseOutput.Write(buffer, offset, count);
        }
Exemplo n.º 6
0
        private void PushHeaders(Net.NetworkStream streamEnclosure)
        {
            this.Header.AddOrUpdate("Date", DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'", new CultureInfo("en-US")));

            if (string.IsNullOrWhiteSpace(this.Header["Content-Type"]))
            {
                this.Header.AddOrUpdate("Content-Type", "text/html");
            }

            if (string.IsNullOrWhiteSpace(this.Header["Content-Length"]))
            {
                this.Header.AddOrUpdate("Content-Length", this._ResponseOutput.Length.ToString());
            }

            StringBuilder sB = new StringBuilder();

            sB.AppendFormat("HTTP/1.1 {0} {1}", this.Header.Status.Code, this.Header.Status.Message);
            sB.AppendLine();

            foreach (string key in this.Header.Keys)
            {
                sB.AppendFormat("{0}: {1}", key, this.Header[key]);
                sB.AppendLine();
            }

            string contentType =
                this.Header["Content-Type"];
            bool skip = (string.IsNullOrEmpty(contentType) || contentType.IndexOf("text/html") == -1);

            this.Header.Cookie.AddOrUpdate(SessionCookieRequested?.Invoke(skip));

            foreach (string key in this.Header.Cookie.Keys)
            {
                sB.AppendFormat("Set-Cookie: {0}", this.Header.Cookie[key].ToString());
                sB.AppendLine();
            }

            sB.AppendLine();

            byte[] buffer = Encoding.ASCII.GetBytes(sB.ToString());
            streamEnclosure.Write(buffer, 0, buffer.Length);
        }
Exemplo n.º 7
0
        public void ActivateStreaming()
        {
            if (this.IsRedirected)
            {
                throw new Exception("Not possible to activate streaming when request has been already redirected!");
            }

            if (string.IsNullOrWhiteSpace(this.Header["Content-Length"]))
            {
                throw new Exception("Content-Length should be defined in header to activate streaming for http response!");
            }

            Net.NetworkStream streamEnclosure = null;
            StreamEnclosureRequested?.Invoke(out streamEnclosure);

            if (streamEnclosure == null)
            {
                throw new Exception("Inaccessible stream enclosure to activate streaming on http response!");
            }

            this.PushHeaders(streamEnclosure);
        }
Exemplo n.º 8
0
        public HttpRequestHeader(Net.NetworkStream streamEnclosure)
        {
            this._StreamEnclosure = streamEnclosure;

            this.Cookie = new HttpCookie();
        }