Back to index

lightning-sunbird  0.9+nobinonly
ScriptFile.java
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 import java.io.*;
00039 import java.util.Date;
00040 import java.util.StringTokenizer;
00041 /*
00042        ScriptFile class reads off the script file and sets up the list
00043        of delimiters.
00044 */
00045 
00046 class ScriptFile {
00047 
00048     public static final String URLMAP = new String("docs/urlmap");
00049 
00050        public ScriptFile(Connection c, String req, PrintWriter stream ) {
00051         con = c;
00052            request = req;
00053            out = stream;
00054            if (out == null)
00055                out = new PrintWriter(System.out);
00056            file = URLMAP;
00057            try {
00058                Parse();
00059            }
00060            catch (IOException e)
00061            {
00062                out.println("HTTP/1.1 500 Server Error\n\n");
00063                out.println("Failed with this exception-");
00064                out.println(e);
00065            }
00066            out.flush();
00067        }
00068 
00069        public ScriptFile(String f) {
00070               file = f;
00071               out = new PrintWriter(System.out);
00072            try {
00073                Parse();
00074            }
00075            catch (FileNotFoundException e)
00076            {
00077                out.println("HTTP/1.1 404 File Not Found\n\n");
00078                out.println("File not found!");
00079                out.println(e);
00080            }
00081            catch (IOException e)
00082            {
00083                out.println("HTTP/1.1 500 Server Error\n\n");
00084                out.println("Failed with this exception-");
00085                out.println(e);
00086            }
00087            out.flush();
00088        }
00089 
00090        public void Parse () throws IOException {
00091 
00092            if ((request == null) ||
00093                      (request.length() == 0) ||
00094                      request.startsWith("GET / ") || request.startsWith("get / "))
00095            {
00096                WriteDefaultResponse();
00097             return;
00098            }
00099 
00100               if (request.startsWith("GET /?") || request.startsWith("get /?"))
00101               {
00102                      WriteSpecialResponse(request.substring(request.indexOf('?')+1));
00103                      return;
00104               }
00105 
00106         if (request.startsWith("HEAD /") || request.startsWith("head /"))
00107         {
00108             WriteHeadResponse();
00109             return;
00110         }
00111 
00112         boolean outDirty = false;
00113            if (file != null)
00114            {
00115                        BufferedReader in = new BufferedReader(
00116                             new InputStreamReader(
00117                                    new FileInputStream(file)));
00118 
00119               String s = new String();
00120               while((s = in.readLine())!= null)
00121               {
00122                 s.trim();
00123                 /* Skip comments */
00124                 if ((s.length() == 0) || (s.charAt(0) == '#'))
00125                     continue;
00126                 if (s.startsWith("START")) {
00127                     // Check to see if this was in the requested URL
00128                     String filename = new String ("GET " + s.substring(6));
00129                     String otherfilename = new String("POST " + s.substring(6));
00130                     if (request.startsWith(filename) || 
00131                             request.startsWith(otherfilename))
00132                     {
00133                         continue;
00134                     }
00135                     else// Else skipto past the END
00136                     {
00137                         while (!s.startsWith("END"))
00138                         {
00139                             s = in.readLine();
00140                             if (s != null)
00141                             {
00142                                 s.trim();
00143                             }
00144                             else
00145                                 break;
00146                         }
00147                     }
00148                 }
00149                 else if (s.startsWith("ECHO")) {
00150                     outDirty = true;
00151                     boolean parameter = false;
00152                     try {
00153                         String header = new String(s.substring(5));
00154                         String req= new String(con.request);
00155                         int t = req.indexOf(header);
00156                         if (t != -1) {
00157                             out.println(req.substring(
00158                                 t, req.indexOf("\n", t)));
00159                             parameter = true;
00160                         }
00161                         else {
00162                             out.println("Error: " + header + 
00163                                     " not specified in request!");
00164                         }
00165                         
00166                     }
00167                     catch (StringIndexOutOfBoundsException e) {}
00168                     if (!parameter)
00169                         out.println(con.request);
00170                 }
00171                 else if (s.startsWith("INCLUDE")) {
00172                     outDirty = true;
00173                     WriteOutFile("docs/" + s.substring(8));
00174                 }
00175                 else if (s.startsWith("CRLF")) {
00176                     outDirty = true;
00177                     out.println();
00178                 }
00179                 else if (s.startsWith("END")) {
00180                     // ignore should never have appeared here though!
00181                     continue;
00182                 }
00183                 else {// Not a recognizable line... just print it as is...
00184                     outDirty = true;
00185                     out.println(s);
00186                 }
00187 
00188               }
00189               in.close();
00190 
00191               if (outDirty)
00192               {
00193                 out.flush();
00194               }
00195               else
00196                 WriteDefaultResponse();
00197            }
00198        }
00199 
00200        public static void main(String args[]) {
00201            if (args.length >= 1) {
00202             ScriptFile sf = new ScriptFile(args[0]);
00203             /* Detect change stuff;
00204             File f = new File(args[0]);
00205             long lastMod = f.lastModified();
00206             while (f.lastModified() == lastMod) {
00207             }
00208             sf.Parse();
00209             */
00210            }
00211        }
00212 
00213     protected void WriteOutFile(String filename) throws IOException {
00214                 BufferedReader incl = new BufferedReader(
00215                      new InputStreamReader(
00216                new FileInputStream(filename)));
00217         // This doesn't have to be line wise... change later TODO
00218         String s;
00219         while ((s = incl.readLine()) != null)
00220         {
00221             out.println(s);
00222         }
00223         incl.close();
00224     }
00225 
00226     protected void WriteDefaultResponse() throws IOException {
00227               WriteOutFile("docs/generic.res");
00228         out.println("Date: " + (new Date()).toString());
00229         if (file != null)
00230         {
00231             File f = new File(file);
00232             out.println("Last-modified: " + (new Date(f.lastModified())).toString());
00233         }
00234         out.println("\n"); // prints 2
00235         if (con != null)
00236         {
00237                      out.println("<HTML><BODY>");
00238             out.println("<H3>Rec'd. the following request-</H3>");
00239             out.println("<PRE>");
00240             out.println(con.request);
00241             out.println("</PRE>");
00242             out.println("From- " + con.client);
00243                      out.println("</BODY></HTML>");
00244          }
00245     }
00246 
00247        protected void WriteSpecialResponse(String specialcase) throws IOException {
00248         out.println("HTTP/1.1 200 OK");
00249         out.println("Server: HTTP Test Server/1.1");
00250         out.println("Content-Type: text/plain");
00251 
00252               StringTokenizer st = new StringTokenizer(specialcase, " &");
00253               while (st.hasMoreTokens()) {
00254                   String pair = st.nextToken();
00255               if (pair.startsWith("Length=") || pair.startsWith("length="))
00256               {
00257 
00258                 int len = Integer.valueOf(pair.substring(
00259                             pair.indexOf('=')+1), 10).intValue();
00260 
00261                      out.println("Date: " + (new Date()).toString());
00262                      out.println("Content-Length: " + len);
00263                      out.println("\n");
00264                      for (int i = 0; i<len; i++) {
00265                     out.print ((char)i%10);
00266                     if ((i>0) && ((i+1)%80) == 0)
00267                              out.print('\n');
00268                          out.flush();
00269                      }
00270                      return;
00271               }
00272        }
00273 
00274        }
00275 
00276     protected void WriteHeadResponse() {
00277         out.println("HTTP/1.1 200 OK");
00278         out.println("Server: HTTP Test Server/1.1");
00279         out.println("Content-Type: text/plain");
00280         out.println("Content-Length: 1000" ); // bogus 
00281         out.println("Date: " + (new Date()).toString());
00282         out.println(); // also test without it
00283         out.flush();
00284     }
00285 
00286     String file = null;
00287     // The string associated with this script occurence
00288 
00289     String request;
00290     PrintWriter out;
00291     Connection con;
00292 }