Selaa lähdekoodia

Consolidate duplicated code in get_tracking_id/get_tracking_ids_mul

pull/62/head
Benny Baumann 1 vuosi sitten
vanhempi
commit
5ab15754db
1 muutettua tiedostoa jossa 25 lisäystä ja 97 poistoa
  1. 25
    97
      ngx_http_uploadprogress_module.c

+ 25
- 97
ngx_http_uploadprogress_module.c Näytä tiedosto

311
 static ngx_array_t ngx_http_uploadprogress_global_templates;
311
 static ngx_array_t ngx_http_uploadprogress_global_templates;
312
 
312
 
313
 static ngx_str_t*
313
 static ngx_str_t*
314
-get_tracking_id(ngx_http_request_t * r)
314
+get_tracking_id_internal(ngx_http_request_t *r, ngx_str_t *header_ref, const char *caller, bool multi)
315
 {
315
 {
316
     u_char                          *p, *start_p;
316
     u_char                          *p, *start_p;
317
     ngx_uint_t                       i;
317
     ngx_uint_t                       i;
318
     ngx_list_part_t                 *part;
318
     ngx_list_part_t                 *part;
319
     ngx_table_elt_t                 *header;
319
     ngx_table_elt_t                 *header;
320
     ngx_str_t                       *ret, args;
320
     ngx_str_t                       *ret, args;
321
-    ngx_http_uploadprogress_conf_t  *upcf;
322
 
321
 
323
-    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
322
+    (void)multi;
324
 
323
 
325
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_id");
324
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: %s", caller);
326
 
325
 
327
     part = &r->headers_in.headers.part;
326
     part = &r->headers_in.headers.part;
328
     header = part->elts;
327
     header = part->elts;
340
         }
339
         }
341
 
340
 
342
         if (
341
         if (
343
-            header[i].key.len == upcf->header.len &&
344
-            ngx_strncasecmp(header[i].key.data, upcf->header.data, header[i].key.len) == 0
342
+            header[i].key.len == header_ref->len &&
343
+            ngx_strncasecmp(header[i].key.data, header_ref->data, header[i].key.len) == 0
345
         ) {
344
         ) {
346
             ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
345
             ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log );
347
             *ret = header[i].value;
346
             *ret = header[i].value;
348
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
347
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
349
-                "upload-progress: get_tracking_id found header: %V", ret);
348
+                "upload-progress: %s: found header: %V", caller, ret);
350
             return ret;
349
             return ret;
351
         }
350
         }
352
     }
351
     }
353
 
352
 
354
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
353
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
355
-        "upload-progress: get_tracking_id no header found");
354
+        "upload-progress: %s: no header found", caller);
356
 
355
 
357
     /* not found, check as a request arg */
356
     /* not found, check as a request arg */
358
     /* it is possible the request args have not been yet created (or already released) */
357
     /* it is possible the request args have not been yet created (or already released) */
361
 
360
 
362
     if (args.len && args.data) {
361
     if (args.len && args.data) {
363
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
362
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
364
-            "upload-progress: get_tracking_id no header found, args found");
363
+            "upload-progress: %s: no header found, args found", caller);
365
         i = 0;
364
         i = 0;
366
         p = args.data;
365
         p = args.data;
367
         do {
366
         do {
368
             ngx_uint_t len = args.len - (p - args.data);
367
             ngx_uint_t len = args.len - (p - args.data);
369
             if (
368
             if (
370
-                len >= (upcf->header.len + 1) &&
371
-                ngx_strncasecmp(p, upcf->header.data, upcf->header.len) == 0 &&
372
-                p[upcf->header.len] == '='
369
+                len >= (header_ref->len + 1) &&
370
+                ngx_strncasecmp(p, header_ref->data, header_ref->len) == 0 &&
371
+                p[header_ref->len] == '='
373
             ) {
372
             ) {
374
                 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
373
                 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
375
-                    "upload-progress: get_tracking_id found args: %s",p);
374
+                    "upload-progress: %s: found args: %s", caller, p);
376
                 i = 1;
375
                 i = 1;
377
                 break;
376
                 break;
378
             }
377
             }
380
         } while (len > 0);
379
         } while (len > 0);
381
 
380
 
