gemini.git

going-flying.com gemini git repository

summary

tree

log

refs

be4894bc1a1d48714ca6b1c3edd0c6503fc0c683 - Matthew Ernisse - 1612630741

more toolbox functions

view tree

view raw

diff --git a/cgi-bin/converter b/cgi-bin/converter
index 97e29d7..0ec205a 100755
--- a/cgi-bin/converter
+++ b/cgi-bin/converter
@@ -27,9 +27,10 @@ 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 base64
 import os
 import sys
-from urllib.parse import parse_qs, urlparse
+from urllib.parse import parse_qs, quote, unquote, urlparse
 
 
 BACK_LINK = '/cgi-bin/converter/'
@@ -45,6 +46,10 @@ Display a number in binary, decimal and hexadecimal.  If your input cannot be re
 => dec	Decimal Input
 => hex	Hexadecimal Input
 
+## Encode / Decode
+
+=> base64/encode	Base64 Encode
+=> base64/decode	Base64 Decode
 
 ## Suggestions?
 Want to see something here?  Feel free to drop me a line.
@@ -117,6 +122,17 @@ class GeminiCGI(object):
 			self.query_parsed = parse_qs(self.query_string)
 
 
+def encode_response(input, result):
+	global BACK_LINK
+	cgi.Response.Ok('text/gemini')
+
+	output = f'# Input\n{input}\n\n'
+	output += f'# Output\n{result}\n\n'
+	output += f'=> {BACK_LINK}	Back\n'
+	output += f'=> /	Home'
+	print(output)
+
+
 def ender(i):
 	s = {
 		42: 'Life, The Universe, and Everything',
@@ -155,7 +171,7 @@ def result_table(cgi, input, b, d, h):
 	output += f'=> {BACK_LINK}	Back\n'
 	output += f'=> /	Home'
 	print(output)
-	
+
 
 if __name__ == '__main__':
 	cgi = GeminiCGI()
@@ -165,6 +181,7 @@ if __name__ == '__main__':
 			cgi.Response.Ok('text/gemini')
 			print(MAIN_SCREEN)
 
+		# Number base conversion
 		elif cgi.path_info == 'bin':
 			cgi.Response.Input('Binary Number?')
 
@@ -174,10 +191,25 @@ if __name__ == '__main__':
 		elif cgi.path_info == 'hex':
 			cgi.Response.Input('Hex Number?')
 
+		# base64 encode/decode
+		elif cgi.path_info == 'base64/encode':
+			cgi.Response.Input('String to encode?')
+
+		elif cgi.path_info == 'base64/decode':
+			cgi.Response.Input('String to decode?')
+
+		# urlencode/decode
+		elif cgi.path_info == 'url/encode':
+			cgi.Response.Input('String to encode?')
+
+		elif cgi.path_info == 'url/decode':
+			cgi.Response.Input('String to decode?')
+
 	else:
 		if not cgi.path_info:
 			cgi.Response.Redirect(BACK_LINK)
 	
+		# Number base conversion
 		elif cgi.path_info == 'bin':
 			s = cgi.query_string
 		
@@ -217,6 +249,48 @@ if __name__ == '__main__':
 
 			result_table(cgi, s, b, d, h)
 
+		# base64 encode/decode
+		elif cgi.path_info == 'base64/encode':
+			s = cgi.query_string
+			try:
+				res = base64.b64encode(s)
+			except Exception:
+				cgi.Response.Fail()
+				sys.exit()
+
+			encode_response(s, res)
+
+		elif cgi.path_info == 'base64/decode':
+			s = cgi.query_string
+			try:
+				res = base64.b64decode(s, validate=True)
+			except Exception:
+				cgi.Response.Fail()
+				sys.exit()
+
+			encode_response(s, res)
+
+		# urlencode/decode
+		elif cgi.path_info == 'url/encode':
+			s = cgi.query_string
+			try:
+				res = quote(s, errors='strict')
+			except Exception:
+				cgi.Response.Fail()
+				sys.exit()
+
+			encode_response(s, res)
+
+		elif cgi.path_info == 'url/decode':
+			s = cgi.query_string
+			try:
+				res = unquote(s, errors='strict')
+			except Exception:
+				cgi.Response.Fail()
+				sys.exit()
+
+			encode_response(s, res)
+
 		else:
 			cgi.Response.NotFound()
 
diff --git a/index.gmi b/index.gmi
index 6db72fa..31bc882 100644
--- a/index.gmi
+++ b/index.gmi
@@ -19,9 +19,12 @@ For the interested this capsule is running on Molly Brown in a Docker container,
 => /cgi-bin/bofh.py	BOFH-style Excuse Server
 => /cgi-bin/rules.py	Random Ferengi Rule of Acquisition
 => /cgi-bin/vfdsay.py	VFD Message Display
-=> /cgi-bin/converter/	Binary, Decimal, Hex Converter
 => /~mernisse/01.gmi	(About the VFD message display)
 
+## CGI Toolbox
+Convert from/to binary, hexidecimal, decimal!  URL quote and unquote!  BASE64 encode and decode!
+=> /cgi-bin/converter/	CGI Toolbox
+
 ## Feeds
 => /~mernisse/atom.xml	~mernisse
 => /thoughts/atom.xml	Thoughts