/[storm]/storm/org/nongnu/storm/util/HTTPProxy.java
ViewVC logotype

Diff of /storm/org/nongnu/storm/util/HTTPProxy.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.15 by benja, Fri May 2 13:44:48 2003 UTC revision 1.16 by benja, Sat May 3 19:45:26 2003 UTC
# Line 34  import org.nongnu.storm.http.server.*; Line 34  import org.nongnu.storm.http.server.*;
34  import java.io.*;  import java.io.*;
35  import java.net.*;  import java.net.*;
36  import java.util.Iterator;  import java.util.Iterator;
37    import java.util.NoSuchElementException;
38    
39  /** An HTTP server serving blocks from a Storm pool. When started from command  /** An HTTP server serving blocks from a Storm pool. When started from command
40   *  line, it servers the directory given as first argument.   *  line, it servers the directory given as first argument.
41   */   */
42  public class HTTPProxy implements Runnable {  public class HTTPProxy implements Runnable {
43      public static boolean dbg = false;      public static boolean dbg = false;
44      private static void p(String s) { System.out.println(s); }      private static void p(String s) { System.out.println("HTTPProxy: "+s); }
45    
46      protected StormPool pool;      protected IndexedPool pool;
47      protected int port;      protected int port;
48      protected String addr;      protected String addr;
49      protected String url;      protected String url;
# Line 51  public class HTTPProxy implements Runnab Line 52  public class HTTPProxy implements Runnab
52    
53      protected HTTPServer server;      protected HTTPServer server;
54            
55      public HTTPProxy(StormPool pool, int port) throws IOException {      public HTTPProxy(IndexedPool pool, int port) throws IOException {
56          this.pool = pool;          this.pool = pool;
57          this.port = port;          this.port = port;
58          this.addr = InetAddress.getLocalHost().getCanonicalHostName();          this.addr = InetAddress.getLocalHost().getCanonicalHostName();
# Line 84  public class HTTPProxy implements Runnab Line 85  public class HTTPProxy implements Runnab
85                  if(HTTPProxy.dbg) p("<"+port+"> GET: "+uri);                  if(HTTPProxy.dbg) p("<"+port+"> GET: "+uri);
86                  if(uri.startsWith("/")) uri = uri.substring(1);                  if(uri.startsWith("/")) uri = uri.substring(1);
87    
88                  boolean rewrite = false;                  boolean rewrite = false, backlinks = false;
89                  if(uri.startsWith("rewrite/")) {                  if(uri.startsWith("rewrite/")) {
90                      uri = uri.substring("rewrite/".length());                      uri = uri.substring("rewrite/".length());
91                      rewrite = true;                      rewrite = true;
# Line 92  public class HTTPProxy implements Runnab Line 93  public class HTTPProxy implements Runnab
93                      return makePAC(req, resf);                      return makePAC(req, resf);
94                  }                  }
95    
96                    if(uri.startsWith("backlinks/")) {
97                        uri = uri.substring("backlinks/".length());
98                        backlinks = true;
99                    }
100                    
101                  if(uri.equals("") || uri.equals("rewrite")) {                  if(uri.equals("") || uri.equals("rewrite")) {
102                      if(uri.equals("rewrite")) rewrite = true;                      if(uri.equals("rewrite")) rewrite = true;
103    
# Line 121  public class HTTPProxy implements Runnab Line 127  public class HTTPProxy implements Runnab
127                  if(HTTPProxy.dbg) p("<"+port+"> Serve: "+id);                  if(HTTPProxy.dbg) p("<"+port+"> Serve: "+id);
128                  HTTPResponse resp = resf.makeResponse(200, "Ok");                  HTTPResponse resp = resf.makeResponse(200, "Ok");
129                  resp.setField("Content-Type", id.getContentType());                  resp.setField("Content-Type", id.getContentType());
130                  if(!rewrite || !id.getContentType().equals("text/html")) {                  if((!rewrite && !backlinks) ||
131                       !id.getContentType().equals("text/html")) {
132                      CopyUtil.copy(block.getInputStream(),                      CopyUtil.copy(block.getInputStream(),
133                                    resp.getOutputStream());                                    resp.getOutputStream());
134                  } else {                  } else {
135                        if(HTTPProxy.dbg) p("Start rewrite");
136    
137                      String s = CopyUtil.readString(block.getInputStream());                      String s = CopyUtil.readString(block.getInputStream());
138                      String l = s.toLowerCase();  
139                      int i = -1;                      String prefix = "";
140                      while((i=l.indexOf("urn:x-storm:1.0:", i)) >= 0) {  
141                          s = s.substring(0, i) + REWRITE + s.substring(i);                      if(rewrite) {
142                          l = s.toLowerCase();                          prefix = REWRITE;
143                          i += REWRITE.length() + 1;                          if(backlinks) prefix += "backlinks/";
144    
145                            String l = s.toLowerCase();
146                            int i = -1;
147                            while((i=l.indexOf("urn:x-storm:1.0:", i)) >= 0) {
148                                s = s.substring(0, i) + prefix + s.substring(i);
149                                l = s.toLowerCase();
150                                i += prefix.length() + 1;
151                            }
152                        }
153    
154                        if(backlinks) {
155                            if(HTTPProxy.dbg) p("Getting HtmlLinkIndex.");
156    
157                            HtmlLinkIndex idx = null;
158                            try {
159                                idx = (HtmlLinkIndex)pool.getIndex(HtmlLinkIndex.uri);
160                            } catch(NoSuchElementException _) {}
161                            
162                            if(HTTPProxy.dbg) p("HtmlLinkIndex = "+idx);
163                            
164                            if(idx != null) {
165                                if(HTTPProxy.dbg) p("Looking for links");
166                                Collector links = idx.getLinksTo(id);
167                                
168                                try {
169                                    Thread.sleep(2000);
170                                } catch(InterruptedException _) {}
171                                
172                                if(HTTPProxy.dbg) p("Iter thru links");
173                                String t = null;
174                                synchronized(links) {
175                                    for(Iterator iter=links.iterator();
176                                    iter.hasNext();) {
177                                        HtmlLinkIndex.Link link =
178                                            (HtmlLinkIndex.Link)iter.next();
179                                        if(HTTPProxy.dbg) p("Link: "+link.linkText);
180                                        if(t == null) t = "";
181                                        t += "[<a href=\""+prefix+link.linkFrom.getURI()+
182                                            "\">"+link.linkText+"</a>]<br>";
183                                    }
184                                }
185                                if(t != null) {
186                                    int i = s.indexOf("</body>");
187                                    if(i < 0) i = s.length();
188                                    s = s.substring(0, i) + "<p><tr><td><small>Backlinks:<br>" +
189                                        t + "</small></p>" + s.substring(i);
190                                }
191                            }
192                      }                      }
193                            
194                      byte[] bytes = s.getBytes("US-ASCII");                      byte[] bytes = s.getBytes("US-ASCII");
195                      resp.getOutputStream().write(bytes);                      resp.getOutputStream().write(bytes);
196                  }                  }
# Line 202  public class HTTPProxy implements Runnab Line 260  public class HTTPProxy implements Runnab
260    
261      public static void main(String[] args) throws Exception {      public static void main(String[] args) throws Exception {
262          dbg = true;          dbg = true;
263          StormPool pool;          IndexedPool pool;
264          java.util.Set empty = java.util.Collections.EMPTY_SET;          java.util.Set empty = java.util.Collections.EMPTY_SET;
265          pool = new DirPool(new File(args[0]), empty);          pool = new DirPool(new File(args[0]), empty);
266    

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.16

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26