/** * Creates an instance of this class. * * @param buffer the session input buffer. * @param parser the line parser. * @param params HTTP parameters. */ public AbstractMessageParser( final SessionInputBuffer buffer, final LineParser parser, final HttpParams params) { super(); if (buffer == null) { throw new IllegalArgumentException("Session input buffer may not be null"); } if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } this.sessionBuffer = buffer; this.maxHeaderCount = params.getIntParameter( CoreConnectionPNames.MAX_HEADER_COUNT, -1); this.maxLineLen = params.getIntParameter( CoreConnectionPNames.MAX_LINE_LENGTH, -1); this.lineParser = (parser != null) ? parser : BasicLineParser.DEFAULT; this.headerLines = new ArrayList<CharArrayBuffer>(); this.state = HEAD_LINE; }
private void acceptClient(@NotNull Socket client) throws IOException { final SessionInputBuffer inputBuffer = wrapInputStream(client.getInputStream()); final HttpMessageParser<HttpRequest> parser = new DefaultHttpRequestParser(inputBuffer, new BasicLineParser(), new DefaultHttpRequestFactory(), MessageConstraints.DEFAULT ); final SessionOutputBuffer outputBuffer = wrapOutputStream(client.getOutputStream()); final HttpMessageWriter<HttpResponse> writer = new DefaultHttpResponseWriter(outputBuffer); while (!socket.isClosed()) { try { service(inputBuffer, outputBuffer, parser, writer); } catch (ConnectionClosedException ignored) { break; } catch (HttpException e) { log.error(e.getMessage(), e); break; } } }
public DefaultHttpResponseParserFactory( final LineParser lineParser, final HttpResponseFactory responseFactory) { super(); this.lineParser = lineParser != null ? lineParser : BasicLineParser.INSTANCE; this.responseFactory = responseFactory != null ? responseFactory : DefaultHttpResponseFactory.INSTANCE; }
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; }
public static HttpResponse parseRecordedResponse(String respstring, MessageMetadata messdata) throws IOException, HttpException { MessageStringInputBuffer buf = new MessageStringInputBuffer(respstring); DefaultHttpResponseParser parser = new DefaultHttpResponseParser(buf, new BasicLineParser(), new RecordedHttpResponseFactory(messdata), new BasicHttpParams()); HttpResponse response = parser.parse(); parseEntity(response, buf); return response; }
private StatusLine parseStatusLine(String line) { if (TextUtils.isEmpty(line)) { return null; } return BasicLineParser.parseStatusLine(line, new BasicLineParser()); }
private Header parseHeader(String line) { return BasicLineParser.parseHeader(line, new BasicLineParser()); }
/** * <p>Returns an HTTP response object parsed from the ARC record payload.<p> * <p>Note: The payload is parsed on-demand, but is only parsed once. The * parsed data is saved for subsequent calls.</p> * * @return The ARC record payload as an HTTP response object. See the Apache * HttpComponents project. */ public HttpResponse getHttpResponse() throws IOException, HttpException { if (this._httpResponse != null) { return this._httpResponse; } if (this._payload == null) { LOG.error("Unable to parse HTTP response: Payload has not been set"); return null; } if (this._url != null && !this._url.startsWith("http://") && !this._url.startsWith("https://")) { LOG.error("Unable to parse HTTP response: URL protocol is not HTTP"); return null; } this._httpResponse = null; // Find where the HTTP headers stop int end = this._searchForCRLFCRLF(this._payload); if (end == -1) { LOG.error("Unable to parse HTTP response: End of HTTP headers not found"); return null; } // Parse the HTTP status line and headers DefaultHttpResponseParser parser = new DefaultHttpResponseParser( new ByteArraySessionInputBuffer(this._payload, 0, end), new BasicLineParser(), new DefaultHttpResponseFactory(), new BasicHttpParams() ); this._httpResponse = parser.parse(); if (this._httpResponse == null) { LOG.error("Unable to parse HTTP response"); return null; } // Set the reset of the payload as the HTTP entity. Use an InputStreamEntity // to avoid a memory copy. //trim trailing '\n' if it exists int entityLength = _payload.length-end; if (_payload.length > 0 && _payload[_payload.length-1]=='\n') { entityLength--; } InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(this._payload, end, entityLength), entityLength); entity.setContentType(this._httpResponse.getFirstHeader("Content-Type")); entity.setContentEncoding(this._httpResponse.getFirstHeader("Content-Encoding")); this._httpResponse.setEntity(entity); return this._httpResponse; }
/** * Parses HTTP headers from the data receiver stream according to the generic * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3. * * @param inbuffer Session input buffer * @param maxHeaderCount maximum number of headers allowed. If the number * of headers received from the data stream exceeds maxCount value, an * IOException will be thrown. Setting this parameter to a negative value * or zero will disable the check. * @param maxLineLen maximum number of characters for a header line, * including the continuation lines. Setting this parameter to a negative * value or zero will disable the check. * @return array of HTTP headers * @param parser line parser to use. Can be <code>null</code>, in which case * the default implementation of this interface will be used. * * @throws IOException in case of an I/O error * @throws HttpException in case of HTTP protocol violation */ public static Header[] parseHeaders( final SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen, LineParser parser) throws HttpException, IOException { if (parser == null) { parser = BasicLineParser.DEFAULT; } List<CharArrayBuffer> headerLines = new ArrayList<CharArrayBuffer>(); return parseHeaders(inbuffer, maxHeaderCount, maxLineLen, parser, headerLines); }