comparison src/http/ngx_http_request_body.c @ 7913:185c86b830ef

Request body: introduced rb->last_saved flag. It indicates that the last buffer was received by the save filter, and can be used to check this at higher levels. To be used in the following changes.
author Maxim Dounin <mdounin@mdounin.ru>
date Sun, 29 Aug 2021 22:21:03 +0300
parents 96e09beaa2cf
children 9cf043a5d9ca
comparison
equal deleted inserted replaced
7912:96e09beaa2cf 7913:185c86b830ef
67 * rb->buf = NULL; 67 * rb->buf = NULL;
68 * rb->free = NULL; 68 * rb->free = NULL;
69 * rb->busy = NULL; 69 * rb->busy = NULL;
70 * rb->chunked = NULL; 70 * rb->chunked = NULL;
71 * rb->received = 0; 71 * rb->received = 0;
72 * rb->last_saved = 0;
72 */ 73 */
73 74
74 rb->rest = -1; 75 rb->rest = -1;
75 rb->post_handler = post_handler; 76 rb->post_handler = post_handler;
76 77
939 ngx_chain_t *cl, *tl, *out, **ll; 940 ngx_chain_t *cl, *tl, *out, **ll;
940 ngx_http_request_body_t *rb; 941 ngx_http_request_body_t *rb;
941 942
942 rb = r->request_body; 943 rb = r->request_body;
943 944
945 out = NULL;
946 ll = &out;
947
944 if (rb->rest == -1) { 948 if (rb->rest == -1) {
945 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 949 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
946 "http request body content length filter"); 950 "http request body content length filter");
947 951
948 rb->rest = r->headers_in.content_length_n; 952 rb->rest = r->headers_in.content_length_n;
949 } 953
950 954 if (rb->rest == 0) {
951 out = NULL; 955
952 ll = &out; 956 tl = ngx_chain_get_free_buf(r->pool, &rb->free);
957 if (tl == NULL) {
958 return NGX_HTTP_INTERNAL_SERVER_ERROR;
959 }
960
961 b = tl->buf;
962
963 ngx_memzero(b, sizeof(ngx_buf_t));
964
965 b->last_buf = 1;
966
967 *ll = tl;
968 ll = &tl->next;
969 }
970 }
953 971
954 for (cl = in; cl; cl = cl->next) { 972 for (cl = in; cl; cl = cl->next) {
955 973
956 if (rb->rest == 0) { 974 if (rb->rest == 0) {
957 break; 975 break;
1011 ngx_http_core_loc_conf_t *clcf; 1029 ngx_http_core_loc_conf_t *clcf;
1012 ngx_http_core_srv_conf_t *cscf; 1030 ngx_http_core_srv_conf_t *cscf;
1013 1031
1014 rb = r->request_body; 1032 rb = r->request_body;
1015 1033
1034 out = NULL;
1035 ll = &out;
1036
1016 if (rb->rest == -1) { 1037 if (rb->rest == -1) {
1017 1038
1018 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 1039 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1019 "http request body chunked filter"); 1040 "http request body chunked filter");
1020 1041
1026 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); 1047 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1027 1048
1028 r->headers_in.content_length_n = 0; 1049 r->headers_in.content_length_n = 0;
1029 rb->rest = cscf->large_client_header_buffers.size; 1050 rb->rest = cscf->large_client_header_buffers.size;
1030 } 1051 }
1031
1032 out = NULL;
1033 ll = &out;
1034 1052
1035 for (cl = in; cl; cl = cl->next) { 1053 for (cl = in; cl; cl = cl->next) {
1036 1054
1037 b = NULL; 1055 b = NULL;
1038 1056
1186 1204
1187 ngx_int_t 1205 ngx_int_t
1188 ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in) 1206 ngx_http_request_body_save_filter(ngx_http_request_t *r, ngx_chain_t *in)
1189 { 1207 {
1190 ngx_buf_t *b; 1208 ngx_buf_t *b;
1191 ngx_chain_t *cl; 1209 ngx_chain_t *cl, *tl, **ll;
1192 ngx_http_request_body_t *rb; 1210 ngx_http_request_body_t *rb;
1193 1211
1194 rb = r->request_body; 1212 rb = r->request_body;
1195 1213
1196 #if (NGX_DEBUG) 1214 ll = &rb->bufs;
1215
1216 for (cl = rb->bufs; cl; cl = cl->next) {
1197 1217
1198 #if 0 1218 #if 0
1199 for (cl = rb->bufs; cl; cl = cl->next) {
1200 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0, 1219 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
1201 "http body old buf t:%d f:%d %p, pos %p, size: %z " 1220 "http body old buf t:%d f:%d %p, pos %p, size: %z "
1202 "file: %O, size: %O", 1221 "file: %O, size: %O",
1203 cl->buf->temporary, cl->buf->in_file, 1222 cl->buf->temporary, cl->buf->in_file,
1204 cl->buf->start, cl->buf->pos, 1223 cl->buf->start, cl->buf->pos,
1205 cl->buf->last - cl->buf->pos, 1224 cl->buf->last - cl->buf->pos,
1206 cl->buf->file_pos, 1225 cl->buf->file_pos,
1207 cl->buf->file_last - cl->buf->file_pos); 1226 cl->buf->file_last - cl->buf->file_pos);
1208 }
1209 #endif 1227 #endif
1210 1228
1229 ll = &cl->next;
1230 }
1231
1211 for (cl = in; cl; cl = cl->next) { 1232 for (cl = in; cl; cl = cl->next) {
1233
1212 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0, 1234 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
1213 "http body new buf t:%d f:%d %p, pos %p, size: %z " 1235 "http body new buf t:%d f:%d %p, pos %p, size: %z "
1214 "file: %O, size: %O", 1236 "file: %O, size: %O",
1215 cl->buf->temporary, cl->buf->in_file, 1237 cl->buf->temporary, cl->buf->in_file,
1216 cl->buf->start, cl->buf->pos, 1238 cl->buf->start, cl->buf->pos,
1217 cl->buf->last - cl->buf->pos, 1239 cl->buf->last - cl->buf->pos,
1218 cl->buf->file_pos, 1240 cl->buf->file_pos,
1219 cl->buf->file_last - cl->buf->file_pos); 1241 cl->buf->file_last - cl->buf->file_pos);
1220 } 1242
1221 1243 if (cl->buf->last_buf) {
1222 #endif 1244
1223 1245 if (rb->last_saved) {
1224 /* TODO: coalesce neighbouring buffers */ 1246 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1225 1247 "duplicate last buf in save filter");
1226 if (ngx_chain_add_copy(r->pool, &rb->bufs, in) != NGX_OK) { 1248 *ll = NULL;
1227 return NGX_HTTP_INTERNAL_SERVER_ERROR; 1249 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1228 } 1250 }
1251
1252 rb->last_saved = 1;
1253 }
1254
1255 tl = ngx_alloc_chain_link(r->pool);
1256 if (tl == NULL) {
1257 *ll = NULL;
1258 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1259 }
1260
1261 tl->buf = cl->buf;
1262 *ll = tl;
1263 ll = &tl->next;
1264 }
1265
1266 *ll = NULL;
1229 1267
1230 if (r->request_body_no_buffering) { 1268 if (r->request_body_no_buffering) {
1231 return NGX_OK; 1269 return NGX_OK;
1232 } 1270 }
1233 1271