remote-curl: show server content on http errors

If an http request to a remote git server fails, we show only the http response code, or sometimes a custom message for particular codes. This gives the server no opportunity to offer a more detailed explanation of the reason for the failure, or to give extra advice. This patch teaches remote-curl to record and display the body content of a failed http response. We only display such responses when the content-type is advertised as text/plain, as it is the most likely to look presentable on the user's terminal (and it is hoped to be a good indication that the message is intended for git clients, and not for a web browser). Each line of the new output is prepended with "remote:". Example output may look like this (assuming the server is configured to display such a helpful message): $ GIT_SMART_HTTP=0 git clone https://example.com/some/repo.git Cloning into 'repo'... remote: Sorry, fetching via dumb http is forbidden. remote: Please upgrade your git client to v1.6.6 or greater remote: and make sure that smart-http is enabled. error: The requested URL returned error: 403 while accessing http://localhost:5001/some/repo.git/info/refs fatal: HTTP request failed For the sake of simplicity, we only record and display these errors during the initial fetch of the ref list, as that is the initial contact with the server and where the most common, interesting errors happen (and there is already precedent, as that is the only place we currently massage http error codes into more helpful messages). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 5, 2013 at 18:17 UTC 426e70d4a11ce3b4f70636d57c6a0ab16ae08a00
1 file changed +32 -1
remote-curl.c
+32 -1
@@ -151,6 +151,33 @@ static void free_discovery(struct discovery *d)
151 }
152 }
153
154 +static int show_http_message(struct strbuf *type, struct strbuf *msg)
155 +{
156 + const char *p, *eol;
157 +
158 + /*
159 + * We only show text/plain parts, as other types are likely
160 + * to be ugly to look at on the user's terminal.
161 + *
162 + * TODO should handle "; charset=XXX", and re-encode into
163 + * logoutputencoding
164 + */
165 + if (strcasecmp(type->buf, "text/plain"))
166 + return -1;
167 +
168 + strbuf_trim(msg);
169 + if (!msg->len)
170 + return -1;
171 +
172 + p = msg->buf;
173 + do {
174 + eol = strchrnul(p, '\n');
175 + fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
176 + p = eol + 1;
177 + } while(*eol);
178 + return 0;
179 +}
180 +
181 static struct discovery* discover_refs(const char *service, int for_push)
182 {
183 struct strbuf exp = STRBUF_INIT;
@@ -176,16 +203,20 @@ static struct discovery* discover_refs(const char *service, int for_push)
203 }
204 refs_url = strbuf_detach(&buffer, NULL);
205
179 - http_ret = http_get_strbuf(refs_url, &type, &buffer, HTTP_NO_CACHE);
206 + http_ret = http_get_strbuf(refs_url, &type, &buffer,
207 + HTTP_NO_CACHE | HTTP_KEEP_ERROR);
208 switch (http_ret) {
209 case HTTP_OK:
210 break;
211 case HTTP_MISSING_TARGET:
212 + show_http_message(&type, &buffer);
213 die("%s not found: did you run git update-server-info on the"
214 " server?", refs_url);
215 case HTTP_NOAUTH:
216 + show_http_message(&type, &buffer);
217 die("Authentication failed");
218 default:
219 + show_http_message(&type, &buffer);
220 http_error(refs_url, http_ret);
221 die("HTTP request failed");
222 }