Browse code

registry: use constants for http status codes

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/10/14 00:25:25
Showing 4 changed files
... ...
@@ -281,12 +281,12 @@ func requiresAuth(w http.ResponseWriter, r *http.Request) bool {
281 281
 		return true
282 282
 	}
283 283
 	w.Header().Add("WWW-Authenticate", "token")
284
-	apiError(w, "Wrong auth", 401)
284
+	apiError(w, "Wrong auth", http.StatusUnauthorized)
285 285
 	return false
286 286
 }
287 287
 
288 288
 func handlerGetPing(w http.ResponseWriter, r *http.Request) {
289
-	writeResponse(w, true, 200)
289
+	writeResponse(w, true, http.StatusOK)
290 290
 }
291 291
 
292 292
 func handlerGetImage(w http.ResponseWriter, r *http.Request) {
... ...
@@ -323,17 +323,17 @@ func handlerPutImage(w http.ResponseWriter, r *http.Request) {
323 323
 	}
324 324
 	if checksum := r.Header.Get("X-Docker-Checksum"); checksum != "" {
325 325
 		if checksum != layer["checksum_simple"] && checksum != layer["checksum_tarsum"] {
326
-			apiError(w, "Wrong checksum", 400)
326
+			apiError(w, "Wrong checksum", http.StatusBadRequest)
327 327
 			return
328 328
 		}
329 329
 	}
330 330
 	body, err := ioutil.ReadAll(r.Body)
331 331
 	if err != nil {
332
-		apiError(w, fmt.Sprintf("Error: %s", err), 500)
332
+		apiError(w, fmt.Sprintf("Error: %s", err), http.StatusInternalServerError)
333 333
 		return
334 334
 	}
335 335
 	layer[action] = string(body)
336
-	writeResponse(w, true, 200)
336
+	writeResponse(w, true, http.StatusOK)
337 337
 }
338 338
 
339 339
 func handlerGetDeleteTags(w http.ResponseWriter, r *http.Request) {
... ...
@@ -342,20 +342,20 @@ func handlerGetDeleteTags(w http.ResponseWriter, r *http.Request) {
342 342
 	}
343 343
 	repositoryName, err := reference.WithName(mux.Vars(r)["repository"])
344 344
 	if err != nil {
345
-		apiError(w, "Could not parse repository", 400)
345
+		apiError(w, "Could not parse repository", http.StatusBadRequest)
346 346
 		return
347 347
 	}
348 348
 	tags, exists := testRepositories[repositoryName.String()]
349 349
 	if !exists {
350
-		apiError(w, "Repository not found", 404)
350
+		apiError(w, "Repository not found", http.StatusNotFound)
351 351
 		return
352 352
 	}
353 353
 	if r.Method == http.MethodDelete {
354 354
 		delete(testRepositories, repositoryName.String())
355
-		writeResponse(w, true, 200)
355
+		writeResponse(w, true, http.StatusOK)
356 356
 		return
357 357
 	}
358
-	writeResponse(w, tags, 200)
358
+	writeResponse(w, tags, http.StatusOK)
359 359
 }
360 360
 
