}
private HttpRequest parseConnection() throws IOException {
System.out.println("Extract request from connection");
HttpRequest httpRequest = new HttpRequest();
String line = connection.readLine();
LineIterator lineIterator = new LineIterator(line);
httpRequest.setMethod(HttpMethod.valueOf(lineIterator.next()));
httpRequest.setURI(lineIterator.next());
httpRequest.setHttpVersion(lineIterator.next());
Map<String, String> headers = new HashMap<String, String>();
String nextLine = "";
while (!(nextLine = connection.readLine()).equals("")) {
String values[] = nextLine.split(":", 2);
headers.put(values[0], values[1].trim());
}
httpRequest.setRequestHeaders(headers);
if (headers.containsKey(HTTP.CONTENT_LENGTH)) {
int size = Integer.parseInt(headers.get(HTTP.CONTENT_LENGTH));
byte[] data = new byte[size];
int n;
for (int i = 0; i < size && (n = connection.read()) != -1; i++) {
data[i] = (byte) n;
}
httpRequest.setPayload(data);
}
return httpRequest;
}