@@ -62,7 +62,7 @@ public HttpURLConnection connect(URL url) throws IOException {
6262 public GitHubResponse .ResponseInfo send (GitHubRequest request ) throws IOException {
6363 HttpURLConnection connection ;
6464 try {
65- connection = HttpURLConnectionResponseInfo . setupConnection (httpConnector , request );
65+ connection = setupConnection (httpConnector , request );
6666 } catch (IOException e ) {
6767 // An error in here should be wrapped to bypass http exception wrapping.
6868 throw new GHIOException (e .getMessage (), e );
@@ -76,6 +76,63 @@ public GitHubResponse.ResponseInfo send(GitHubRequest request) throws IOExceptio
7676 return new HttpURLConnectionResponseInfo (request , statusCode , headers , connection );
7777 }
7878
79+ @ Nonnull
80+ static HttpURLConnection setupConnection (@ Nonnull HttpConnector connector , @ Nonnull GitHubRequest request )
81+ throws IOException {
82+ HttpURLConnection connection = connector .connect (request .url ());
83+ setRequestMethod (request .method (), connection );
84+ buildRequest (request , connection );
85+
86+ return connection ;
87+ }
88+
89+ /**
90+ * Set up the request parameters or POST payload.
91+ */
92+ private static void buildRequest (GitHubRequest request , HttpURLConnection connection ) throws IOException {
93+ for (Map .Entry <String , List <String >> e : request .allHeaders ().entrySet ()) {
94+ List <String > v = e .getValue ();
95+ if (v != null )
96+ connection .setRequestProperty (e .getKey (), String .join (", " , v ));
97+ }
98+
99+ if (request .inBody ()) {
100+ connection .setDoOutput (true );
101+ IOUtils .copyLarge (request .body (), connection .getOutputStream ());
102+ }
103+ }
104+
105+ private static void setRequestMethod (String method , HttpURLConnection connection ) throws IOException {
106+ try {
107+ connection .setRequestMethod (method );
108+ } catch (ProtocolException e ) {
109+ // JDK only allows one of the fixed set of verbs. Try to override that
110+ try {
111+ Field $method = HttpURLConnection .class .getDeclaredField ("method" );
112+ $method .setAccessible (true );
113+ $method .set (connection , method );
114+ } catch (Exception x ) {
115+ throw (IOException ) new IOException ("Failed to set the custom verb" ).initCause (x );
116+ }
117+ // sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
118+ try {
119+ Field $delegate = connection .getClass ().getDeclaredField ("delegate" );
120+ $delegate .setAccessible (true );
121+ Object delegate = $delegate .get (connection );
122+ if (delegate instanceof HttpURLConnection ) {
123+ HttpURLConnection nested = (HttpURLConnection ) delegate ;
124+ setRequestMethod (method , nested );
125+ }
126+ } catch (NoSuchFieldException x ) {
127+ // no problem
128+ } catch (IllegalAccessException x ) {
129+ throw (IOException ) new IOException ("Failed to set the custom verb" ).initCause (x );
130+ }
131+ }
132+ if (!connection .getRequestMethod ().equals (method ))
133+ throw new IllegalStateException ("Failed to set the request method to " + method );
134+ }
135+
79136 /**
80137 * Initial response information supplied to a {@link GitHubResponse.BodyHandler} when a response is initially
81138 * received and before the body is processed.
@@ -95,80 +152,17 @@ static class HttpURLConnectionResponseInfo extends GitHubResponse.ResponseInfo {
95152 this .connection = connection ;
96153 }
97154
98- @ Nonnull
99- static HttpURLConnection setupConnection (@ Nonnull HttpConnector connector , @ Nonnull GitHubRequest request )
100- throws IOException {
101- HttpURLConnection connection = connector .connect (request .url ());
102- setRequestMethod (request .method (), connection );
103- buildRequest (request , connection );
104-
105- return connection ;
106- }
107-
108- /**
109- * Set up the request parameters or POST payload.
110- */
111- private static void buildRequest (GitHubRequest request , HttpURLConnection connection ) throws IOException {
112- for (Map .Entry <String , String > e : request .headers ().entrySet ()) {
113- String v = e .getValue ();
114- if (v != null )
115- connection .setRequestProperty (e .getKey (), v );
116- }
117-
118- if (request .inBody ()) {
119- connection .setDoOutput (true );
120- try (InputStream body = request .body ()) {
121- byte [] bytes = new byte [32768 ];
122- int read ;
123- while ((read = body .read (bytes )) != -1 ) {
124- connection .getOutputStream ().write (bytes , 0 , read );
125- }
126- }
127- }
128- }
129-
130- private static void setRequestMethod (String method , HttpURLConnection connection ) throws IOException {
131- try {
132- connection .setRequestMethod (method );
133- } catch (ProtocolException e ) {
134- // JDK only allows one of the fixed set of verbs. Try to override that
135- try {
136- Field $method = HttpURLConnection .class .getDeclaredField ("method" );
137- $method .setAccessible (true );
138- $method .set (connection , method );
139- } catch (Exception x ) {
140- throw (IOException ) new IOException ("Failed to set the custom verb" ).initCause (x );
141- }
142- // sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
143- try {
144- Field $delegate = connection .getClass ().getDeclaredField ("delegate" );
145- $delegate .setAccessible (true );
146- Object delegate = $delegate .get (connection );
147- if (delegate instanceof HttpURLConnection ) {
148- HttpURLConnection nested = (HttpURLConnection ) delegate ;
149- setRequestMethod (method , nested );
150- }
151- } catch (NoSuchFieldException x ) {
152- // no problem
153- } catch (IllegalAccessException x ) {
154- throw (IOException ) new IOException ("Failed to set the custom verb" ).initCause (x );
155- }
156- }
157- if (!connection .getRequestMethod ().equals (method ))
158- throw new IllegalStateException ("Failed to set the request method to " + method );
159- }
160-
161155 /**
162156 * {@inheritDoc}
163157 */
164- InputStream bodyStream () throws IOException {
158+ public InputStream bodyStream () throws IOException {
165159 return wrapStream (connection .getInputStream ());
166160 }
167161
168162 /**
169163 * {@inheritDoc}
170164 */
171- String errorMessage () {
165+ public String errorMessage () {
172166 String result = null ;
173167 InputStream stream = null ;
174168 try {
@@ -192,7 +186,7 @@ String errorMessage() {
192186 *
193187 */
194188 private InputStream wrapStream (InputStream stream ) throws IOException {
195- String encoding = headerField ("Content-Encoding" );
189+ String encoding = header ("Content-Encoding" );
196190 if (encoding == null || stream == null )
197191 return stream ;
198192 if (encoding .equals ("gzip" ))
0 commit comments