org/objectweb/proactive/core/rmi/HTTPInputStream.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.rmi;
00032 
00033 import java.io.DataInputStream;
00034 import java.io.IOException;
00035 import java.io.InputStream;
00036 import java.io.PushbackInputStream;
00037 import java.util.HashMap;
00038 import java.util.regex.Pattern;
00039 
00040 
00041 /*
00042  * Created on Jul 29, 2004
00043  */
00044 
00051 public class HTTPInputStream extends DataInputStream {
00052     private static Pattern pColon = Pattern.compile(": *");
00053     HashMap headers = new HashMap();
00054 
00055     public HTTPInputStream(InputStream is) {
00056         super(is);
00057         in = new PushbackInputStream(in);
00058     }
00059 
00064     public String getLine() throws IOException {
00065         StringBuilder sb = new StringBuilder();
00066         int c;
00067         boolean readAtLeastOneChar = false;
00068 
00069         // read until meet '\r', '\n' or '\r\n'
00070         while (((c = in.read()) > 0) && (c != '\n')) {
00071             readAtLeastOneChar = true;
00072 
00073             if (c == '\r') {
00074                 int c2 = in.read();
00075 
00076                 if ((c2 > 0) && (c2 != '\n')) {
00077                     // insert the character back in the stream
00078                     ((PushbackInputStream) in).unread(c2);
00079                 }
00080 
00081                 // else { ignoreLF(); }
00082                 break;
00083             } else {
00084                 sb.append((char) c);
00085             }
00086         }
00087 
00088         if (readAtLeastOneChar == false) {
00089             return null; // read nothing
00090         } else {
00091             return sb.toString();
00092         }
00093     }
00094 
00095     /* Read message headers */
00096     public void parseHeaders() throws IOException {
00097         String line;
00098         String prevFieldName = null;
00099         headers.clear();
00100 
00101         do {
00102             if ((line = getLine()) == null) {
00103                 throw new IOException(
00104                     "Connection ended before reading all headers");
00105             }
00106 
00107             if ((line.length() > 0)) { // we don't care about headers in GET requests
00108                 if (line.startsWith(" ") || line.startsWith("\t")) {
00109                     headers.put(prevFieldName, headers.get(prevFieldName) +
00110                         line); // folding
00111                 }
00112 
00113                 String[] pair = pColon.split(line, 2);
00114                 String fieldName = prevFieldName = pair[0].toLowerCase();
00115                 String fieldValue = pair[1];
00116 
00117                 String storedFieldValue = (String) headers.get(fieldName);
00118 
00119                 if (storedFieldValue == null) {
00120                     headers.put(fieldName, fieldValue);
00121                 } else {
00122                     headers.put(fieldName, storedFieldValue + "," + fieldValue);
00123                 }
00124 
00125                 headers.put(fieldName, fieldValue);
00126             }
00127         } while (line.length() > 0); // empty line, end of headers
00128     }
00129 
00130     public String getHeader(String fieldName) {
00131         return (String) headers.get(fieldName.toLowerCase());
00132     }
00133 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1