📄 httpserver.java
字号:
/* * @(#)$Id: HTTPServer.java,v 1.3 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.handlers;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.util.HashMap;import org.apache.log4j.Logger;import pier.components.PierNetServer;import pier.data.TupleCollection;import pier.query.QueryTag;import services.network.tcpraw.TCPRawConnection;import util.logging.LogMessage;/** * Class HTTPServer */public class HTTPServer implements NetHandler { private static Logger logger = Logger.getLogger(HTTPServer.class); private static final int STATE_METHOD_READ = 1; private static final int STATE_URI_READ = 2; private static final int STATE_HTTPVERSION_READ = 3; private static final int STATE_HEADER_READ = 4; private static final int STATE_BODY_READ = 5; private static final int STATE_REQUEST_PROCESS = 6; protected static final int READSIZE = 1500; private TCPRawConnection connection; private PierNetServer parent; private StringBuffer partialRead; private ByteBuffer readBuffer; private HashMap headers; private ByteBuffer body; private int bodyLength; private int curState; /** * Constructor HTTPServer */ public HTTPServer() { this.partialRead = new StringBuffer(); this.readBuffer = ByteBuffer.allocate(READSIZE); this.headers = new HashMap(); this.curState = STATE_METHOD_READ; } /** * Method init * * @param connection * @param parent */ public void init(TCPRawConnection connection, PierNetServer parent) { this.connection = connection; this.parent = parent; } /** * Method closeConnection */ public void closeConnection() {} /** * Method readConnection */ public void readConnection() { // Read new data readBuffer.clear(); int bytesRead = connection.read(readBuffer); readBuffer.position(0); if (curState < STATE_BODY_READ) { // Append new data to partialReadString byte[] theBytes = new byte[bytesRead]; try { partialRead.append(new String(theBytes, "US-ASCII")); } catch (Exception e) { logger.error( new LogMessage(new Object[]{"Unable to read headers"}), e); } // Parse as much of partialReadString as possible switch (curState) { case STATE_METHOD_READ: // Look for <space>* <token> <space>+ case STATE_URI_READ: // Look for <space>* <token> <space>+ case STATE_HTTPVERSION_READ: // Look for <space>* <token> <space>+ <crlf>, <token>=HTTP/<INT>.<INT> case STATE_HEADER_READ: // Look for <token> <space>+ <token-w/space> <crlf> // End if <crlf> is found on its own line } } else { body.put(readBuffer); if (body.position() > bodyLength) { curState = STATE_REQUEST_PROCESS; } } } /** * Method result * * @param tuples * @param refnum * @param queryTag * @param source */ public void result(TupleCollection tuples, int refnum, QueryTag queryTag, InetSocketAddress source) {} /** * Method error * * @param errorMessage * @param refnum * @param queryTag * @param source */ public void error(String errorMessage, int refnum, QueryTag queryTag, InetSocketAddress source) {}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -