Exemple #1
0
 public static void Response(IFuContext c)
 {
     output(TraceLevel.Info, @"< {0} - {1} bytes of {2}",
     c.Response.StatusCode,
     c.Response.ContentLength64,
     c.Response.ContentType);
 }
Exemple #2
0
        public override long Render(IFuContext c, Stream output)
        {
            var count = (int)ContentLength64;
              output.Write(_data, _offset, count);

              return count;
        }
Exemple #3
0
        public override long Render(IFuContext c, Stream output)
        {
            // TODO: Eliminate this buffer?
              // Renders actual result to a buffer first
              Stream ms = new MemoryStream();
              ActualResult.Render(c, ms);

              // then runs it through the compressor
              ms.Seek(0, SeekOrigin.Begin);
              var buffer = _compressor(c, ms);

              // then render the result to the output
              buffer.CopyTo(output);

              // checks wether we can have the content length
              var length = -1L;
              if (buffer.CanSeek)
            length = buffer.Length;

              buffer.Close();
              buffer.Dispose();

              if (ms != buffer) {
            ms.Close();
            ms.Dispose();
              }

              return length;
        }
        protected override string CreateKeyCore(IFuContext c)
        {
            var buffer = new byte[KeyLength];
              _rng.GetBytes(buffer);

              buffer = _sha1.ComputeHash(buffer);
              return Convert.ToBase64String(buffer);
        }
Exemple #5
0
 private static IDictionary<string, string> buildMappings(IFuContext c)
 {
     return File
     .ReadAllLines(c.ResolvePath("~/UrlMap.txt"))
     .Select(line => line.Trim())
     .Where(line => !line.StartsWith("#")) // ignore comments
     .Select(line => line.Split(' '))
     .ToDictionary(arr => arr[0], arr => arr[1]);
 }
Exemple #6
0
        private static string replaceSessionVariables(IFuContext context, string source)
        {
            var session = context.Get<IMySession>();

              source = source.Replace("{%USER_ID%}", session.UserID.ToString());
              source = source.Replace("{%USERNAME%}", session.Username);

              return source;
        }
Exemple #7
0
        public override long Render(IFuContext c, Stream output)
        {
            var charSet = Encoding.GetEncoding(this.ContentType.CharSet);
              var bytes = Encoding.UTF8.GetBytes(Text);

              var buffer = Encoding.Convert(Encoding.UTF8, charSet, bytes);
              output.Write(buffer, 0, buffer.Length);

              return buffer.Length;
        }
        public void DeleteId(IFuContext c)
        {
            var cookie = c.Request.Cookies[CookieName];

              if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            return;

              cookie.Expires = DateTime.Now.AddMonths(-1);

              saveCookie(c, cookie);
        }
Exemple #9
0
        public override long Render(IFuContext c, Stream output)
        {
            var filePath = c.ResolvePath(Filename, allowUnsafePath: true);

              var fs = File.OpenRead(filePath);
              var length = fs.Length;

              fs.CopyTo(output);
              fs.Close();
              fs.Dispose();

              return length;
        }
        public string CreateId(IFuContext c)
        {
            // create a new session key
              var sessionId = CreateKeyCore(c);
              var settings = c.Settings.Session;

              // save the session into the response cookie
              var cookie = new Cookie(CookieName, sessionId,
            settings.CookiePath,
            settings.CookieDomain);

              cookie.HttpOnly = true;
              cookie.Expires = DateTime.Now.AddMonths(1);

              saveCookie(c, cookie);
              return sessionId;
        }
Exemple #11
0
        protected Template GetTemplate(IFuContext context,
            IEnumerable<string> templateNames, Type templateType)
        {
            if (!typeof(HamlTemplateBase).IsAssignableFrom(templateType))
            throw new InvalidOperationException(
              "Template types must derive from HamlTemplateBase");

              // NOTE: TemplateEngine already maintains a caching system
              var engine = context.Get<TemplateEngine>();
              var compiled = engine.Compile(templateNames.ToList(), templateType);

              var instance = (HamlTemplateBase)compiled.CreateInstance();
              instance.Context = context;
              instance.Result = this;

              return instance;
        }
