Yesterday I discovered an app for iOS which allowed me to write my Jekyll blog posts.

The app is called Octopage. It is far from perfect when it comes to actually typing the content of the blog posts, but it allows me to create offline drafts, handles all of my customized headers, and just works. I hope the developer will spend more time on polishing up this app, as it is great for when I am not near a computer and have some ideas I want to share.

I made this blog post with the app, as proof of it working!

Today I was playing around with some code, and I was thinking that it would be really nice to hide some admin modules from public access without creating user access security.

So since I was using NancyFx, I decided to create a bit of code to achieve this, and here it is.

        public static void EnsureLocalOnly(this NancyModule module)
        {
#if (!DEBUG)
            module.Before.AddItemToEndOfPipeline(c => !c.Request.IsLocal() ? new Response {StatusCode = HttpStatusCode.NotFound} : null);
#endif
        }

The wrapping if statement is simply to only perform this check on release builds of the code (Production builds), leaving me to experiment as much as I please on my development box. This is completely optional, and you are of course welcome to change/modify it as much as you would like to.

Here is an example of how to use the code.

public class AdminModule : NancyModule
{
    public AdminModule() : base("/admin")
    {
        this.EnsureLocalOnly();

        Get["/"] = _ => "Only localhost can see this";

        Post["/{name}"] = _ => $"Welcome to localhost, {_.name}"; 
    }
}

Another way the code could be used would be.

public class SimpleModule : NancyModule
{
    public SimpleModule()
    {
        Get["/"] = _ => "Public can see this";

        Get["/local"] = _ => {
            this.EnsureLocalOnly();
            return "Only localhost can see this";
        };
    }
}

Enjoy!

Recently I have been working a lot with ElasticSearch and when debugging on localhost, my requests do not show up in fiddler.

Normally the trick to solving this is changing from http://localhost:9200 towards http://ipv4.fiddler:9200 or http://ipv6.fiddler:9200, which is really not a good solution, because the moment I close Fiddler, my code stops working as the proxy address no longer resolves.

A co-worker at my current client had a previous job at an internet service provider and taught me a wonderful trick for this! Simple add a fullstop, like so http://localhost.:9200. Simply by seeing the fullstop, your computer will automatically route the traffic through the DNS, which forces it to go via Fiddler (if Fiddler is running), and if Fiddler is not running, the DNS will resolve it as a regular localhost call. You get the best of both worlds!

Short blog post, but an extremely useful tip!

I have a Macbook and make use of Parallels to run my client projects in VMs, which are often Windows. Since I have never been a fan of Git on Windows, I store my project files in my Mac drive and share it with Windows over a network share, but this can cause problems.

You can run into some very strange errors with this setup, including Visual Studio constantly asking you if you trust the projects you open, and recently I received errors when trying to make use of Cake. Ultimately, it came down to a simple script (run as Administrator) to fix it all for me!

net use y: \\Mac\Home
C:\Windows\Microsoft.NET\Framework\v4.0.30319\CasPol.exe -pp off -machine -addgroup 1.2 -url file:///Y:/Documents/Projects/* FullTrust

This script mapped my Mac’s Home directory to y: for the Administrator, and then told .NET applications to trust everything inside of my Projects folder. Now since the only things that make use of my Projects folders are .NET, this worked perfectly!

I was playing around with a small project yesterday evening, where I was receiving uploaded files to my NancyFX service but needed to send those on to another web service, but this was not as simple as I had hoped.

So originally I had hoped there would be a content type you could send to the HttpClient object, which would allow you to add an array of files to be sent and it would just work, but unfortunately it was not that simple.

Fortunately, I was not the first person to encounter this problem and found somebody on StackOverflow with a simple solution to the problem, so I tried it with my code and it worked beautifully!

        public UploadModule(IConfigurations configurations)
        {
#if (!DEBUG)
            this.RequiresAuthentication();
#endif
            
            Post["/upload", true] = async(_, token) =>
            {
                using (var content = new MultipartFormDataContent())
                using (var client = new HttpClient())
                {
                    foreach (var file in Request.Files)
                    {
                        content.Add(CreateFileContent(file.Value, file.Name, file.ContentType));
                    }

                    var response = await client.PostAsync(configurations.ImageServer, content);
                    response.EnsureSuccessStatusCode();
                    return HttpStatusCode.OK;
                }
            };
        }

        private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
        {
            var fileContent = new StreamContent(stream);
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"files\"",
                FileName = "\"" + fileName + "\""
            }; // the extra quotes are key here
            fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            return fileContent;
        }