Skip to content

Commit aadcbd7

Browse files
author
Ivan Genchev
committed
Fixed mdirolf#58 - Do not send the file on 304, the browser does not expect that...
1 parent 7622552 commit aadcbd7

File tree

1 file changed

+90
-88
lines changed

1 file changed

+90
-88
lines changed

ngx_http_gridfs_module.c

+90-88
Original file line numberDiff line numberDiff line change
@@ -849,109 +849,111 @@ static ngx_int_t ngx_http_gridfs_handler(ngx_http_request_t* request) {
849849
ngx_http_send_header(request);
850850

851851
// ---------- SEND THE BODY ---------- //
852+
// Do not send the file on 304 (not modified) - fix for #58
853+
if (request->headers_out.status != NGX_HTTP_NOT_MODIFIED) {
854+
/* Empty file */
855+
if (numchunks == 0) {
856+
/* Allocate space for the response buffer */
857+
buffer = ngx_pcalloc(request->pool, sizeof(ngx_buf_t));
858+
if (buffer == NULL) {
859+
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
860+
"Failed to allocate response buffer");
861+
gridfile_destroy(&gfile);
862+
gridfs_destroy(&gfs);
863+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
864+
}
865+
866+
buffer->pos = NULL;
867+
buffer->last = NULL;
868+
buffer->memory = 1;
869+
buffer->last_buf = 1;
870+
out.buf = buffer;
871+
out.next = NULL;
852872

853-
/* Empty file */
854-
if (numchunks == 0) {
855-
/* Allocate space for the response buffer */
856-
buffer = ngx_pcalloc(request->pool, sizeof(ngx_buf_t));
857-
if (buffer == NULL) {
858-
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
859-
"Failed to allocate response buffer");
860873
gridfile_destroy(&gfile);
861874
gridfs_destroy(&gfs);
862-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
863-
}
864-
865-
buffer->pos = NULL;
866-
buffer->last = NULL;
867-
buffer->memory = 1;
868-
buffer->last_buf = 1;
869-
out.buf = buffer;
870-
out.next = NULL;
871875

872-
gridfile_destroy(&gfile);
873-
gridfs_destroy(&gfs);
874-
875-
return ngx_http_output_filter(request, &out);
876-
}
876+
return ngx_http_output_filter(request, &out);
877+
}
877878

878-
cursors = (mongo_cursor **)ngx_pcalloc(request->pool, sizeof(mongo_cursor *) * numchunks);
879-
if (cursors == NULL) {
880-
gridfile_destroy(&gfile);
881-
gridfs_destroy(&gfs);
882-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
883-
}
879+
cursors = (mongo_cursor **)ngx_pcalloc(request->pool, sizeof(mongo_cursor *) * numchunks);
880+
if (cursors == NULL) {
881+
gridfile_destroy(&gfile);
882+
gridfs_destroy(&gfs);
883+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
884+
}
884885

