Comment by 🐦 wasolili

Re: "How to deal with this issue?"

In: s/bash

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

🐦 wasolili [flaired user]

2025-03-04 Β· 1 year ago

1 Later Comment

πŸš€ asdf [OP] Β· 2025-03-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.

Original Post

πŸŒ’ s/bash

πŸš€ asdf:

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...

πŸ’¬ 2 comments Β· 2025-03-03 Β· 1 year ago