[/pygopherd/head: changeset 58 jgoerzen**20020327054147 HTTP support now nominally working. ] { hunk ./conf/pygopherd.conf 80 -protocols = [gopherp.GopherPlusProtocol, rfc1436.GopherProtocol] +protocols = [http.HTTPProtocol, + gopherp.GopherPlusProtocol, rfc1436.GopherProtocol] hunk ./conf/pygopherd.conf 84 - hunk ./pygopherd/handlers/dir.py 31 + startstr = self.protocol.renderdirstart(self.entry) + if (startstr): + wfile.write(startstr) + hunk ./pygopherd/handlers/dir.py 44 + + endstr = self.protocol.renderdirend(self.entry) + if (endstr): + wfile.write(endstr) + hunk ./pygopherd/protocols/base.py 75 + def renderdirstart(self, entry): + """Renders the start of a directory. Most protocols will not need + this. Exception might be HTML. Returns None if not needed.""" + return None + + def renderdirend(self, entry): + """Likewise for the end of a directory.""" + return None + hunk ./pygopherd/protocols/http.py 5 -import cgi +import cgi, GopherExceptions hunk ./pygopherd/protocols/http.py 7 -class GopherProtocol(protocols.base.BaseGopherProtocol): +class HTTPProtocol(protocols.base.BaseGopherProtocol): hunk ./pygopherd/protocols/http.py 10 - return len(self.requestparts == 3) and \ + print 'len', len(self.requestparts) + print self.requestparts[0] + return len(self.requestparts) == 3 and \ hunk ./pygopherd/protocols/http.py 14 - self.requestparts[0:5] == 'HTTP/' + self.requestparts[2][0:5] == 'HTTP/' hunk ./pygopherd/protocols/http.py 25 - # Use these in renderobjinfo -- it's used if we're displaying a dir. - - self.htmlstarted = 0 - self.htmlended = 1 - hunk ./pygopherd/protocols/http.py 26 + print 'HTTP: getting handler for', self.selector hunk ./pygopherd/protocols/http.py 35 - mimetype = self.getmimetype() + mimetype = self.entry.getmimetype() hunk ./pygopherd/protocols/http.py 43 - if not self.htmlended: - self.endhtml() hunk ./pygopherd/protocols/http.py 49 - if not self.htmlstarted: - self.starthtml() - self.htmlstarted = 1 hunk ./pygopherd/protocols/http.py 50 - #url = 'http:// - #if entry.gettype != 'i': - # retstr += '' % urllib.quote("http + url = None + # Decision time.... + if (not entry.gethost()) and (not entry.getport()): + # It's a link to our own server. Make it as such. (relative) + url = entry.getselector() + else: + # Link to a different server. Make it a gopher URL. + url = 'gopher://%s:%d/%s%s' % \ + (entry.gethost(self.server.server_name), + entry.getport(70), + entry.gettype('0'), + entry.getselector()) + if re.match('(/|)URL:', entry.getselector()): + # It's a plain URL. Make it that. + url = re.match('(/|)URL:(.+)$', entry.getselector()).group(1) + + # OK. Render. + + retstr += " " + if entry.gettype() != 'i': + retstr += '' % urllib.quote(url) + retstr += "" + if entry.getname() != None: + retstr += cgi.escape(entry.getname()) + else: + retstr += cgi.escape(entry.getselector()) + retstr += "" + if entry.gettype() != 'i': + retstr += '' + retstr += '' + if entry.getmimetype(): + subtype = re.search('/.+$', entry.getmimetype()) + if subtype: + retstr += cgi.escape(subtype.group()[1:]) + retstr += '\n' + return retstr hunk ./pygopherd/protocols/http.py 87 - def starthtml(self): - self.wfile.write('\n') - self.wfile.write("""Gopher""") + def renderdirstart(self, entry): + retstr ='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">\n' + retstr += "<HTML><HEAD><TITLE>Gopher" hunk ./pygopherd/protocols/http.py 91 - self.wfile.write(": " + cgi.escape(self.entry.getname())) - self.wfile.write("""

Gopher""") + retstr += ": " + cgi.escape(self.entry.getname()) + retstr += "

Gopher" hunk ./pygopherd/protocols/http.py 94 - self.wfile.write(": " + cgi.escape(self.entry.getname())) - self.wfile.write('

') + retstr += ": " + cgi.escape(self.entry.getname()) + retstr += '
' + return retstr hunk ./pygopherd/protocols/http.py 98 - def endhtml(self): - self.wfile.write('
') + def renderdirend(self, entry): + return '' }