rest.py

Sun, 19 Apr 2015 20:04:05 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 19 Apr 2015 20:04:05 +0300
changeset 125
c44b1aa85257
parent 124
7b2cd8b1ba86
child 146
c17b82b1f573
permissions
-rw-r--r--

- added hex support to the calculator

124
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
1 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
2 Copyright 2014-2015 Teemu Piippo
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
3 All rights reserved.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
4
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
5 Redistribution and use in source and binary forms, with or without
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
6 modification, are permitted provided that the following conditions
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
7 are met:
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
8
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
9 1. Redistributions of source code must retain the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
10 notice, this list of conditions and the following disclaimer.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
12 notice, this list of conditions and the following disclaimer in the
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
13 documentation and/or other materials provided with the distribution.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
14 3. The name of the author may not be used to endorse or promote products
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
15 derived from this software without specific prior written permission.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
16
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
27 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 123
diff changeset
28
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 import ssl
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 import socket
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 import errno
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 import select
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 import asyncore
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 import base64
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 import re
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 import json
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 import urllib
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
38 import irc as Irc
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 from datetime import datetime, timedelta
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 from configfile import Config
97
55d7d9aea160 - link rest to hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 96
diff changeset
41 import hgpoll as HgPoll
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 g_credentials = None
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 g_portnumber = None
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 g_throttle = []
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46
120
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 112
diff changeset
47 valid_repos = ['torr_samaho/zandronum', 'torr_samaho/zandronum-stable',
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 112
diff changeset
48 'crimsondusk/zandronum-sandbox', 'crimsondusk/zandronum-sandbox-stable', 'crimsondusk/zfc9000', 'blzut3/doomseeker']
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 def is_throttled (address):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 i = 0
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 while i < len (g_throttle):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 if g_throttle[i][1] <= datetime.utcnow():
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
55 print 'Throttle of %s expired' % g_throttle[i][0]
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 item = g_throttle.pop (i) # expired
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
58 if item[0] == address:
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 return False # this address expired
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 continue
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
63 if g_throttle[i][0] == address:
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 return True # is throttled
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 i += 1
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 return False # not throttled
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 def throttle (address):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 tt = datetime.utcnow() + timedelta (0, 30)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
73 if address in g_throttle:
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 g_throttle[g_throttle.index (address)][1] = tt
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
75 else:
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 g_throttle.append ([address, tt])
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77
96
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 95
diff changeset
78 Irc.broadcast ('Throttling %s for 30 seconds' % address)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 def handle_rest_http (data, address):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 global g_credentials
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
82 displayaddress = address
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 authrex = re.compile (r'^authorization: Basic (.+)$')
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 payloadrex = re.compile (r'^payload=(.+)$')
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 authenticated = False
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 payload = ''
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 if not g_credentials:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 g_credentials = base64.b64encode (Config.get_node ('rest').get_value ('credentials'))
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 # Authenticate and find the payload
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 for line in data:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 match = authrex.match (line)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
95 if match and match.group (1) == g_credentials:
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 authenticated = True
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 continue
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 match = payloadrex.match (line)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 if match:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 payload = match.group (1)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 if not authenticated:
96
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 95
diff changeset
104 Irc.broadcast ('%s failed to authenticate' % address)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 throttle (address)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 return
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 jsonstring = urllib.unquote_plus (payload).decode ('utf-8')
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 try:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 jsondata = json.loads (jsonstring)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 repodata = jsondata['repository']
99
147d78eea80b - more fixing
Teemu Piippo <crimsondusk64@gmail.com>
parents: 97
diff changeset
113 repo_fullname = '%s/%s' % (repodata['owner'], repodata['name'])
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114
120
9880bb697149 - added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents: 112
diff changeset
115 if repo_fullname.lower() not in valid_repos:
99
147d78eea80b - more fixing
Teemu Piippo <crimsondusk64@gmail.com>
parents: 97
diff changeset
116 raise ValueError ('unknown repository %s' % repo_fullname)
97
55d7d9aea160 - link rest to hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 96
diff changeset
117
55d7d9aea160 - link rest to hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 96
diff changeset
118 commit_data = []
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 if 'commits' in jsondata:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 for commit in jsondata['commits']:
97
55d7d9aea160 - link rest to hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 96
diff changeset
122 commit_data.append ([commit['node'], commit['message']])
55d7d9aea160 - link rest to hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 96
diff changeset
123
55d7d9aea160 - link rest to hg
Teemu Piippo <crimsondusk64@gmail.com>
parents: 96
diff changeset
124 if len (commit_data) > 0:
99
147d78eea80b - more fixing
Teemu Piippo <crimsondusk64@gmail.com>
parents: 97
diff changeset
125 HgPoll.process_new_commits (repodata['name'], commit_data)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 except Exception as e:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 Irc.broadcast ('%s provided bad JSON: %s' % (address, e))
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 try:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 with open ('rejected_json.txt', 'w') as fp:
123
aeb0d0788869 - added commits.db and mercurial support restructure
Teemu Piippo <crimsondusk64@gmail.com>
parents: 121
diff changeset
131 fp.write (str (e))
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 fp.write (jsonstring)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 Irc.broadcast ('bad json written to rejected_json.txt')
121
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
134 except Exception as e:
ac07779f788d - reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents: 120
diff changeset
135 Irc.broadcast ('failed to log json because %s' % e)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 pass
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 throttle (address)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 return
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 class RESTConnection (asyncore.dispatcher):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 httpdata = ''
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 writebuffer = ''
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
144 address = None
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 def __init__ (self, conn, address):
96
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 95
diff changeset
147 asyncore.dispatcher.__init__ (self, conn)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 self.socket = ssl.wrap_socket (conn, server_side=True, keyfile='key.pem',
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 certfile='cert.pem', do_handshake_on_connect=False)
96
24f0b13e87d3 - fixed REST
Teemu Piippo <crimsondusk64@gmail.com>
parents: 95
diff changeset
150 self.socket.settimeout (3)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 self.address = address
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 while True:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 try:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155 self.socket.do_handshake()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 break
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 except ssl.SSLError, err:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 if err.args[0] == ssl.SSL_ERROR_WANT_READ:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 select.select([self.socket], [], [])
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 select.select([], [self.socket], [])
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 else:
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
163 Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], err))
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
164 throttle (self.address[0])
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 self.close()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 return
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 def write (self, msg):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 self.writebuffer += msg
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171 def readable (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 return True
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 def writable (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 return self.writebuffer
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 def handle_close (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 self.finish()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 def handle_read (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 while 1:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
182 try:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183 data = self.recv (4096)
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
184 except:
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 # EOF
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 self.finish()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 return
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 self.httpdata += data.replace ('\r', '')
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 def finish (self):
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
192 handle_rest_http (self.httpdata.split ('\n'), self.address[0])
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 self.close()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 def handle_write (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 self.send (self.writebuffer)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 self.writebuffer=''
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 def handle_error (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200 raise
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
202 class RESTServer (asyncore.dispatcher):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 def __init__ (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204 global g_portnumber
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 if g_portnumber == None:
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 g_portnumber = Config.get_node ('rest').get_value ('port')
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 asyncore.dispatcher.__init__ (self)
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210 self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
211 self.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
212 self.bind (('', g_portnumber))
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213 self.listen (5)
93
Teemu Piippo <crimsondusk64@gmail.com>
parents: 92
diff changeset
214 print 'REST server initialized'
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216 def handle_accept (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 sock, address = self.accept()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 if is_throttled (address):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 # throttled
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
221 sock.close()
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 return
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
223
94
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
224 conn = RESTConnection (sock, address)
2c93636202a5 - argrrgh
Teemu Piippo <crimsondusk64@gmail.com>
parents: 93
diff changeset
225
92
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 def handle_error (self):
da291d9426ea - first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227 return

mercurial