From 29b84b37752ee80eac025fe53dcdc8de2bc9ff73 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 26 Nov 2014 23:08:21 -0500 Subject: jsonp support, possibly.. --- cgi-bin/proxy | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'cgi-bin/proxy') diff --git a/cgi-bin/proxy b/cgi-bin/proxy index 9c2ca3d..801e0f2 100755 --- a/cgi-bin/proxy +++ b/cgi-bin/proxy @@ -2,6 +2,7 @@ from os import environ as env import urllib.request +import json import sys def error(): @@ -9,18 +10,25 @@ def error(): print() print("HELLO!") -def proxy(url,ext): +def proxy(url,ext,callback): req = urllib.request.urlopen(url) print("Content-type: image/" + ext) print() sys.stdout.flush() - sys.stdout.buffer.write( req.read() ) + if callback: + sys.stdout.buffer.write( callback + "('" ) + sys.stdout.buffer.write( json.dump({ data: req.read() }) + sys.stdout.buffer.write( callback + "')" ) + else: + sys.stdout.buffer.write( req.read() ) -path = env['QUERY_STRING'] +qs = env['QUERY_STRING'].split('&callback=') +path = qs[0] +callback = qs[1] ext = path[-3:].lower() if path[0:4] == "http" and ext in ("gif","jpg","png","peg"): - proxy(path,ext) + proxy(path,ext,callback) else: error() -- cgit v1.2.3-70-g09d2 From 0c55852f1be83cc727b7c9c1560ccddd064f15ce Mon Sep 17 00:00:00 2001 From: Jules Date: Wed, 26 Nov 2014 23:44:46 -0500 Subject: make proxy a generic jsonp proxy --- cgi-bin/proxy | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'cgi-bin/proxy') diff --git a/cgi-bin/proxy b/cgi-bin/proxy index 801e0f2..df208a0 100755 --- a/cgi-bin/proxy +++ b/cgi-bin/proxy @@ -2,6 +2,7 @@ from os import environ as env import urllib.request +import base64 import json import sys @@ -12,19 +13,24 @@ def error(): def proxy(url,ext,callback): req = urllib.request.urlopen(url) - print("Content-type: image/" + ext) - print() - sys.stdout.flush() - if callback: - sys.stdout.buffer.write( callback + "('" ) - sys.stdout.buffer.write( json.dump({ data: req.read() }) - sys.stdout.buffer.write( callback + "')" ) + if callback != "": + print("Content-type: text/plain") + print() + sys.stdout.flush() + sys.stdout.buffer.write( bytes( callback + "(btoa(" + json.dumps( "".join(map(chr, base64.b64encode( req.read() )))) + "))" , 'utf-8')) else: - sys.stdout.buffer.write( req.read() ) + print("Content-type: image/" + ext) + print() + sys.stdout.flush() + sys.stdout.buffer.write( req.read() ) qs = env['QUERY_STRING'].split('&callback=') +if len(qs) == 2: + callback = qs[1] +else: + callback = '' path = qs[0] -callback = qs[1] + ext = path[-3:].lower() if path[0:4] == "http" and ext in ("gif","jpg","png","peg"): -- cgit v1.2.3-70-g09d2 From 89258b8979943f56e73adc134fabf5e54cbd3a26 Mon Sep 17 00:00:00 2001 From: Jules Date: Wed, 26 Nov 2014 23:57:03 -0500 Subject: dont btoa --- cgi-bin/proxy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cgi-bin/proxy') diff --git a/cgi-bin/proxy b/cgi-bin/proxy index df208a0..5e3539c 100755 --- a/cgi-bin/proxy +++ b/cgi-bin/proxy @@ -17,7 +17,7 @@ def proxy(url,ext,callback): print("Content-type: text/plain") print() sys.stdout.flush() - sys.stdout.buffer.write( bytes( callback + "(btoa(" + json.dumps( "".join(map(chr, base64.b64encode( req.read() )))) + "))" , 'utf-8')) + sys.stdout.buffer.write( bytes( callback + "(" + json.dumps( "".join(map(chr, base64.b64encode( req.read() )))) + ")" , 'utf-8')) else: print("Content-type: image/" + ext) print() -- cgit v1.2.3-70-g09d2