361 361
 func handlerGetTag(w http.ResponseWriter, r *http.Request) {
... ...
@@ -365,21 +365,21 @@ func handlerGetTag(w http.ResponseWriter, r *http.Request) {
365 365
 	vars := mux.Vars(r)
366 366
 	repositoryName, err := reference.WithName(vars["repository"])
367 367
 	if err != nil {
368
-		apiError(w, "Could not parse repository", 400)
368
+		apiError(w, "Could not parse repository", http.StatusBadRequest)
369 369
 		return
370 370
 	}
371 371
 	tagName := vars["tag"]
372 372
 	tags, exists := testRepositories[repositoryName.String()]
373 373
 	if !exists {
374
-		apiError(w, "Repository not found", 404)
374
+		apiError(w, "Repository not found", http.StatusNotFound)
375 375
 		return
376 376
 	}
377 377
 	tag, exists := tags[tagName]
378 378
 	if !exists {
379
-		apiError(w, "Tag not found", 404)
379
+		apiError(w, "Tag not found", http.StatusNotFound)
380 380
 		return
381 381
 	}
382
-	writeResponse(w, tag, 200)
382
+	writeResponse(w, tag, http.StatusOK)
383 383
 }
384 384
 
385 385
 func handlerPutTag(w http.ResponseWriter, r *http.Request) {
... ...
@@ -389,7 +389,7 @@ func handlerPutTag(w http.ResponseWriter, r *http.Request) {
389 389
 	vars := mux.Vars(r)
390 390
 	repositoryName, err := reference.WithName(vars["repository"])
391 391
 	if err != nil {
392
-		apiError(w, "Could not parse repository", 400)
392
+		apiError(w, "Could not parse repository", http.StatusBadRequest)
393 393
 		return
394 394
 	}
395 395
 	tagName := vars["tag"]
... ...
@@ -401,15 +401,15 @@ func handlerPutTag(w http.ResponseWriter, r *http.Request) {
401 401
 	tagValue := ""
402 402
 	readJSON(r, tagValue)
403 403
 	tags[tagName] = tagValue
404
-	writeResponse(w, true, 200)
404
+	writeResponse(w, true, http.StatusOK)
405 405
 }
406 406
 
407 407
 func handlerUsers(w http.ResponseWriter, r *http.Request) {
408
-	code := 200
408
+	code := http.StatusOK
409 409
 	if r.Method == http.MethodPost {
410
-		code = 201
410
+		code = http.StatusCreated
411 411
 	} else if r.Method == http.MethodPut {
412
-		code = 204
412
+		code = http.StatusNoContent
413 413
 	}
414 414
 	writeResponse(w, "", code)
415 415
 }
... ...
@@ -420,14 +420,14 @@ func handlerImages(w http.ResponseWriter, r *http.Request) {
420 420
 	w.Header().Add("X-Docker-Token", fmt.Sprintf("FAKE-SESSION-%d", time.Now().UnixNano()))
421 421
 	if r.Method == http.MethodPut {
422 422
 		if strings.HasSuffix(r.URL.Path, "images") {
423
-			writeResponse(w, "", 204)
423
+			writeResponse(w, "", http.StatusNoContent)
424 424
 			return
425 425
 		}
426
-		writeResponse(w, "", 200)
426
+		writeResponse(w, "", http.StatusOK)
427 427
 		return
428 428
 	}
429 429
 	if r.Method == http.MethodDelete {
430
-		writeResponse(w, "", 204)
430
+		writeResponse(w, "", http.StatusNoContent)
431 431
 		return
432 432
 	}
433 433
 	var images []map[string]string
... ...
@@ -438,11 +438,11 @@ func handlerImages(w http.ResponseWriter, r *http.Request) {
438 438
 		image["Tag"] = "latest"
439 439
 		images = append(images, image)
440 440
 	}
441
-	writeResponse(w, images, 200)
441
+	writeResponse(w, images, http.StatusOK)
442 442
 }
443 443
 
444 444
 func handlerAuth(w http.ResponseWriter, r *http.Request) {
445
-	writeResponse(w, "OK", 200)
445
+	writeResponse(w, "OK", http.StatusOK)
446 446
 }
447 447
 
448 448
 func handlerSearch(w http.ResponseWriter, r *http.Request) {
... ...
@@ -451,7 +451,7 @@ func handlerSearch(w http.ResponseWriter, r *http.Request) {
451 451
 		NumResults: 1,
452 452
 		Results:    []registrytypes.SearchResult{{Name: "fakeimage", StarCount: 42}},
453 453
 	}
454
-	writeResponse(w, result, 200)
454
+	writeResponse(w, result, http.StatusOK)
455 455
 }
456 456
 
457 457
 func TestPing(t *testing.T) {
... ...
@@ -459,7 +459,7 @@ func TestPing(t *testing.T) {
459 459
 	if err != nil {
460 460
 		t.Fatal(err)
461 461
 	}
462
-	assertEqual(t, res.StatusCode, 200, "")
462
+	assertEqual(t, res.StatusCode, http.StatusOK, "")
463 463
 	assertEqual(t, res.Header.Get("X-Docker-Registry-Config"), "mock",
464 464
 		"This is not a Mocked Registry")
465 465
 }
... ...
@@ -56,10 +56,10 @@ func (r *requestReader) Read(p []byte) (n int, err error) {
56 56
 		r.cleanUpResponse()
57 57
 		return 0, err
58 58
 	}
59
-	if r.currentResponse.StatusCode == 416 && r.lastRange == r.totalSize && r.currentResponse.ContentLength == 0 {
59
+	if r.currentResponse.StatusCode == http.StatusRequestedRangeNotSatisfiable && r.lastRange == r.totalSize && r.currentResponse.ContentLength == 0 {
60 60
 		r.cleanUpResponse()
61 61
 		return 0, io.EOF
62
-	} else if r.currentResponse.StatusCode != 206 && r.lastRange != 0 && isFreshRequest {
62
+	} else if r.currentResponse.StatusCode != http.StatusPartialContent && r.lastRange != 0 && isFreshRequest {
63 63
 		r.cleanUpResponse()
64 64
 		return 0, fmt.Errorf("the server doesn't support byte ranges")
65 65
 	}
... ...
@@ -99,7 +99,7 @@ func TestResumableRequestReaderWithReadError(t *testing.T) {
99 99
 
100 100
 	response := &http.Response{
101 101
 		Status:        "500 Internal Server",
102
-		StatusCode:    500,
102
+		StatusCode:    http.StatusInternalServerError,
103 103
 		ContentLength: 0,
104 104
 		Close:         true,
105 105
 		Body:          errorReaderCloser{},
... ...
@@ -130,7 +130,7 @@ func TestResumableRequestReaderWithEOFWith416Response(t *testing.T) {
130 130
 
131 131
 	response := &http.Response{
132 132
 		Status:        "416 Requested Range Not Satisfiable",
133
-		StatusCode:    416,
133
+		StatusCode:    http.StatusRequestedRangeNotSatisfiable,
134 134
 		ContentLength: 0,
135 135
 		Close:         true,
136 136
 		Body:          ioutil.NopCloser(strings.NewReader("")),
... ...
@@ -225,8 +225,8 @@ func (r *Session) GetRemoteHistory(imgID, registry string) ([]string, error) {
225 225
 		return nil, err
226 226
 	}
227 227
 	defer res.Body.Close()
228
-	if res.StatusCode != 200 {
229
-		if res.StatusCode == 401 {
228
+	if res.StatusCode != http.StatusOK {
229
+		if res.StatusCode == http.StatusUnauthorized {
230 230
 			return nil, errcode.ErrorCodeUnauthorized.WithArgs()
231 231
 		}
232 232
 		return nil, newJSONError(fmt.Sprintf("Server error: %d trying to fetch remote history for %s", res.StatusCode, imgID), res)
... ...
@@ -248,7 +248,7 @@ func (r *Session) LookupRemoteImage(imgID, registry string) error {
248 248
 		return err
249 249
 	}
250 250
 	res.Body.Close()
251
-	if res.StatusCode != 200 {
251
+	if res.StatusCode != http.StatusOK {
252 252
 		return newJSONError(fmt.Sprintf("HTTP code %d", res.StatusCode), res)
253 253
 	}
254 254
 	return nil
... ...
@@ -261,7 +261,7 @@ func (r *Session) GetRemoteImageJSON(imgID, registry string) ([]byte, int64, err
261 261
 		return nil, -1, fmt.Errorf("Failed to download json: %s", err)
262 262
 	}
263 263
 	defer res.Body.Close()
264
-	if res.StatusCode != 200 {
264
+	if res.StatusCode != http.StatusOK {
265 265
 		return nil, -1, newJSONError(fmt.Sprintf("HTTP code %d", res.StatusCode), res)
266 266
 	}
267 267
 	// if the size header is not present, then set it to '-1'
... ...
@@ -308,7 +308,7 @@ func (r *Session) GetRemoteImageLayer(imgID, registry string, imgSize int64) (io
308 308
 			statusCode, imgID)
309 309
 	}
310 310
 
311
-	if res.StatusCode != 200 {
311
+	if res.StatusCode != http.StatusOK {
312 312
 		res.Body.Close()
313 313
 		return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
314 314
 			res.StatusCode, imgID)
... ...
@@ -347,7 +347,7 @@ func (r *Session) GetRemoteTag(registries []string, repositoryRef reference.Name
347 347
 		if res.StatusCode == 404 {
348 348
 			return "", ErrRepoNotFound
349 349
 		}
350
-		if res.StatusCode != 200 {
350
+		if res.StatusCode != http.StatusOK {
351 351
 			continue
352 352
 		}
353 353
 
... ...
@@ -385,7 +385,7 @@ func (r *Session) GetRemoteTags(registries []string, repositoryRef reference.Nam
385 385
 		if res.StatusCode == 404 {
386 386
 			return nil, ErrRepoNotFound
387 387
 		}
388
-		if res.StatusCode != 200 {
388
+		if res.StatusCode != http.StatusOK {
389 389
 			continue
390 390
 		}
391 391
 
... ...
@@ -441,14 +441,14 @@ func (r *Session) GetRepositoryData(name reference.Named) (*RepositoryData, erro
441 441
 		return nil, fmt.Errorf("Error while pulling image: %v", err)
442 442
 	}
443 443
 	defer res.Body.Close()
444
-	if res.StatusCode == 401 {
444
+	if res.StatusCode == http.StatusUnauthorized {
445 445
 		return nil, errcode.ErrorCodeUnauthorized.WithArgs()
446 446
 	}
447 447
 	// TODO: Right now we're ignoring checksums in the response body.
448 448
 	// In the future, we need to use them to check image validity.
449 449
 	if res.StatusCode == 404 {
450 450
 		return nil, newJSONError(fmt.Sprintf("HTTP code: %d", res.StatusCode), res)
451
-	} else if res.StatusCode != 200 {
451
+	} else if res.StatusCode != http.StatusOK {
452 452
 		errBody, err := ioutil.ReadAll(res.Body)
453 453
 		if err != nil {
454 454
 			logrus.Debugf("Error reading response body: %s", err)
... ...
@@ -505,7 +505,7 @@ func (r *Session) PushImageChecksumRegistry(imgData *ImgData, registry string) e
505 505
 	if len(res.Cookies()) > 0 {
506 506
 		r.client.Jar.SetCookies(req.URL, res.Cookies())
507 507
 	}
508
-	if res.StatusCode != 200 {
508
+	if res.StatusCode != http.StatusOK {
509 509
 		errBody, err := ioutil.ReadAll(res.Body)
510 510
 		if err != nil {
511 511
 			return fmt.Errorf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err)
... ...
@@ -539,10 +539,10 @@ func (r *Session) PushImageJSONRegistry(imgData *ImgData, jsonRaw []byte, regist
539 539
 		return fmt.Errorf("Failed to upload metadata: %s", err)
540 540
 	}
541 541
 	defer res.Body.Close()
542
-	if res.StatusCode == 401 && strings.HasPrefix(registry, "http://") {
542
+	if res.StatusCode == http.StatusUnauthorized && strings.HasPrefix(registry, "http://") {
543 543
 		return newJSONError("HTTP code 401, Docker will not send auth headers over HTTP.", res)
544 544
 	}
545
-	if res.StatusCode != 200 {
545
+	if res.StatusCode != http.StatusOK {
546 546
 		errBody, err := ioutil.ReadAll(res.Body)
547 547
 		if err != nil {
548 548
 			return newJSONError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
... ...
@@ -591,7 +591,7 @@ func (r *Session) PushImageLayerRegistry(imgID string, layer io.Reader, registry
591 591
 	}
592 592
 	defer res.Body.Close()
593 593
 
594
-	if res.StatusCode != 200 {
594
+	if res.StatusCode != http.StatusOK {
595 595
 		errBody, err := ioutil.ReadAll(res.Body)
596 596
 		if err != nil {
597 597
 			return "", "", newJSONError(fmt.Sprintf("HTTP code %d while uploading metadata and error when trying to parse response body: %s", res.StatusCode, err), res)
... ...
@@ -621,7 +621,7 @@ func (r *Session) PushRegistryTag(remote reference.Named, revision, tag, registr
621 621
 		return err
622 622
 	}
623 623
 	res.Body.Close()
624
-	if res.StatusCode != 200 && res.StatusCode != 201 {
624
+	if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
625 625
 		return newJSONError(fmt.Sprintf("Internal server error: %d trying to push tag %s on %s", res.StatusCode, tag, reference.Path(remote)), res)
626 626
 	}
627 627
 	return nil
... ...
@@ -675,13 +675,13 @@ func (r *Session) PushImageJSONIndex(remote reference.Named, imgList []*ImgData,
675 675
 	}
676 676
 	defer res.Body.Close()
677 677
 
678
-	if res.StatusCode == 401 {
678
+	if res.StatusCode == http.StatusUnauthorized {
679 679
 		return nil, errcode.ErrorCodeUnauthorized.WithArgs()
680 680
 	}
681 681
 
682 682
 	var tokens, endpoints []string
683 683
 	if !validate {
684
-		if res.StatusCode != 200 && res.StatusCode != 201 {
684
+		if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
685 685
 			errBody, err := ioutil.ReadAll(res.Body)
686 686
 			if err != nil {
687 687
 				logrus.Debugf("Error reading response body: %s", err)
... ...
@@ -699,7 +699,7 @@ func (r *Session) PushImageJSONIndex(remote reference.Named, imgList []*ImgData,
699 699
 			return nil, err
700 700
 		}
701 701
 	} else {
702
-		if res.StatusCode != 204 {
702
+		if res.StatusCode != http.StatusNoContent {
703 703
 			errBody, err := ioutil.ReadAll(res.Body)
704 704
 			if err != nil {
705 705
 				logrus.Debugf("Error reading response body: %s", err)
... ...
@@ -752,7 +752,7 @@ func (r *Session) SearchRepositories(term string, limit int) (*registrytypes.Sea
752 752
 		return nil, errdefs.System(err)
753 753
 	}
754 754
 	defer res.Body.Close()
755
-	if res.StatusCode != 200 {
755
+	if res.StatusCode != http.StatusOK {
756 756
 		return nil, newJSONError(fmt.Sprintf("Unexpected status code %d", res.StatusCode), res)
757 757
 	}
758 758
 	result := new(registrytypes.SearchResults)