From 6854da9271bf30dc8273ca446dd5f18b4036c7a9 Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Fri, 16 Jul 2021 10:22:42 +0300 Subject: libfetch: simplify code by merging protocol error handling branches removes some code duplication --- libfetch/ftp.c | 35 +++++++++++++---------------------- libfetch/http.c | 23 +++++++++-------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/libfetch/ftp.c b/libfetch/ftp.c index d489559..8f9f04f 100644 --- a/libfetch/ftp.c +++ b/libfetch/ftp.c @@ -722,8 +722,8 @@ retry_mode: } break; default: - e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ - goto ouch; + /* XXX: error code should be prepared */ + goto protocol_error; } /* @@ -736,33 +736,22 @@ retry_mode: case FTP_LPASSIVE_MODE: for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++) /* nothing */ ; - if (!*p) { - e = FTP_PROTOCOL_ERROR; - goto ouch; - } + if (!*p) goto protocol_error; l = (e == FTP_PASSIVE_MODE ? 6 : 21); for (i = 0; *p && i < l; i++, p++) addr[i] = strtol(p, &p, 10); - if (i < l) { - e = FTP_PROTOCOL_ERROR; - goto ouch; - } + if (i < l) goto protocol_error; break; case FTP_EPASSIVE_MODE: for (p = ln + 3; *p && *p != '('; p++) /* nothing */ ; - if (!*p) { - e = FTP_PROTOCOL_ERROR; - goto ouch; - } + if (!*p) goto protocol_error; ++p; if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2], &port, &addr[3]) != 5 || addr[0] != addr[1] || - addr[0] != addr[2] || addr[0] != addr[3]) { - e = FTP_PROTOCOL_ERROR; - goto ouch; - } + addr[0] != addr[2] || addr[0] != addr[3]) + goto protocol_error; break; case FTP_SYNTAX_ERROR: if (verbose) @@ -803,8 +792,8 @@ retry_mode: } break; default: - e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ - break; + /* XXX: error code should be prepared */ + goto protocol_error; } /* connect to data port */ @@ -907,8 +896,8 @@ retry_mode: } break; default: - e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */ - goto ouch; + /* XXX: error code should be prepared */ + goto protocol_error; } if (e != FTP_OK) goto ouch; @@ -946,6 +935,8 @@ sysouch: close(sd); return (NULL); +protocol_error: + e = FTP_PROTOCOL_ERROR; ouch: if (e != -1) ftp_seterr(e); diff --git a/libfetch/http.c b/libfetch/http.c index bb01fdc..59d6292 100644 --- a/libfetch/http.c +++ b/libfetch/http.c @@ -1044,8 +1044,7 @@ http_request(struct url *URL, const char *op, struct url_stat *us, fetch_syserr(); goto ouch; case hdr_error: - http_seterr(HTTP_PROTOCOL_ERROR); - goto ouch; + goto protocol_error; case hdr_connection: /* XXX too weak? */ 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 */ - if (clength != -1 && length != -1 && clength != length) { - http_seterr(HTTP_PROTOCOL_ERROR); - goto ouch; - } + if (clength != -1 && length != -1 && clength != length) + goto protocol_error; if (clength == -1) clength = length; if (clength != -1) length = offset + clength; - if (length != -1 && size != -1 && length != size) { - http_seterr(HTTP_PROTOCOL_ERROR); - goto ouch; - } + if (length != -1 && size != -1 && length != size) + goto protocol_error; if (size == -1) size = length; @@ -1176,10 +1171,8 @@ http_request(struct url *URL, const char *op, struct url_stat *us, } /* too far? */ - if (URL->offset > 0 && offset > URL->offset) { - http_seterr(HTTP_PROTOCOL_ERROR); - goto ouch; - } + if (URL->offset > 0 && offset > URL->offset) + goto protocol_error; /* report back real offset and size */ URL->offset = offset; @@ -1222,6 +1215,8 @@ http_request(struct url *URL, const char *op, struct url_stat *us, return (f); +protocol_error: + http_seterr(HTTP_PROTOCOL_ERROR); ouch: if (url != URL) fetchFreeURL(url); -- cgit v1.2.3-60-g2f50