Skip to content

Commit e9cef7c

Browse files
committed
http: introduce GIT_ERROR_HTTP
Disambiguate between general network problems and HTTP problems in error codes.
1 parent 7fd9b3f commit e9cef7c

File tree

4 files changed

+58
-57
lines changed

4 files changed

+58
-57
lines changed

include/git2/errors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ typedef enum {
106106
GIT_ERROR_FILESYSTEM,
107107
GIT_ERROR_PATCH,
108108
GIT_ERROR_WORKTREE,
109-
GIT_ERROR_SHA1
109+
GIT_ERROR_SHA1,
110+
GIT_ERROR_HTTP
110111
} git_error_t;
111112

112113
/**

src/transports/http.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static int handle_auth(
158158
}
159159

160160
if (error > 0) {
161-
git_error_set(GIT_ERROR_NET, "%s authentication required but no callback set", server_type);
161+
git_error_set(GIT_ERROR_HTTP, "%s authentication required but no callback set", server_type);
162162
error = -1;
163163
}
164164

@@ -175,7 +175,7 @@ GIT_INLINE(int) handle_remote_auth(
175175
http_subtransport *transport = OWNING_SUBTRANSPORT(stream);
176176

177177
if (response->server_auth_credtypes == 0) {
178-
git_error_set(GIT_ERROR_NET, "server requires authentication that we do not support");
178+
git_error_set(GIT_ERROR_HTTP, "server requires authentication that we do not support");
179179
return -1;
180180
}
181181

@@ -197,7 +197,7 @@ GIT_INLINE(int) handle_proxy_auth(
197197
http_subtransport *transport = OWNING_SUBTRANSPORT(stream);
198198

199199
if (response->proxy_auth_credtypes == 0) {
200-
git_error_set(GIT_ERROR_NET, "proxy requires authentication that we do not support");
200+
git_error_set(GIT_ERROR_HTTP, "proxy requires authentication that we do not support");
201201
return -1;
202202
}
203203

@@ -226,7 +226,7 @@ static int handle_response(
226226

227227
if (allow_replay && git_http_response_is_redirect(response)) {
228228
if (!response->location) {
229-
git_error_set(GIT_ERROR_NET, "redirect without location");
229+
git_error_set(GIT_ERROR_HTTP, "redirect without location");
230230
return -1;
231231
}
232232

@@ -236,7 +236,7 @@ static int handle_response(
236236

237237
return 0;
238238
} else if (git_http_response_is_redirect(response)) {
239-
git_error_set(GIT_ERROR_NET, "unexpected redirect");
239+
git_error_set(GIT_ERROR_HTTP, "unexpected redirect");
240240
return -1;
241241
}
242242

@@ -255,24 +255,24 @@ static int handle_response(
255255
return git_http_client_skip_body(transport->http_client);
256256
} else if (response->status == GIT_HTTP_STATUS_UNAUTHORIZED ||
257257
response->status == GIT_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED) {
258-
git_error_set(GIT_ERROR_NET, "unexpected authentication failure");
258+
git_error_set(GIT_ERROR_HTTP, "unexpected authentication failure");
259259
return -1;
260260
}
261261

262262
if (response->status != GIT_HTTP_STATUS_OK) {
263-
git_error_set(GIT_ERROR_NET, "unexpected http status code: %d", response->status);
263+
git_error_set(GIT_ERROR_HTTP, "unexpected http status code: %d", response->status);
264264
return -1;
265265
}
266266

267267
/* The response must contain a Content-Type header. */
268268
if (!response->content_type) {
269-
git_error_set(GIT_ERROR_NET, "no content-type header in response");
269+
git_error_set(GIT_ERROR_HTTP, "no content-type header in response");
270270
return -1;
271271
}
272272

273273
/* The Content-Type header must match our expectation. */
274274
if (strcmp(response->content_type, stream->service->response_type) != 0) {
275-
git_error_set(GIT_ERROR_NET, "invalid content-type: '%s'", response->content_type);
275+
git_error_set(GIT_ERROR_HTTP, "invalid content-type: '%s'", response->content_type);
276276
return -1;
277277
}
278278

@@ -411,7 +411,7 @@ static int http_stream_read(
411411
}
412412

413413
if (stream->state == HTTP_STATE_SENDING_REQUEST) {
414-
git_error_set(GIT_ERROR_NET, "too many redirects or authentication replays");
414+
git_error_set(GIT_ERROR_HTTP, "too many redirects or authentication replays");
415415
error = -1;
416416
goto done;
417417
}
@@ -548,7 +548,7 @@ static int http_stream_write(
548548
}
549549

