List of usage examples for org.apache.http.impl.io AbstractMessageParser parseHeaders
public static Header[] parseHeaders(SessionInputBuffer sessionInputBuffer, int i, int i2, LineParser lineParser) throws HttpException, IOException
From source file:it.unimi.di.law.warc.io.AbstractWarcReader_NYU.java
protected WarcRecord read(final boolean consecutive) throws IOException { if (consecutive && this.payload != null) { this.payload.consume(); this.payload = null; this.line.clear(); // check WARC version if (!(this.version.getMajor() == 0 && this.version.getMinor() == 18)) { this.buffer.readLine(this.line); this.buffer.readLine(this.line); } else {// w w w.ja va 2 s .c om if (!this.skip) { this.buffer.readLine(this.line); this.skip = true; } } if (line.length() != 0) throw new WarcFormatException("Missing CRLFs at WARC record end, got \"" + line + "\""); this.line.clear(); } // first header line this.version = parseHead(); if (this.version == null) return null; if (VERSION && (this.version.getMajor() != 1 || this.version.getMinor() != 0)) throw new IllegalArgumentException("Unsupported WARC version " + this.version); // rest of headers final HeaderGroup warcHeaders = new HeaderGroup(); try { warcHeaders.setHeaders(AbstractMessageParser.parseHeaders(this.buffer, -1, -1, null)); } catch (HttpException e) { throw new WarcFormatException("Can't parse WARC headers", e); } // payload final Header payloadLengthHeader = WarcHeader.getFirstHeader(warcHeaders, WarcHeader.Name.CONTENT_LENGTH); if (payloadLengthHeader == null) throw new WarcFormatException("Missing 'Content-Length' WARC header"); long payloadLength = -1; try { payloadLength = Long.parseLong(payloadLengthHeader.getValue()); } catch (NumberFormatException e) { throw new WarcFormatException( "Can't parse 'Content-Length' WARC header (is \"" + payloadLengthHeader.getValue() + "\")", e); } this.payload = new BoundSessionInputBuffer(this.buffer, payloadLength); return AbstractWarcRecord.fromPayload(warcHeaders, this.payload); }
From source file:org.saintandreas.serket.ssdp.Message.java
public static Message parseMessage(DatagramPacket packet) throws IOException, HttpException { Type type;/* w w w.j a v a 2s . com*/ Map<String, Header> headers = new HashMap<String, Header>(); String usn = null; LineParser parser = new BasicLineParser(); SessionInputBuffer inBuffer = new ByteArrayInputBuffer(packet.getData()); String command = inBuffer.readLine(); Header[] hs = AbstractMessageParser.parseHeaders(inBuffer, 64, 1024, parser); for (Header h : hs) { headers.put(h.getName(), h); } if (Message.NOTIFY_START.startsWith(command)) { Header h = headers.get("NTS"); usn = headers.get("USN").getValue(); if ("ssdp:alive".equals(h.getValue())) { // LogFactory.getLog(SSDPTests.class).debug("Notify message alive " + usn); type = Type.NOTIFY_ALIVE; } else if ("ssdp:update".equals(h.getValue())) { // LogFactory.getLog(SSDPTests.class).debug("Notify message update " + usn); type = Type.NOTIFY_UPDATE; } else if ("ssdp:byebye".equals(h.getValue())) { // LogFactory.getLog(SSDPTests.class).debug("Notify message byebye " + usn); type = Type.NOTIFY_BYEBYE; } else throw new RuntimeException("unknown type"); } else if (Message.SEARCH_START.startsWith(command)) { usn = headers.get("ST").getValue(); // LogFactory.getLog(SSDPTests.class).debug("Search message " + usn); type = Type.SEARCH; } else if (Message.HTTP_OK_START.startsWith(command)) { // LogFactory.getLog(SSDPTests.class).debug("Response message"); type = Type.RESPONSE; } else throw new RuntimeException("unknown type"); return new Message(type, headers, usn, packet, new String(packet.getData(), 0, packet.getLength(), Charsets.US_ASCII)); }