How to deal with this issue?

So I'm writing a Spartan server in bash, and it has a config file for different hostnames.

At first, the file would just have lines with a hostname followed by a directory separated by a space, like this:

example.com /var/spartan/example

example.net /var/spartan/examplenet

However, then I decided I should add some way of specifying redirects, since that is a part of the protocol. So I rewrote it so that it would have sections and subsections. sections would be defined by a line with a single string surrounded by square brackets, and subsections would be defined by a line with a single string surrounded by curly brackets.

When the parser encountered a section it would create an associative array named SECTION_, (it would be named after the section, not just the literal word "section"), and when it encountered a subsection it would create an associative array named SECTION_SUBSECTION.

The config file would then look something like this:

[example.com]

dir /var/spartan/example

{redirect}

old/ new/

old.gmi new.gmi

[example.net]

dir /var/spartan/examplenet

{redirect}

older/ newer/

older.gmi newer.gmi

When the server received a request for "example.com", it would check if the key "dir" existed in the associative array named "example.com_". If it does then it would check if the requested path matches the name of any key in the associative array named "example.com_redirect", if it does then it would redirect the client to the value of that key, if not, then it would just try to find the file using the value that dir was associated with.

This worked when I tested it with the hostname "localhost", however when I tried with other hostnames such as example.com, it did not.

It seems associative arrays can not have dots in their name.

How should I deal with this? try to replace illegal characters such as dots with something else? Name the associative array after a hash of the hostname, then hash the hostname in the request and check the associative arrays named HASH_ and HASH_redirect? Restructure the config file in some way?

Posted in: s/bash

🚀 asdf

Mar 03 · 4 months ago

2 Comments ↓

🐦 wasolili [...] · Mar 04 at 20:53:

Your approach is that whenever someone sends a request, it runs this entire bash script, right?

If that's the case, I think you're overcomplicating things since you'll never serve two different requests under the same execution. You can just parse the config file for whatever URL is requested, which would mean you won't have to name variables after any dynamic content.

if you get a request for example.com, you'd parse the config and make something like $site_dir set to /path/to/example and build a $redirects associative array for the redirect mappings, and use those for whatever you need to do

🚀 asdf [OP] · Mar 07 at 17:00:

@wasolili

Your approach is that whenever someone sends a request, it runs this entire bash script, right?

That is correct, you would run a command like "nc -lk 300 -e ./spartanserver" so that it gets run whenever someone opens a TCP connection to port 300.

If that's the case, I think you're overcomplicating things since you'll never serve two different requests under the same execution. You can just parse the config file for whatever URL is requested, which would mean you won't have to name variables after any dynamic content.
if you get a request for example.com, you'd parse the config and make something like $site_dir set to /path/to/example and build a $redirects associative array for the redirect mappings, and use those for whatever you need to do

When I originally started writing the server it had less features so each line in the config would only be a hostname and directory separated by a space, and I would associate the hostname with the directory in an associative array named hostnames. I needed multiple options (one of which would specify the config), and I did not know how to write that in Bash so I just googled it and copy pasted some code and put it at the beginning of the script, and then put the parser in there.

When I then decided to add the redirect feature and started rewriting the parser it did not occur to me that I could parse the file after the hostname specified in the request-line, since the config is parsed when the options are handled at the beginning of the script and the request is parsed later.

Then when I got errors which I think were related to putting dots in the names of variables and associative arrays, it still did not occur to me because I was focused on trying to compare a specified hostname with the name of an associative array that can’t have dots the name.

Your solution is much better and less complicated than what I came up with, I’ll just move the parser to when the request is parsed and rewrite it a little so that the sections that don’t match the (now already known) hostname are ignored.


Source