Skip to content
This repository

Recognize and report server-side error messages #654

Merged
merged 1 commit into from about 2 years ago

3 participants

Carlos Martín Nieto Don't Add Me To Your Organization a.k.a The Travis Bot Vicent Marti
Carlos Martín Nieto
Owner

When e.g. a repository isn't found, the server sends an error saying
so. Put that error message in our error buffer.

This actually produces a better error message than git, as we pretend that the dumb HTTP transport never existed and actually say what the other end gave us. If you typo really bad and hit a real HTTP server, we'll probably get HTML in return, but I'm going to say that's outside of the library's scope.

Carlos Martín Nieto carlosmn net: recognize and report server-side error messages
When e.g. a repository isn't found, the server sends an error saying
so. Put that error message in our error buffer.
39e6af6
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 39e6af6 into 8b9ec20).

Vicent Marti vmg merged commit ced9da5 into from
Vicent Marti vmg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 30, 2012
Carlos Martín Nieto carlosmn net: recognize and report server-side error messages
When e.g. a repository isn't found, the server sends an error saying
so. Put that error message in our error buffer.
39e6af6
This page is out of date. Refresh to see the latest.
21 src/pkt.c
@@ -97,6 +97,25 @@ static int comment_pkt(git_pkt **out, const char *line, size_t len)
97 97 return 0;
98 98 }
99 99
  100 +static int err_pkt(git_pkt **out, const char *line, size_t len)
  101 +{
  102 + git_pkt_err *pkt;
  103 +
  104 + /* Remove "ERR " from the line */
  105 + line += 4;
  106 + len -= 4;
  107 + pkt = git__malloc(sizeof(git_pkt_err) + len + 1);
  108 + GITERR_CHECK_ALLOC(pkt);
  109 +
  110 + pkt->type = GIT_PKT_ERR;
  111 + memcpy(pkt->error, line, len);
  112 + pkt->error[len] = '\0';
  113 +
  114 + *out = (git_pkt *) pkt;
  115 +
  116 + return 0;
  117 +}
  118 +
100 119 /*
101 120 * Parse an other-ref line.
102 121 */
@@ -234,6 +253,8 @@ int git_pkt_parse_line(
234 253 ret = ack_pkt(head, line, len);
235 254 else if (!git__prefixcmp(line, "NAK"))
236 255 ret = nak_pkt(head);
  256 + else if (!git__prefixcmp(line, "ERR "))
  257 + ret = err_pkt(head, line, len);
237 258 else if (*line == '#')
238 259 ret = comment_pkt(head, line, len);
239 260 else
6 src/pkt.h
@@ -23,6 +23,7 @@ enum git_pkt_type {
23 23 GIT_PKT_NAK,
24 24 GIT_PKT_PACK,
25 25 GIT_PKT_COMMENT,
  26 + GIT_PKT_ERR,
26 27 };
27 28
28 29 /* Used for multi-ack */
@@ -64,6 +65,11 @@ typedef struct {
64 65 char comment[GIT_FLEX_ARRAY];
65 66 } git_pkt_comment;
66 67
  68 +typedef struct {
  69 + enum git_pkt_type type;
  70 + char error[GIT_FLEX_ARRAY];
  71 +} git_pkt_err;
  72 +
67 73 int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_t len);
68 74 int git_pkt_buffer_flush(git_buf *buf);
69 75 int git_pkt_send_flush(GIT_SOCKET s);
7 src/protocol.c
@@ -40,6 +40,13 @@ int git_protocol_store_refs(git_protocol *p, const char *data, size_t len)
40 40 return p->error = -1;
41 41
42 42 git_buf_consume(buf, line_end);
  43 +
  44 + if (pkt->type == GIT_PKT_ERR) {
  45 + giterr_set(GITERR_NET, "Remote error: %s", ((git_pkt_err *)pkt)->error);
  46 + git__free(pkt);
  47 + return -1;
  48 + }
  49 +
43 50 if (git_vector_insert(refs, pkt) < 0)
44 51 return p->error = -1;
45 52
16 src/transports/http.c
@@ -165,6 +165,12 @@ static int on_headers_complete(http_parser *parser)
165 165 transport_http *t = (transport_http *) parser->data;
166 166 git_buf *buf = &t->buf;
167 167
  168 + /* The content-type is text/plain for 404, so don't validate */
  169 + if (parser->status_code == 404) {
  170 + git_buf_clear(buf);
  171 + return 0;
  172 + }
  173 +
168 174 if (t->content_type == NULL) {
169 175 t->content_type = git__strdup(git_buf_cstr(buf));
170 176 if (t->content_type == NULL)
@@ -187,6 +193,10 @@ static int on_body_store_refs(http_parser *parser, const char *str, size_t len)
187 193 {
188 194 transport_http *t = (transport_http *) parser->data;
189 195
  196 + if (parser->status_code == 404) {
  197 + return git_buf_put(&t->buf, str, len);
  198 + }
  199 +
190 200 return git_protocol_store_refs(&t->proto, str, len);
191 201 }
192 202
@@ -195,6 +205,12 @@ static int on_message_complete(http_parser *parser)
195 205 transport_http *t = (transport_http *) parser->data;
196 206
197 207 t->transfer_finished = 1;
  208 +
  209 + if (parser->status_code == 404) {
  210 + giterr_set(GITERR_NET, "Remote error: %s", git_buf_cstr(&t->buf));
  211 + t->error = -1;
  212 + }
  213 +
198 214 return 0;
199 215 }
200 216

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.