public void LoadFile(string physicalPathFile, DateTime modifiedSince)
        {
            FileInfo fi = new FileInfo(physicalPathFile);

            if (fi.Exists)
            {
                if (fi.LastWriteTime >= modifiedSince)
                {
                    ContentType   = ContentTypes.GetExtensionType(fi.Name);
                    ContentStream = (Stream)(new FileStream(physicalPathFile, FileMode.Open, FileAccess.Read, FileShare.Read));

                    if (ContentType.StartsWith("text"))
                    {
                        // StreamReader reader = new StreamReader(streamContent);
                        // auto detect by browser
                        CharSet = "";
                    }
                }

                LastModified = HttpTime.RoundDate(fi.LastWriteTime);
            }
            else
            {
                throw new FileNotFoundException("File Not Found!", physicalPathFile);
            }
        }
        public void AddField(HttpField field)
        {
            switch (field.Name)
            {
            case HttpConst.Host:
                Host = field.Value;
                break;

            case HttpConst.Accept:
                Accept.AddRange(field.Values);
                break;

            case HttpConst.AcceptLanguage:
                AcceptLanguage.AddRange(field.Values);
                break;

            case HttpConst.AcceptEncoding:
                AcceptEncoding.AddRange(field.Values);
                break;

            case HttpConst.AcceptCharset:
                AcceptCharset.AddRange(field.Values);

                if (AcceptCharset.Count > 0)
                {
                    ContentTransferEncoding = Encoding.GetEncoding(AcceptCharset[0]);
                }
                break;

            case HttpConst.KeepAlive:
                KeepAlive = field.Value;
                break;

            case HttpConst.Connection:
                Connection = field.Value;
                break;

            case HttpConst.UserAgent:
                UserAgent = field.RawValue;
                break;

            case HttpConst.Referer:
                Referer = field.Value;
                break;

            case HttpConst.Cookie:
                Cookie = field.Value;
                break;

            case HttpConst.IfUnmodifiedSince:
                // lastModified = HtmlTime.TimeHtml2Date(value);
                break;

            case HttpConst.IfModifiedSince:
                // se pueden recibir varios parametros "If-Modified-Since: Tue, 10 Oct 2006 13:16:15 GMT; length=13757"
                IfModifiedSince = HttpTime.TimeHtml2Date(field.RawValue);
                break;

            case HttpConst.IfNoneMatch:
                break;

            case HttpConst.ContentType:
                ContentType = field.Value;
                break;

            case HttpConst.ContentLength:
                ContentLength = int.Parse(field.Value);
                break;

            case HttpConst.ContentTransferEncoding:
                ContentTransferEncoding = Encoding.GetEncoding(field.Value);
                break;

            case HttpConst.Authorization:
                ParserUserAndPassword(field.Value);
                break;

            default:
                // currentRequest.RestHeaders.Add(tag.ToLower(), field);
                // Head.Fields.Add(field.Name, field);
                break;
            }

            Head.Fields.Add(field.Name, field);
        }
        public void PrepareSend()
        {
            HeadStream = (Stream) new MemoryStream();
            StreamWriter headWriter = new StreamWriter(HeadStream, Encoding.ASCII);

            string codeReason = HttpConst.CodeReasons[Code];

            headWriter.WriteLine("{0} {1} {2}",
                                 HttpConst.HttpVersion, (int)Code, codeReason);

            if (Location != null)
            {
                headWriter.WriteLine("Location: {0}", Location);
            }

            headWriter.WriteLine("Server: {0}", WebConfig.ServerVersion);
            headWriter.WriteLine("Connection: {0}", Connection);
            headWriter.WriteLine("Date: {0}", HttpTime.HtmlNow());

            if (Body != null && Body.HasContent)
            {
                if (Body.ContentEncoding != ContentEncodings.NORMAL)
                {
                    string cenc = "";

                    switch (Body.ContentEncoding)
                    {
                    case ContentEncodings.GZIP:
                        cenc = "gzip";
                        break;

                    case ContentEncodings.DEFLATE:
                        cenc = "deflate";
                        break;
                    }

                    headWriter.WriteLine("Content-Encoding: {0}", cenc);
                }

                if (Body.ContentEncoding == ContentEncodings.GZIP)
                {
                    MemoryStream body = new MemoryStream();
                    GZip.Compress(Body.ContentStream, body);
                    Body.ContentStream = body;
                }

                if (LastModified != null)
                {
                    headWriter.WriteLine("Last-Modified: {0}",
                                         HttpTime.Date2TimeHtml(LastModified));
                }

                headWriter.Write("Content-Type: {0}", Body.ContentType);

                if (Body.ContentType.StartsWith("text") && Body.CharSet.Length > 0)
                {
                    headWriter.Write("; charset={0}", Body.CharSet);
                }

                headWriter.WriteLine();

                if (Body.ContentDisposition != null && Body.ContentDisposition.Length > 0)
                {
                    headWriter.WriteLine("Content-Disposition: {0}", Body.ContentDisposition);
                }

                headWriter.WriteLine("Content-Length: {0}", Body.Length);

                // BUG para MSIE para imagenes en CSS
                if (Body.ContentType.StartsWith("image") && Request.IsMSIEBrowser)
                {
                    headWriter.WriteLine("Expires: {0}", HttpTime.Date2TimeHtml(DateTime.Now.AddHours(2)));
                }

                headWriter.WriteLine("Cache-Control: {0}", "public, must-revalidate, max-age = 1");
            }
            else
            {
                headWriter.WriteLine("Content-Length: {0}", 0);
            }

            foreach (object item in Head.Fields.Values)
            {
                var field = (HttpField)item;
                switch (field.Name)
                {
                default:
                    headWriter.WriteLine("{0}: {1}", field.Name, field.Value);
                    break;
                }
            }

            headWriter.WriteLine();
            headWriter.Flush();
        }
Exemple #4
0
 public static string HtmlNow()
 {
     return(HttpTime.Date2TimeHtml(DateTime.Now));
 }
Exemple #5
0
 public static DateTime RoundedNow()
 {
     return(HttpTime.RoundDate(DateTime.Now));
 }
Exemple #6
0
 /// <summary>
 /// redondear una fecha para que sea compatible con las fechas que llegan desde mensajes
 /// HTTP que no tienen milisegundos
 /// </summary>
 /// <param name="dateTime"></param>
 /// <returns></returns>
 public static DateTime RoundDate(DateTime dateTime)
 {
     return(HttpTime.TimeHtml2Date(HttpTime.Date2TimeHtml(dateTime)));;
 }