/** * @return the first {@code Content-Length} header, or {@code -1} if not found * @throws NumberFormatException if parsing the content length header fails */ public long getContentLength() throws NumberFormatException { String contentLengthHeader = httpResponse.getHeaders().getFirstHeaderStringValue(HttpHeaders.CONTENT_LENGTH); if (contentLengthHeader == null) { return -1; } try { return Long.parseLong(contentLengthHeader); } catch (NumberFormatException ex) { return -1; } }
@InternalApi("Visible for testing") static boolean headerContainsMetadataFlavor(HttpResponse response) { String metadataFlavorValue = response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor"); return "Google".equals(metadataFlavorValue); }
private String makeCall(HttpTransport transport) throws IOException { HttpRequest get = transport.createRequestFactory() .buildPostRequest(new GenericUrl(INRPUT_LOGIN_SERVER), null) .setFollowRedirects(false) .setThrowExceptionOnExecuteError(false); HttpResponse response = get.execute(); if (response.getStatusCode() != 302) { throw new IOException("Unexpected return code: " + response.getStatusCode() + "\nMessage:\n" + response.getStatusMessage()); } String cookieValue = response.getHeaders().getFirstHeaderStringValue("set-cookie"); if (Strings.isNullOrEmpty(cookieValue)) { throw new IOException("Couldn't extract cookie value from headers: " + response.getHeaders()); } return cookieValue; } }
boolean verifyJwtForAppEngine(HttpRequest request, long projectNumber, String projectId) throws Exception { // Check for iap jwt header in incoming request String jwt = request.getHeaders().getFirstHeaderStringValue("x-goog-iap-jwt-assertion"); if (jwt == null) { return false; } return verifyJwt( jwt, String.format("/projects/%s/apps/%s", Long.toUnsignedString(projectNumber), projectId)); }
boolean verifyJwtForComputeEngine( HttpRequest request, long projectNumber, long backendServiceId) throws Exception { // Check for iap jwt header in incoming request String jwtToken = request.getHeaders() .getFirstHeaderStringValue("x-goog-iap-jwt-assertion"); if (jwtToken == null) { return false; } return verifyJwt( jwtToken, String.format( "/projects/%s/global/backendServices/%s", Long.toUnsignedString(projectNumber), Long.toUnsignedString(backendServiceId))); }
/** * @return the first {@code Content-Length} header, or {@code -1} if not found * @throws NumberFormatException if parsing the content length header fails */ public long getContentLength() throws NumberFormatException { String contentLengthHeader = httpResponse.getHeaders().getFirstHeaderStringValue(HttpHeaders.CONTENT_LENGTH); if (contentLengthHeader == null) { return -1; } try { return Long.parseLong(contentLengthHeader); } catch (NumberFormatException ex) { return -1; } }
/** * @return the first {@code Content-Length} header, or {@code -1} if not found * @throws NumberFormatException if parsing the content length header fails */ public long getContentLength() throws NumberFormatException { String contentLengthHeader = httpResponse.getHeaders().getFirstHeaderStringValue(HttpHeaders.CONTENT_LENGTH); if (contentLengthHeader == null) { return -1; } try { return Long.parseLong(contentLengthHeader); } catch (NumberFormatException ex) { return -1; } }
@InternalApi("Visible for testing") static boolean headerContainsMetadataFlavor(HttpResponse response) { String metadataFlavorValue = response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor"); return "Google".equals(metadataFlavorValue); }
/** Initializes metadata (size, encoding, etc) from HTTP {@code headers}. */ @VisibleForTesting protected void initMetadata(HttpHeaders headers) throws IOException { checkState( !metadataInitialized, "can not initialize metadata, it already initialized for '%s'", resourceIdString); long sizeFromMetadata; String range = headers.getContentRange(); if (range != null) { sizeFromMetadata = Long.parseLong(range.substring(range.lastIndexOf('/') + 1)); } else { sizeFromMetadata = headers.getContentLength(); } String generation = headers.getFirstHeaderStringValue("x-goog-generation"); initMetadata(headers.getContentEncoding(), sizeFromMetadata, generation); }
@Override public void interceptResponse(HttpResponse response) throws IOException { if (!LOG.isDebugEnabled()) { return; } String uploadId = response.getHeaders().getFirstHeaderStringValue(UPLOAD_HEADER); if (uploadId == null) { return; } GenericUrl url = response.getRequest().getUrl(); // The check for no upload id limits the output to one log line per upload. // The check for upload type makes sure this is an upload and not a read. if (url.get(UPLOAD_ID_PARAM) == null && url.get(UPLOAD_TYPE_PARAM) != null) { LOG.debug( "Upload ID for url {} on worker {} is {}", url, System.getProperty("worker_id"), uploadId); } } }
@VisibleForTesting RequestInfo buildRequestInfo(HttpRequest request) { RequestInfo.Builder requestBuilder = new RequestInfo.Builder(); if (request != null) { requestBuilder.withServiceName("reportdownload").withMethodName(request.getRequestMethod()); try { requestBuilder.withUrl(request.getUrl().toURL().toString()); } catch (IllegalArgumentException e) { requestBuilder.withUrl("Malformed URL: " + request.getUrl()); } if (request.getHeaders() != null) { String clientCustomerId = request.getHeaders().getFirstHeaderStringValue(CLIENT_CUSTOMER_ID); requestBuilder.withContext(CLIENT_CUSTOMER_ID, clientCustomerId); } // Get the payload from the request. requestBuilder.withPayload(extractPayload(request.getHeaders(), request.getContent())); } return requestBuilder.build(); }
@VisibleForTesting RequestInfo buildRequestInfo(HttpRequest request) { RequestInfo.Builder requestBuilder = new RequestInfo.Builder(); if (request != null) { requestBuilder.withServiceName("reportdownload").withMethodName(request.getRequestMethod()); try { requestBuilder.withUrl(request.getUrl().toURL().toString()); } catch (IllegalArgumentException e) { requestBuilder.withUrl("Malformed URL: " + request.getUrl()); } if (request.getHeaders() != null) { String clientCustomerId = request.getHeaders().getFirstHeaderStringValue(CLIENT_CUSTOMER_ID); requestBuilder.withContext(CLIENT_CUSTOMER_ID, clientCustomerId); } // Get the payload from the request. requestBuilder.withPayload(extractPayload(request.getHeaders(), request.getContent())); } return requestBuilder.build(); }