📄 fipa_2000_http_connection.java
字号:
/*
* The contents of this file are subject to the BT "ZEUS" Open Source
* Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
* except in compliance with the Licence. You may obtain a copy of the Licence
* from $ZEUS_INSTALL/licence.html or alternatively from
* http://www.labs.bt.com/projects/agents/zeus/licence.htm
*
* Except as stated in Clause 7 of the Licence, software distributed under the
* Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the Licence for the specific language governing rights and
* limitations under the Licence.
*
* The Original Code is within the package zeus.*.
* The Initial Developer of the Original Code is British Telecommunications
* public limited company, whose registered office is at 81 Newgate Street,
* London, EC1A 7AJ, England. Portions created by British Telecommunications
* public limited company are Copyright 1996-2001. All Rights Reserved.
*
* THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
*/
package zeus.actors.intrays;
import java.net.*;
import java.io.*;
import zeus.util.*;
import FIPA.*;
import zeus.concepts.*;
import zeus.actors.outtrays.*;
import zeus.actors.*;
import zeus.actors.factories.*;
/**
* FIPA_2000_HTTP_Connection handles incomming connections on this host/port socket
* and decides
* whether or not they are meant for it (ie: is the name on the connection the
* same as the name in the init of this class)<p>
* If the connection is relevant then the data will be read, a response (as per
* the spec ) will be sent and the message will be decoded into a FIPA.FipaMessage
* and placed on the registered queue for processing by the relevant server <p>
* The class constructor takes a host a port and a name : should this agent only listen
* for connections for itself at this port and host? That is what is implemented here...
* comments on a postcard please<p>
* @author Simon Thompson
* @since 1.1
*/
public class FIPA_2000_HTTP_Connection implements Runnable {
protected Queue queue;
private String response_ok = new String("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nCache-Control: no-cache\r\nConnection: close\n");
private String response_not_ok = new String("HTTP/1.1 401 NOT_OK \nContent-Type: text/plain\nCache-Control: no-cache\nConnection: close\n");
private String host = null;
private String name = null;
private String port = null;
private int MAX_HTTP_QUEUE = 10;
/**
* MAX_LINES_TO_READ_WITHOUT_YEILDING is used to control how
* many lines will be read in from a connection in one lump. Without this I
* think that a DOS attack could be mounted via a http connection just
* by sending it an infinitely long message: perhaps another agent could
* use this to prevent this agent from bidding in a round of contracts, or
* from sending some alert information to another agent! <p>
* This is protected to prevent it being reset in Agent specific code
*/
protected int MAX_LINES_TO_READ_WITHOUT_YEILDING = 5000;
protected int MAX_LINES_TO_READ_WITHOUT_SHUTTING = 5000;
/**
* if MAX_CONTENT_LENGHT is greater than 200001 it will be ignored and
* max_int length content will be accepted
*/
protected int MAX_CONTENT_LENGTH = 100000;
private ServerSocket serverSocket = null;
/**
* run method that makes sure a thread is trying to pick up inputs from a
* connection <p>
* The loop blocks on the ServerSocket.accept() method. A count variable
* is used to prevent an DOS on this agent by yeilding after a certain number
* of lines have been read.
*/
public void run() {
while (true) {
try {
String currentMessage = new String();
debug("Listening in http");
Socket connection = serverSocket.accept();
// InetAddress inet = connection.getInetAddress();
// debug("name " + inet.getHostName() + " address " + inet.getHostAddress());
PrintWriter out = new PrintWriter(connection.getOutputStream(), true );
InputStreamReader ins = new InputStreamReader(connection.getInputStream());
BufferedReader in = new BufferedReader(ins);
int count = 0;
String ln;
boolean done = false;
String contentStr = null;
String boundaryVal = null;
int contentLength = 0;
int bigCount = 0;
while(!done) {
ln=in.readLine();
debug(ln);
currentMessage += ln +"\n";
if (boundaryVal == null)
boundaryVal = testAndSetBoundary(ln);
if (contentLength == 0)
contentLength = testAndSetContentLength(ln);
if (contentLength > MAX_CONTENT_LENGTH) {
respondNotOK(out);
in.close();
out.close();
connection.close(); // reset peer, no DOS!
}
debug("boundary=" + boundaryVal);
// debug (String.valueOf(contentLength));
if ((boundaryVal != null) && (contentLength > 0)) { // start watching for a header end
// debug("finding boundary");
if (ln.equals("--" + boundaryVal)) {
debug("found boundary");
contentStr = new String();
ln = in.readLine();
String ender = "--" + boundaryVal + "--";
while (!ender.equals(ln.trim())) {
debug("content:" + ln);
contentStr += ln +" "; // added " "
ln = in.readLine();
}
contentStr += ender;
/* char [] content = new char [contentLength];
in.read(content,0,contentLength);
contentStr = new String (content); */
// debug ("content length = " + contentStr.length());
// debug ("\n\ncontent = " + contentStr);
done = true;
}
}
debug("readIn >> " + ln);
bigCount++;
count++;
//debug("before maxlines");
if (count > MAX_LINES_TO_READ_WITHOUT_YEILDING) {
Thread.yield();
count = 0; }
//debug("before connection clsoser");
if (bigCount > MAX_LINES_TO_READ_WITHOUT_SHUTTING) {
//respondNotOK(out);
in.close();
out.close();
connection.close(); // reset peer, no DOS!
}
//debug("after connection closer");
/*if (done) {
debug ("DONE!!");
}
else {
debug ("NOT DONE!!!");
} */
}
// in.close();
//debug ("1");
debug(contentStr);
FIPAPerformative fmess = process(contentStr,boundaryVal);
//debug("2");
if (fmess!= null) {
respondOK(out);
message(fmess); }
else {
respondNotOK(out);
}
// debug("3");
// Thread.yield();
connection.close();
//debug("4");
}
catch (IOException ioe) {
ioe.printStackTrace();
System.out.println("IO error opening http connection from another agent: recovering");
}
catch (Exception e) {
e.printStackTrace();}
catch (Error er) {
System.out.println("Error trapped and handled in FIPA_2000_HTTP_Connection: recovering");
er.printStackTrace(); }
catch (Throwable tr) {
System.out.println("*** Throwable trapped and handled in FIPA_2000_HTTP_Connection: recovering");
}
}
}
/**
* hitch this connection to a message queue
*/
public void register(Queue q) {
this.queue = q;
}
/**
* pull the boundary val from the header
*/
private String testAndSetBoundary(String ln) {
String tmp = ln.toLowerCase();
debug(">> test boundary:" + tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -