private static HttpRequest readPayload(final BoundSessionInputBuffer buffer) throws IOException { DefaultHttpRequestParser requestParser = new DefaultHttpRequestParser(buffer); try { return requestParser.parse(); } catch (HttpException e) { throw new WarcFormatException("Can't parse the request", e); } }
public static HttpRequest parseRecordedRequest(String reqstring, MessageMetadata messdata) throws IOException, HttpException{ MessageStringInputBuffer buf = new MessageStringInputBuffer(reqstring); DefaultHttpRequestParser parser = new DefaultHttpRequestParser(buf, new BasicLineParser(), new RecordedHttpRequestFactory(messdata), new BasicHttpParams()); HttpRequest request = parser.parse(); if(request instanceof HttpEntityEnclosingRequest){ parseEntity((HttpEntityEnclosingRequest)request, buf); } return request; }
private HttpRequest parseRequest(Element element, Evaluator evaluator, ResultRecorder resultRecorder) { String contents = getTextAndRemoveIndent(element); contents = replaceVariableReferences(evaluator, contents, resultRecorder); SessionInputBufferImpl buffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), contents.length()); buffer.bind(new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8))); DefaultHttpRequestParser defaultHttpRequestParser = new DefaultHttpRequestParser(buffer); LinkedListMultimap<String, String> queryParameters = LinkedListMultimap.create(); String method = ""; String url = ""; LinkedListMultimap<String, String> headers = LinkedListMultimap.create(); String body = null; String server = null; try { org.apache.http.HttpRequest httpRequest = defaultHttpRequestParser.parse(); method = httpRequest.getRequestLine().getMethod(); url = httpRequest.getRequestLine().getUri(); if (url.startsWith("#")) { url = "" + evaluator.evaluate(url); } Matcher matcher = Pattern.compile("(https?://[^/]+)(/.*)").matcher(url); if (matcher.matches()) { server = matcher.group(1); url = matcher.group(2); } if (url.contains("?")) { String[] urlAndQueryParameters = url.split("\\?"); url = urlAndQueryParameters[0]; for (String queryParameter : urlAndQueryParameters[1].split("&")) { String[] parameter = queryParameter.split("="); queryParameters.put(parameter[0], parameter[1]); } } for (Header header : httpRequest.getAllHeaders()) { headers.put(header.getName(), header.getValue()); } if (buffer.hasBufferedData()) { body = ""; while (buffer.hasBufferedData()) { body += (char) buffer.read(); } } } catch (IOException | HttpException e) { e.printStackTrace(); } return new HttpRequest(method, url, headers, body, server, queryParameters); }
/** * Creates an instance of {@link HttpMessageParser} to be used for parsing * HTTP requests received over this connection. * <p> * This method can be overridden in a super class in order to provide * a different implementation of the {@link HttpMessageParser} interface or * to pass a different implementation of the {@link LineParser} to the * the {@link DefaultHttpRequestParser} constructor. * * @param buffer the session input buffer. * @param requestFactory the HTTP request factory. * @param params HTTP parameters. * @return HTTP message parser. */ protected HttpMessageParser<HttpRequest> createRequestParser( final SessionInputBuffer buffer, final HttpRequestFactory requestFactory, final HttpParams params) { return new DefaultHttpRequestParser(buffer, null, requestFactory, params); }