diff --git a/plugins/gridfs/gridfs.cc b/plugins/gridfs/gridfs.cc index 095593fd..f2fdcfd9 100644 --- a/plugins/gridfs/gridfs.cc +++ b/plugins/gridfs/gridfs.cc @@ -1,3 +1,7 @@ +#include +#include +#include + #include #include @@ -7,6 +11,7 @@ struct uwsgi_gridfs_mountpoint { char *mountpoint; uint16_t mountpoint_len; char *server; + char *replica; char *db; char *timeout_str; int timeout; @@ -23,6 +28,7 @@ struct uwsgi_gridfs_mountpoint { char *password; char *bucket; uint16_t bucket_len; + std::vector servers; }; struct uwsgi_gridfs { @@ -40,33 +46,44 @@ extern struct uwsgi_server uwsgi; extern struct uwsgi_plugin gridfs_plugin; static void uwsgi_gridfs_do(struct wsgi_request *wsgi_req, struct uwsgi_gridfs_mountpoint *ugm, char *itemname, int need_free) { - try { - mongo::scoped_ptr conn( mongo::ScopedDbConnection::getScopedDbConnection(ugm->server, ugm->timeout) ); + std::unique_ptr conn; + + if (ugm->replica) { + conn = std::unique_ptr (new mongo::DBClientReplicaSet(ugm->replica, ugm->servers, ugm->timeout)); + dynamic_cast(conn.get())->connect(); + } + else { + conn = std::unique_ptr (new mongo::DBClientConnection(true, 0, ugm->timeout)); + dynamic_cast(conn.get())->connect(ugm->server); + } + try { if (ugm->username && ugm->password) { std::string errmsg; - if ((*conn).conn().auth(ugm->db, ugm->username, ugm->password, errmsg)) { + if (!conn->auth(ugm->db, ugm->username, ugm->password, errmsg)) { uwsgi_log("[uwsgi-gridfs]: %s\n", errmsg.c_str()); - (*conn).done(); uwsgi_403(wsgi_req); return; } } - mongo::GridFS gridfs((*conn).conn(), ugm->db, ugm->bucket); - mongo::GridFile gfile = gridfs.findFile(itemname); + mongo::GridFS gridfs((*conn.get()), ugm->db); + mongo::GridFile gfile = gridfs.findFileByName(itemname); + if (need_free) { free(itemname); itemname = NULL; } + if (!gfile.exists()) { - (*conn).done(); uwsgi_404(wsgi_req); return; } + uwsgi_response_prepare_headers(wsgi_req, (char *)"200 OK", 6); // first get the content_type (if possibile) std::string filename = gfile.getFilename(); + if (!ugm->no_mime) { size_t mime_type_len = 0; char *mime_type = uwsgi_get_mime_type((char *)filename.c_str(), filename.length(), &mime_type_len); @@ -74,11 +91,13 @@ static void uwsgi_gridfs_do(struct wsgi_request *wsgi_req, struct uwsgi_gridfs_m uwsgi_response_add_content_type(wsgi_req, mime_type, mime_type_len); } } + if (ugm->orig_filename) { char *filename_header = uwsgi_concat3((char *)"inline; filename=\"", (char *)filename.c_str(), (char *)"\""); uwsgi_response_add_header(wsgi_req, (char *)"Content-Disposition", 19, filename_header, 19 + filename.length()); free(filename_header); } + uwsgi_response_add_content_length(wsgi_req, gfile.getContentLength()); char http_last_modified[49]; @@ -92,7 +111,7 @@ static void uwsgi_gridfs_do(struct wsgi_request *wsgi_req, struct uwsgi_gridfs_m char *etag = uwsgi_concat3((char *)"\"", (char *)g_md5.c_str(), (char *)"\""); uwsgi_response_add_header(wsgi_req, (char *)"ETag", 4, etag, 2+g_md5.length()); free(etag); - } + } } if (ugm->md5) { @@ -108,22 +127,20 @@ static void uwsgi_gridfs_do(struct wsgi_request *wsgi_req, struct uwsgi_gridfs_m int i; for(i=0;i