Exemple #12
0
        public override long Render(IFuContext c, Stream output)
        {
            // TODO: Check first wether TemplateEngine is supported
              //       throw an exception, if it's not
              // TODO: Implement a proper exception system that make senses
              var engine = c.Get<TemplateEngine>();

              var templateNames = GetTemplateNames(c).Reverse();
              var templateType = GetTemplateType(c);
              var template = GetTemplate(c, templateNames, templateType);

              OnBeforeRender(c, template);

              var encoding = Encoding.GetEncoding(c.Settings.Encoding);
              var sw = new StreamWriter(output, encoding);
              template.Render(sw);

              sw.Flush();
              sw.Close();

              // TODO: Support content-length, if possible
              return -1;
        }
Exemple #13
0
 // ran just before rending the template, configure any properties here
 protected virtual void OnBeforeRender(IFuContext context, Template template)
 {
     /* no-op */
 }
Exemple #14
0
 public static void Request(IFuContext c)
 {
     output(TraceLevel.Info, @"> {0} {1}",
     c.Request.HttpMethod,
     c.Request.Url.PathAndQuery);
 }
Exemple #15
0
 public static ResultContext From(IFuContext c, Filter<Stream> filter)
 {
     return new ResultContext(c,
     new FilteredResult(c.As<IResultContext>().Result, filter));
 }
Exemple #16
0
 protected override System.Type GetTemplateType(IFuContext context)
 {
     return typeof(HamlTemplateBase);
 }
Exemple #17
0
 protected override IEnumerable<string> GetTemplateNames(IFuContext context)
 {
     return (new[] { HamlFilename }).Concat(LayoutFilenames);
 }
Exemple #18
0
 public static ResultContext From(IFuContext input, string text)
 {
     return new ResultContext(input, new StringResult(text));
 }
Exemple #19
0
 public static ResultContext From(IFuContext c, string filename, string contentType)
 {
     return new ResultContext(c, new FileResult(filename, contentType));
 }
Exemple #20
0
 public abstract long Render(IFuContext c, Stream output);
        private void saveCookie(IFuContext c, Cookie cookie)
        {
            // NOTE: Manual cookie string building is required to allow Fu to
              //       stay with "client" framework subset because the
              //       System.Net.Cookie class doesn't actually use all the http fields
              var cookieString = new StringBuilder();
              cookieString.AppendFormat("{0}={1}; ", cookie.Name, cookie.Value);

              if (!cookie.Expired)
            cookieString.AppendFormat("expires={0}; ",
              cookie.Expires.ToUniversalTime().ToString("R"));

              if (!string.IsNullOrEmpty(cookie.Path))
            cookieString.AppendFormat("path={0}; ", cookie.Path);
              if (!string.IsNullOrEmpty(cookie.Domain))
            cookieString.AppendFormat("domain={0}; ", cookie.Domain);

              c.Response.Headers.Add(HttpResponseHeader.SetCookie,
            cookieString.ToString());
        }
        public string GetId(IFuContext c)
        {
            var cookie = c.Response.Cookies[CookieName] ??
            c.Request.Cookies[CookieName];

              if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            return null;

              return cookie.Value;
        }
Exemple #23
0
 // should return types derived from HamlTemplateBase
 protected abstract Type GetTemplateType(IFuContext context);
Exemple #24
0
 // should return filenames relative to template base paths specified in HamlTemplateService
 protected abstract IEnumerable<string> GetTemplateNames(IFuContext context);
Exemple #25
0
 private static bool isFolder(IFuContext c)
 {
     return c.Request.Url.AbsolutePath.EndsWith("/");
 }
Exemple #26
0
 public override long Render(IFuContext c, Stream output)
 {
     return _render(c, output);
 }
 protected abstract string CreateKeyCore(IFuContext c);
Exemple #28
0
 public ResultContext(IFuContext context, IResult result)
     : base(context)
 {
     Result = result;
 }
Exemple #29
0
 // Continuation utilities
 public static void Execute(this Continuation cont, IFuContext context)
 {
     cont(fu.EndAct)(context);
 }
Exemple #30
0
 public static ResultContext From(IFuContext input,
     Reduce<object> jsonStep)
 {
     return new ResultContext(input, new JsonResult(jsonStep(input)));
 }