libfetch: simplify code by merging protocol error handling branches

removes some code duplication
cute-signatures
Timo Teräs 2021-07-16 10:22:42 +03:00
parent b2819a6d5a
commit d909ebc25b
2 changed files with 22 additions and 36 deletions

View File

@ -722,8 +722,8 @@ retry_mode:
} }
break; break;
default: default:
e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ /* XXX: error code should be prepared */
goto ouch; goto protocol_error;
} }
/* /*
@ -736,33 +736,22 @@ retry_mode:
case FTP_LPASSIVE_MODE: case FTP_LPASSIVE_MODE:
for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++) for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++)
/* nothing */ ; /* nothing */ ;
if (!*p) { if (!*p) goto protocol_error;
e = FTP_PROTOCOL_ERROR;
goto ouch;
}
l = (e == FTP_PASSIVE_MODE ? 6 : 21); l = (e == FTP_PASSIVE_MODE ? 6 : 21);
for (i = 0; *p && i < l; i++, p++) for (i = 0; *p && i < l; i++, p++)
addr[i] = strtol(p, &p, 10); addr[i] = strtol(p, &p, 10);
if (i < l) { if (i < l) goto protocol_error;
e = FTP_PROTOCOL_ERROR;
goto ouch;
}
break; break;
case FTP_EPASSIVE_MODE: case FTP_EPASSIVE_MODE:
for (p = ln + 3; *p && *p != '('; p++) for (p = ln + 3; *p && *p != '('; p++)
/* nothing */ ; /* nothing */ ;
if (!*p) { if (!*p) goto protocol_error;
e = FTP_PROTOCOL_ERROR;
goto ouch;
}
++p; ++p;
if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2], if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
&port, &addr[3]) != 5 || &port, &addr[3]) != 5 ||
addr[0] != addr[1] || addr[0] != addr[1] ||
addr[0] != addr[2] || addr[0] != addr[3]) { addr[0] != addr[2] || addr[0] != addr[3])
e = FTP_PROTOCOL_ERROR; goto protocol_error;
goto ouch;
}
break; break;
case FTP_SYNTAX_ERROR: case FTP_SYNTAX_ERROR:
if (verbose) if (verbose)
@ -803,8 +792,8 @@ retry_mode:
} }
break; break;
default: default:
e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ /* XXX: error code should be prepared */
break; goto protocol_error;
} }
/* connect to data port */ /* connect to data port */
@ -907,8 +896,8 @@ retry_mode:
} }
break; break;
default: default:
e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ /* XXX: error code should be prepared */
goto ouch; goto protocol_error;
} }
if (e != FTP_OK) if (e != FTP_OK)
goto ouch; goto ouch;
@ -946,6 +935,8 @@ sysouch:
close(sd); close(sd);
return (NULL); return (NULL);
protocol_error:
e = FTP_PROTOCOL_ERROR;
ouch: ouch:
if (e != -1) if (e != -1)
ftp_seterr(e); ftp_seterr(e);

View File

@ -1044,8 +1044,7 @@ http_request(struct url *URL, const char *op, struct url_stat *us,
fetch_syserr(); fetch_syserr();
goto ouch; goto ouch;
case hdr_error: case hdr_error:
http_seterr(HTTP_PROTOCOL_ERROR); goto protocol_error;
goto ouch;
case hdr_connection: case hdr_connection:
/* XXX too weak? */ /* XXX too weak? */
keep_alive = (strcasecmp(p, "keep-alive") == 0); keep_alive = (strcasecmp(p, "keep-alive") == 0);
@ -1154,18 +1153,14 @@ http_request(struct url *URL, const char *op, struct url_stat *us,
} }
/* check for inconsistencies */ /* check for inconsistencies */
if (clength != -1 && length != -1 && clength != length) { if (clength != -1 && length != -1 && clength != length)
http_seterr(HTTP_PROTOCOL_ERROR); goto protocol_error;
goto ouch;
}
if (clength == -1) if (clength == -1)
clength = length; clength = length;
if (clength != -1) if (clength != -1)
length = offset + clength; length = offset + clength;
if (length != -1 && size != -1 && length != size) { if (length != -1 && size != -1 && length != size)
http_seterr(HTTP_PROTOCOL_ERROR); goto protocol_error;
goto ouch;
}
if (size == -1) if (size == -1)
size = length; size = length;
@ -1176,10 +1171,8 @@ http_request(struct url *URL, const char *op, struct url_stat *us,
} }
/* too far? */ /* too far? */
if (URL->offset > 0 && offset > URL->offset) { if (URL->offset > 0 && offset > URL->offset)
http_seterr(HTTP_PROTOCOL_ERROR); goto protocol_error;
goto ouch;
}
/* report back real offset and size */ /* report back real offset and size */
URL->offset = offset; URL->offset = offset;
@ -1222,6 +1215,8 @@ http_request(struct url *URL, const char *op, struct url_stat *us,
return (f); return (f);
protocol_error:
http_seterr(HTTP_PROTOCOL_ERROR);
ouch: ouch:
if (url != URL) if (url != URL)
fetchFreeURL(url); fetchFreeURL(url);