Tue, 18 Aug 2015 14:38:54 +0300
Use python3 in the shebang
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 | |
146
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
29 | from __future__ import print_function |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | import ssl |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | import socket |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | import errno |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | import select |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | import asyncore |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | import base64 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | import re |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | import json |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | import urllib |
94 | 39 | import irc as Irc |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | from datetime import datetime, timedelta |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | from configfile import Config |
97 | 42 | import hgpoll as HgPoll |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | g_credentials = None |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | g_portnumber = None |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | g_throttle = [] |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | |
146
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
48 | # TODO: get rid of this array |
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
|
49 | valid_repos = ['torr_samaho/zandronum', 'torr_samaho/zandronum-stable', |
146
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
50 | 'crimsondusk/zandronum-sandbox', 'crimsondusk/zandronum-sandbox-stable', |
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
51 | 'crimsondusk/zfc9000', 'blzut3/doomseeker'] |
92
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 | def is_throttled (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | i = 0 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | while i < len (g_throttle): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | if g_throttle[i][1] <= datetime.utcnow(): |
146
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
58 | print ('Throttle of %s expired' % g_throttle[i][0]) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | item = g_throttle.pop (i) # expired |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | |
94 | 61 | if item[0] == address: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | return False # this address expired |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | continue |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | |
94 | 66 | if g_throttle[i][0] == address: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | return True # is throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | i += 1 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | return False # not throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | def throttle (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | tt = datetime.utcnow() + timedelta (0, 30) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | |
121
ac07779f788d
- reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents:
120
diff
changeset
|
76 | if address in g_throttle: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | 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
|
78 | else: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | g_throttle.append ([address, tt]) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | |
96 | 81 | Irc.broadcast ('Throttling %s for 30 seconds' % address) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | def handle_rest_http (data, address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | global g_credentials |
94 | 85 | displayaddress = address |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | authrex = re.compile (r'^authorization: Basic (.+)$') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | payloadrex = re.compile (r'^payload=(.+)$') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | authenticated = False |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | payload = '' |
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 | if not g_credentials: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | g_credentials = base64.b64encode (Config.get_node ('rest').get_value ('credentials')) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | # Authenticate and find the payload |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | for line in data: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | match = authrex.match (line) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | |
94 | 98 | if match and match.group (1) == g_credentials: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | authenticated = True |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | continue |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | match = payloadrex.match (line) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | if match: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | payload = match.group (1) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | if not authenticated: |
96 | 107 | Irc.broadcast ('%s failed to authenticate' % address) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | throttle (address) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | jsonstring = urllib.unquote_plus (payload).decode ('utf-8') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | jsondata = json.loads (jsonstring) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | repodata = jsondata['repository'] |
99 | 116 | repo_fullname = '%s/%s' % (repodata['owner'], repodata['name']) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | |
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
|
118 | if repo_fullname.lower() not in valid_repos: |
99 | 119 | raise ValueError ('unknown repository %s' % repo_fullname) |
97 | 120 | |
121 | commit_data = [] | |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | if 'commits' in jsondata: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | for commit in jsondata['commits']: |
97 | 125 | commit_data.append ([commit['node'], commit['message']]) |
126 | ||
127 | if len (commit_data) > 0: | |
99 | 128 | HgPoll.process_new_commits (repodata['name'], commit_data) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | except Exception as e: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | Irc.broadcast ('%s provided bad JSON: %s' % (address, e)) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | 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
|
134 | fp.write (str (e)) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | fp.write (jsonstring) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | 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
|
137 | except Exception as e: |
ac07779f788d
- reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents:
120
diff
changeset
|
138 | Irc.broadcast ('failed to log json because %s' % e) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | pass |
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 | throttle (address) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | class RESTConnection (asyncore.dispatcher): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | httpdata = '' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | writebuffer = '' |
94 | 147 | address = None |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | def __init__ (self, conn, address): |
96 | 150 | asyncore.dispatcher.__init__ (self, conn) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | 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
|
152 | certfile='cert.pem', do_handshake_on_connect=False) |
96 | 153 | self.socket.settimeout (3) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | self.address = address |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | while True: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | self.socket.do_handshake() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | break |
146
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
160 | except ssl.SSLError as err: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | select.select([self.socket], [], []) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | select.select([], [self.socket], []) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | else: |
94 | 166 | Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], err)) |
167 | throttle (self.address[0]) | |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | self.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | return |
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 write (self, msg): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | self.writebuffer += msg |
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 readable (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | return True |
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 writable (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
178 | return self.writebuffer |
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_close (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
181 | self.finish() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
182 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | def handle_read (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
184 | while 1: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | data = self.recv (4096) |
94 | 187 | except: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | # EOF |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | self.finish() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | self.httpdata += data.replace ('\r', '') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | def finish (self): |
152 | 195 | handle_rest_http (self.httpdata.splitlines(), self.address[0]) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | self.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | def handle_write (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | self.send (self.writebuffer) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | self.writebuffer='' |
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 | def handle_error (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
203 | raise |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
204 | |
152 | 205 | class RestServer (asyncore.dispatcher): |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
206 | def __init__ (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
207 | global g_portnumber |
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 | if g_portnumber == None: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
210 | g_portnumber = Config.get_node ('rest').get_value ('port') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
211 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
212 | asyncore.dispatcher.__init__ (self) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
213 | self.create_socket (socket.AF_INET, socket.SOCK_STREAM) |
94 | 214 | self.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
215 | self.bind (('', g_portnumber)) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
216 | self.listen (5) |
146
c17b82b1f573
Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents:
124
diff
changeset
|
217 | print ('REST server initialized') |
92
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 | def handle_accept (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
220 | sock, address = self.accept() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
221 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
222 | if is_throttled (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
223 | # throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
224 | sock.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
225 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
226 | |
94 | 227 | conn = RESTConnection (sock, address) |
228 | ||
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
229 | def handle_error (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
230 | return |