Skip to content

Commit d317ca5

Browse files
committed
http: print reason phrase from HTTP status line on error
Bug: https://bugzilla.redhat.com/676596
1 parent 487406c commit d317ca5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

RELEASE-NOTES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This release includes the following changes:
1919
o pop3: Added support for apop authentication
2020
o Added support for Schannel (Native Windows) SSL/TLS encryption [2]
2121
o Added support for Darwin SSL (Native Mac OS X and iOS) [6]
22+
o http: print reason phrase from HTTP status line on error [8]
2223

2324
This release includes the following bugfixes:
2425

@@ -68,3 +69,4 @@ References to bug reports and discussions on issues:
6869
[5] = http://daniel.haxx.se/blog/2012/06/03/curling-the-metalink/
6970
[6] = http://daniel.haxx.se/blog/2012/06/28/darwin-native-ssl-for-curl/
7071
[7] = http://daniel.haxx.se/blog/2012/07/08/curls-new-http-cookies-docs/
72+
[8] = https://bugzilla.redhat.com/676596

lib/http.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,42 @@ static CURLcode header_append(struct SessionHandle *data,
27262726
return CURLE_OK;
27272727
}
27282728

2729+
static void print_http_error(struct SessionHandle *data)
2730+
{
2731+
struct SingleRequest *k = &data->req;
2732+
char *beg = k->p;
2733+
2734+
/* make sure that data->req.p points to the HTTP status line */
2735+
if(!strncmp(beg, "HTTP", 4)) {
2736+
2737+
/* skip to HTTP status code */
2738+
beg = strchr(beg, ' ');
2739+
if(beg && *++beg) {
2740+
2741+
/* find trailing CR */
2742+
char end_char = '\r';
2743+
char *end = strchr(beg, end_char);
2744+
if(!end) {
2745+
/* try to find LF (workaround for non-compliant HTTP servers) */
2746+
end_char = '\n';
2747+
end = strchr(beg, end_char);
2748+
}
2749+
2750+
if(end) {
2751+
/* temporarily replace CR or LF by NUL and print the error message */
2752+
*end = '\0';
2753+
failf(data, "The requested URL returned error: %s", beg);
2754+
2755+
/* restore the previously replaced CR or LF */
2756+
*end = end_char;
2757+
return;
2758+
}
2759+
}
2760+
}
2761+
2762+
/* fall-back to printing the HTTP status code only */
2763+
failf(data, "The requested URL returned error: %d", k->httpcode);
2764+
}
27292765

27302766
/*
27312767
* Read any HTTP header lines from the server and pass them to the client app.
@@ -3114,8 +3150,7 @@ CURLcode Curl_http_readwrite_headers(struct SessionHandle *data,
31143150
}
31153151
else {
31163152
/* serious error, go home! */
3117-
failf (data, "The requested URL returned error: %d",
3118-
k->httpcode);
3153+
print_http_error(data);
31193154
return CURLE_HTTP_RETURNED_ERROR;
31203155
}
31213156
}

tests/data/test24

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ http
2424
HTTP GET fail silently on HTTP error return
2525
</name>
2626
<command>
27-
http://%HOSTIP:%HTTPPORT/24 --fail
27+
http://%HOSTIP:%HTTPPORT/24 --fail --silent --show-error
2828
</command>
2929
</client>
3030

@@ -43,5 +43,8 @@ Accept: */*
4343
<errorcode>
4444
22
4545
</errorcode>
46+
<file2 name="log/stderr24">
47+
curl: (22) The requested URL returned error: 404 BAD BOY
48+
</file2>
4649
</verify>
4750
</testcase>

0 commit comments

Comments
 (0)