📄 httpreader.java
字号:
/* * @(#)$Id: HTTPReader.java,v 1.4 2004/07/02 23:59:21 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package pier.helpers.readers;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import org.apache.log4j.Logger;import pier.exceptions.ReaderException;import services.LocalNode;import services.Output;import services.network.tcpraw.TCPRawClient;import services.network.tcpraw.TCPRawConnection;import util.EasyParser;import util.logging.LogMessage;/** * Class HTTPReader * */public class HTTPReader implements Reader, TCPRawClient { private static Logger logger = Logger.getLogger(HTTPReader.class); protected static final String AGENT = "PIER-SCANNER"; protected static final int READ_BUFFER_SIZE = 1500; protected static final int STATE_DISCONNECTED = 0; protected static final int STATE_PREHEADERS = 1; protected static final int STATE_VERSION = 2; protected static final int STATE_STATUSCODE = 3; protected static final int STATE_STATUSTEXT = 4; protected static final int STATE_HEADERS = 5; protected static final int STATE_DATA = 6; protected static final int STATE_ERROR = 7; protected static final int HTTP_OKAY_CODE = 200; protected ReaderClient client; protected ByteBuffer readBuffer; protected ByteBuffer requestBuffer; protected InetSocketAddress destination; protected TCPRawConnection connection; protected int state; protected EasyParser parser; protected int statusCode; protected String statusText; /** * Constructor HTTPReader * * @param client * @param path * @param host * @param port */ public HTTPReader(ReaderClient client, String path, String host, int port) { this.client = client; this.readBuffer = ByteBuffer.allocate(READ_BUFFER_SIZE); String requestString = "GET " + path + " HTTP/1.0\r\n" + "Host: " + host + ":" + port + "\r\n" + "Connection: close\r\n" + "User-Agent: " + AGENT + "\r\n\r\n"; this.requestBuffer = LocalNode.myTCPRawMessenger.convertStringToBuffer(requestString); this.destination = new InetSocketAddress(host, port); this.state = STATE_DISCONNECTED; } /** * Method read * @throws ReaderException */ public void read() throws ReaderException { if (state == STATE_DISCONNECTED) { this.state = STATE_PREHEADERS; this.parser = new EasyParser(); connection = LocalNode.myTCPRawMessenger.connect(destination, this); requestBuffer.position(0); connection.write(requestBuffer); } else { throw new ReaderException("Read already in progress"); } } /** * Method stop */ public void stop() { connection.disconnect(); state = STATE_DISCONNECTED; } /** * Method executorHandleTCPRawNetwork * * @param connection */ public void handleTCPRawNetwork(TCPRawConnection connection) { try { processNetwork(connection); } catch (Exception exception) { client.handleReaderException(exception); } } /** * Method handleTCPRawNetworkError * * @param connection */ public void handleTCPRawNetworkError(TCPRawConnection connection) { client.handleReaderException( new ReaderException("There was an error connecting to the source")); } /** * Method handleTCPRawNetworkNew * * @param connection */ public void handleTCPRawNetworkNew(TCPRawConnection connection) {} /** * Method processNetwork * * @param connection */ protected void processNetwork(TCPRawConnection connection) { readBuffer.clear(); int bytesRead = connection.read(readBuffer); readBuffer.position(0); if (bytesRead == -1) { if (Output.debuggingEnabled) { logger.debug(new LogMessage(new Object[]{ "Remote source disconnect"})); } connection.disconnect(); state = STATE_DISCONNECTED; client.handleReaderEnd(); } if (bytesRead > 0) { byte[] theBytes = new byte[bytesRead]; String theString = null; readBuffer.get(theBytes); theString = new String(theBytes); if (Output.debuggingEnabled) { logger.debug(new LogMessage(new Object[]{"Received ", theString, " from remote source"})); } boolean parseResult = processString(theString); if (parseResult == false) { connection.disconnect(); state = STATE_DISCONNECTED; } } } /** * Method processString * * @param theString * @return */ protected boolean processString(String theString) { String splitResults[]; switch (state) { case STATE_PREHEADERS: state = STATE_VERSION; case STATE_VERSION: // HTTP-Version (Version is a decimal number) splitResults = parser.match("HTTP/([0-9,\\p{Punct}])+", theString); if (splitResults != null) { theString = null; state = STATE_STATUSCODE; } else { break; } case STATE_STATUSCODE: splitResults = parser.match("[0-9]+", theString); if (splitResults != null) { theString = null; try { statusCode = Integer.parseInt(splitResults[EasyParser.PATTERN]); } catch (Exception exception) { client.handleReaderException( new ReaderException( "Unable to parse status code " + splitResults[EasyParser.PATTERN])); state = STATE_ERROR; break; } state = STATE_STATUSTEXT; } else { break; } case STATE_STATUSTEXT: splitResults = parser.match("(\\r)?\\n", theString); if (splitResults != null) { theString = null; statusText = splitResults[EasyParser.PRE_PATTERN].trim(); if (statusCode != HTTP_OKAY_CODE) { client.handleReaderException( new ReaderException( "HTTP Error " + statusCode + " " + statusText)); state = STATE_ERROR; break; } state = STATE_HEADERS; } else { break; } case STATE_HEADERS: splitResults = parser.match("(\\r)?\\n(\\r)?\\n", theString); if (splitResults != null) { theString = null; String remainingData = splitResults[EasyParser.POST_PATTERN]; if (remainingData != null) { client.handleReaderString(remainingData); } state = STATE_DATA; } else { break; } case STATE_DATA: client.handleReaderString(theString); break; default: return false; } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -