You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpConnection.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2013, 2017 Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport.http;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.net.HttpURLConnection;
  15. import java.net.ProtocolException;
  16. import java.net.URL;
  17. import java.security.KeyManagementException;
  18. import java.security.NoSuchAlgorithmException;
  19. import java.security.SecureRandom;
  20. import java.util.List;
  21. import java.util.Map;
  22. import javax.net.ssl.HostnameVerifier;
  23. import javax.net.ssl.KeyManager;
  24. import javax.net.ssl.TrustManager;
  25. import org.eclipse.jgit.annotations.NonNull;
  26. /**
  27. * The interface of connections used during HTTP communication. This interface
  28. * is that subset of the interface exposed by {@link java.net.HttpURLConnection}
  29. * which is used by JGit
  30. *
  31. * @since 3.3
  32. */
  33. public interface HttpConnection {
  34. /**
  35. * @see HttpURLConnection#HTTP_OK
  36. */
  37. int HTTP_OK = java.net.HttpURLConnection.HTTP_OK;
  38. /**
  39. * @see HttpURLConnection#HTTP_MOVED_PERM
  40. * @since 4.7
  41. */
  42. int HTTP_MOVED_PERM = java.net.HttpURLConnection.HTTP_MOVED_PERM;
  43. /**
  44. * @see HttpURLConnection#HTTP_MOVED_TEMP
  45. * @since 4.9
  46. */
  47. int HTTP_MOVED_TEMP = java.net.HttpURLConnection.HTTP_MOVED_TEMP;
  48. /**
  49. * @see HttpURLConnection#HTTP_SEE_OTHER
  50. * @since 4.9
  51. */
  52. int HTTP_SEE_OTHER = java.net.HttpURLConnection.HTTP_SEE_OTHER;
  53. /**
  54. * HTTP 1.1 additional "temporary redirect" status code; value = 307.
  55. *
  56. * @see #HTTP_MOVED_TEMP
  57. * @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.7">RFC
  58. * 7231, section 6.4.7: 307 Temporary Redirect</a>
  59. * @since 4.9
  60. */
  61. int HTTP_11_MOVED_TEMP = 307;
  62. /**
  63. * HTTP 1.1 additional "permanent redirect" status code; value = 308.
  64. *
  65. * @see #HTTP_MOVED_TEMP
  66. * @see <a href="https://tools.ietf.org/html/rfc7538#section-3">RFC 7538,
  67. * section 3: 308 Permanent Redirect</a>
  68. * @since 5.8
  69. */
  70. int HTTP_11_MOVED_PERM = 308;
  71. /**
  72. * @see HttpURLConnection#HTTP_NOT_FOUND
  73. */
  74. int HTTP_NOT_FOUND = java.net.HttpURLConnection.HTTP_NOT_FOUND;
  75. /**
  76. * @see HttpURLConnection#HTTP_UNAUTHORIZED
  77. */
  78. int HTTP_UNAUTHORIZED = java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
  79. /**
  80. * @see HttpURLConnection#HTTP_FORBIDDEN
  81. */
  82. int HTTP_FORBIDDEN = java.net.HttpURLConnection.HTTP_FORBIDDEN;
  83. /**
  84. * Get response code
  85. *
  86. * @see HttpURLConnection#getResponseCode()
  87. * @return the HTTP Status-Code, or -1
  88. * @throws java.io.IOException
  89. */
  90. int getResponseCode() throws IOException;
  91. /**
  92. * Get URL
  93. *
  94. * @see HttpURLConnection#getURL()
  95. * @return the URL.
  96. */
  97. URL getURL();
  98. /**
  99. * Get response message
  100. *
  101. * @see HttpURLConnection#getResponseMessage()
  102. * @return the HTTP response message, or <code>null</code>
  103. * @throws java.io.IOException
  104. */
  105. String getResponseMessage() throws IOException;
  106. /**
  107. * Get map of header fields
  108. *
  109. * @see HttpURLConnection#getHeaderFields()
  110. * @return a Map of header fields
  111. */
  112. Map<String, List<String>> getHeaderFields();
  113. /**
  114. * Set request property
  115. *
  116. * @see HttpURLConnection#setRequestProperty(String, String)
  117. * @param key
  118. * the keyword by which the request is known (e.g., "
  119. * <code>Accept</code>").
  120. * @param value
  121. * the value associated with it.
  122. */
  123. void setRequestProperty(String key, String value);
  124. /**
  125. * Set request method
  126. *
  127. * @see HttpURLConnection#setRequestMethod(String)
  128. * @param method
  129. * the HTTP method
  130. * @exception ProtocolException
  131. * if the method cannot be reset or if the requested method
  132. * isn't valid for HTTP.
  133. * @throws java.net.ProtocolException
  134. * if any.
  135. */
  136. void setRequestMethod(String method)
  137. throws ProtocolException;
  138. /**
  139. * Set if to use caches
  140. *
  141. * @see HttpURLConnection#setUseCaches(boolean)
  142. * @param usecaches
  143. * a <code>boolean</code> indicating whether or not to allow
  144. * caching
  145. */
  146. void setUseCaches(boolean usecaches);
  147. /**
  148. * Set connect timeout
  149. *
  150. * @see HttpURLConnection#setConnectTimeout(int)
  151. * @param timeout
  152. * an <code>int</code> that specifies the connect timeout value
  153. * in milliseconds
  154. */
  155. void setConnectTimeout(int timeout);
  156. /**
  157. * Set read timeout
  158. *
  159. * @see HttpURLConnection#setReadTimeout(int)
  160. * @param timeout
  161. * an <code>int</code> that specifies the timeout value to be
  162. * used in milliseconds
  163. */
  164. void setReadTimeout(int timeout);
  165. /**
  166. * Get content type
  167. *
  168. * @see HttpURLConnection#getContentType()
  169. * @return the content type of the resource that the URL references, or
  170. * <code>null</code> if not known.
  171. */
  172. String getContentType();
  173. /**
  174. * Get input stream
  175. *
  176. * @see HttpURLConnection#getInputStream()
  177. * @return an input stream that reads from this open connection.
  178. * @exception IOException
  179. * if an I/O error occurs while creating the input stream.
  180. * @throws java.io.IOException
  181. * if any.
  182. */
  183. InputStream getInputStream() throws IOException;
  184. /**
  185. * Get header field. According to
  186. * {@link <a href="https://tools.ietf.org/html/rfc2616#section-4.2">RFC
  187. * 2616</a>} header field names are case insensitive. Header fields defined
  188. * as a comma separated list can have multiple header fields with the same
  189. * field name. This method only returns one of these header fields. If you
  190. * want the union of all values of all multiple header fields with the same
  191. * field name then use {@link #getHeaderFields(String)}
  192. *
  193. * @see HttpURLConnection#getHeaderField(String)
  194. * @param name
  195. * the name of a header field.
  196. * @return the value of the named header field, or <code>null</code> if
  197. * there is no such field in the header.
  198. */
  199. String getHeaderField(@NonNull String name);
  200. /**
  201. * Get all values of given header field. According to
  202. * {@link <a href="https://tools.ietf.org/html/rfc2616#section-4.2">RFC
  203. * 2616</a>} header field names are case insensitive. Header fields defined
  204. * as a comma separated list can have multiple header fields with the same
  205. * field name. This method does not validate if the given header field is
  206. * defined as a comma separated list.
  207. *
  208. * @param name
  209. * the name of a header field.
  210. * @return the list of values of the named header field
  211. * @since 5.2
  212. */
  213. List<String> getHeaderFields(@NonNull String name);
  214. /**
  215. * Get content length
  216. *
  217. * @see HttpURLConnection#getContentLength()
  218. * @return the content length of the resource that this connection's URL
  219. * references, {@code -1} if the content length is not known, or if
  220. * the content length is greater than Integer.MAX_VALUE.
  221. */
  222. int getContentLength();
  223. /**
  224. * Set whether or not to follow HTTP redirects.
  225. *
  226. * @see HttpURLConnection#setInstanceFollowRedirects(boolean)
  227. * @param followRedirects
  228. * a <code>boolean</code> indicating whether or not to follow
  229. * HTTP redirects.
  230. */
  231. void setInstanceFollowRedirects(boolean followRedirects);
  232. /**
  233. * Set if to do output
  234. *
  235. * @see HttpURLConnection#setDoOutput(boolean)
  236. * @param dooutput
  237. * the new value.
  238. */
  239. void setDoOutput(boolean dooutput);
  240. /**
  241. * Set fixed length streaming mode
  242. *
  243. * @see HttpURLConnection#setFixedLengthStreamingMode(int)
  244. * @param contentLength
  245. * The number of bytes which will be written to the OutputStream.
  246. */
  247. void setFixedLengthStreamingMode(int contentLength);
  248. /**
  249. * Get output stream
  250. *
  251. * @see HttpURLConnection#getOutputStream()
  252. * @return an output stream that writes to this connection.
  253. * @throws java.io.IOException
  254. */
  255. OutputStream getOutputStream() throws IOException;
  256. /**
  257. * Set chunked streaming mode
  258. *
  259. * @see HttpURLConnection#setChunkedStreamingMode(int)
  260. * @param chunklen
  261. * The number of bytes to write in each chunk. If chunklen is
  262. * less than or equal to zero, a default value will be used.
  263. */
  264. void setChunkedStreamingMode(int chunklen);
  265. /**
  266. * Get request method
  267. *
  268. * @see HttpURLConnection#getRequestMethod()
  269. * @return the HTTP request method
  270. */
  271. String getRequestMethod();
  272. /**
  273. * Whether we use a proxy
  274. *
  275. * @see HttpURLConnection#usingProxy()
  276. * @return a boolean indicating if the connection is using a proxy.
  277. */
  278. boolean usingProxy();
  279. /**
  280. * Connect
  281. *
  282. * @see HttpURLConnection#connect()
  283. * @throws java.io.IOException
  284. */
  285. void connect() throws IOException;
  286. /**
  287. * Configure the connection so that it can be used for https communication.
  288. *
  289. * @param km
  290. * the keymanager managing the key material used to authenticate
  291. * the local SSLSocket to its peer
  292. * @param tm
  293. * the trustmanager responsible for managing the trust material
  294. * that is used when making trust decisions, and for deciding
  295. * whether credentials presented by a peer should be accepted.
  296. * @param random
  297. * the source of randomness for this generator or null. See
  298. * {@link javax.net.ssl.SSLContext#init(KeyManager[], TrustManager[], SecureRandom)}
  299. * @throws java.security.NoSuchAlgorithmException
  300. * @throws java.security.KeyManagementException
  301. */
  302. void configure(KeyManager[] km, TrustManager[] tm,
  303. SecureRandom random) throws NoSuchAlgorithmException,
  304. KeyManagementException;
  305. /**
  306. * Set the {@link javax.net.ssl.HostnameVerifier} used during https
  307. * communication
  308. *
  309. * @param hostnameverifier
  310. * a {@link javax.net.ssl.HostnameVerifier} object.
  311. * @throws java.security.NoSuchAlgorithmException
  312. * @throws java.security.KeyManagementException
  313. */
  314. void setHostnameVerifier(HostnameVerifier hostnameverifier)
  315. throws NoSuchAlgorithmException, KeyManagementException;
  316. }