885-
ngx_memzero( cursors, sizeof(mongo_cursor *) * numchunks);
886-
887-
/* Hook in the cleanup function */
888-
gridfs_cln = ngx_pool_cleanup_add(request->pool, sizeof(ngx_http_gridfs_cleanup_t));
889-
if (gridfs_cln == NULL) {
890-
gridfile_destroy(&gfile);
891-
gridfs_destroy(&gfs);
892-
return NGX_HTTP_INTERNAL_SERVER_ERROR;
893-
}
894-
gridfs_cln->handler = ngx_http_gridfs_cleanup;
895-
gridfs_clndata = gridfs_cln->data;
896-
gridfs_clndata->cursors = cursors;
897-
gridfs_clndata->numchunks = numchunks;
898-
899-
/* Read and serve chunk by chunk */
900-
for (i = 0; i < numchunks; i++) {
901-
902-
/* Allocate space for the response buffer */
903-
buffer = ngx_pcalloc(request->pool, sizeof(ngx_buf_t));
904-
if (buffer == NULL) {
905-
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
906-
"Failed to allocate response buffer");
886+
ngx_memzero( cursors, sizeof(mongo_cursor *) * numchunks);
887+
888+
/* Hook in the cleanup function */
889+
gridfs_cln = ngx_pool_cleanup_add(request->pool, sizeof(ngx_http_gridfs_cleanup_t));
890+
if (gridfs_cln == NULL) {
907891
gridfile_destroy(&gfile);
908892
gridfs_destroy(&gfs);
909893
return NGX_HTTP_INTERNAL_SERVER_ERROR;
910894
}
895+
gridfs_cln->handler = ngx_http_gridfs_cleanup;
896+
gridfs_clndata = gridfs_cln->data;
897+
gridfs_clndata->cursors = cursors;
898+
gridfs_clndata->numchunks = numchunks;
899+
900+
/* Read and serve chunk by chunk */
901+
for (i = 0; i < numchunks; i++) {
902+
903+
/* Allocate space for the response buffer */
904+
buffer = ngx_pcalloc(request->pool, sizeof(ngx_buf_t));
905+
if (buffer == NULL) {
906+
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
907+
"Failed to allocate response buffer");
908+
gridfile_destroy(&gfile);
909+
gridfs_destroy(&gfs);
910+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
911+
}
911912

912-
/* Fetch the chunk from mongo */
913-
do {
914-
e = FALSE;
915-
cursors[i] = gridfile_get_chunks(&gfile, i, 1);
916-
if (!(cursors[i] && mongo_cursor_next(cursors[i]) == MONGO_OK)) {
917-
e = TRUE; ecounter++;
918-
if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST
919-
|| ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
920-
|| ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) {
921-
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
922-
"Mongo connection dropped, could not reconnect");
923-
if(&mongo_conn->conn.connected) { mongo_disconnect(&mongo_conn->conn); }
924-
gridfile_destroy(&gfile);
925-
gridfs_destroy(&gfs);
926-
return NGX_HTTP_SERVICE_UNAVAILABLE;
913+
/* Fetch the chunk from mongo */
914+
do {
915+
e = FALSE;
916+
cursors[i] = gridfile_get_chunks(&gfile, i, 1);
917+
if (!(cursors[i] && mongo_cursor_next(cursors[i]) == MONGO_OK)) {
918+
e = TRUE; ecounter++;
919+
if (ecounter > MONGO_MAX_RETRIES_PER_REQUEST
920+
|| ngx_http_mongo_reconnect(request->connection->log, mongo_conn) == NGX_ERROR
921+
|| ngx_http_mongo_reauth(request->connection->log, mongo_conn) == NGX_ERROR) {
922+
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
923+
"Mongo connection dropped, could not reconnect");
924+
if(&mongo_conn->conn.connected) { mongo_disconnect(&mongo_conn->conn); }
925+
gridfile_destroy(&gfile);
926+
gridfs_destroy(&gfs);
927+
return NGX_HTTP_SERVICE_UNAVAILABLE;
928+
}
927929
}
930+
} while (e);
931+
932+
chunk = cursors[i]->current;
933+
bson_find(&it, &chunk, "data");
934+
chunk_len = bson_iterator_bin_len( &it );
935+
chunk_data = bson_iterator_bin_data( &it );
936+
937+
/* Set up the buffer chain */
938+
buffer->pos = (u_char*)chunk_data;
939+
buffer->last = (u_char*)chunk_data + chunk_len;
940+
buffer->memory = 1;
941+
buffer->last_buf = (i == numchunks-1);
942+
out.buf = buffer;
943+
out.next = NULL;
944+
945+
/* Serve the Chunk */
946+
rc = ngx_http_output_filter(request, &out);
947+
948+
/* TODO: More Codes to Catch? */
949+
if (rc == NGX_ERROR) {
950+
gridfile_destroy(&gfile);
951+
gridfs_destroy(&gfs);
952+
return NGX_ERROR;
928953
}
929-
} while (e);
930-
931-
chunk = cursors[i]->current;
932-
bson_find(&it, &chunk, "data");
933-
chunk_len = bson_iterator_bin_len( &it );
934-
chunk_data = bson_iterator_bin_data( &it );
935-
936-
/* Set up the buffer chain */
937-
buffer->pos = (u_char*)chunk_data;
938-
buffer->last = (u_char*)chunk_data + chunk_len;
939-
buffer->memory = 1;
940-
buffer->last_buf = (i == numchunks-1);
941-
out.buf = buffer;
942-
out.next = NULL;
943-
944-
/* Serve the Chunk */
945-
rc = ngx_http_output_filter(request, &out);
946-
947-
/* TODO: More Codes to Catch? */
948-
if (rc == NGX_ERROR) {
949-
gridfile_destroy(&gfile);
950-
gridfs_destroy(&gfs);
951-
return NGX_ERROR;
952954
}
953955
}
954-
956+
955957
gridfile_destroy(&gfile);
956958
gridfs_destroy(&gfs);
957959

0 commit comments

Comments
 (0)