/** * Get the HTTP response code from the request. * <p> * Roughly the same as <code>c.getResponseCode()</code> but the * ConnectException is translated to be more understandable. * * @param c * connection the code should be obtained from. * @return r HTTP status code, usually 200 to indicate success. See * {@link org.eclipse.jgit.transport.http.HttpConnection} for other * defined constants. * @throws java.io.IOException * communications error prevented obtaining the response code. * @since 3.3 */ public static int response(HttpConnection c) throws IOException { try { return c.getResponseCode(); } catch (ConnectException ce) { final URL url = c.getURL(); final String host = (url == null) ? "<null>" : url.getHost(); //$NON-NLS-1$ // The standard J2SE error message is not very useful. // if ("Connection timed out: connect".equals(ce.getMessage())) //$NON-NLS-1$ throw new ConnectException(MessageFormat.format(JGitText.get().connectionTimeOut, host)); throw new ConnectException(ce.getMessage() + " " + host); //$NON-NLS-1$ } }
conn.setInstanceFollowRedirects(false); conn.setRequestMethod(method); conn.setUseCaches(false); if (acceptEncoding == AcceptEncoding.GZIP) { conn.setRequestProperty(HDR_ACCEPT_ENCODING, ENCODING_GZIP); conn.setRequestProperty(HDR_PRAGMA, "no-cache"); //$NON-NLS-1$ if (UserAgent.get() != null) { conn.setRequestProperty(HDR_USER_AGENT, UserAgent.get()); conn.setConnectTimeout(effTimeOut); conn.setReadTimeout(effTimeOut); conn.setRequestProperty(entry.getKey(), entry.getValue());
/** * Disable SSL and hostname verification for given HTTP connection * * @param conn * a {@link org.eclipse.jgit.transport.http.HttpConnection} * object. * @throws java.io.IOException * @since 4.3 */ public static void disableSslVerify(HttpConnection conn) throws IOException { final TrustManager[] trustAllCerts = new TrustManager[] { new DummyX509TrustManager() }; try { conn.configure(null, trustAllCerts, null); conn.setHostnameVerifier(new DummyHostnameVerifier()); } catch (KeyManagementException e) { throw new IOException(e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new IOException(e.getMessage()); } }
void openStream() throws IOException { conn = httpOpen(METHOD_POST, new URL(baseUrl, serviceName), AcceptEncoding.GZIP); conn.setInstanceFollowRedirects(false); conn.setDoOutput(true); conn.setRequestProperty(HDR_CONTENT_TYPE, requestType); conn.setRequestProperty(HDR_ACCEPT, responseType); }
@Override protected OutputStream overflow() throws IOException { openStream(); conn.setChunkedStreamingMode(0); return conn.getOutputStream(); } }
void openResponse() throws IOException { final int status = HttpSupport.response(conn); if (status != HttpConnection.HTTP_OK) { throw new TransportException(uri, status + " " //$NON-NLS-1$ + conn.getResponseMessage()); } final String contentType = conn.getContentType(); if (!responseType.equals(contentType)) { conn.getInputStream().close(); throw wrongContentType(responseType, contentType); } }
conn.setRequestProperty(HDR_CONTENT_ENCODING, ENCODING_GZIP); conn.setFixedLengthStreamingMode((int) buf.length()); try (OutputStream httpOut = conn.getOutputStream()) { buf.writeTo(httpOut, null); throw createNotFoundException(uri, conn.getURL(), conn.getResponseMessage()); currentUri = redirect(conn.getHeaderField(HDR_LOCATION), '/' + serviceName, redirects++); try { MessageFormat.format( JGitText.get().authenticationNotSupported, conn.getURL())); case NEGOTIATE:
HttpSupport.disableSslVerify(conn); conn.setRequestMethod(METHOD_HEAD); conn.connect(); int status = conn.getResponseCode(); if (status == SC_OK) { String contentLengthHeader = conn .getHeaderField(HDR_CONTENT_LENGTH); if (contentLengthHeader != null) { return Integer.parseInt(contentLengthHeader);
private void uploadFile(Protocol.ObjectInfo o, Protocol.Action uploadAction, Path path) throws IOException, CorruptMediaFile { HttpConnection contentServer = LfsConnectionFactory .getLfsContentConnection(getRepository(), uploadAction, METHOD_PUT); contentServer.setDoOutput(true); try (OutputStream out = contentServer .getOutputStream()) { long size = Files.copy(path, out); if (size != o.size) { throw new CorruptMediaFile(path, o.size, size); } } int responseCode = contentServer.getResponseCode(); if (responseCode != HTTP_OK) { throw new IOException(MessageFormat.format( LfsText.get().serverFailure, contentServer.getURL(), Integer.valueOf(responseCode))); } } }
if (useSmartHttp) { String exp = "application/x-" + service + "-advertisement"; //$NON-NLS-1$ //$NON-NLS-2$ conn.setRequestProperty(HDR_ACCEPT, exp + ", */*"); //$NON-NLS-1$ } else { conn.setRequestProperty(HDR_ACCEPT, "*/*"); //$NON-NLS-1$ && conn.getHeaderField(HDR_WWW_AUTHENTICATE) != null) authMethod = HttpAuthMethod.scanResponse(conn, ignoreTypes); return conn; conn.getResponseMessage()); Integer.valueOf(status))); URIish newUri = redirect(conn.getHeaderField(HDR_LOCATION), Constants.INFO_REFS, redirects++); setURI(newUri); break; default: String err = status + " " + conn.getResponseMessage(); //$NON-NLS-1$ throw new TransportException(uri, err);
HttpSupport.METHOD_POST, Protocol.OPERATION_DOWNLOAD); Gson gson = Protocol.gson(); lfsServerConn.getOutputStream() .write(gson .toJson(LfsConnectionFactory .toRequest(Protocol.OPERATION_DOWNLOAD, res)) .getBytes(UTF_8)); int responseCode = lfsServerConn.getResponseCode(); if (responseCode != HttpConnection.HTTP_OK) { throw new IOException( MessageFormat.format(LfsText.get().serverFailure, lfsServerConn.getURL(), Integer.valueOf(responseCode))); new InputStreamReader(lfsServerConn.getInputStream(), UTF_8))) { Protocol.Response resp = gson.fromJson(reader, throw new IOException(MessageFormat.format( LfsText.get().inconsistentContentLength, lfsServerConn.getURL(), Long.valueOf(ptr.getSize()), Long.valueOf(o.size))); responseCode = contentServerConn.getResponseCode(); if (responseCode != HttpConnection.HTTP_OK) { throw new IOException( MessageFormat.format(LfsText.get().serverFailure, contentServerConn.getURL(), Integer.valueOf(responseCode)));
final String nonce = params.get("nonce"); //$NON-NLS-1$ final String cnonce = params.get("cnonce"); //$NON-NLS-1$ final String uri = uri(conn.getURL()); final String qop = params.get("qop"); //$NON-NLS-1$ final String method = conn.getRequestMethod(); v.append('"'); conn.setRequestProperty(HDR_AUTHORIZATION, type.getSchemeName() + " " + v); //$NON-NLS-1$
private Map<String, LfsPointer> requestBatchUpload(HttpConnection api, Set<LfsPointer> toPush) throws IOException { LfsPointer[] res = toPush.toArray(new LfsPointer[0]); Map<String, LfsPointer> oidStr2ptr = new HashMap<>(); for (LfsPointer p : res) { oidStr2ptr.put(p.getOid().name(), p); } Gson gson = Protocol.gson(); api.getOutputStream().write( gson.toJson(toRequest(OPERATION_UPLOAD, res)).getBytes(UTF_8)); int responseCode = api.getResponseCode(); if (responseCode != HTTP_OK) { throw new IOException( MessageFormat.format(LfsText.get().serverFailure, api.getURL(), Integer.valueOf(responseCode))); } return oidStr2ptr; }
HttpConnection connection = HttpTransport.getConnectionFactory().create( url, HttpSupport.proxyFor(ProxySelector.getDefault(), url)); connection.setDoOutput(true); if (url.getProtocol().equals(SCHEME_HTTPS) && !config.getBoolean(HttpConfig.HTTP, HttpSupport.disableSslVerify(connection); connection.setRequestMethod(method); connection.setRequestProperty(HDR_ACCEPT, Protocol.CONTENTTYPE_VND_GIT_LFS_JSON); connection.setRequestProperty(HDR_CONTENT_TYPE, Protocol.CONTENTTYPE_VND_GIT_LFS_JSON); additionalHeaders .forEach((k, v) -> connection.setRequestProperty(k, v)); return connection;
/** * Extract a HTTP header from the response. * * @param c * connection the header should be obtained from. * @param headerName * the header name * @return the header value * @throws java.io.IOException * communications error prevented obtaining the header. * @since 4.7 */ public static String responseHeader(final HttpConnection c, final String headerName) throws IOException { return c.getHeaderField(headerName); }
@Nonnull protected HttpConnection configure(@Nonnull HttpConnection input) throws IOException { try { final KeyStore keyStore = loadKeyStore(); final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); final TrustManager[] defaultTrustManagers = trustManagerFactory.getTrustManagers(); final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keyStore, null); final KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); input.configure(keyManagers, defaultTrustManagers, null); input.setInstanceFollowRedirects(true); return input; } catch (final GeneralSecurityException e) { throw new IOException(e.getMessage(), e); } }
@Override void configureRequest(HttpConnection conn) throws IOException { GSSManager gssManager = GSS_MANAGER_FACTORY.newInstance(conn .getURL()); String host = conn.getURL().getHost(); String peerName = "HTTP@" + host.toLowerCase(Locale.ROOT); //$NON-NLS-1$ try { GSSName gssName = gssManager.createName(peerName, GSSName.NT_HOSTBASED_SERVICE); GSSContext context = gssManager.createContext(gssName, OID, null, GSSContext.DEFAULT_LIFETIME); // Respect delegation policy in HTTP/SPNEGO. context.requestCredDeleg(true); byte[] token = context.initSecContext(prevToken, 0, prevToken.length); conn.setRequestProperty(HDR_AUTHORIZATION, getType().getSchemeName() + " " + Base64.encodeBytes(token)); //$NON-NLS-1$ } catch (GSSException e) { throw new IOException(e); } } }
final InputStream openInputStream(HttpConnection conn) throws IOException { InputStream input = conn.getInputStream(); if (ENCODING_GZIP.equals(conn.getHeaderField(HDR_CONTENT_ENCODING))) input = new GZIPInputStream(input); return input; }
void sendRequest() throws IOException { // Try to compress the content, but only if that is smaller. TemporaryBuffer buf = new TemporaryBuffer.Heap(http.postBuffer); try { GZIPOutputStream gzip = new GZIPOutputStream(buf); out.writeTo(gzip, null); gzip.close(); if (out.length() < buf.length()) buf = out; } catch (IOException err) { // Most likely caused by overflowing the buffer, meaning // its larger if it were compressed. Don't compress. buf = out; } openStream(); if (buf != out) conn.setRequestProperty(HDR_CONTENT_ENCODING, ENCODING_GZIP); conn.setFixedLengthStreamingMode((int) buf.length()); final OutputStream httpOut = conn.getOutputStream(); try { buf.writeTo(httpOut, null); } finally { httpOut.close(); } }
.create(contentUrl, HttpSupport .proxyFor(ProxySelector.getDefault(), contentUrl)); contentServerConn.setRequestMethod(method); if (action.header != null) { action.header.forEach( (k, v) -> contentServerConn.setRequestProperty(k, v)); contentServerConn.setRequestProperty(HDR_ACCEPT_ENCODING, ENCODING_GZIP);