private MessageHeaders parseMessageHeaders(StringBuilder sb) { MessageHeaders messageHeaders = new MessageHeaders(); try { // read headers until no more are found while (true) { String headerName = readUntil(sb, HEADER_SEPARATOR); expect(sb, HEADER_SEPARATOR); String headerValue = readUntil(sb, CRLF); expect(sb, CRLF); messageHeaders.addHeader(headerName, headerValue); } } catch (RequestMessageParsingException e) { // done reading headers } return messageHeaders; }
public RequestMessage parseRequestMessage(String msg) throws RequestMessageParsingException { StringBuilder sb = new StringBuilder(msg); RequestMethod requestMethod = parseRequestMethod(sb); RequestTarget requestTarget = parseRequestTarget(sb); HttpVersion httpVersion = parseHttpVersion(sb); MessageHeaders messageHeaders = parseMessageHeaders(sb); String messageBody = parseMessageBody(sb); Map<String, String> headers = messageHeaders.getHeaders(); if (headers.containsKey(HOST_HEADER_NAME)) { String hostName = headers.get(HOST_HEADER_NAME); requestTarget.setHost(hostName); } else { System.err.println("Host header not found."); } return new RequestMessage( requestMethod, requestTarget, httpVersion, messageHeaders, messageBody); }