package com.google.net.stubby.transport;

import com.google.net.stubby.Metadata;
import com.google.net.stubby.Status;

import java.net.HttpURLConnection;

/**
 * Constants for GRPC-over-HTTP (or HTTP/2)
 */
public final class HttpUtil {
  /**
   * The Content-Type header name. Defined here since it is not explicitly defined by the HTTP/2
   * spec.
   */
  public static final Metadata.Key<String> CONTENT_TYPE =
      Metadata.Key.of("content-type", Metadata.STRING_MARSHALLER);

  /**
   * Content-Type used for GRPC-over-HTTP/2.
   */
  public static final String CONTENT_TYPE_GRPC = "application/grpc";

  /**
   * The HTTP method used for GRPC requests.
   */
  public static final String HTTP_METHOD = "POST";

  /**
   * Maps HTTP error response status codes to transport codes.
   */
  public static Status httpStatusToGrpcStatus(int httpStatusCode) {
    // Specific HTTP code handling.
    switch (httpStatusCode) {
      case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
        return Status.UNAUTHENTICATED;
      case HttpURLConnection.HTTP_FORBIDDEN: // 403
        return Status.PERMISSION_DENIED;
      default:
    }
    // Generic HTTP code handling.
    if (httpStatusCode < 300) {
      return Status.OK;
    }
    if (httpStatusCode < 400) {
      return Status.UNAVAILABLE;
    }
    if (httpStatusCode < 500) {
      return Status.INVALID_ARGUMENT;
    }
    if (httpStatusCode < 600) {
      return Status.FAILED_PRECONDITION;
    }
    return Status.INTERNAL;
  }

  private HttpUtil() {}
}

@@ -917,24 +917,119 @@
 atusCode) %7B%0A
+      case HttpURLConnection.HTTP_BAD_REQUEST:  // 400%0A        return Status.INVALID_ARGUMENT;%0A
       case H
@@ -1063,16 +1063,17 @@
 HORIZED:
+ 
  // 401%0A
@@ -1155,16 +1155,17 @@
 RBIDDEN:
+ 
  // 403%0A
@@ -1215,57 +1215,830 @@
     
-default:%0A    %7D%0A    // Generic HTTP code handling.
+case HttpURLConnection.HTTP_NOT_FOUND:  // 404%0A        return Status.NOT_FOUND;%0A      case HttpURLConnection.HTTP_CONFLICT:  // 409%0A        return Status.ABORTED;%0A      case 416:  // Requested range not satisfiable%0A        return Status.OUT_OF_RANGE;%0A      case 429:  // Too many requests%0A        return Status.RESOURCE_EXHAUSTED;%0A      case 499:  // Client closed request%0A        return Status.CANCELLED;%0A      case HttpURLConnection.HTTP_NOT_IMPLEMENTED:  // 501%0A        return Status.UNIMPLEMENTED;%0A      case HttpURLConnection.HTTP_UNAVAILABLE:  // 503%0A        return Status.UNAVAILABLE;%0A      case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:  // 504%0A        return Status.DEADLINE_EXCEEDED;%0A    %7D%0A    // Generic HTTP code handling.%0A    if (httpStatusCode %3C 200) %7B%0A      // 1xx and below%0A      return Status.UNKNOWN;%0A    %7D
 %0A   
@@ -2062,24 +2062,37 @@
 de %3C 300) %7B%0A
+      // 2xx%0A
       return
@@ -2137,24 +2137,37 @@
 de %3C 400) %7B%0A
+      // 3xx%0A
       return
@@ -2172,33 +2172,29 @@
 rn Status.UN
-AVAILABLE
+KNOWN
 ;%0A    %7D%0A    
@@ -2217,24 +2217,37 @@
 de %3C 500) %7B%0A
+      // 4xx%0A
       return
@@ -2254,32 +2254,35 @@
  Status.
-INVALID_ARGUMENT
+FAILED_PRECONDITION
 ;%0A    %7D%0A
@@ -2309,24 +2309,37 @@
 de %3C 600) %7B%0A
+      // 5xx%0A
       return
@@ -2346,35 +2346,24 @@
  Status.
-FAILED_PRECONDITION
+INTERNAL
 ;%0A    %7D%0A
@@ -2376,32 +2376,31 @@
 turn Status.
-INTERNAL
+UNKNOWN
 ;%0A  %7D%0A%0A  pri

Change the HTTP code -> GRPC Status mapping to comply with http canonical mapping