@matheusbsbs / matheus-bagliano / commits / f696a2b1c8
Fork 0

mailinfo: factor out some repeated header handling

We do the same thing for each header: match it, copy it to a strbuf, and decode it. Let's put that in a helper function to avoid repetition. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 11, 2020 at 12:20 UTC f696a2b1c850c81130740945835beec72727debf
1 file changed +23 -16
mailinfo.c
+23 -16
@@ -549,23 +549,36 @@ release_return:
549 mi->input_error = -1;
550 }
551
552 +/*
553 + * Returns true if "line" contains a header matching "hdr", in which case "val"
554 + * will contain the value of the header with any RFC2047 B and Q encoding
555 + * unwrapped, and optionally normalize the meta information to utf8.
556 + */
557 +static int parse_header(const struct strbuf *line,
558 + const char *hdr,
559 + struct mailinfo *mi,
560 + struct strbuf *val)
561 +{
562 + const char *val_str;
563 +
564 + if (!skip_header(line, hdr, &val_str))
565 + return 0;
566 + strbuf_addstr(val, val_str);
567 + decode_header(mi, val);
568 + return 1;
569 +}
570 +
571 static int check_header(struct mailinfo *mi,
572 const struct strbuf *line,
573 struct strbuf *hdr_data[], int overwrite)
574 {
575 int i, ret = 0;
576 struct strbuf sb = STRBUF_INIT;
558 - const char *val;
577
578 /* search for the interesting parts */
579 for (i = 0; header[i]; i++) {
580 if ((!hdr_data[i] || overwrite) &&
563 - skip_header(line, header[i], &val)) {
564 - /* Unwrap inline B and Q encoding, and optionally
565 - * normalize the meta information to utf8.
566 - */
567 - strbuf_addstr(&sb, val);
568 - decode_header(mi, &sb);
581 + parse_header(line, header[i], mi, &sb)) {
582 handle_header(&hdr_data[i], &sb);
583 ret = 1;
584 goto check_header_out;
@@ -573,23 +586,17 @@ static int check_header(struct mailinfo *mi,
586 }
587
588 /* Content stuff */
576 - if (skip_header(line, "Content-Type", &val)) {
577 - strbuf_addstr(&sb, val);
578 - decode_header(mi, &sb);
589 + if (parse_header(line, "Content-Type", mi, &sb)) {
590 handle_content_type(mi, &sb);
591 ret = 1;
592 goto check_header_out;
593 }
583 - if (skip_header(line, "Content-Transfer-Encoding", &val)) {
584 - strbuf_addstr(&sb, val);
585 - decode_header(mi, &sb);
594 + if (parse_header(line, "Content-Transfer-Encoding", mi, &sb)) {
595 handle_content_transfer_encoding(mi, &sb);
596 ret = 1;
597 goto check_header_out;
598 }
590 - if (skip_header(line, "Message-Id", &val)) {
591 - strbuf_addstr(&sb, val);
592 - decode_header(mi, &sb);
599 + if (parse_header(line, "Message-Id", mi, &sb)) {
600 if (mi->add_message_id)
601 mi->message_id = strbuf_detach(&sb, NULL);
602 ret = 1;