private int readInt(InputStream in) throws IOException { String intString = Streams.readAsciiLine(in); try { return Integer.parseInt(intString); } catch (NumberFormatException e) { throw new IOException("expected an int but was \"" + intString + "\""); } }
private void readChunkSize() throws IOException { // read the suffix of the previous chunk if (bytesRemainingInChunk != NO_CHUNK_YET) { Streams.readAsciiLine(in); } String chunkSizeString = Streams.readAsciiLine(in); int index = chunkSizeString.indexOf(";"); if (index != -1) { chunkSizeString = chunkSizeString.substring(0, index); } try { bytesRemainingInChunk = Integer.parseInt(chunkSizeString.trim(), 16); } catch (NumberFormatException e) { throw new IOException("Expected a hex chunk size, but was " + chunkSizeString); } if (bytesRemainingInChunk == 0) { hasMoreChunks = false; httpEngine.readTrailers(); endOfInput(true); } }
private Certificate[] readCertArray(InputStream in) throws IOException { int length = readInt(in); if (length == -1) { return null; } try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); Certificate[] result = new Certificate[length]; for (int i = 0; i < result.length; i++) { String line = Streams.readAsciiLine(in); byte[] bytes = Base64.decode(Strings.getBytes(line,Charsets.US_ASCII)); result[i] = certificateFactory.generateCertificate( new ByteArrayInputStream(bytes)); } return result; } catch (CertificateException e) { throw new IOException(e.toString()); } }
public Entry(InputStream in) throws IOException { try { uri = Streams.readAsciiLine(in); requestMethod = Streams.readAsciiLine(in); varyHeaders = new RawHeaders(); int varyRequestHeaderLineCount = readInt(in); for (int i = 0; i < varyRequestHeaderLineCount; i++) { varyHeaders.addLine(Streams.readAsciiLine(in)); responseHeaders.setStatusLine(Streams.readAsciiLine(in)); int responseHeaderLineCount = readInt(in); for (int i = 0; i < responseHeaderLineCount; i++) { responseHeaders.addLine(Streams.readAsciiLine(in)); String blank = Streams.readAsciiLine(in); if (!Strings.isEmpty(blank)) { throw new IOException("expected \"\" but was \"" + blank + "\""); cipherSuite = Streams.readAsciiLine(in); peerCertificates = readCertArray(in); localCertificates = readCertArray(in);
private void readHeaders(RawHeaders headers) throws IOException { // parse the result headers until the first blank line String line; while (!Strings.isEmpty(line = Streams.readAsciiLine(socketIn))) { headers.addLine(line); } CookieHandler cookieHandler = CookieHandler.getDefault(); if (cookieHandler != null) { cookieHandler.put(uri, headers.toMultimap()); } }
private void readResponseHeaders() throws IOException { RawHeaders headers; do { headers = new RawHeaders(); headers.setStatusLine(Streams.readAsciiLine(socketIn)); readHeaders(headers); } while (headers.getResponseCode() == HTTP_CONTINUE); setResponse(new ResponseHeaders(uri, headers), null); }