From e594999925c753d3eda93ae88b3fbdf7d588e1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Bouillot?= Date: Tue, 4 Apr 2017 16:27:18 +0200 Subject: [PATCH] Implement new URL scheme --- http/template-head | 2 +- http/web.py | 98 ++++++++++++++++++++++++++-------------------- 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/http/template-head b/http/template-head index 222da2b..b0b8067 100644 --- a/http/template-head +++ b/http/template-head @@ -20,7 +20,7 @@

Free Electrons

Embedded Linux Experts

-  • source navigation • identifier search • freetext search • Back to LXR •  +  • source navigation • identifier search • freetext search • Back to LXR • 

diff --git a/http/web.py b/http/web.py index 84526c2..506660e 100755 --- a/http/web.py +++ b/http/web.py @@ -54,30 +54,51 @@ from collections import OrderedDict status = 200 -form = cgi.FieldStorage() -version = form.getvalue ('v') -if not (version and search ('^[A-Za-z0-9.-]+$', version)): - version = 'v4.10' - url = os.environ['SCRIPT_URL'] -m = search ('^/source(.*)$', url) +m = search ('^/e/linux/([^/]*)/([^/]*)(.*)$', url) if m: - mode = 'source' - path = m.group (1) - if not search ('^[A-Za-z0-9_/.,+-]*$', path): - path = 'INVALID' - url2 = 'source'+path+'?' + version = m.group (1) + cmd = m.group (2) + arg = m.group (3) + if not (version and search ('^[A-Za-z0-9.-]+$', version)): + status = 302 + location = '/e/linux/v4.10/'+cmd+arg + cmd = '' + if cmd == 'source': + path = arg + if len (path) > 0 and path[-1] == '/': + path = path[:-1] + status = 301 + location = '/e/linux/'+version+'/source'+path + else: + mode = 'source' + if not search ('^[A-Za-z0-9_/.,+-]*$', path): + path = 'INVALID' + url2 = 'source'+path + elif cmd == 'ident': + ident = arg[1:] + form = cgi.FieldStorage() + ident2 = form.getvalue ('i') + if ident == '' and ident2: + status = 302 + location = '/e/linux/'+version+'/ident/'+ident2 + else: + mode = 'ident' + if not (ident and search ('^[A-Za-z0-9_-]*$', ident)): + ident = '' + url2 = 'ident/'+ident + elif cmd == 'search': + mode = 'search' + url2 = 'search' -elif url == '/ident': - mode = 'ident' - ident = form.getvalue ('i') - if not (ident and search ('^[A-Za-z0-9_-]*$', ident)): - ident = '' - url2 = 'ident?i='+ident+'&' - -elif url == '/search': - mode = 'search' - url2 = 'search?' +if status == 301: + realprint ('Status: 301 Moved Permanently') + realprint ('Location: '+location+'\n') + exit() +elif status == 302: + realprint ('Status: 302 Found') + realprint ('Location: '+location+'\n') + exit() head = open ('template-head').read() head = sub ('\$baseurl', 'http://' + os.environ['HTTP_HOST'], head) @@ -112,7 +133,7 @@ for v1k in va: v += '
  • '+v2k+'\n' v += '
  • \n' v += ' \n' v += '\n' @@ -120,12 +141,12 @@ v += '\n' head = sub ('\$versions', v, head) if mode == 'source': - banner = 'Linux' + banner = 'Linux' p2 = '' p3 = path.split ('/') [1:] for p in p3: - banner += '/'+p+'' p2 += '/'+p + banner += '/'+p+'' head = sub ('\$banner', banner, head) head = sub ('\$title', 'Linux'+path+' - Linux Cross Reference - Free Electrons', head) @@ -137,12 +158,7 @@ if mode == 'source': if len (type) == 1: type = type[0] if type == 'tree': - if path[-1:] != '/': - lines += shell_exec ('cd ..; ./query.py dir '+version+' \''+path+'\'') - else: - status = 302 - path = path[:-1] - location = '/source'+path+'?v='+version + lines += shell_exec ('cd ..; ./query.py dir '+version+' \''+path+'\'') elif type == 'blob': lines += shell_exec ('cd ..; ./query.py file '+version+' \''+path+'\'') else: @@ -176,8 +192,8 @@ if mode == 'source': name = 'Parent directory' print (' ') - print (' '+icon+'') - print (' '+name+'') + print (' '+icon+'') + print (' '+name+'') print (' '+size+'') print (' \n') @@ -195,7 +211,7 @@ if mode == 'source': width2 = len (str (num)) space = ' ' * (width1 - width2) for l in lines: - print (' '+space+''+str(num)+' ') + print (' '+space+''+str(num)+' ') num += 1 width2 = len (str (num)) space = ' ' * (width1 - width2) @@ -205,7 +221,7 @@ if mode == 'source': for l in lines: l = cgi.escape (l) l = sub ('"', '"', l) - l = sub ('\033\[31m(.*?)\033\[0m', '\\1', l) + l = sub ('\033\[31m(.*?)\033\[0m', '\\1', l) l = sub ('\033\[32m', '', l) l = sub ('\033\[33m', '', l) l = sub ('\033\[0m', '', l) @@ -216,7 +232,7 @@ if mode == 'source': elif mode == 'ident': - field = '\n
    \n\nIdentifier: \n\n
    \n

    ' + ident + field = '

    \n
    \nIdentifier: \n\n
    \n

    ' + ident head = sub ('\$banner', field, head) head = sub ('\$title', 'Linux identifier search "'+ident+'" - Linux Cross Reference - Free Electrons', head) print (head, end='') @@ -236,7 +252,7 @@ elif mode == 'ident': l = next (lines) m = search ('^(.*): (\d*) \((.*)\)$', l) f, n, t = m.groups() - print ('
  • '+f+', line '+n+' (as a '+t+')', end='') + print ('
  • '+f+', line '+n+' (as a '+t+')', end='') print ('') next (lines) @@ -253,16 +269,16 @@ elif mode == 'ident': ln = m.group (2).split (',') if len (ln) == 1: n = ln[0] - print ('
  • '+f+', line '+str(n)+'', end='') + print ('
  • '+f+', line '+str(n)+'', end='') else: if num > 100: # Concise display n = len (ln) - print ('
  • '+f+', '+str(n)+' times') + print ('
  • '+f+', '+str(n)+' times') else: # Verbose display print ('
  • '+f) print ('') print ('') else: @@ -285,10 +301,6 @@ print (open ('template-tail').read(), end='') if status == 404: realprint ('Status: 404 Not Found') -elif status == 302: - realprint ('Status: 302 Found') - realprint ('Location: '+location+'\n') - exit() realprint ('Content-Type: text/html;charset=utf-8\n') realprint (outputBuffer, end='')