Skip to content

Commit f696a2b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ffbea18 commit f696a2b

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

mailinfo.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -549,47 +549,54 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
549549
mi->input_error = -1;
550550
}
551551

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+
552571
static int check_header(struct mailinfo *mi,
553572
const struct strbuf *line,
554573
struct strbuf *hdr_data[], int overwrite)
555574
{
556575
int i, ret = 0;
557576
struct strbuf sb = STRBUF_INIT;
558-
const char *val;
559577

560578
/* search for the interesting parts */
561579
for (i = 0; header[i]; i++) {
562580
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)) {
569582
handle_header(&hdr_data[i], &sb);
570583
ret = 1;
571584
goto check_header_out;
572585
}
573586
}
574587

575588
/* 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)) {
579590
handle_content_type(mi, &sb);
580591
ret = 1;
581592
goto check_header_out;
582593
}
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)) {
586595
handle_content_transfer_encoding(mi, &sb);
587596
ret = 1;
588597
goto check_header_out;
589598
}
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)) {
593600
if (mi->add_message_id)
594601
mi->message_id = strbuf_detach(&sb, NULL);
595602
ret = 1;

0 commit comments

Comments
 (0)