diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-10-05 10:06:13 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-10-05 10:06:13 +0000 |
commit | 72f05a8364e251076a458b897b4efcd339be0e23 (patch) | |
tree | 23d061134efd18dbdb43ca8eef4df3a81a6a2fda /src/microhttpd/digestauth.c | |
parent | dd749c2274ef01d0476f3471d28374dbf7c4b36b (diff) | |
download | libmicrohttpd-72f05a8364e251076a458b897b4efcd339be0e23.tar.gz libmicrohttpd-72f05a8364e251076a458b897b4efcd339be0e23.zip |
deduplicate arg parsing logic between connection.c and digestauth.c (now in internal.c)
Diffstat (limited to 'src/microhttpd/digestauth.c')
-rw-r--r-- | src/microhttpd/digestauth.c | 118 |
1 files changed, 16 insertions, 102 deletions
diff --git a/src/microhttpd/digestauth.c b/src/microhttpd/digestauth.c index f0d0a806..3ed76a35 100644 --- a/src/microhttpd/digestauth.c +++ b/src/microhttpd/digestauth.c | |||
@@ -443,19 +443,21 @@ calculate_nonce (uint32_t nonce_time, | |||
443 | * @param connection the connection | 443 | * @param connection the connection |
444 | * @param key the key | 444 | * @param key the key |
445 | * @param value the value, can be NULL | 445 | * @param value the value, can be NULL |
446 | * @param kind type of the header | ||
446 | * @return #MHD_YES if the key-value pair is in the headers, | 447 | * @return #MHD_YES if the key-value pair is in the headers, |
447 | * #MHD_NO if not | 448 | * #MHD_NO if not |
448 | */ | 449 | */ |
449 | static int | 450 | static int |
450 | test_header (struct MHD_Connection *connection, | 451 | test_header (struct MHD_Connection *connection, |
451 | const char *key, | 452 | const char *key, |
452 | const char *value) | 453 | const char *value, |
454 | enum MHD_ValueKind kind) | ||
453 | { | 455 | { |
454 | struct MHD_HTTP_Header *pos; | 456 | struct MHD_HTTP_Header *pos; |
455 | 457 | ||
456 | for (pos = connection->headers_received; NULL != pos; pos = pos->next) | 458 | for (pos = connection->headers_received; NULL != pos; pos = pos->next) |
457 | { | 459 | { |
458 | if (MHD_GET_ARGUMENT_KIND != pos->kind) | 460 | if (kind != pos->kind) |
459 | continue; | 461 | continue; |
460 | if (0 != strcmp (key, pos->header)) | 462 | if (0 != strcmp (key, pos->header)) |
461 | continue; | 463 | continue; |
@@ -488,114 +490,26 @@ check_argument_match (struct MHD_Connection *connection, | |||
488 | { | 490 | { |
489 | struct MHD_HTTP_Header *pos; | 491 | struct MHD_HTTP_Header *pos; |
490 | char *argb; | 492 | char *argb; |
491 | char *argp; | ||
492 | char *equals; | ||
493 | char *amper; | ||
494 | unsigned int num_headers; | 493 | unsigned int num_headers; |
494 | int ret; | ||
495 | 495 | ||
496 | argb = strdup (args); | 496 | argb = strdup (args); |
497 | if (NULL == argb) | 497 | if (NULL == argb) |
498 | { | 498 | { |
499 | #if HAVE_MESSAGES | 499 | #if HAVE_MESSAGES |
500 | MHD_DLOG (connection->daemon, | 500 | MHD_DLOG (connection->daemon, |
501 | "Failed to allocate memory for copy of URI arguments\n"); | 501 | "Failed to allocate memory for copy of URI arguments\n"); |
502 | #endif /* HAVE_MESSAGES */ | 502 | #endif /* HAVE_MESSAGES */ |
503 | return MHD_NO; | 503 | return MHD_NO; |
504 | } | ||
505 | num_headers = 0; | ||
506 | argp = argb; | ||
507 | while ( (NULL != argp) && | ||
508 | ('\0' != argp[0]) ) | ||
509 | { | ||
510 | equals = strchr (argp, '='); | ||
511 | amper = strchr (argp, '&'); | ||
512 | if (NULL == amper) | ||
513 | { | ||
514 | /* last argument */ | ||
515 | if (NULL == equals) | ||
516 | { | ||
517 | /* last argument, without '=' */ | ||
518 | MHD_unescape_plus (argp); | ||
519 | if (MHD_YES != test_header (connection, | ||
520 | argp, | ||
521 | NULL)) | ||
522 | { | ||
523 | free (argb); | ||
524 | return MHD_NO; | ||
525 | } | ||
526 | num_headers++; | ||
527 | break; | ||
528 | } | ||
529 | /* got 'foo=bar' */ | ||
530 | equals[0] = '\0'; | ||
531 | equals++; | ||
532 | MHD_unescape_plus (argp); | ||
533 | /* add with 'value' NULL */ | ||
534 | connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, | ||
535 | connection, | ||
536 | argp); | ||
537 | MHD_unescape_plus (equals); | ||
538 | /* add with 'value' NULL */ | ||
539 | connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, | ||
540 | connection, | ||
541 | equals); | ||
542 | if (MHD_YES != test_header (connection, | ||
543 | argp, | ||
544 | equals)) | ||
545 | { | ||
546 | free (argb); | ||
547 | return MHD_NO; | ||
548 | } | ||
549 | num_headers++; | ||
550 | break; | ||
551 | } | ||
552 | /* amper is non-NULL here */ | ||
553 | amper[0] = '\0'; | ||
554 | amper++; | ||
555 | if ( (NULL == equals) || | ||
556 | (equals >= amper) ) | ||
557 | { | ||
558 | /* got 'foo&bar' or 'foo&bar=val', add key 'foo' with NULL for value */ | ||
559 | MHD_unescape_plus (argp); | ||
560 | connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, | ||
561 | connection, | ||
562 | argp); | ||
563 | if (MHD_YES != | ||
564 | test_header (connection, | ||
565 | argp, | ||
566 | NULL)) | ||
567 | { | ||
568 | free (argb); | ||
569 | return MHD_NO; | ||
570 | } | ||
571 | /* continue with 'bar' */ | ||
572 | num_headers++; | ||
573 | args = amper; | ||
574 | continue; | ||
575 | } | ||
576 | equals[0] = '\0'; | ||
577 | equals++; | ||
578 | MHD_unescape_plus (argp); | ||
579 | connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, | ||
580 | connection, | ||
581 | argp); | ||
582 | MHD_unescape_plus (equals); | ||
583 | connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, | ||
584 | connection, | ||
585 | equals); | ||
586 | if (MHD_YES != | ||
587 | test_header (connection, | ||
588 | argp, | ||
589 | equals)) | ||
590 | { | ||
591 | free (argb); | ||
592 | return MHD_NO; | ||
593 | } | ||
594 | num_headers++; | ||
595 | argp = amper; | ||
596 | } | 504 | } |
505 | ret = MHD_parse_arguments_ (connection, | ||
506 | MHD_GET_ARGUMENT_KIND, | ||
507 | argb, | ||
508 | &test_header, | ||
509 | &num_headers); | ||
597 | free (argb); | 510 | free (argb); |
598 | 511 | if (MHD_YES != ret) | |
512 | return MHD_NO; | ||
599 | /* also check that the number of headers matches */ | 513 | /* also check that the number of headers matches */ |
600 | for (pos = connection->headers_received; NULL != pos; pos = pos->next) | 514 | for (pos = connection->headers_received; NULL != pos; pos = pos->next) |
601 | { | 515 | { |