private HttpResponse _send() { if (httpConnection == null) { open(); } // sends data HttpResponse httpResponse; try { OutputStream outputStream = httpConnection.getOutputStream(); sendTo(outputStream); InputStream inputStream = httpConnection.getInputStream(); httpResponse = HttpResponse.readFrom(inputStream); httpResponse.assignHttpRequest(this); } catch (IOException ioex) { throw new HttpException(ioex); } boolean keepAlive = httpResponse.isConnectionPersistent(); if (!keepAlive) { // closes connection if keep alive is false, or if counter reached 0 httpConnection.close(); httpConnection = null; } return httpResponse; }
/** * Continues using the same keep-alive connection. Don't use any variant of <code>open()</code> * when continuing the communication! First it checks if "Connection" header exist in the response * and if it is equal to "Keep-Alive" value. Then it checks the "Keep-Alive" headers "max" * parameter. If its value is positive, then the existing {@link jodd.http.HttpConnection} from * the request will be reused. If max value is 1, connection will be sent with "Connection: Close" * header, indicating its the last request. When new connection is created, the same {@link * jodd.http.HttpConnectionProvider} that was used for creating initial connection is used for * opening the new connection. * * @param doContinue set it to <code>false</code> to indicate the last connection */ public HttpRequest keepAlive(HttpResponse httpResponse, boolean doContinue) { boolean keepAlive = httpResponse.isConnectionPersistent(); if (keepAlive) { HttpConnection previousConnection = httpResponse.getHttpRequest().httpConnection; if (previousConnection != null) { // keep using the connection! this.httpConnection = previousConnection; this.httpConnectionProvider = httpResponse.getHttpRequest().connectionProvider(); } // keepAlive = true; (already set) } else { // close previous connection httpResponse.close(); // force keep-alive on new request keepAlive = true; } // if we don't want to continue with this persistent session, mark this connection as closed if (!doContinue) { keepAlive = false; } connectionKeepAlive(keepAlive); // if connection is not opened, open it using previous connection provider if (httpConnection == null) { open(httpResponse.getHttpRequest().connectionProvider()); } return this; }