550550
if (stream->state == HTTP_STATE_NONE) {
551-
git_error_set(GIT_ERROR_NET,
551+
git_error_set(GIT_ERROR_HTTP,
552552
"too many redirects or authentication replays");
553553
error = -1;
554554
goto done;
@@ -653,7 +653,7 @@ static int http_action(
653653
return error;
654654

655655
if ((service = select_service(action)) == NULL) {
656-
git_error_set(GIT_ERROR_NET, "invalid action");
656+
git_error_set(GIT_ERROR_HTTP, "invalid action");
657657
return -1;
658658
}
659659

src/transports/httpclient.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static int on_header_complete(http_parser *parser)
154154

155155
if (!strcasecmp("Content-Type", name->ptr)) {
156156
if (response->content_type) {
157-
git_error_set(GIT_ERROR_NET,
157+
git_error_set(GIT_ERROR_HTTP,
158158
"multiple content-type headers");
159159
return -1;
160160
}
@@ -166,14 +166,14 @@ static int on_header_complete(http_parser *parser)
166166
int64_t len;
167167

168168
if (response->content_length) {
169-
git_error_set(GIT_ERROR_NET,
169+
git_error_set(GIT_ERROR_HTTP,
170170
"multiple content-length headers");
171171
return -1;
172172
}
173173

174174
if (git__strntol64(&len, value->ptr, value->size,
175175
NULL, 10) < 0 || len < 0) {
176-
git_error_set(GIT_ERROR_NET,
176+
git_error_set(GIT_ERROR_HTTP,
177177
"invalid content-length");
178178
return -1;
179179
}
@@ -196,7 +196,7 @@ static int on_header_complete(http_parser *parser)
196196
return -1;
197197
} else if (!strcasecmp("Location", name->ptr)) {
198198
if (response->location) {
199-
git_error_set(GIT_ERROR_NET,
199+
git_error_set(GIT_ERROR_HTTP,
200200
"multiple location headers");
201201
return -1;
202202
}
@@ -235,7 +235,7 @@ static int on_header_field(http_parser *parser, const char *str, size_t len)
235235
break;
236236

237237
default:
238-
git_error_set(GIT_ERROR_NET,
238+
git_error_set(GIT_ERROR_HTTP,
239239
"header name seen at unexpected time");
240240
return ctx->parse_status = PARSE_STATUS_ERROR;
241241
}
@@ -258,7 +258,7 @@ static int on_header_value(http_parser *parser, const char *str, size_t len)
258258
break;
259259

260260
default:
261-
git_error_set(GIT_ERROR_NET,
261+
git_error_set(GIT_ERROR_HTTP,
262262
"header value seen at unexpected time");
263263
return ctx->parse_status = PARSE_STATUS_ERROR;
264264
}
@@ -348,7 +348,7 @@ static int on_headers_complete(http_parser *parser)
348348
break;
349349

350350
default:
351-
git_error_set(GIT_ERROR_NET,
351+
git_error_set(GIT_ERROR_HTTP,
352352
"header completion at unexpected time");
353353
return ctx->parse_status = PARSE_STATUS_ERROR;
354354
}
@@ -511,14 +511,14 @@ static const char *init_auth_context(
511511
int error;
512512

513513
if (!best_scheme_and_challenge(&scheme, &challenge, challenges, credentials)) {
514-
git_error_set(GIT_ERROR_NET, "could not find appropriate mechanism for credentials");
514+
git_error_set(GIT_ERROR_HTTP, "could not find appropriate mechanism for credentials");
515515
return NULL;
516516
}
517517

518518
error = scheme->init_context(&server->auth_context, &server->url);
519519

520520
if (error == GIT_PASSTHROUGH) {
521-
git_error_set(GIT_ERROR_NET, "'%s' authentication is not supported", scheme->name);
521+
git_error_set(GIT_ERROR_HTTP, "'%s' authentication is not supported", scheme->name);
522522
return NULL;
523523
}
524524

@@ -585,7 +585,7 @@ static int apply_credentials(
585585
if (auth->connection_affinity)
586586
free_auth_context(server);
587587
} else if (!token.size) {
588-
git_error_set(GIT_ERROR_NET, "failed to respond to authentication challange");
588+
git_error_set(GIT_ERROR_HTTP, "failed to respond to authentication challange");
589589
error = -1;
590590
goto done;
591591
}
@@ -745,7 +745,7 @@ static int check_certificate(
745745
else if (error == GIT_PASSTHROUGH)
746746
error = 0;
747747
else if (error && !git_error_last())
748-
git_error_set(GIT_ERROR_NET,
748+
git_error_set(GIT_ERROR_HTTP,
749749
"user rejected certificate for %s", url->host);
750750

751751
git_error_state_free(&last_error);
@@ -864,7 +864,7 @@ GIT_INLINE(int) server_create_stream(git_http_server *server)
864864
else if (strcasecmp(url->scheme, "http") == 0)
865865
return git_socket_stream_new(&server->stream, url->host, url->port);
866866

867-
git_error_set(GIT_ERROR_NET, "unknown http scheme '%s'", url->scheme);
867+
git_error_set(GIT_ERROR_HTTP, "unknown http scheme '%s'", url->scheme);
868868
return -1;
869869
}
870870

@@ -920,7 +920,7 @@ static int proxy_connect(
920920
error = GIT_RETRY;
921921
goto done;
922922
} else if (response.status != GIT_HTTP_STATUS_OK) {
923-
git_error_set(GIT_ERROR_NET, "proxy returned unexpected status: %d", response.status);
923+
git_error_set(GIT_ERROR_HTTP, "proxy returned unexpected status: %d", response.status);
924924
error = -1;
925925
goto done;
926926
}
@@ -1044,7 +1044,7 @@ GIT_INLINE(int) client_read(git_http_client *client)
10441044
max_len = min(max_len, INT_MAX);
10451045

10461046
if (max_len == 0) {
1047-
git_error_set(GIT_ERROR_NET, "no room in output buffer");
1047+
git_error_set(GIT_ERROR_HTTP, "no room in output buffer");
10481048
return -1;
10491049
}
10501050

@@ -1101,12 +1101,12 @@ GIT_INLINE(int) client_read_and_parse(git_http_client *client)
11011101
http_errno = client->parser.http_errno;
11021102

11031103
if (parsed_len > INT_MAX) {
1104-
git_error_set(GIT_ERROR_NET, "unexpectedly large parse");
1104+
git_error_set(GIT_ERROR_HTTP, "unexpectedly large parse");
11051105
return -1;
11061106
}
11071107

11081108
if (parser->upgrade) {
1109-
git_error_set(GIT_ERROR_NET, "server requested upgrade");
1109+
git_error_set(GIT_ERROR_HTTP, "server requested upgrade");
11101110
return -1;
11111111
}
11121112

@@ -1140,22 +1140,22 @@ GIT_INLINE(int) client_read_and_parse(git_http_client *client)
11401140

11411141
/* Most failures will be reported in http_errno */
11421142
else if (parser->http_errno != HPE_OK) {
1143-
git_error_set(GIT_ERROR_NET, "http parser error: %s",
1143+
git_error_set(GIT_ERROR_HTTP, "http parser error: %s",
11441144
http_errno_description(http_errno));
11451145
return -1;
11461146
}
11471147

11481148
/* Otherwise we should have consumed the entire buffer. */
11491149
else if (parsed_len != client->read_buf.size) {
1150-
git_error_set(GIT_ERROR_NET,
1150+
git_error_set(GIT_ERROR_HTTP,
11511151
"http parser did not consume entire buffer: %s",
11521152
http_errno_description(http_errno));
11531153
return -1;
11541154
}
11551155

11561156
/* recv returned 0, the server hung up on us */
11571157
else if (!parsed_len) {
1158-
git_error_set(GIT_ERROR_NET, "unexpected EOF");
1158+
git_error_set(GIT_ERROR_HTTP, "unexpected EOF");
11591159
return -1;
11601160
}
11611161

@@ -1281,7 +1281,7 @@ int git_http_client_send_body(
12811281
return 0;
12821282

12831283
if (client->state != SENDING_BODY) {
1284-
git_error_set(GIT_ERROR_NET, "client is in invalid state");
1284+
git_error_set(GIT_ERROR_HTTP, "client is in invalid state");
12851285
return -1;
12861286
}
12871287

@@ -1317,7 +1317,7 @@ static int complete_request(git_http_client *client)
13171317
assert(client && client->state == SENDING_BODY);
13181318

13191319
if (client->request_body_len && client->request_body_remain) {
1320-
git_error_set(GIT_ERROR_NET, "truncated write");
1320+
git_error_set(GIT_ERROR_HTTP, "truncated write");
13211321
error = -1;
13221322
} else if (client->request_chunked) {
13231323
error = stream_write(&client->server, "0\r\n\r\n", 5);
@@ -1349,7 +1349,7 @@ int git_http_client_read_response(
13491349
}
13501350

13511351
if (client->state != SENT_REQUEST) {
1352-
git_error_set(GIT_ERROR_NET, "client is in invalid state");
1352+
git_error_set(GIT_ERROR_HTTP, "client is in invalid state");
13531353
error = -1;
13541354
goto done;
13551355
}
@@ -1392,7 +1392,7 @@ int git_http_client_read_body(
13921392
return 0;
13931393

13941394
if (client->state != READING_BODY) {
1395-
git_error_set(GIT_ERROR_NET, "client is in invalid state");
1395+
git_error_set(GIT_ERROR_HTTP, "client is in invalid state");
13961396
return -1;
13971397
}
13981398

@@ -1438,7 +1438,7 @@ int git_http_client_skip_body(git_http_client *client)
14381438
return 0;
14391439

14401440
if (client->state != READING_BODY) {
1441-
git_error_set(GIT_ERROR_NET, "client is in invalid state");
1441+
git_error_set(GIT_ERROR_HTTP, "client is in invalid state");
14421442
return -1;
14431443
}
14441444

@@ -1451,7 +1451,7 @@ int git_http_client_skip_body(git_http_client *client)
14511451
if (parser_context.error != HPE_OK ||
14521452
(parser_context.parse_status != PARSE_STATUS_OK &&
14531453
parser_context.parse_status != PARSE_STATUS_NO_OUTPUT)) {
1454-
git_error_set(GIT_ERROR_NET,
1454+
git_error_set(GIT_ERROR_HTTP,
14551455
"unexpected data handled in callback");
14561456
error = -1;
14571457
}

0 commit comments

Comments
 (0)