Skip to content

Commit

Permalink
axel: Improve HTTP status code reporting
Browse files Browse the repository at this point in the history
Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
  • Loading branch information
ismaell authored and Jason23347 committed Dec 28, 2020
1 parent 450e14c commit 815d913
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/axel.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ axel_new(conf_t *conf, int count, const search_t *res)
* depends on the protocol used. */
status = conn_info(&axel->conn[0]);
if (!status) {
axel_message(axel, "%s", axel->conn[0].message);
char msg[80];
int code = conn_info_status_get(msg, sizeof(msg), axel->conn);
fprintf(stderr, _("ERROR %d: %s.\n"), code, msg);
axel->ready = -1;
return axel;
}
Expand Down
18 changes: 18 additions & 0 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,21 @@ conn_info(conn_t *conn)
}
return 1;
}

int
conn_info_status_get(char *msg, size_t size, conn_t *conn)
{
if (is_proto_http(conn->proto)) {
char *p = conn->http->headers->p;
/* Skip protocol and code */
while (*p++ != ' ');
while (*p++ != ' ');
size_t len = strcspn(p, "\r\n");
if (len) {
strlcpy(msg, p, min(len + 1, size));
return conn->http->status;
}
}
strlcpy(msg, _("Unknown Error"), size);
return -1;
}
8 changes: 8 additions & 0 deletions src/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
#define PROTO_DEFAULT PROTO_HTTP
#define PROTO_DEFAULT_PORT PROTO_HTTP_PORT

static inline
int
is_proto_http(int proto)
{
return (proto & PROTO_PROTO_MASK) == PROTO_PROTO_HTTP;
}

typedef struct {
conf_t *conf;

Expand Down Expand Up @@ -107,6 +114,7 @@ int conn_init(conn_t *conn);
int conn_setup(conn_t *conn);
int conn_exec(conn_t *conn);
int conn_info(conn_t *conn);
int conn_info_status_get(char *msg, size_t size, conn_t *conn);
const char *scheme_from_proto(int proto);

#endif /* AXEL_CONN_H */

0 comments on commit 815d913

Please sign in to comment.