javaandroidokhttp

OkHttpClient throws IllegalStateException when server responds with 100-Continue


OkHttpClient version 4-12.0 sends multipart-form request using default configuration ; Sever responds with 100-Continue . After receiving couple of 100-Continue OkHttpClient fails with IllegalStateException State 3.

Other HttpClient (e.g Curl 7.81.0 ,Apache HttpClient 4.5.13, Vertx etc.. ) using the default configuration works as expected with the same server. Is 100-Continue not supported by OkHttpClient or Is there anything else required to make OkHttpClient work with 100-Continue response?

I have tried using Expect:100-continue header in the request, but it makes no difference.

Exception in thread "main" java.lang.IllegalStateException: state: 3
    at okhttp3.internal.http1.Http1ExchangeCodec.newFixedLengthSource(Http1ExchangeCodec.kt:227)
    at okhttp3.internal.http1.Http1ExchangeCodec.openResponseBodySource(Http1ExchangeCodec.kt:132)
    at okhttp3.internal.connection.Exchange.openResponseBody(Exchange.kt:129)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:130)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)

 RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);

        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("somefile",file.getName(),fileBody)
                .build();

        Request request = new Request.Builder()
               // .header("Expect","100-continue")  //makes no difference ... 
                .url(url)
                .post(multipartBody)
                .build();


        try (Response response = client.newCall(request).execute()){
            log.info("Response Code {}",response.code());
        } catch (IOException e) {
...         
}

Issue seems to be similar to https://github.com/square/okhttp/issues/3628 and is present in version 3.11; Any suggestion to get 100-continue working ?

==================================================================

On further investigation

100-continue response from the server

Response{protocol=http/1.1, code=100, message=Continue, url=http://localhost:8443/someendpoint}

CallServerInterceptor:

 response = if (forWebSocket && code == 101) {
        // Connection is upgrading, but we need to ensure interceptors see a non-null response body.
        response.newBuilder()
            .body(EMPTY_RESPONSE)
            .build()
      } else {
        response.newBuilder()
            .body(exchange.openResponseBody(response))
            .build()
      }

I think the issue is caused by setting the state to STATE_READ_RESPONSE_HEADERS on the first 100-Continue response and expect the state to be in STATE_OPEN_RESPONSE_BODY when subsequent 100-Continue responses are received. If not it will throw illegalStateException.

It appears OkHttpClient is not able to ignore subsequent 100-Continue responses from the server when Expect 100-continue is not used. This appears to be a bug, as the client is not able to handle 100-continues from the server when it is not expected. Is this the case?


Solution

  • It is a bug with OkHttpClient and is raised in https://github.com/square/okhttp/issues/8739