gemini.git

going-flying.com gemini git repository

summary

tree

log

refs

2e3f8c358573121df2bfe3e658a77c09383f4aa6 - Matthew Ernisse - 1612576595

start working on a rando toolbox of sorts

view tree

view raw

diff --git a/cgi-bin/converter b/cgi-bin/converter
new file mode 100755
index 0000000..57ba1f9
--- /dev/null
+++ b/cgi-bin/converter
@@ -0,0 +1,143 @@
+#!/usr/bin/env python3
+# -*- coding: UTF-8 -*-
+'''converter (c) 2021 Matthew J Ernisse <matt@going-flying.com>
+All Rights Reserved.
+
+Redistribution and use in source and binary forms,
+with or without modification, are permitted provided
+that the following conditions are met:
+
+    * Redistributions of source code must retain the
+      above copyright notice, this list of conditions
+      and the following disclaimer.
+    * Redistributions in binary form must reproduce
+      the above copyright notice, this list of conditions
+      and the following disclaimer in the documentation
+      and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+'''
+import os
+import sys
+from urllib.parse import parse_qs, urlparse
+
+
+class GeminiCGI(object):
+	class Response(object):
+
+		codes = {
+			10: '10',
+			20: '20',
+			50: '50 PERMANENT FAILURE',
+			51: '51 NOT FOUND',
+			59: '59 BAD REQUEST',
+		}
+
+		def __init__(self, code=10, meta=''):
+			''' Construct the response header per spec 3.1 '''
+			self.header = self.codes[code]
+			if code <= 20:
+				self.header += ' ' + meta
+
+			self.header += '\r\n'
+
+		@classmethod
+		def Input(cls, meta):
+			return cls(10, meta)
+
+		@classmethod
+		def Ok(cls, meta):
+			return cls(20, meta)
+
+		@classmethod
+		def Fail(cls):
+			return cls(50)
+
+		@classmethod
+		def NotFound(cls):
+			return cls(51)
+
+		@classmethod
+		def BadRequest(cls):
+			return cls(59)
+
+		def print_header(self):
+			print(self.header)
+
+
+	def __init__(self):
+		self.path_info = os.environ.get('PATH_INFO')
+		self.script_path = os.environ.get('SCRIPT_PATH')
+		self.query_string = os.environ.get('QUERY_STRING')
+
+		if self.query_string:
+			self.query_parsed = parse_qs(self.query_string)
+
+
+def display_main_menu(cgi):
+	print('''# Matt's Gemini Toolbox.
+
+=> bintohex	Binary to Hexadecimal
+=> dectohex	Decimal to Hexadecimal
+=> hextobin	Hexadecimal to Binary
+
+
+=> /	Home
+''')
+
+
+if __name__ == '__main__':
+	cgi = GeminiCGI()
+
+	if not cgi.query_string:
+		if not cgi.path_info:
+			display_main_menu(cgi)
+			sys.exit()
+
+		elif cgi.path_info == 'bintohex':
+			response = cgi.Response.Input('Binary Number?')
+			response.print_header()
+			sys.exit()
+
+		elif cgi.path_info == 'dectohex':
+			response = cgi.Response.Input('Number?')
+			response.print_header()
+			sys.exit()
+
+		elif cgi.path_info == 'hextobin':
+			response = cgi.Response.Input('Hex Number?')
+			response.print_header()
+			sys.exit()
+
+	else:
+		if not cgi.path_info:
+			response = cgi.Response.BadRequest
+			response.print_header()
+	
+		elif cgi.path_info == 'dectohex':
+			response = cgi.Response.Ok('text/plain')
+			response.print_header()
+			print(cgi.query_string)
+			sys.exit()
+	
+		elif cgi.path_info == 'bintohex':
+			response = cgi.Response.Ok('text/plain')
+			response.print_header()
+			print(cgi.query_string)
+			sys.exit()
+
+		elif cgi.path_info == 'hextobin':
+			response = cgi.Response.Ok('text/plain')
+			response.print_header()
+			print(cgi.query_string)
+			sys.exit()