Source code: raining/server/DefaultPriorityHandler.java
1 /*
2 * $Author: rahul_kumar $
3 * $Id: DefaultPriorityHandler.java,v 1.2 2003/10/18 11:29:26 rahul_kumar Exp rahul $
4 */
5
6 package raining.server;
7 import raining.core.*;
8 import raining.protocol.http.*;
9 import raining.util.*;
10 import java.util.*;
11 import java.io.*;
12 import java.net.*;
13 import java.nio.*;
14 import java.nio.channels.*;
15
16 /**
17 * a default priority handler - handles priority of read and host (on
18 * connect).
19 * Connects:
20 * Checks a debarred(deny) hosts list and returns -1.
21 * Checks a allowed hosts list. if it exists then only these are
22 * allowed, all else are debarred.
23 * If not, it checks a deny list, all these are denied, all else are
24 * allowed.
25 * Checks a favored hosts list and returns 1.
26 * For all others returns 0.
27 *
28 * Needs to look into subnets also.
29 * Needs to pick from a file.
30 *
31 * Read:
32 * Checks operations.
33 * Returns 1 for GET and 0 for POST.
34 * Returns -1 for others.
35 * Checks resource.
36 * returns -1 for some folders
37 * returns 1 for some resources
38 * returns 0 for all others.
39 * Under heavy loads the one operation may take REJECT_PRIORITY
40 * or some folders may get REJECT_PRIORITY
41 *
42 * For us incoming requests can only be ASCII, not unicode, so we shuold
43 * find a way to avoid Strings and deal with char arrays.
44 *
45 */
46
47 public class DefaultPriorityHandler implements PriorityHandler {
48
49
50
51 /** returns priority after read , based on operation
52 * requested, or file requested, or speed of data sent in.
53 * returns -1 if this should be closed.
54 */
55 public int read_priority(Request request){
56 if (request.getOperation().equals("GET")) return HIGH_PRIORITY;
57 if (request.getOperation().equals("POST")) return NORMAL_PRIORITY;
58 return REJECT_PRIORITY;
59 }
60 /** returns the priority of the incoming host.
61 * returns -1 if the host could be a DOS attack or should be closed
62 * for some other reason.
63 * TODO based on access.deny and allow policy
64 */
65 public int host_priority(SocketChannel sc){
66 Socket s = sc.socket();
67 InetAddress ia = s.getInetAddress();
68 //String host = ia.getHostName();
69 String host = ia.getHostAddress();
70 if (favored != null)
71 {
72 if (favored.containsKey(host)) return HIGH_PRIORITY;
73 }
74 if (denied != null)
75 {
76 if (denied.containsKey(host)) return REJECT_PRIORITY;
77 }
78 return NORMAL_PRIORITY;
79 }
80
81 /** constructor. TODO loading.
82 */
83 public DefaultPriorityHandler (){
84 // need to load favored and debarred from a file
85 try {
86 allowed = NioUtil.readRowsFromFile("access.allow");
87 } catch (FileNotFoundException fnf) {
88 allowed = null;
89 try {
90 denied = NioUtil.readRowsFromFile("access.deny");
91 } catch (FileNotFoundException fnfexc) {
92 System.err.println( "BOTH access.deny and access.allow NOT FOUND. Ignoring in this release. ");
93 denied = null;
94 } catch (Exception exc) {
95 denied = null;
96 System.err.println( "BOTH access.deny and access.allow not found. Ignoring in this release ");
97 System.err.println( P+" L EXC:"+ exc.toString());
98 System.err.println( " === This has yet to be implemented === dont worry.");
99
100 }
101 } catch (Exception exc) {
102 //allowed = new HashMap();
103 allowed = null;
104 System.err.println( P+" L EXC:"+ exc.toString()); exc.printStackTrace();
105
106 }
107 try {
108 favored = NioUtil.readRowsFromFile("access.high");
109 } catch (Exception exc) { System.err.println( P+" L EXC:"+ exc.toString()); exc.printStackTrace(); }
110
111
112 }
113
114 static final String P = "DefaultPriorityHandler"; // used in exception strings
115 public static final int HIGH_PRIORITY = 1;
116 public static final int NORMAL_PRIORITY = 0;
117 public static final int REJECT_PRIORITY = -1;
118
119 protected Map favored = null;
120 protected Map denied = null;
121 protected Map allowed = null;
122
123 } // end of class
124
125
126