Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 134 lines (108 sloc) 3.662 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
import random

from twisted.internet import reactor, defer
from twisted.web import http

import simplejson

import web

class Request(http.Request):
    def process(self):
        self.content.seek(0, 0)
        env = {
          'REMOTE_ADDR': self.client.host,
          'REQUEST_METHOD': self.method,
          'PATH_INFO': self.path,
          'CONTENT_LENGTH': web.intget(self.getHeader('content-length'), 0),
          'wsgi.input': self.content
        }
        if '?' in self.uri:
            env['QUERY_STRING'] = self.uri.split('?', 1)[1]

        for k, v in self.received_headers.iteritems():
            env['HTTP_' + k.upper()] = v
        
        if self.path.startswith('/static/'):
            f = web.lstrips(self.path, '/static/')
            assert '/' not in f
            #@@@ big security hole
            self.write(file('static/' + f).read())
            return self.finish()

        web.webapi._load(env)
        web.ctx.trequest = self
        result = self.actualfunc()
        self.setResponseCode(int(web.ctx.status.split()[0]))
        for (h, v) in web.ctx.headers:
            self.setHeader(h, v)
        self.write(web.ctx.output)
        if not web.ctx.get('persist'):
            self.finish()

class Server(http.HTTPFactory):
    def __init__(self, func):
        self.func = func

    def buildProtocol(self, addr):
        """Generate a channel attached to this site.
"""
        channel = http.HTTPFactory.buildProtocol(self, addr)
        class MyRequest(Request):
            actualfunc = staticmethod(self.func)
        channel.requestFactory = MyRequest
        channel.site = self
        return channel

def runtwisted(func):
    reactor.listenTCP(8086, Server(func))
    reactor.run()

def newrun(inp, fvars):
    print "Running on http://0.0.0.0:8086/"
    runtwisted(web.webpyfunc(inp, fvars, False))

def iframe(url):
    return """
<iframe height="0" width="0" style="display: none" src="%s"/></iframe>
""" % url #("http://%s.ajaxpush.lh.theinfo.org:8086%s" % (random.random(), url))

class Feed:
    def __init__(self):
        self.sessions = []
    
    def subscribe(self):
        request = web.ctx.trequest
        self.sessions.append(request)
        request.connectionLost = lambda reason: self.sessions.remove(request)
        web.ctx.persist = True
    
    def publish(self, text):
        for x in self.sessions:
            x.write(text)

class JSFeed(Feed):
    def __init__(self, callback="callback"):
        Feed.__init__(self)
        self.callback = callback
        
    def publish(self, obj):
        web.debug("publishing")
        Feed.publish(self,
          '<script type="text/javascript">window.parent.%s(%s)</script>' % (self.callback, simplejson.dumps(obj) +
          " " * 2048))

if __name__ == "__main__":
    mfeed = JSFeed()

    urls = (
      '/', 'view',
      '/js', 'js',
      '/send', 'send'
    )

    class view:
        def GET(self):
            print """
<script type="text/javascript">
function callback(item) {
document.getElementById('content').innerHTML += "<p>" + item + "</p>";
}
</script>

<h2>Today's News</h2>

<div id="content"></div>

<h2>Contribute</h2>
<form method="post" action="/send">
<textarea name="text"></textarea>
<input type="submit" value="send" />
</form>
<iframe id="foo" height="0" width="0" style="display: none" src="/js"/></iframe>
"""
        
    class js:
        def GET(self):
            mfeed.subscribe()
    
    class send:
        def POST(self):
            mfeed.publish('<p>%s</p>' % web.input().text + (" " * 2048))
            web.seeother('/')
    
    newrun(urls, globals())
Something went wrong with that request. Please try again.