/[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.23 by tuukkah, Thu May 8 13:19:00 2003 UTC revision 1.24 by tuukkah, Thu May 8 14:10:43 2003 UTC
# Line 46  public class HTTPProxy implements Runnab Line 46  public class HTTPProxy implements Runnab
46      protected IndexedPool pool;      protected IndexedPool pool;
47      protected int port;      protected int port;
48      protected String addr;      protected String addr;
49      protected String url;      protected String ROOTURL;
50      protected String REWRITE;      protected String REWRITE;
51      protected String URNPAC;      protected String URNPAC;
52        protected String BACKLINKS;
53      protected HTTPServer server;      protected HTTPServer server;
54            
55      public HTTPProxy(IndexedPool 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().getHostName();          this.addr = InetAddress.getLocalHost().getHostName();
59          this.url = "http://"+addr+":"+port+"/";          this.ROOTURL = "http://"+addr+":"+port+"/";
60          this.REWRITE = url+"rewrite/";          this.REWRITE = "rewrite";
61            this.BACKLINKS = "backlinks";
62          this.URNPAC = "urn-proxy.pac";          this.URNPAC = "urn-proxy.pac";
63    
64          this.server = new HTTPServer(new Factory(), port);          this.server = new HTTPServer(new Factory(), port);
65      }      }
66    
67      public void run() {      public void run() {
68          if(dbg) p("Starting Storm URN server @ "+url);          if(dbg) p("Starting Storm URN server @ "+ROOTURL);
69          server.run();          server.run();
70      }      }
71    
72      public String getURL() {      public String getURL() {
73          return url;          return ROOTURL;
74      }      }
75            
76      protected class StormConnection extends HTTPConnection {      protected class StormConnection extends HTTPConnection {
# Line 85  public class HTTPProxy implements Runnab Line 86  public class HTTPProxy implements Runnab
86                  if(HTTPProxy.dbg) p("<"+port+"> GET: "+uri + " (" + this.getRemoteIPAddress() + ")");                  if(HTTPProxy.dbg) p("<"+port+"> GET: "+uri + " (" + this.getRemoteIPAddress() + ")");
87    
88                  /* URN proxy requests start directly with "urn:" */                  /* URN proxy requests start directly with "urn:" */
89                  if(uri.startsWith("/")) uri = uri.substring(1);                  if(!uri.startsWith("/"))
90                        return serveBlock(uri, resf);
91                    else
92                        uri = uri.substring(1);
93    
94                  if(uri.equals(URNPAC)) {                  if(uri.equals(URNPAC)) {
95                      return makePAC(req, resf);                      return makePAC(req, resf);
# Line 117  public class HTTPProxy implements Runnab Line 121  public class HTTPProxy implements Runnab
121                          i.hasNext();) {                          i.hasNext();) {
122                          BlockId id = (BlockId)i.next();                          BlockId id = (BlockId)i.next();
123                          String s = id.getURI();                          String s = id.getURI();
124                          if(rewrite) s = REWRITE + s;                          if(rewrite) s = ROOTURL+REWRITE+"/" + s;
125                          w.write("<a href=\""+s+"\">"+id+"</a><br>\n");                          w.write("<a href=\""+s+"\">"+id+"</a><br>\n");
126                      }                      }
127                      w.write("</body></html>\n");                      w.write("</body></html>\n");
# Line 126  public class HTTPProxy implements Runnab Line 130  public class HTTPProxy implements Runnab
130                  }                  }
131    
132                  BlockId id = new BlockId(uri);                  BlockId id = new BlockId(uri);
133    
134                    if((!rewrite && !backlinks) ||
135                       !id.getContentType().equals("text/html"))
136                        return serveBlock(uri, resf);
137    
138    
139                    if(HTTPProxy.dbg) p("Start rewrite");
140    
141                  Block block = pool.get(id);                  Block block = pool.get(id);
142                                    
143                  HTTPResponse resp = resf.makeResponse(200, "Ok");                  HTTPResponse resp = resf.makeResponse(200, "Ok");
144                  resp.setField("Content-Type", id.getContentType());                  resp.setField("Content-Type", id.getContentType());
                 if((!rewrite && !backlinks) ||  
                    !id.getContentType().equals("text/html")) {  
                     int blocksize = CopyUtil.copy(block.getInputStream(),  
                                   resp.getOutputStream());  
                     if(HTTPProxy.dbg) p("<"+port+"> Serve: "+id + " (Size: " +  
                         String.valueOf(blocksize) + ")");                    
                 } else {  
                     if(HTTPProxy.dbg) p("Start rewrite");  
   
                     String s = CopyUtil.readString(block.getInputStream());  
   
                     String prefix = "";  
   
                     if(rewrite) {  
                         prefix = REWRITE;  
                         if(backlinks) prefix += "backlinks/";  
   
                         String l = s.toLowerCase();  
                         int i = -1;  
                         while((i=l.indexOf("urn:x-storm:1.0:", i)) >= 0) {  
                             s = s.substring(0, i) + prefix + s.substring(i);  
                             l = s.toLowerCase();  
                             i += prefix.length() + 1;  
                         }  
                     }  
145    
146                      if(backlinks) {                  String s = CopyUtil.readString(block.getInputStream());
                         if(HTTPProxy.dbg) p("Getting HtmlLinkIndex.");  
147    
148                          HtmlLinkIndex idx = null;                  String prefix = "";
149                          try {  
150                              idx = (HtmlLinkIndex)pool.getIndex(HtmlLinkIndex.uri);                  if(rewrite) {
151                          } catch(NoSuchElementException _) {}                      prefix = ROOTURL+REWRITE+"/";
152                        if(backlinks) prefix += BACKLINKS+"/";
153                        
154                        String l = s.toLowerCase(); // XXX Why this?
155                        int i = -1;
156                        while((i=l.indexOf("urn:x-storm:1.0:", i)) >= 0) {
157                            s = s.substring(0, i) + prefix + s.substring(i);
158                            l = s.toLowerCase(); // XXX and this?
159                            i += prefix.length() + 1;
160                        }
161                    }
162    
163                    if(backlinks) {
164                        if(HTTPProxy.dbg) p("Getting HtmlLinkIndex.");
165                        
166                        HtmlLinkIndex idx = null;
167                        try {
168                            idx = (HtmlLinkIndex)pool.getIndex(HtmlLinkIndex.uri);
169                        } catch(NoSuchElementException _) {}
170                        
171                        if(HTTPProxy.dbg) p("HtmlLinkIndex = "+idx);
172                        
173                        if(idx != null) {
174                            if(HTTPProxy.dbg) p("Looking for links");
175                            Collector links = idx.getLinksTo(id);
176                                                    
177                          if(HTTPProxy.dbg) p("HtmlLinkIndex = "+idx);                          try {
178                                // XXX Are we trying to wait for indexing?
179                                Thread.sleep(2000);
180                            } catch(InterruptedException _) {}
181                                                    
182                          if(idx != null) {                          if(HTTPProxy.dbg) p("Iter thru links");
183                              if(HTTPProxy.dbg) p("Looking for links");                          String t = "";
184                              Collector links = idx.getLinksTo(id);                          synchronized(links) {
185                                                            for(Iterator iter=links.iterator();
                             try {  
                                 // XXX Are we trying to wait for indexing?  
                                 Thread.sleep(2000);  
                             } catch(InterruptedException _) {}  
                               
                             if(HTTPProxy.dbg) p("Iter thru links");  
                             String t = "";  
                             synchronized(links) {  
                                 for(Iterator iter=links.iterator();  
186                                  iter.hasNext();) {                                  iter.hasNext();) {
187                                      HtmlLinkIndex.Link link =                                  HtmlLinkIndex.Link link =
188                                          (HtmlLinkIndex.Link)iter.next();                                      (HtmlLinkIndex.Link)iter.next();
189                                      if(HTTPProxy.dbg) p("Link: "+link.linkText);                                  if(HTTPProxy.dbg) p("Link: "+link.linkText);
190                                      t += "[<a href=\""+prefix+link.linkFrom.getURI()+                                  t += "[<a href=\""+prefix+link.linkFrom.getURI()+
191                                          "\">"+link.linkText+"</a>]<br>";                                      "\">"+link.linkText+"</a>]<br>";
                                 }  
                             }  
                             if(!t.equals("")) {  
                                 int i = s.indexOf("<body");  
                                 i = s.indexOf(">", i);  
                                 if(i < 0) i = 0;  
                                 else i++;  
                                 s = s.substring(0, i)  
                                     + "\n<p><small>Backlinks:<br>"  
                                     + t  
                                     + "</small></p>\n"  
                                     + s.substring(i);  
192                              }                              }
193                          }                          }
194                            if(!t.equals("")) {
195                                int i = s.indexOf("<body");
196                                i = s.indexOf(">", i);
197                                if(i < 0) i = 0;
198                                else i++;
199                                s = s.substring(0, i)
200                                    + "\n<p><small>Backlinks:<br>"
201                                    + t
202                                    + "</small></p>\n"
203                                    + s.substring(i);
204                            }
205                      }                      }
                           
                     byte[] bytes = s.getBytes("US-ASCII");  
                     resp.getOutputStream().write(bytes);  
206                  }                  }
207                            
208                    byte[] bytes = s.getBytes("US-ASCII");
209                    resp.getOutputStream().write(bytes);
210                  return resp;                  return resp;
211              } catch(FileNotFoundException e) {              } catch(FileNotFoundException e) {
212                  return resf.makeError(404, "Not found");                  return resf.makeError(404, "Not found");
# Line 240  public class HTTPProxy implements Runnab Line 243  public class HTTPProxy implements Runnab
243              w.write("<p>");              w.write("<p>");
244              w.write("To configure URN proxy in Netscape Navigator 4, ");              w.write("To configure URN proxy in Netscape Navigator 4, ");
245              w.write("Automatic Proxy Configuration location can be set to ");              w.write("Automatic Proxy Configuration location can be set to ");
246              w.write("<a href=\""+url+URNPAC+"\">"+url+URNPAC+"</a></p>\n");              w.write("<a href=\""+ROOTURL+URNPAC+"\">"
247                        +ROOTURL+URNPAC+"</a></p>\n");
248            }
249    
250            protected HTTPResponse serveBlock(String uri,
251                                              HTTPResponse.Factory resf)
252                throws Exception {
253                    BlockId id = new BlockId(uri);
254                    Block block = pool.get(id);
255                    
256                    HTTPResponse resp = resf.makeResponse(200, "Ok");
257                    resp.setField("Content-Type", id.getContentType());
258    
259                    int blocksize = CopyUtil.copy(block.getInputStream(),
260                                                  resp.getOutputStream());
261                    return resp;
262          }          }
263    
264          protected HTTPResponse makePAC(HTTPRequest req,          protected HTTPResponse makePAC(HTTPRequest req,

Legend:
Removed from v.1.23  
changed lines
  Added in v.1.24

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