Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 99d68d5

Browse files
author
Kristina Chodorow
committed
allow https connections
1 parent 26ef013 commit 99d68d5

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ Sleepy.Mongoose is a REST interface for MongoDB.
44

55
## PREREQUISITES
66

7-
You need 3 things installed:
8-
97
1. MongoDB, which can be downloaded from
108
[http://www.mongodb.org](http://www.mongodb.org).
119

12-
2. Pymongo, the MongoDB python driver. Sleepy.Mongoose requires pymongo version
13-
1.4 or greater. You can install this with easy_install:
14-
15-
$ sudo easy_install pymongo
16-
17-
3. The Python JSON package, which can also be installed with easy_install:
10+
2. You'll need to install some Python packages, if you don't have them already.
11+
Sleepy.Mongoose uses Pymongo, the MongoDB python driver (version 1.4 or
12+
greater required). It also requires the JSON package and OpenSSL. You can
13+
install these with easy_install:
1814

19-
$ sudo easy_install python-json
15+
$ sudo easy_install pymongo python-json pyOpenSSL
2016

2117
Sleepy.Mongoose only works with Python 2.5 and higher.
2218

httpd.py

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
15+
from SocketServer import BaseServer
16+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
1617
from handlers import MongoHandler
18+
from OpenSSL import SSL
1719

18-
import os.path
20+
import os.path, socket
1921
import urlparse
2022
import cgi
2123
import getopt
@@ -27,7 +29,25 @@
2729
import simplejson as json
2830

2931

30-
class MongoServer(BaseHTTPRequestHandler):
32+
class MongoServer(HTTPServer):
33+
34+
pem = None
35+
36+
def __init__(self, server_address, HandlerClass):
37+
BaseServer.__init__(self, server_address, HandlerClass)
38+
ctx = SSL.Context(SSL.SSLv23_METHOD)
39+
40+
fpem = MongoServer.pem
41+
ctx.use_privatekey_file(fpem)
42+
ctx.use_certificate_file(fpem)
43+
44+
self.socket = SSL.Connection(ctx, socket.socket(self.address_family,
45+
self.socket_type))
46+
self.server_bind()
47+
self.server_activate()
48+
49+
50+
class MongoHTTPRequest(BaseHTTPRequestHandler):
3151

3252
mh = None
3353

@@ -80,10 +100,10 @@ def call_handler(self, uri, args):
80100
else:
81101
name = args.getvalue("name")
82102

83-
func = getattr(MongoServer.mh, func_name, None)
103+
func = getattr(MongoHTTPRequest.mh, func_name, None)
84104
if callable(func):
85105
self.send_response(200, 'OK')
86-
self.send_header('Content-type', MongoServer.mimetypes['json'])
106+
self.send_header('Content-type', MongoHTTPRequest.mimetypes['json'])
87107
self.end_headers()
88108

89109
func(args, self.wfile.write, name = name, db = db, collection = collection)
@@ -106,7 +126,7 @@ def process_uri(self, method):
106126
'CONTENT_TYPE':self.headers['Content-Type']})
107127
else:
108128
self.send_response(100, "Continue")
109-
self.send_header('Content-type', MongoServer.mimetypes['json'])
129+
self.send_header('Content-type', MongoHTTPRequest.mimetypes['json'])
110130
self.end_headers()
111131
self.wfile.write('{"ok" : 0, "errmsg" : "100-continue msgs not handled yet"}')
112132

@@ -132,12 +152,12 @@ def do_GET(self):
132152

133153
# serve up a plain file
134154
if len(type) != 0:
135-
if type in MongoServer.mimetypes and os.path.exists(MongoServer.docroot+uri):
155+
if type in MongoHTTPRequest.mimetypes and os.path.exists(MongoHTTPRequest.docroot+uri):
136156

137-
fh = open(MongoServer.docroot+uri, 'r')
157+
fh = open(MongoHTTPRequest.docroot+uri, 'r')
138158

139159
self.send_response(200, 'OK')
140-
self.send_header('Content-type', MongoServer.mimetypes[type])
160+
self.send_header('Content-type', MongoHTTPRequest.mimetypes[type])
141161
self.end_headers()
142162
self.wfile.write(fh.read())
143163

@@ -171,12 +191,16 @@ def serve_forever(port):
171191
print "\n================================="
172192
print "| MongoDB REST Server |"
173193
print "=================================\n"
174-
print "listening for connections on http://localhost:27080\n"
175-
176-
MongoServer.mh = MongoHandler()
177194

178-
server = HTTPServer(('', port), MongoServer)
195+
if MongoServer.pem == None:
196+
server = HTTPServer(('', port), MongoHTTPRequest)
197+
else:
198+
print "--------Secure Connection--------\n"
199+
server = MongoServer(('', port), MongoHTTPSRequest)
179200

201+
MongoHTTPRequest.mh = MongoHandler()
202+
203+
print "listening for connections on http://localhost:27080\n"
180204
try:
181205
server.serve_forever()
182206
except KeyboardInterrupt:
@@ -185,19 +209,34 @@ def serve_forever(port):
185209
print "\nGood bye!\n"
186210

187211

212+
class MongoHTTPSRequest(MongoHTTPRequest):
213+
def setup(self):
214+
self.connection = self.request
215+
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
216+
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
217+
218+
219+
def usage():
220+
print "python httpd.py [-d docroot/dir] [-s certificate.pem]"
221+
print "\t-d|--docroot\tlocation from which to load files"
222+
print "\t-s|--secure\tlocation of .pem file if ssl is desired"
223+
188224
if __name__ == "__main__":
225+
189226
try:
190-
opts, args = getopt.getopt(sys.argv[1:], "d:", ["docroot="])
227+
opts, args = getopt.getopt(sys.argv[1:], "d:s:", ["docroot=", "secure="])
191228

192229
for o, a in opts:
193230
if o == "-d" or o == "--docroot":
194231
if not a.endswith('/'):
195232
a = a+'/'
196-
MongoServer.docroot = a
233+
MongoHTTPRequest.docroot = a
234+
if o == "-s" or o == "--secure":
235+
MongoServer.pem = a
197236

198237
except getopt.GetoptError:
199238
print "error parsing cmd line args."
239+
usage()
200240
sys.exit(2)
201241

202-
MongoServer.serve_forever(27080)
203-
242+
MongoHTTPRequest.serve_forever(27080)

0 commit comments

Comments
 (0)