Python Http Server

Introduction Quite often I find that I need to serve some files for viewing in a web browser. Most recently, I needed to do this with an in progress OpenAPI document as rendered by ReDoc. All I needed was something that can serve static files. I really didn’t want to take the time to setup and configure something like Apache or Nginx. These are overkill for static files on a developer machine....

December 10, 2019 · John

Python Self Signed Cert Gen

Introduction Sometimes I need to write a simple network server to emulate an application I’m integrating with. Typically, this is ends up being a throw away Python script that allows me to easily inspect at a request and returns a basic response. It’s handy to verify what I’m sending isn’t malformed. Also, it helps to ensure my response parser is at least somewhat sane. The software I work on requires a TLS secured connection to all remote end points....

November 14, 2019 · John

Python Binary to C Header

Introduction Once again I needed to embed some text files into a C application. The right way to do this is turn the data into a byte array and compile it in. At least it’s the most portable way because some compilers have string length limitations. xxd -i is the easiest way to format the data so it can be compiled in. However, just like the last time, I don’t have access to xxd -i....

October 9, 2019 · John

Making MiniZip Easier to Use

Introduction Zip files are one of the most popular archive formats out there, and there are a lot of things you can do with them. While working with the ePub ebook format I spent a lot of time working with zip files. Thankfully, the standards committee for ePub used zip as the container format instead of designing their own. If you’ve ever done anything with Android you’ve dealt with zip files and probably don’t even realize it....

September 8, 2019 · John

Read Write File C Helpers

Introduction Reading and writing files in C isn’t as difficult as it sounds. A few simple loops are all you really need. That said, it’s nice to have a few helper functions ready to drop into a project. Before we write anything we need to think about the choice between fopen and open. The major differences are fopen is portable, part of the C standard, and unbuffered. While open is technically not portable it’s ubiquitous across *nix systems....

August 18, 2019 · John

Recursive Create Directory in C Revisited

Awhile back I wrote a function to recursively create directories in C. It used a string builder to split the parts and rebuild the path. The way mkdir works is by taking a single directory that does not exist and creates it. If there are multiple path parts that don’t exit it will error. Hence needing to split the string into parts and create each part of the path separately. Earlier parts of the path must exist before trying to add a later part....

July 10, 2019 · John

Unsigned Count Down

Introduction Something that comes up surprisingly often is traversing an array backwards. Maybe you’re emptying a queue. How about my personal favorite, reversing the order of elements. Counting in a for loop is so common you just don’t think about it. But counting backwards can lead to issues if you don’t do it right. The Wrong Way When you count down to and include 0 with an unsigned integer you’d think you could do something like this:...

June 2, 2019 · John

Lua For Loop Scope

Introduction One odd thing about Lua when compared to other programming languages is, everything is global by default. Take this for example: function func_a() q_var = 7 end func_a() print(q_var) Once func_a is called q_var is available and set to 7. Even outside of func_a. You can mark a variable or function as local so it’s not global and pretty much anything written in Lua that’s mildly complex will use local a lot....

May 20, 2019 · John

Thread Pool in C

Introduction When I was writing Poddown I needed a thread pool and I needed one that is cross platform. Since it’s a lightweight app I didn’t want to include a big third party threading library. So I wrote my own. Why a Thread Pool Creating threads can be quite expensive. Typically each thread is going to do essentially the same thing so it’s good to keep reusing them. Threads are actually quite heavy and creating or destroying threads takes time away from what you’re trying to accomplish....

April 12, 2019 · John

Cross Platform Thread Wrapper

Introduction There are many open source applications which use threading and are limited to either *nix or Windows because Windows handles threading a bit differently than *nix. I develop on macOS so pthreads is my go to but using it effectively locks me out of Windows because Windows doesn’t implement pthread. Instead it has it’s own thread API. There is a pthread implementation that works on Windows but it’s big, and heavy....

April 5, 2019 · John