00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
00078 ((PushbackInputStream) in).unread(c2);
00079 }
00080
00081
00082 break;
00083 } else {
00084 sb.append((char) c);
00085 }
00086 }
00087
00088 if (readAtLeastOneChar == false) {
00089 return null;
00090 } else {
00091 return sb.toString();
00092 }
00093 }
00094
00095
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)) {
00108 if (line.startsWith(" ") || line.startsWith("\t")) {
00109 headers.put(prevFieldName, headers.get(prevFieldName) +
00110 line);
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);
00128 }
00129
00130 public String getHeader(String fieldName) {
00131 return (String) headers.get(fieldName.toLowerCase());
00132 }
00133 }