Back to index

lightning-sunbird  0.9+nobinonly
Connection.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.net.*;
00040 
00041 class Connection extends Thread {
00042 
00043     public static final boolean DEBUG = true;
00044        public Socket client;
00045        protected BufferedReader in;
00046        protected PrintWriter out;
00047 
00048     final static String SERVER = "HTTP Test Server/1.1";
00049 
00050     public Connection(Socket client_socket) {
00051               client = client_socket;
00052 
00053               if (DEBUG)
00054               {
00055                      System.out.println(
00056                          "---------------------------------------------------------");
00057                      System.out.println(client);
00058               }
00059               try {
00060                      in = new BufferedReader(new InputStreamReader(
00061                             client.getInputStream()));
00062                      out = new PrintWriter(client.getOutputStream());
00063                      this.start();
00064               }
00065               catch (IOException e) {
00066                      try {
00067                             client.close();
00068                      }
00069                      catch (IOException another) {
00070                      }
00071                      System.out.println("Exception while getting socket streams."
00072                             + e);
00073               }
00074        }
00075 
00076        final public void run() {
00077 
00078               String line = null;
00079               String firstline = null;
00080               request = new StringBuffer();
00081 
00082               int len = 0;
00083         int extralen= 0;
00084               try {
00085                      while(true) {
00086                             if (!in.ready()) {
00087                                    continue;
00088                          }
00089                 line = in.readLine();
00090                             if (line == null)
00091                                    break;
00092                             if (firstline == null)
00093                                 firstline = new String(line);
00094                             len = line.length();
00095                 if (line.regionMatches(true, 0,
00096                         "Content-length: ", 0, 16)) {
00097                     extralen = Integer.valueOf(line.substring(16,
00098                         line.length())).intValue();
00099                 }
00100                             if (len == 0)
00101                 {
00102                     // Now read only the extralen if any-
00103                     if (extralen > 0)
00104                     {
00105                         char[] postbuffer = new char[extralen];
00106                         in.read(postbuffer);
00107                         request.append("\n");
00108                         request.append(postbuffer);
00109                         if (DEBUG) {
00110                             System.out.println();
00111                             System.out.println(postbuffer);
00112                         }
00113                     }
00114                     break;
00115                 }
00116                 request.append(line);
00117                             request.append("\n");
00118                             if (DEBUG)
00119                                 System.out.println(line);
00120                      }
00121             // This will appropriately write all the stuff based
00122             // on the first line and the script file
00123             ScriptFile sf = new ScriptFile(this, firstline, out);
00124               out.flush();
00125               // mark for garbage collect
00126               sf = null;
00127               } // try
00128               catch (IOException e) {
00129                      System.out.println("Some problem while communicating. " + e);
00130               }
00131               finally {
00132                      try {
00133                             client.close();
00134                      }
00135                      catch (IOException e2) {
00136                      }
00137               }
00138        }
00139        StringBuffer request;
00140 }