@@ -72,6 +72,7 @@ class MongoHTTPRequest(BaseHTTPRequestHandler):
72
72
73
73
docroot = "."
74
74
mongos = []
75
+ response_headers = []
75
76
76
77
def _parse_call (self , uri ):
77
78
"""
@@ -80,7 +81,6 @@ def _parse_call(self, uri):
80
81
81
82
returns the database, collection, and action
82
83
"""
83
-
84
84
parts = uri .split ('/' )
85
85
86
86
# operations always start with _
@@ -114,6 +114,8 @@ def call_handler(self, uri, args):
114
114
if callable (func ):
115
115
self .send_response (200 , 'OK' )
116
116
self .send_header ('Content-type' , MongoHTTPRequest .mimetypes ['json' ])
117
+ for header in self .response_headers :
118
+ self .send_header (header [0 ], header [1 ])
117
119
self .end_headers ()
118
120
119
121
func (args , self .wfile .write , name = name , db = db , collection = collection )
@@ -137,6 +139,8 @@ def process_uri(self, method):
137
139
else :
138
140
self .send_response (100 , "Continue" )
139
141
self .send_header ('Content-type' , MongoHTTPRequest .mimetypes ['json' ])
142
+ for header in self .response_headers :
143
+ self .send_header (header [0 ], header [1 ])
140
144
self .end_headers ()
141
145
self .wfile .write ('{"ok" : 0, "errmsg" : "100-continue msgs not handled yet"}' )
142
146
@@ -168,6 +172,8 @@ def do_GET(self):
168
172
169
173
self .send_response (200 , 'OK' )
170
174
self .send_header ('Content-type' , MongoHTTPRequest .mimetypes [type ])
175
+ for header in self .response_headers :
176
+ self .send_header (header [0 ], header [1 ])
171
177
self .end_headers ()
172
178
self .wfile .write (fh .read ())
173
179
@@ -195,7 +201,6 @@ def do_POST(self):
195
201
return
196
202
self .call_handler (uri , args )
197
203
198
-
199
204
@staticmethod
200
205
def serve_forever (port ):
201
206
print "\n ================================="
@@ -234,15 +239,17 @@ def setup(self):
234
239
235
240
236
241
def usage ():
237
- print "python httpd.py [-d docroot/dir] [-s certificate.pem] [-m list,of,mongods]"
242
+ print "python httpd.py [-x] [-d docroot/dir] [-s certificate.pem] [-m list,of,mongods]"
243
+ print "\t -x|--xorigin\t Allow cross-origin http requests"
238
244
print "\t -d|--docroot\t location from which to load files"
239
245
print "\t -s|--secure\t location of .pem file if ssl is desired"
240
246
print "\t -m|--mongos\t comma-separated list of mongo servers to connect to"
241
247
242
248
if __name__ == "__main__" :
243
249
244
250
try :
245
- opts , args = getopt .getopt (sys .argv [1 :], "d:s:m:" , ["docroot=" , "secure=" , "mongos=" ])
251
+ opts , args = getopt .getopt (sys .argv [1 :], "xd:s:m:" , ["xorigin" , "docroot=" ,
252
+ "secure=" , "mongos=" ])
246
253
247
254
for o , a in opts :
248
255
if o == "-d" or o == "--docroot" :
@@ -253,6 +260,8 @@ def usage():
253
260
MongoServer .pem = a
254
261
if o == "-m" or o == "--mongos" :
255
262
MongoHTTPRequest .mongos = a .split (',' )
263
+ if o == "-x" or o == "--xorigin" :
264
+ MongoHTTPRequest .response_headers .append (("Access-Control-Allow-Origin" ,"*" ))
256
265
257
266
except getopt .GetoptError :
258
267
print "error parsing cmd line args."
0 commit comments