Skip to content

Commit d880c3d

Browse files
committed
Merge branch 'jk/mailinfo-cleanup'
Code clean-up. * jk/mailinfo-cleanup: mailinfo: factor out some repeated header handling mailinfo: be more liberal with header whitespace mailinfo: simplify parsing of header values mailinfo: treat header values as C strings
2 parents 5d55554 + f696a2b commit d880c3d

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

mailinfo.c

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,17 @@ static const char *header[MAX_HDR_PARSED] = {
346346
"From","Subject","Date",
347347
};
348348

349-
static inline int cmp_header(const struct strbuf *line, const char *hdr)
349+
static inline int skip_header(const struct strbuf *line, const char *hdr,
350+
const char **outval)
350351
{
351-
int len = strlen(hdr);
352-
return !strncasecmp(line->buf, hdr, len) && line->len > len &&
353-
line->buf[len] == ':' && isspace(line->buf[len + 1]);
352+
const char *val;
353+
if (!skip_iprefix(line->buf, hdr, &val) ||
354+
*val++ != ':')
355+
return 0;
356+
while (isspace(*val))
357+
val++;
358+
*outval = val;
359+
return 1;
354360
}
355361

356362
static int is_format_patch_separator(const char *line, int len)
@@ -543,49 +549,54 @@ static void decode_header(struct mailinfo *mi, struct strbuf *it)
543549
mi->input_error = -1;
544550
}
545551

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+
546571
static int check_header(struct mailinfo *mi,
547572
const struct strbuf *line,
548573
struct strbuf *hdr_data[], int overwrite)
549574
{
550-
int i, ret = 0, len;
575+
int i, ret = 0;
551576
struct strbuf sb = STRBUF_INIT;
552577

553578
/* search for the interesting parts */
554579
for (i = 0; header[i]; i++) {
555-
int len = strlen(header[i]);
556-
if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
557-
/* Unwrap inline B and Q encoding, and optionally
558-
* normalize the meta information to utf8.
559-
*/
560-
strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
561-
decode_header(mi, &sb);
580+
if ((!hdr_data[i] || overwrite) &&
581+
parse_header(line, header[i], mi, &sb)) {
562582
handle_header(&hdr_data[i], &sb);
563583
ret = 1;
564584
goto check_header_out;
565585
}
566586
}
567587

568588
/* Content stuff */
569-
if (cmp_header(line, "Content-Type")) {
570-
len = strlen("Content-Type: ");
571-
strbuf_add(&sb, line->buf + len, line->len - len);
572-
decode_header(mi, &sb);
589+
if (parse_header(line, "Content-Type", mi, &sb)) {
573590
handle_content_type(mi, &sb);
574591
ret = 1;
575592
goto check_header_out;
576593
}
577-
if (cmp_header(line, "Content-Transfer-Encoding")) {
578-
len = strlen("Content-Transfer-Encoding: ");
579-
strbuf_add(&sb, line->buf + len, line->len - len);
580-
decode_header(mi, &sb);
594+
if (parse_header(line, "Content-Transfer-Encoding", mi, &sb)) {
581595
handle_content_transfer_encoding(mi, &sb);
582596
ret = 1;
583597
goto check_header_out;
584598
}
585-
if (cmp_header(line, "Message-Id")) {
586-
len = strlen("Message-Id: ");
587-
strbuf_add(&sb, line->buf + len, line->len - len);
588-
decode_header(mi, &sb);
599+
if (parse_header(line, "Message-Id", mi, &sb)) {
589600
if (mi->add_message_id)
590601
mi->message_id = strbuf_detach(&sb, NULL);
591602
ret = 1;
@@ -606,8 +617,9 @@ static int is_inbody_header(const struct mailinfo *mi,
606617
const struct strbuf *line)
607618
{
608619
int i;
620+
const char *val;
609621
for (i = 0; header[i]; i++)
610-
if (!mi->s_hdr_data[i] && cmp_header(line, header[i]))
622+
if (!mi->s_hdr_data[i] && skip_header(line, header[i], &val))
611623
return 1;
612624
return 0;
613625
}

t/t5100-mailinfo.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,19 @@ test_expect_failure 'mailinfo -b separated double [PATCH]' '
213213
test z"$subj" = z"Subject: [other] message"
214214
'
215215

216+
test_expect_success 'mailinfo handles unusual header whitespace' '
217+
git mailinfo /dev/null /dev/null >actual <<-\EOF &&
218+
From:Real Name <[email protected]>
219+
Subject: extra spaces
220+
EOF
221+
222+
cat >expect <<-\EOF &&
223+
Author: Real Name
224+
225+
Subject: extra spaces
226+
227+
EOF
228+
test_cmp expect actual
229+
'
230+
216231
test_done

0 commit comments

Comments
 (0)