382
         if (i) {
381
         if (i) {
383
-            start_p = p += upcf->header.len + 1;
382
+            start_p = p += header_ref->len + 1;
384
             while (p < args.data + args.len) {
383
             while (p < args.data + args.len) {
385
                 if (*(++p) == '&') {
384
                 if (*(++p) == '&') {
386
                     break;
385
                     break;
391
             ret->data = start_p;
390
             ret->data = start_p;
392
             ret->len = p - start_p;
391
             ret->len = p - start_p;
393
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
392
             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
394
-                "upload-progress: get_tracking_id found args: %V",ret);
393
+                "upload-progress: %s: found args: %V", caller, ret);
395
             return ret;
394
             return ret;
396
         }
395
         }
397
     }
396
     }
398
 
397
 
399
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
398
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
400
-        "upload-progress: get_tracking_id no id found");
399
+        "upload-progress: %s: no id found", caller);
401
     return NULL;
400
     return NULL;
402
 }
401
 }
403
 
402
 
404
 static ngx_str_t*
403
 static ngx_str_t*
405
-get_tracking_ids_mul(ngx_http_request_t * r)
404
+get_tracking_id(ngx_http_request_t *r)
406
 {
405
 {
407
-    u_char                          *p, *start_p;
408
-    ngx_uint_t                       i;
409
-    ngx_list_part_t                 *part;
410
-    ngx_table_elt_t                 *header;
411
-    ngx_str_t                       *ret, args;
412
-    ngx_http_uploadprogress_conf_t  *upcf;
406
+    ngx_http_uploadprogress_conf_t *upcf;
413
 
407
 
414
     upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
408
     upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
415
 
409
 
416
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "upload-progress: get_tracking_ids");
417
-
418
-    part = &r->headers_in.headers.part;
419
-    header = part->elts;
420
-
421
-    for (i = 0; /* void */; i++) {
422
-
423
-        if (i >= part->nelts) {
424
-            if (part->next == NULL) {
425
-                break;
426
-            }
427
-
428
-            part = part->next;
429
-            header = part->elts;
430
-            i = 0;
431
-        }
432
-
433
-        if (
434
-            header[i].key.len == upcf->header_mul.len &&
435
-            ngx_strncasecmp(header[i].key.data, upcf->header_mul.data, header[i].key.len) == 0
436
-        ) {
437
-            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
438
-            *ret = header[i].value;
439
-            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
440
-                "upload-progress: get_tracking_ids found header: %V", ret);
441
-            return ret;
442
-        }
443
-    }
444
-
445
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
446
-        "upload-progress: get_tracking_ids no header found");
447
-
448
-    /* not found, check as a request arg */
449
-    /* it is possible the request args have not been yet created (or already released) */
450
-    /* so let's try harder first from the request line */
451
-    args = r->args;
452
-
453
-    if (args.len && args.data) {
454
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
455
-            "upload-progress: get_tracking_id no header found, args found");
456
-        i = 0;
457
-        p = args.data;
458
-        do {
459
-            ngx_uint_t len = args.len - (p - args.data);
460
-            if (
461
-                len >= (upcf->header_mul.len + 1) &&
462
-                ngx_strncasecmp(p, upcf->header_mul.data, upcf->header_mul.len) == 0 &&
463
-                p[upcf->header_mul.len] == '='
464
-            ) {
465
-                ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
466
-                    "upload-progress: get_tracking_id found args: %s",p);
467
-                i = 1;
468
-                break;
469
-            }
470
-            p++;
471
-        } while (len > 0);
410
+    return get_tracking_id_internal(r, &upcf->header, "get_tracking_id", false);
411
+}
472
 
412
 
473
-        if (i) {
474
-            start_p = p += upcf->header_mul.len + 1;
475
-            while (p < args.data + args.len) {
476
-                if (*(++p) == '&') {
477
-                    break;
478
-                }
479
-            }
413
+static ngx_str_t*
414
+get_tracking_ids_mul(ngx_http_request_t *r)
415
+{
416
+    ngx_http_uploadprogress_conf_t *upcf;
480
 
417
 
481
-            ret = ngx_calloc(sizeof(ngx_str_t), r->connection->log);
482
-            ret->data = start_p;
483
-            ret->len = p - start_p;
484
-            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
485
-                "upload-progress: get_tracking_id found args: %V",ret);
486
-            return ret;
487
-        }
488
-    }
418
+    upcf = ngx_http_get_module_loc_conf(r, ngx_http_uploadprogress_module);
489
 
419
 
490
-    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
491
-        "upload-progress: get_tracking_id no id found");
492
-    return NULL;
420
+    return get_tracking_id_internal(r, &upcf->header_mul, "get_tracking_ids_mul", true);
493
 }
421
 }
494
 
422
 
495
 static ngx_http_uploadprogress_node_t *
423
 static ngx_http_uploadprogress_node_t *

Loading…
Peruuta
Tallenna